SPB Git

Repository Public

Catalog.tsx 6.2 KB 94 lines tsx

1"use client";2import Link from "next/link";3import { useMemo, useState } from "react";4import { ProgressMark } from "@/components/session/ProgressMark";5import { Level, StateBadge } from "@/components/ui/Bits";6import { Icon } from "@/components/ui/Icon";7import { LEVEL_LABEL, accessLabel, pad2, plural } from "@/lib/labels";8import { sessionPath, type Part, type SessionView } from "@/modules/content-schema";9import { fold } from "@/modules/learning-engine/text";1011type S = Pick<SessionView, "id" | "number" | "slug" | "part" | "level" | "title" | "short" | "outcomes" | "dependsOn" | "tags" | "access" | "previewOnly" | "slidesOnly" | "lessonOnly"> & { capsuleIds: { id: string; optional: boolean }[]; hasQuiz: boolean };1213const LEVELS = ["débutant", "intermédiaire", "avancé"] as const;1415/** The 24 sessions grouped by part, with instant accent-insensitive filtering. Without JavaScript the full list is shown. */16export function Catalog({ parts, sessions }: { parts: Part[]; sessions: S[] }) {17  const [q, setQ] = useState("");18  const [part, setPart] = useState("");19  const [level, setLevel] = useState("");20  const [state, setState] = useState("");21  const states = useMemo(() => [...new Set(sessions.map((s) => accessLabel(s)))], [sessions]);22  const titles = useMemo(() => new Map(sessions.map((s) => [s.id, s])), [sessions]);23  const needle = fold(q);24  const shown = sessions.filter((s) =>25    (!part || s.part === part) && (!level || s.level === level) && (!state || accessLabel(s) === state) &&26    (!needle || needle.split(" ").every((w) => fold(`${s.title} ${s.short} ${s.tags.join(" ")} ${s.outcomes.join(" ")} séance ${s.number} ${s.id}`).includes(w))));27  const active = q || part || level || state;28  const reset = () => { setQ(""); setPart(""); setLevel(""); setState(""); };2930  return (31    <div>32      <form role="search" aria-label="Filtrer le programme" onSubmit={(e) => e.preventDefault()} className="card grid gap-3 p-4 md:grid-cols-[1.6fr_1fr_1fr_1fr_auto] md:items-end">33        <label className="grid gap-1 text-sm font-medium">Mot-clé34          <span className="relative"><Icon name="search" size={18} className="pointer-events-none absolute left-3 top-1/2 -translate-y-1/2 text-faint" />35            <input type="search" className="field !pl-10" placeholder="agent, jetons, Git…" value={q} onChange={(e) => setQ(e.target.value)} data-testid="catalog-search" /></span>36        </label>37        <label className="grid gap-1 text-sm font-medium">Partie38          <select className="field" value={part} onChange={(e) => setPart(e.target.value)} data-testid="catalog-part"><option value="">Toutes les parties</option>{parts.map((p) => <option key={p.id} value={p.id}>{p.id} — {p.short}</option>)}</select>39        </label>40        <label className="grid gap-1 text-sm font-medium">Niveau41          <select className="field" value={level} onChange={(e) => setLevel(e.target.value)} data-testid="catalog-level"><option value="">Tous les niveaux</option>{LEVELS.map((l) => <option key={l} value={l}>{LEVEL_LABEL[l]}</option>)}</select>42        </label>43        <label className="grid gap-1 text-sm font-medium">État44          <select className="field" value={state} onChange={(e) => setState(e.target.value)}><option value="">Tous les états</option>{states.map((s) => <option key={s}>{s}</option>)}</select>45        </label>46        <button type="button" className="btn btn-ghost" onClick={reset} disabled={!active}>Réinitialiser</button>47      </form>48      <p className="mt-4 text-sm text-muted" role="status" aria-live="polite" data-testid="catalog-count">{plural(shown.length, "séance affichée", "séances affichées")} sur {sessions.length}</p>4950      {shown.length === 0 && (51        <div className="card mt-6 p-8 text-center" data-testid="catalog-empty">52          <p className="font-display text-xl">Aucune séance ne correspond à ces critères.</p>53          <p className="mt-2 text-muted">Essayez un autre mot-clé ou retirez un filtre.</p>54          <button type="button" className="btn btn-quiet mt-5" onClick={reset}>Tout afficher</button>55        </div>56      )}5758      <div className="mt-8 grid gap-14">59        {parts.map((p) => {60          const list = shown.filter((s) => s.part === p.id);61          if (!list.length) return null;62          return (63            <section key={p.id} aria-labelledby={`partie-${p.id}`} className="grid gap-6 lg:grid-cols-[16rem_1fr] lg:gap-10">64              <div className="lg:sticky lg:top-24 lg:self-start">65                <p className="eyebrow">Partie {p.id}</p>66                <h2 id={`partie-${p.id}`} className="mt-2 text-[1.7rem]">{p.title}</h2>67              </div>68              <ol className="border-t border-line-strong">69                {list.map((s) => (70                  <li key={s.id} className="group relative grid gap-x-5 gap-y-2 border-b border-line py-5 sm:grid-cols-[3.2rem_1fr_auto]" data-session={s.id}>71                    <span className="font-display text-[2rem] leading-none text-faint" aria-hidden="true">{pad2(s.number)}</span>72                    <div>73                      <h3 className="text-[1.3rem]"><Link href={sessionPath(s.slug)} className="after:absolute after:inset-0 hover:underline"><span className="sr-only">Séance {s.number} : </span>{s.title}</Link></h3>74                      <p className="mt-2 flex flex-wrap items-center gap-x-4 gap-y-1 text-sm text-muted">75                        <Level level={s.level} />76                        <span>{s.dependsOn.length ? `Préalables : ${s.dependsOn.map((d) => `${Number(d.slice(1))} (${titles.get(d)?.short ?? d})`).join(" ; ")}` : "Aucun préalable"}</span>77                      </p>78                      {s.tags.length > 0 && <ul className="mt-3 flex flex-wrap gap-1.5" aria-label="Thèmes">{s.tags.map((t) => <li key={t} className="chip">{t}</li>)}</ul>}79                    </div>80                    <div className="flex items-start gap-3 sm:flex-col sm:items-end">81                      <StateBadge session={s} />82                      <ProgressMark session={{ id: s.id, capsules: s.capsuleIds, quizId: s.hasQuiz ? `${s.id}-quiz` : null }} />83                    </div>84                  </li>85                ))}86              </ol>87            </section>88          );89        })}90      </div>91    </div>92  );93}94
Files main
Menu

Theme