Repository Public
1"use client";2import Link from "next/link";3import { useMemo, useState } from "react";4import { LEVEL_LABEL, LEVEL_RANK, accessLabel, pad2 } from "@/lib/labels";5import { sessionPath, type Part, type SessionView } from "@/modules/content-schema";67type S = Pick<SessionView, "id" | "number" | "slug" | "part" | "level" | "title" | "short" | "dependsOn" | "unlocks" | "access" | "previewOnly">;89const W = 1180, COL = 190, X0 = 28, NODE_W = 164, NODE_H = 64, Y0 = 78, ROW = 92;1011/** Breaks a short title into at most two lines of about 20 characters. */12function wrap(text: string): string[] {13 const words = text.split(" "), lines: string[] = [""];14 for (const w of words) {15 const last = lines[lines.length - 1]!;16 if (last && (last + " " + w).length > 21 && lines.length < 2) lines.push(w);17 else lines[lines.length - 1] = last ? `${last} ${w}` : w;18 }19 if (lines[1] && lines[1].length > 22) lines[1] = `${lines[1].slice(0, 20).trimEnd()}…`;20 return lines;21}2223/**24 * The course as a prerequisite graph: six columns (parts), one node per session, one edge per real25 * prerequisite from the course plan. Selecting a session shows everything that must come before it and26 * what it opens. The same information exists as plain lists on /programme and on each session page.27 */28export function CourseMap({ parts, sessions }: { parts: Part[]; sessions: S[] }) {29 const [focus, setFocus] = useState<string | null>(null);30 const byId = useMemo(() => new Map(sessions.map((s) => [s.id, s])), [sessions]);31 const pos = useMemo(() => {32 const m = new Map<string, { x: number; y: number }>();33 parts.forEach((p, col) => sessions.filter((s) => s.part === p.id).forEach((s, row) => m.set(s.id, { x: X0 + col * COL, y: Y0 + row * ROW })));34 return m;35 }, [parts, sessions]);36 const rows = Math.max(...parts.map((p) => sessions.filter((s) => s.part === p.id).length));37 const H = Y0 + rows * ROW + 6;3839 // All ancestors of the focused session (transitive prerequisites).40 const ancestors = useMemo(() => {41 const out = new Set<string>();42 const visit = (id: string) => { for (const d of byId.get(id)?.dependsOn ?? []) if (!out.has(d)) { out.add(d); visit(d); } };43 if (focus) visit(focus);44 return out;45 }, [focus, byId]);46 const current = focus ? byId.get(focus) : null;47 const unlocks = new Set(current?.unlocks ?? []);48 const role = (id: string) => (!focus ? "idle" : id === focus ? "self" : ancestors.has(id) ? "before" : unlocks.has(id) ? "after" : "dim");4950 const edges = sessions.flatMap((s) => s.dependsOn.map((d) => ({ from: d, to: s.id })));51 const edgePath = (from: string, to: string) => {52 const a = pos.get(from)!, b = pos.get(to)!;53 if (a.x === b.x) { // same part: bow out on the left side54 const x = a.x, bend = 18 + Math.abs(b.y - a.y) * 0.08;55 return `M${x} ${a.y + NODE_H / 2} C${x - bend} ${a.y + NODE_H / 2}, ${x - bend} ${b.y + NODE_H / 2}, ${x} ${b.y + NODE_H / 2}`;56 }57 const x1 = a.x + NODE_W, y1 = a.y + NODE_H / 2, x2 = b.x, y2 = b.y + NODE_H / 2, dx = Math.max(26, (x2 - x1) * 0.45);58 return `M${x1} ${y1} C${x1 + dx} ${y1}, ${x2 - dx} ${y2}, ${x2} ${y2}`;59 };6061 return (62 <div>63 <div className="card hidden overflow-hidden lg:block">64 <svg viewBox={`0 0 ${W} ${H}`} className="block h-auto w-full" role="group" aria-label="Carte du cours : les 24 séances et leurs préalables" onMouseLeave={() => setFocus(null)}>65 {parts.map((p, col) => (66 <g key={p.id}>67 <text x={X0 + col * COL} y={30} className="fill-faint font-mono" fontSize="11" letterSpacing="1.5">PARTIE {p.id}</text>68 <text x={X0 + col * COL} y={52} className="fill-fg font-display" fontSize="17" fontWeight="560">{p.short}</text>69 </g>70 ))}71 <g aria-hidden="true">72 {edges.map(({ from, to }) => {73 const on = focus && (to === focus || (ancestors.has(to) && ancestors.has(from))), after = focus && from === focus;74 return <path key={`${from}-${to}`} d={edgePath(from, to)} fill="none" stroke={on ? "var(--blue)" : after ? "var(--teal)" : "var(--line-strong)"} strokeWidth={on || after ? 2.2 : 1.1} opacity={focus ? (on || after ? 1 : 0.12) : 0.5} strokeDasharray={after ? "5 4" : undefined} />;75 })}76 </g>77 {sessions.map((s) => {78 const p = pos.get(s.id)!, r = role(s.id), lines = wrap(s.short);79 const stroke = r === "self" ? "var(--fg)" : r === "before" ? "var(--blue)" : r === "after" ? "var(--teal)" : "var(--line)";80 const fill = r === "self" ? "var(--raised)" : r === "before" ? "var(--blue-soft)" : r === "after" ? "var(--teal-soft)" : "var(--surface)";81 return (82 <a key={s.id} href={sessionPath(s.slug)} onMouseEnter={() => setFocus(s.id)} onFocus={() => setFocus(s.id)} onBlur={() => setFocus(null)} style={{ opacity: r === "dim" ? 0.35 : 1 }}>83 <desc>{`Séance ${s.number} : ${s.title}. Niveau ${LEVEL_LABEL[s.level].toLowerCase()}. ${s.dependsOn.length ? `Préalables : séances ${s.dependsOn.map((d) => Number(d.slice(1))).join(", ")}.` : "Aucun préalable."} ${accessLabel(s)}.`}</desc>84 <rect x={p.x} y={p.y} width={NODE_W} height={NODE_H} rx={12} fill={fill} stroke={stroke} strokeWidth={r === "idle" || r === "dim" ? 1.2 : 2.2} />85 <text x={p.x + 12} y={p.y + 20} className="fill-faint font-mono" fontSize="10.5">{pad2(s.number)}</text>86 {[1, 2, 3].map((i) => <circle key={i} cx={p.x + NODE_W - 30 + i * 7} cy={p.y + 16} r={2.4} fill={i <= LEVEL_RANK[s.level] ? "var(--fg)" : "var(--line)"} />)}87 {lines.map((l, i) => <text key={i} x={p.x + 12} y={p.y + 38 + i * 15} className="fill-fg" fontSize="12.5" fontWeight="500">{l}</text>)}88 </a>89 );90 })}91 </svg>92 <div className="flex min-h-[4.5rem] flex-wrap items-center gap-x-8 gap-y-2 border-t border-line bg-panel px-5 py-3 text-sm" aria-live="polite">93 {current ? (94 <>95 <p className="font-medium">Séance {current.number} — {current.title}</p>96 <p className="text-muted"><span className="mr-1.5 inline-block size-2.5 rounded-sm bg-blue align-baseline" aria-hidden="true" />À suivre avant : {ancestors.size ? [...ancestors].sort().map((d) => Number(d.slice(1))).join(", ") : "aucune séance"}</p>97 <p className="text-muted"><span className="mr-1.5 inline-block size-2.5 rounded-sm bg-teal align-baseline" aria-hidden="true" />Ouvre l’accès à : {current.unlocks.length ? current.unlocks.map((d) => Number(d.slice(1))).join(", ") : "aucune autre séance"}</p>98 </>99 ) : (100 <p className="text-muted">Survolez une séance, ou parcourez-les avec la touche Tab, pour voir ses préalables et ce qu’elle ouvre. Les points indiquent le niveau : un pour débutant, deux pour intermédiaire, trois pour avancé.</p>101 )}102 </div>103 </div>104 {/* Small screens: the same structure as six stacked lists. */}105 <ol className="grid gap-4 sm:grid-cols-2 lg:hidden">106 {parts.map((p) => (107 <li key={p.id} className="card p-5">108 <p className="eyebrow">Partie {p.id}</p>109 <h3 className="mt-1 text-xl">{p.short}</h3>110 <ol className="mt-3 grid gap-2.5">111 {sessions.filter((s) => s.part === p.id).map((s) => (112 <li key={s.id} className="flex gap-3 text-[0.95rem]">113 <span className="mt-0.5 font-mono text-sm text-faint">{pad2(s.number)}</span>114 <span>115 <Link href={sessionPath(s.slug)} className="hover:underline">{s.short}</Link>116 {s.dependsOn.length > 0 && <span className="block text-sm text-faint">Après {s.dependsOn.map((d) => Number(d.slice(1))).join(", ")}</span>}117 </span>118 </li>119 ))}120 </ol>121 </li>122 ))}123 </ol>124 </div>125 );126}127