"use client"; import { useEffect, useId, useRef, useState, type CSSProperties, type KeyboardEvent } from "react"; import "./lesson-path.css"; export type LessonState = "done" | "current" | "locked"; export type PathLesson = { id: string; title: string; xp: number; state: LessonState }; export type PathUnit = { id: string; title: string; subtitle: string; color: "green" | "blue" | "purple" | "orange" | "pink"; lessons: PathLesson[] }; export type LessonPathProps = { units: PathUnit[]; /** Called with the lesson id when Start or Practice is pressed. */ onStart?: (lessonId: string) => void; theme?: "light" | "dark"; motion?: "auto" | "reduced"; className?: string; }; /** Sideways offset for the nth node, as a fraction of the swing: a gentle S-curve. */ const swing = (i: number) => Math.round(Math.sin(i * 0.95) * 100) / 100; function Glyph({ state }: { state: LessonState }) { if (state === "done") return ; if (state === "locked") return ; return ; } /** * Lesson Path * A course drawn as a winding path of round lessons: finished ones carry a * tick, the next one waits with a Start flag, locked ones sit grey. Press a * lesson to see what it is and how much it's worth. */ export function LessonPath({ units, onStart, theme = "light", motion = "auto", className = "" }: LessonPathProps) { const id = useId(); const all = units.flatMap((u) => u.lessons.map((l) => ({ ...l, unit: u }))); const current = all.find((l) => l.state === "current") ?? all[0]; const [open, setOpen] = useState(null); const [focus, setFocus] = useState(current?.id); const nodes = useRef(new Map()); const root = useRef(null); useEffect(() => { if (!open) return; const down = (e: PointerEvent) => { if (!root.current?.contains(e.target as Node) || !(e.target as Element).closest("[data-lesson]")) setOpen(null); }; document.addEventListener("pointerdown", down); return () => document.removeEventListener("pointerdown", down); }, [open]); const key = (lessonId: string, e: KeyboardEvent) => { const i = all.findIndex((l) => l.id === lessonId); let next = -1; if (e.key === "ArrowDown" || e.key === "ArrowRight") next = Math.min(all.length - 1, i + 1); if (e.key === "ArrowUp" || e.key === "ArrowLeft") next = Math.max(0, i - 1); if (e.key === "Home") next = 0; if (e.key === "End") next = all.length - 1; if (next >= 0) { e.preventDefault(); setFocus(all[next].id); setOpen(null); nodes.current.get(all[next].id)?.focus(); } if (e.key === "Escape" && open) { e.preventDefault(); setOpen(null); } }; let n = 0; return (
{units.map((unit, ui) => { const total = unit.lessons.length; return (

Unit {ui + 1}

{unit.title}

{unit.subtitle}

    {unit.lessons.map((lesson, li) => { const index = n++; const isOpen = open === lesson.id; const done = unit.lessons.filter((l) => l.state === "done").length; const prev = all[index - 1]; return (
  1. {lesson.state === "current" && }
  2. ); })}
); })}
); }