import { For, Show, createMemo, createSignal } from "solid-js"; import type { BadgeCatalogEntry, EarnedBadge, ProfileStats, Role } from "@/api/client"; import { PixelIcon, type PixelIconName } from "@/components/ui/pixel-icon"; import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip"; import { badgeDescKey, badgeGroupKey, badgeLadders, badgeLevel, badgeNameKey, badgeProgress, badgeProgressLabel, badgeProgressRatio, badgeRemaining, badgeThresholdLabel, badgeTier, type BadgeTier, } from "@/lib/badges"; import { cn } from "@/lib/cn"; import { formatDate } from "@/lib/format"; import { usePreferences, useT } from "@/stores/preferences-context"; const STAT_ICON: Record = { homework_submitted: "notes", homework_on_time: "check-double", exam_sat: "pen-square", pomodoro_finished: "zap", pomodoro_focus_ms: "hourglass", marks_given: "pencil", lessons_held: "book-open", pool_approved: "checkbox-on", pool_published: "message-text", lessons_attended: "calendar-weeks", high_mark: "star", study_streak: "fire", }; const TIER_MEDAL: Record = { bronze: "bg-amber-700/12 text-amber-700 ring-amber-700/25 dark:text-amber-500", silver: "bg-slate-400/15 text-slate-500 ring-slate-400/35 dark:text-slate-300", gold: "bg-yellow-400/20 text-yellow-600 ring-yellow-500/40 dark:text-yellow-400", }; export const statIcon = (stat: string): PixelIconName => STAT_ICON[stat] ?? "sparkles"; /** * The profile's badge board: one card per counter, each a ladder of * bronze → silver → gold rungs. Unearned rungs stay visible and locked, so the * next goal is legible before it is reached; nothing here is scored beyond the * backend's own counters and thresholds. */ export function BadgeGrid(props: { /** The whole catalogue, from limits.badges.catalog. */ catalog: BadgeCatalogEntry[]; earned: EarnedBadge[]; stats: ProfileStats; /** Whose profile this is — their role's ladders lead the board. */ role?: Role; }) { const t = useT(); const prefs = usePreferences(); const earnedAt = createMemo(() => new Map(props.earned.map((b) => [b.id, b.earned_at] as const))); const earnedCount = createMemo(() => props.catalog.filter((entry) => earnedAt().has(entry.id)).length); const current = (stat: string) => badgeProgress(stat, props.stats); const [showAll, setShowAll] = createSignal(false); // Ladders with earned rungs first, then the ones already in progress, then // the counters the owner's role actually moves; the rest fold away. const ladders = createMemo(() => { const staff = props.role === "teacher" || props.role === "manager" || props.role === "admin"; const staffStats = new Set(["marks_given", "lessons_held", "pool_approved"]); const score = (ladder: { stat: string; entries: BadgeCatalogEntry[] }) => { const earned = ladder.entries.filter((entry) => earnedAt().has(entry.id)).length; const progress = (current(ladder.stat) ?? 0) > 0 ? 1 : 0; const relevant = props.role == null || staffStats.has(ladder.stat) === staff ? 1 : 0; return earned * 100 + progress * 10 + relevant; }; return badgeLadders(props.catalog) .map((ladder, index) => ({ ladder, index, score: score(ladder) })) .sort((a, b) => b.score - a.score || a.index - b.index) .map((item) => item.ladder); }); // Folded, the board shows exactly one grid row: 1 card on phones, 2 from // sm, 3 from xl — the extra preview cards hide below their breakpoint. const LADDER_PREVIEW = 3; const visibleLadders = () => (showAll() ? ladders() : ladders().slice(0, LADDER_PREVIEW)); const previewClass = (index: number) => showAll() ? "" : index === 1 ? "hidden sm:flex" : index === 2 ? "hidden xl:flex" : ""; // A badge the backend ships before this build has copy for must still render. const label = (id: string) => { const key = badgeNameKey(id); return key ? t(key) : id; }; const description = (id: string) => { const key = badgeDescKey(id); return key ? t(key) : ""; }; // The closest unearned rung across every tracked ladder. const next = createMemo(() => { let best: { entry: BadgeCatalogEntry; ratio: number } | null = null; for (const entry of props.catalog) { if (earnedAt().has(entry.id)) continue; const value = current(entry.stat); if (value === null) continue; const ratio = badgeProgressRatio(value, entry.threshold); if (!best || ratio > best.ratio) best = { entry, ratio }; } return best; }); const level = () => badgeLevel(earnedCount(), props.catalog.length); return (
0} fallback={

{t("badges.none")}

}> {t("badges.allEarned")} } > {(n) => (

{t("badges.next")}

{description(n().entry.id) || label(n().entry.id)}

{t(n().entry.stat === "pomodoro_focus_ms" ? "badges.remainingHours" : "badges.remaining", { count: badgeRemaining(n().entry.stat, current(n().entry.stat) ?? 0, n().entry.threshold), })}

)}
{(ladder, ladderIndex) => { const groupKey = badgeGroupKey(ladder.stat); const earnedInLadder = () => ladder.entries.filter((entry) => earnedAt().has(entry.id)).length; return (
0 ? "bg-primary/12 text-primary-text" : "bg-muted text-muted-foreground", )} >

{groupKey ? t(groupKey) : ladder.stat}

{(entry, index) => { const at = () => earnedAt().get(entry.id); const value = () => current(entry.stat) ?? 0; const tracked = () => current(entry.stat) !== null; const tier = () => badgeTier(index(), ladder.entries.length); const isHours = entry.stat === "pomodoro_focus_ms"; // The tooltip enriches; the row itself carries every // number, since a touch device can never open one. const hint = () => at() ? t("badges.earnedOn", { date: formatDate(at()!, prefs.locale()) }) : tracked() ? t(isHours ? "badges.remainingHours" : "badges.remaining", { count: badgeRemaining(entry.stat, value(), entry.threshold), }) : t("badges.locked"); return (

{label(entry.id)}

{hint()}

} >
{badgeProgressLabel(entry.stat, value())} / {badgeThresholdLabel(entry.stat, entry.threshold)}

{label(entry.id)}

{description(entry.id)}

{hint()}

); }}
); }}
1}>
); }