hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | import { For, Show, createMemo, type Component } from "solid-js"; |
| 2 | import { cn } from "@/lib/cn"; |
| 3 | import { usePreferences, useT } from "@/stores/preferences-context"; |
| 4 | import { EmptyInline } from "@/components/ui/empty-inline"; |
| 5 | |
| 6 | /** One dated observation. Several entries may share a day — they are summed. */ |
| 7 | export type HeatmapEntry = { |
| 8 | /** Epoch millis. Bucketed into a local-time calendar day. */ |
| 9 | at: number; |
| 10 | /** Amount contributed by this entry (minutes, records, …). */ |
| 11 | value: number; |
| 12 | }; |
| 13 | |
| 14 | export type ChartHeatmapProps = { |
| 15 | title?: string; |
| 16 | subtitle?: string; |
| 17 | entries: HeatmapEntry[]; |
| 18 | /** Calendar weeks to render, ending with the week containing `endsAt`. */ |
| 19 | weeks?: number; |
| 20 | /** Right-hand edge of the range; defaults to now. */ |
| 21 | endsAt?: number; |
| 22 | /** Cell tooltip / screen-reader text for a day that has data. */ |
| 23 | dayLabel: (value: number, dayStart: number) => string; |
| 24 | /** Cell text for an empty day. */ |
| 25 | emptyDayLabel: (dayStart: number) => string; |
| 26 | /** Summary line under the grid, e.g. "312 dk odak · son 26 hafta". */ |
| 27 | footer?: string; |
| 28 | class?: string; |
| 29 | }; |
| 30 | |
| 31 | const DEFAULT_WEEKS = 26; |
| 32 | |
| 33 | /** Local midnight for the day containing `ms` — DST-safe, unlike `ms - ms % DAY_MS`. */ |
| 34 | function startOfDay(ms: number): number { |
| 35 | const d = new Date(ms); |
| 36 | d.setHours(0, 0, 0, 0); |
| 37 | return d.getTime(); |
| 38 | } |
| 39 | |
| 40 | /** Local midnight of the Monday on or before `ms`. */ |
| 41 | function startOfWeek(ms: number): number { |
| 42 | const d = new Date(startOfDay(ms)); |
| 43 | // getDay(): 0 = Sunday. Shift so Monday is column-row 0. |
| 44 | d.setDate(d.getDate() - ((d.getDay() + 6) % 7)); |
| 45 | return d.getTime(); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * GitHub-contribution-style calendar. Columns are weeks (oldest → newest), |
| 50 | * rows are Monday → Sunday. Intensity is bucketed against the busiest day in |
| 51 | * range, so the scale is always relative to real data rather than a guess. |
| 52 | */ |
| 53 | export const ChartHeatmap: Component<ChartHeatmapProps> = (props) => { |
| 54 | const t = useT(); |
| 55 | const { locale } = usePreferences(); |
| 56 | |
| 57 | const weeks = () => props.weeks ?? DEFAULT_WEEKS; |
| 58 | |
| 59 | // Totals per local day, keyed by that day's midnight timestamp. |
| 60 | const totals = createMemo(() => { |
| 61 | const map = new Map<number, number>(); |
| 62 | for (const entry of props.entries) { |
| 63 | if (!Number.isFinite(entry.at) || entry.value <= 0) continue; |
| 64 | const day = startOfDay(entry.at); |
| 65 | map.set(day, (map.get(day) ?? 0) + entry.value); |
| 66 | } |
| 67 | return map; |
| 68 | }); |
| 69 | |
| 70 | const firstWeekStart = createMemo(() => { |
| 71 | const lastWeek = startOfWeek(props.endsAt ?? Date.now()); |
| 72 | const d = new Date(lastWeek); |
| 73 | d.setDate(d.getDate() - (weeks() - 1) * 7); |
| 74 | return d.getTime(); |
| 75 | }); |
| 76 | |
| 77 | // Columns of 7 local-midnight timestamps. Built by date arithmetic rather |
| 78 | // than fixed DAY_MS steps so a DST shift can't slide the grid by an hour. |
| 79 | const columns = createMemo(() => { |
| 80 | const start = firstWeekStart(); |
| 81 | return Array.from({ length: weeks() }, (_, week) => |
| 82 | Array.from({ length: 7 }, (_, day) => { |
| 83 | const d = new Date(start); |
| 84 | d.setDate(d.getDate() + week * 7 + day); |
| 85 | return d.getTime(); |
| 86 | }), |
| 87 | ); |
| 88 | }); |
| 89 | |
| 90 | const maxValue = createMemo(() => Math.max(0, ...totals().values())); |
| 91 | const todayStart = createMemo(() => startOfDay(props.endsAt ?? Date.now())); |
| 92 | |
| 93 | /** 0 = no data, 1..4 = quartiles of the busiest day. */ |
| 94 | const levelOf = (value: number) => { |
| 95 | if (value <= 0) return 0; |
| 96 | const peak = maxValue(); |
| 97 | if (peak <= 0) return 0; |
| 98 | return Math.min(4, Math.ceil((value / peak) * 4)); |
| 99 | }; |
| 100 | |
| 101 | const levelClasses = [ |
| 102 | "bg-muted/60 dark:bg-muted/30", |
| 103 | "bg-primary/25", |
| 104 | "bg-primary/45", |
| 105 | "bg-primary/70", |
| 106 | "bg-primary", |
| 107 | ]; |
| 108 | |
| 109 | const monthFormat = createMemo( |
| 110 | () => new Intl.DateTimeFormat(locale() === "tr" ? "tr-TR" : "en-GB", { month: "short" }), |
| 111 | ); |
| 112 | const weekdayFormat = createMemo( |
| 113 | () => new Intl.DateTimeFormat(locale() === "tr" ? "tr-TR" : "en-GB", { weekday: "short" }), |
| 114 | ); |
| 115 | |
| 116 | // A month label sits above the first column whose Monday opens a new month. |
| 117 | const monthLabels = createMemo(() => |
| 118 | columns().map((week, index) => { |
| 119 | const monday = new Date(week[0]); |
| 120 | if (index > 0) { |
| 121 | const previous = new Date(columns()[index - 1][0]); |
| 122 | if (previous.getMonth() === monday.getMonth()) return ""; |
| 123 | } |
| 124 | return monthFormat().format(monday); |
| 125 | }), |
| 126 | ); |
| 127 | |
| 128 | // Monday / Wednesday / Friday only — the full seven would not fit the gutter. |
| 129 | const weekdayLabels = createMemo(() => { |
| 130 | const week = columns()[0] ?? []; |
| 131 | return week.map((day, index) => (index % 2 === 0 && index < 6 ? weekdayFormat().format(day) : "")); |
| 132 | }); |
| 133 | |
| 134 | const hasData = createMemo(() => maxValue() > 0); |
| 135 | |
| 136 | return ( |
| 137 | <div class={cn("flex flex-col gap-4 rounded-xl border border-border-line bg-surface-base p-4", props.class)}> |
| 138 | <Show when={props.title}> |
| 139 | <div class="border-b border-border-hairline pb-3"> |
| 140 | <h3 class="text-sm font-semibold tracking-tight text-foreground">{props.title}</h3> |
| 141 | <Show when={props.subtitle}> |
| 142 | <p class="mt-0.5 text-xs text-muted-foreground">{props.subtitle}</p> |
| 143 | </Show> |
| 144 | </div> |
| 145 | </Show> |
| 146 | |
| 147 | <Show |
| 148 | when={hasData()} |
| 149 | fallback={ |
| 150 | <EmptyInline illustration="charts" title={t("dashboard.chartEmpty")} hint={t("dashboard.chartEmptyHint")} /> |
| 151 | } |
| 152 | > |
| 153 | {/* One grid for labels and cells together: a weekday gutter column plus |
| 154 | an equal fraction per week, so the calendar always spans the card's |
| 155 | full width and every label stays on its own row. Below the min-width |
| 156 | it scrolls instead of shrinking cells past legibility. */} |
| 157 | {/* `px-1` keeps today's focus ring off the scroll container's clip |
| 158 | edge, which otherwise shaves the last column's cells in half. */} |
| 159 | <div class="overflow-x-auto px-1 pb-1"> |
| 160 | <div |
| 161 | class="grid min-w-[460px] gap-1" |
| 162 | style={{ "grid-template-columns": `auto repeat(${weeks()}, minmax(0, 1fr))` }} |
| 163 | > |
| 164 | <span aria-hidden="true" /> |
| 165 | <For each={monthLabels()}> |
| 166 | {(label) => ( |
| 167 | <span class="text-[11px] leading-none text-muted-foreground">{label}</span> |
| 168 | )} |
| 169 | </For> |
| 170 | |
| 171 | <For each={weekdayLabels()}> |
| 172 | {(weekdayLabel, dayIndex) => ( |
| 173 | <> |
| 174 | <span class="pr-1 text-right text-[11px] leading-none text-muted-foreground"> |
| 175 | {weekdayLabel} |
| 176 | </span> |
| 177 | <For each={columns()}> |
| 178 | {(week) => { |
| 179 | const day = week[dayIndex()]; |
| 180 | const value = () => totals().get(day) ?? 0; |
| 181 | // Days after today are outside the observable range — |
| 182 | // drawn as an empty slot so the last week keeps its shape. |
| 183 | const future = () => day > todayStart(); |
| 184 | const label = () => |
| 185 | value() > 0 ? props.dayLabel(value(), day) : props.emptyDayLabel(day); |
| 186 | return ( |
| 187 | <Show |
| 188 | when={!future()} |
| 189 | fallback={ |
| 190 | // Days after today simply do not exist yet. They hold |
| 191 | // the last column's shape but stay invisible — an |
| 192 | // outlined placeholder reads as a half-drawn cell. |
| 193 | <span aria-hidden="true" class="aspect-square w-full" /> |
| 194 | } |
| 195 | > |
| 196 | <span |
| 197 | class={cn( |
| 198 | "aspect-square w-full rounded-[3px] transition-colors", |
| 199 | levelClasses[levelOf(value())], |
| 200 | day === todayStart() && "ring-1 ring-ring", |
| 201 | )} |
| 202 | title={label()} |
| 203 | aria-label={label()} |
| 204 | role="img" |
| 205 | /> |
| 206 | </Show> |
| 207 | ); |
| 208 | }} |
| 209 | </For> |
| 210 | </> |
| 211 | )} |
| 212 | </For> |
| 213 | </div> |
| 214 | </div> |
| 215 | |
| 216 | <div class="flex flex-wrap items-center justify-between gap-2 border-t border-border-hairline pt-3"> |
| 217 | <Show when={props.footer}> |
| 218 | <p class="text-[11px] text-muted-foreground">{props.footer}</p> |
| 219 | </Show> |
| 220 | <div class="ml-auto flex items-center gap-1.5"> |
| 221 | <span class="text-[11px] text-muted-foreground">{t("dashboard.heatmapLess")}</span> |
| 222 | <For each={levelClasses}> |
| 223 | {(levelClass) => <span class={cn("h-3 w-3 rounded-[3px]", levelClass)} />} |
| 224 | </For> |
| 225 | <span class="text-[11px] text-muted-foreground">{t("dashboard.heatmapMore")}</span> |
| 226 | </div> |
| 227 | </div> |
| 228 | </Show> |
| 229 | </div> |
| 230 | ); |
| 231 | }; |