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, createSignal } from "solid-js"; |
| 2 | import type { BadgeCatalogEntry, EarnedBadge, ProfileStats, Role } from "@/api/client"; |
| 3 | import { PixelIcon, type PixelIconName } from "@/components/ui/pixel-icon"; |
| 4 | import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip"; |
| 5 | import { |
| 6 | badgeDescKey, |
| 7 | badgeGroupKey, |
| 8 | badgeLadders, |
| 9 | badgeLevel, |
| 10 | badgeNameKey, |
| 11 | badgeProgress, |
| 12 | badgeProgressLabel, |
| 13 | badgeProgressRatio, |
| 14 | badgeRemaining, |
| 15 | badgeThresholdLabel, |
| 16 | badgeTier, |
| 17 | type BadgeTier, |
| 18 | } from "@/lib/badges"; |
| 19 | import { cn } from "@/lib/cn"; |
| 20 | import { formatDate } from "@/lib/format"; |
| 21 | import { usePreferences, useT } from "@/stores/preferences-context"; |
| 22 | |
| 23 | const STAT_ICON: Record<string, PixelIconName> = { |
| 24 | homework_submitted: "notes", |
| 25 | homework_on_time: "check-double", |
| 26 | exam_sat: "pen-square", |
| 27 | pomodoro_finished: "zap", |
| 28 | pomodoro_focus_ms: "hourglass", |
| 29 | marks_given: "pencil", |
| 30 | lessons_held: "book-open", |
| 31 | pool_approved: "checkbox-on", |
| 32 | pool_published: "message-text", |
| 33 | lessons_attended: "calendar-weeks", |
| 34 | high_mark: "star", |
| 35 | study_streak: "fire", |
| 36 | }; |
| 37 | |
| 38 | const TIER_MEDAL: Record<BadgeTier, string> = { |
| 39 | bronze: "bg-amber-700/12 text-amber-700 ring-amber-700/25 dark:text-amber-500", |
| 40 | silver: "bg-slate-400/15 text-slate-500 ring-slate-400/35 dark:text-slate-300", |
| 41 | gold: "bg-yellow-400/20 text-yellow-600 ring-yellow-500/40 dark:text-yellow-400", |
| 42 | }; |
| 43 | |
| 44 | export const statIcon = (stat: string): PixelIconName => STAT_ICON[stat] ?? "sparkles"; |
| 45 | |
| 46 | /** |
| 47 | * The profile's badge board: one card per counter, each a ladder of |
| 48 | * bronze → silver → gold rungs. Unearned rungs stay visible and locked, so the |
| 49 | * next goal is legible before it is reached; nothing here is scored beyond the |
| 50 | * backend's own counters and thresholds. |
| 51 | */ |
| 52 | export function BadgeGrid(props: { |
| 53 | /** The whole catalogue, from limits.badges.catalog. */ |
| 54 | catalog: BadgeCatalogEntry[]; |
| 55 | earned: EarnedBadge[]; |
| 56 | stats: ProfileStats; |
| 57 | /** Whose profile this is — their role's ladders lead the board. */ |
| 58 | role?: Role; |
| 59 | }) { |
| 60 | const t = useT(); |
| 61 | const prefs = usePreferences(); |
| 62 | |
| 63 | const earnedAt = createMemo(() => new Map(props.earned.map((b) => [b.id, b.earned_at] as const))); |
| 64 | const earnedCount = createMemo(() => props.catalog.filter((entry) => earnedAt().has(entry.id)).length); |
| 65 | const current = (stat: string) => badgeProgress(stat, props.stats); |
| 66 | const [showAll, setShowAll] = createSignal(false); |
| 67 | // Ladders with earned rungs first, then the ones already in progress, then |
| 68 | // the counters the owner's role actually moves; the rest fold away. |
| 69 | const ladders = createMemo(() => { |
| 70 | const staff = props.role === "teacher" || props.role === "manager" || props.role === "admin"; |
| 71 | const staffStats = new Set(["marks_given", "lessons_held", "pool_approved"]); |
| 72 | const score = (ladder: { stat: string; entries: BadgeCatalogEntry[] }) => { |
| 73 | const earned = ladder.entries.filter((entry) => earnedAt().has(entry.id)).length; |
| 74 | const progress = (current(ladder.stat) ?? 0) > 0 ? 1 : 0; |
| 75 | const relevant = props.role == null || staffStats.has(ladder.stat) === staff ? 1 : 0; |
| 76 | return earned * 100 + progress * 10 + relevant; |
| 77 | }; |
| 78 | return badgeLadders(props.catalog) |
| 79 | .map((ladder, index) => ({ ladder, index, score: score(ladder) })) |
| 80 | .sort((a, b) => b.score - a.score || a.index - b.index) |
| 81 | .map((item) => item.ladder); |
| 82 | }); |
| 83 | // Folded, the board shows exactly one grid row: 1 card on phones, 2 from |
| 84 | // sm, 3 from xl — the extra preview cards hide below their breakpoint. |
| 85 | const LADDER_PREVIEW = 3; |
| 86 | const visibleLadders = () => (showAll() ? ladders() : ladders().slice(0, LADDER_PREVIEW)); |
| 87 | const previewClass = (index: number) => |
| 88 | showAll() ? "" : index === 1 ? "hidden sm:flex" : index === 2 ? "hidden xl:flex" : ""; |
| 89 | |
| 90 | // A badge the backend ships before this build has copy for must still render. |
| 91 | const label = (id: string) => { |
| 92 | const key = badgeNameKey(id); |
| 93 | return key ? t(key) : id; |
| 94 | }; |
| 95 | const description = (id: string) => { |
| 96 | const key = badgeDescKey(id); |
| 97 | return key ? t(key) : ""; |
| 98 | }; |
| 99 | |
| 100 | // The closest unearned rung across every tracked ladder. |
| 101 | const next = createMemo(() => { |
| 102 | let best: { entry: BadgeCatalogEntry; ratio: number } | null = null; |
| 103 | for (const entry of props.catalog) { |
| 104 | if (earnedAt().has(entry.id)) continue; |
| 105 | const value = current(entry.stat); |
| 106 | if (value === null) continue; |
| 107 | const ratio = badgeProgressRatio(value, entry.threshold); |
| 108 | if (!best || ratio > best.ratio) best = { entry, ratio }; |
| 109 | } |
| 110 | return best; |
| 111 | }); |
| 112 | |
| 113 | const level = () => badgeLevel(earnedCount(), props.catalog.length); |
| 114 | |
| 115 | return ( |
| 116 | <section class="data-shell space-y-4 p-4"> |
| 117 | <button |
| 118 | type="button" |
| 119 | class="flex w-full flex-wrap items-center justify-between gap-3 rounded-lg text-left outline-hidden focus-visible:ring-2 focus-visible:ring-ring" |
| 120 | aria-expanded={showAll()} |
| 121 | onClick={() => setShowAll(!showAll())} |
| 122 | > |
| 123 | <div class="min-w-0"> |
| 124 | <h2 class="flex items-center gap-2 text-lg font-semibold tracking-tight text-foreground"> |
| 125 | <PixelIcon name="trophy" class="h-5 w-5 text-primary-text" /> |
| 126 | {t("badges.title")} |
| 127 | </h2> |
| 128 | <p class="mt-1 text-sm text-muted-foreground">{t("badges.subtitle")}</p> |
| 129 | </div> |
| 130 | <div class="flex items-center gap-3"> |
| 131 | <Show when={props.catalog.length > 0}> |
| 132 | <div class="hidden items-center gap-3 rounded-lg border border-border/70 bg-muted/40 px-3 py-2 sm:flex"> |
| 133 | <PixelIcon name="crown" class="h-5 w-5 text-yellow-500" /> |
| 134 | <div class="min-w-0"> |
| 135 | <p class="text-[11px] font-medium uppercase tracking-wide text-muted-foreground">{t("badges.levelLabel")}</p> |
| 136 | <p class="text-sm font-semibold">{t(`badges.level.${level()}`)}</p> |
| 137 | </div> |
| 138 | <div class="w-28"> |
| 139 | <span class="block h-1.5 overflow-hidden rounded-full bg-muted"> |
| 140 | <span |
| 141 | class="block h-full rounded-full bg-primary transition-[width] duration-700" |
| 142 | style={{ width: `${(earnedCount() / props.catalog.length) * 100}%` }} |
| 143 | /> |
| 144 | </span> |
| 145 | <p class="mt-1 text-right text-[11px] tabular-nums text-muted-foreground"> |
| 146 | {t("badges.earnedCount", { earned: earnedCount(), total: props.catalog.length })} |
| 147 | </p> |
| 148 | </div> |
| 149 | </div> |
| 150 | </Show> |
| 151 | <PixelIcon name={showAll() ? "chevron-up" : "chevron-down"} class="h-5 w-5 shrink-0 text-muted-foreground" /> |
| 152 | </div> |
| 153 | </button> |
| 154 | |
| 155 | <Show when={showAll()}> |
| 156 | <Show when={props.catalog.length > 0} fallback={<p class="text-sm text-muted-foreground">{t("badges.none")}</p>}> |
| 157 | <Show |
| 158 | when={next()} |
| 159 | fallback={ |
| 160 | <div class="flex items-center gap-3 rounded-lg border border-yellow-500/30 bg-yellow-400/10 px-4 py-3 text-sm font-medium"> |
| 161 | <PixelIcon name="trophy" class="h-5 w-5 text-yellow-600" /> |
| 162 | {t("badges.allEarned")} |
| 163 | </div> |
| 164 | } |
| 165 | > |
| 166 | {(n) => ( |
| 167 | <div class="flex flex-wrap items-center gap-3 rounded-lg border border-primary/25 bg-primary/5 px-4 py-3"> |
| 168 | <span class="flex h-9 w-9 items-center justify-center rounded-md bg-primary/15 text-primary-text"> |
| 169 | <PixelIcon name={statIcon(n().entry.stat)} class="h-5 w-5" /> |
| 170 | </span> |
| 171 | <div class="min-w-0 flex-1"> |
| 172 | <p class="text-[11px] font-medium uppercase tracking-wide text-primary-text">{t("badges.next")}</p> |
| 173 | <p class="truncate text-sm text-muted-foreground">{description(n().entry.id) || label(n().entry.id)}</p> |
| 174 | </div> |
| 175 | <div class="w-40"> |
| 176 | <span class="block h-2 overflow-hidden rounded-full bg-primary/10"> |
| 177 | <span class="block h-full rounded-full bg-primary transition-[width] duration-700" style={{ width: `${n().ratio * 100}%` }} /> |
| 178 | </span> |
| 179 | <p class="mt-1 text-right text-[11px] tabular-nums text-muted-foreground"> |
| 180 | {t(n().entry.stat === "pomodoro_focus_ms" ? "badges.remainingHours" : "badges.remaining", { |
| 181 | count: badgeRemaining(n().entry.stat, current(n().entry.stat) ?? 0, n().entry.threshold), |
| 182 | })} |
| 183 | </p> |
| 184 | </div> |
| 185 | </div> |
| 186 | )} |
| 187 | </Show> |
| 188 | |
| 189 | <div class="grid gap-3 sm:grid-cols-2 xl:grid-cols-3"> |
| 190 | <For each={visibleLadders()}> |
| 191 | {(ladder, ladderIndex) => { |
| 192 | const groupKey = badgeGroupKey(ladder.stat); |
| 193 | const earnedInLadder = () => ladder.entries.filter((entry) => earnedAt().has(entry.id)).length; |
| 194 | return ( |
| 195 | <article class={cn("flex flex-col gap-3 rounded-xl border border-border/70 bg-card p-3", previewClass(ladderIndex()))}> |
| 196 | <header class="flex items-center gap-2.5"> |
| 197 | <span |
| 198 | class={cn( |
| 199 | "flex h-8 w-8 items-center justify-center rounded-md", |
| 200 | earnedInLadder() > 0 ? "bg-primary/12 text-primary-text" : "bg-muted text-muted-foreground", |
| 201 | )} |
| 202 | > |
| 203 | <PixelIcon name={statIcon(ladder.stat)} class="h-[18px] w-[18px]" /> |
| 204 | </span> |
| 205 | <p class="min-w-0 flex-1 truncate text-sm font-semibold">{groupKey ? t(groupKey) : ladder.stat}</p> |
| 206 | <span class="flex gap-0.5" aria-hidden="true"> |
| 207 | <For each={ladder.entries}> |
| 208 | {(entry, index) => ( |
| 209 | <span |
| 210 | class={cn( |
| 211 | "h-2 w-2 rounded-[1px]", |
| 212 | earnedAt().has(entry.id) |
| 213 | ? { bronze: "bg-amber-700", silver: "bg-slate-400", gold: "bg-yellow-500" }[badgeTier(index(), ladder.entries.length)] |
| 214 | : "bg-muted-foreground/20", |
| 215 | )} |
| 216 | /> |
| 217 | )} |
| 218 | </For> |
| 219 | </span> |
| 220 | </header> |
| 221 | |
| 222 | <div class="grid gap-1.5" role="list"> |
| 223 | <For each={ladder.entries}> |
| 224 | {(entry, index) => { |
| 225 | const at = () => earnedAt().get(entry.id); |
| 226 | const value = () => current(entry.stat) ?? 0; |
| 227 | const tracked = () => current(entry.stat) !== null; |
| 228 | const tier = () => badgeTier(index(), ladder.entries.length); |
| 229 | const isHours = entry.stat === "pomodoro_focus_ms"; |
| 230 | // The tooltip enriches; the row itself carries every |
| 231 | // number, since a touch device can never open one. |
| 232 | const hint = () => |
| 233 | at() |
| 234 | ? t("badges.earnedOn", { date: formatDate(at()!, prefs.locale()) }) |
| 235 | : tracked() |
| 236 | ? t(isHours ? "badges.remainingHours" : "badges.remaining", { |
| 237 | count: badgeRemaining(entry.stat, value(), entry.threshold), |
| 238 | }) |
| 239 | : t("badges.locked"); |
| 240 | |
| 241 | return ( |
| 242 | <Tooltip openDelay={200} closeDelay={80}> |
| 243 | <TooltipTrigger |
| 244 | as="div" |
| 245 | role="listitem" |
| 246 | tabindex="0" |
| 247 | aria-label={`${label(entry.id)} — ${hint()}`} |
| 248 | class={cn( |
| 249 | "group/rung flex items-center gap-2.5 rounded-lg border p-2 outline-hidden transition-[transform,border-color,background-color] duration-200 hover:-translate-y-px focus-visible:ring-2 focus-visible:ring-ring", |
| 250 | at() ? "border-transparent bg-muted/40" : "border-dashed border-border/70", |
| 251 | )} |
| 252 | > |
| 253 | <span |
| 254 | class={cn( |
| 255 | "relative flex h-9 w-9 shrink-0 items-center justify-center rounded-md ring-1", |
| 256 | at() ? TIER_MEDAL[tier()] : "bg-muted text-muted-foreground/50 ring-transparent", |
| 257 | )} |
| 258 | > |
| 259 | <PixelIcon name={at() ? "trophy" : "lock"} class="h-5 w-5 transition-transform duration-200 group-hover/rung:scale-110" /> |
| 260 | </span> |
| 261 | <div class="min-w-0 flex-1"> |
| 262 | <p class={cn("truncate text-[13px] font-semibold", !at() && "text-muted-foreground")}>{label(entry.id)}</p> |
| 263 | <Show |
| 264 | when={!at() && tracked()} |
| 265 | fallback={<p class="truncate text-[11px] tabular-nums text-muted-foreground">{hint()}</p>} |
| 266 | > |
| 267 | <div class="flex items-center gap-2"> |
| 268 | <span class="block h-1 flex-1 overflow-hidden rounded-full bg-muted"> |
| 269 | <span |
| 270 | class="block h-full rounded-full bg-primary/70 transition-[width] duration-500" |
| 271 | style={{ width: `${badgeProgressRatio(value(), entry.threshold) * 100}%` }} |
| 272 | /> |
| 273 | </span> |
| 274 | <span class="shrink-0 text-[11px] tabular-nums text-muted-foreground"> |
| 275 | {badgeProgressLabel(entry.stat, value())} / {badgeThresholdLabel(entry.stat, entry.threshold)} |
| 276 | </span> |
| 277 | </div> |
| 278 | </Show> |
| 279 | </div> |
| 280 | </TooltipTrigger> |
| 281 | <TooltipContent> |
| 282 | <p class="font-semibold">{label(entry.id)}</p> |
| 283 | <Show when={description(entry.id)}> |
| 284 | <p class="mt-0.5 text-muted-foreground">{description(entry.id)}</p> |
| 285 | </Show> |
| 286 | <p class="mt-1 tabular-nums">{hint()}</p> |
| 287 | </TooltipContent> |
| 288 | </Tooltip> |
| 289 | ); |
| 290 | }} |
| 291 | </For> |
| 292 | </div> |
| 293 | </article> |
| 294 | ); |
| 295 | }} |
| 296 | </For> |
| 297 | </div> |
| 298 | <Show when={ladders().length > 1}> |
| 299 | <div class="flex justify-center"> |
| 300 | <button |
| 301 | type="button" |
| 302 | class="inline-flex h-8 items-center gap-2 rounded-lg border border-border/70 bg-muted/40 px-3 text-[13px] font-medium text-foreground transition-colors hover:bg-muted focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring" |
| 303 | aria-expanded={showAll()} |
| 304 | onClick={() => setShowAll(!showAll())} |
| 305 | > |
| 306 | <PixelIcon name={showAll() ? "chevron-up" : "chevron-down"} class="h-4 w-4" /> |
| 307 | {showAll() ? t("badges.showLess") : t("badges.showAll", { count: ladders().length })} |
| 308 | </button> |
| 309 | </div> |
| 310 | </Show> |
| 311 | </Show> |
| 312 | </Show> |
| 313 | </section> |
| 314 | ); |
| 315 | } |