SPB Git

Repository Public

Transcript.tsx 3.2 KB 60 lines tsx

1"use client";2import { useEffect, useId, useMemo, useState } from "react";3import { formatDuration } from "@/lib/i18n";4import { onTime, requestSeek } from "./bus";56interface Line { start: number; end: number; text: string }7const fold = (s: string) => s.normalize("NFD").replace(/[̀-ͯ]/g, "").toLowerCase();89function Highlight({ text, q }: { text: string; q: string }) {10  if (!q) return <>{text}</>;11  const folded = fold(text), needle = fold(q);12  // Le repliement NFD peut changer la longueur : on ne surligne que si les index restent alignés.13  if (folded.length !== text.length) return <>{text}</>;14  const parts: React.ReactNode[] = [];15  let i = 0, k = 0;16  for (let at = folded.indexOf(needle); at !== -1; at = folded.indexOf(needle, i)) {17    if (at > i) parts.push(text.slice(i, at));18    parts.push(<mark key={k++}>{text.slice(at, at + needle.length)}</mark>);19    i = at + needle.length;20  }21  parts.push(text.slice(i));22  return <>{parts}</>;23}2425/** Transcription consultable : recherche dans le texte, clic sur un passage = saut dans la vidéo, passage courant mis en évidence. */26export function Transcript({ lines, labels }: { lines: Line[]; labels: { search: string; none: string; jumpTo: string; one: string; many: string } }) {27  const [q, setQ] = useState("");28  const [active, setActive] = useState(-1);29  const id = useId();30  useEffect(() => onTime((t) => {31    let i = -1;32    for (let k = 0; k < lines.length; k++) if (t >= lines[k].start - 0.05) i = k;33    setActive(i);34  }), [lines]);35  const query = q.trim();36  const shown = useMemo(() => {37    const n = fold(query);38    return lines.map((l, i) => ({ l, i })).filter(({ l }) => !n || fold(l.text).includes(n));39  }, [lines, query]);40  return (41    <div data-testid="transcript">42      <label htmlFor={id} className="label">{labels.search}</label>43      <input id={id} type="search" className="input max-w-md" value={q} onChange={(e) => setQ(e.target.value)} autoComplete="off" enterKeyHint="search" data-testid="transcript-search" />44      <p className="mt-2 text-xs text-muted" role="status" aria-live="polite">{query ? (shown.length ? (shown.length === 1 ? labels.one : labels.many).replace("{n}", String(shown.length)) : labels.none) : ""}</p>45      <ol className="list-card mt-2 max-h-[28rem] overflow-y-auto overscroll-contain" tabIndex={-1}>46        {shown.map(({ l, i }) => (47          <li key={`${l.start}-${i}`} className="border-b border-line/60 last:border-b-0">48            <button type="button" onClick={() => requestSeek(l.start)} aria-current={i === active ? "true" : undefined} data-testid="transcript-line"49              aria-label={`${labels.jumpTo} ${formatDuration(l.start)} — ${l.text}`}50              className={`flex min-h-11 w-full cursor-pointer items-start gap-3 px-4 py-2.5 text-left text-[0.95rem] leading-6 transition-colors hover:bg-surface-2/70 ${i === active ? "bg-accent-soft" : ""}`}>51              <span className="mt-0.5 w-12 shrink-0 text-xs font-semibold tabular-nums leading-5 text-accent-deep">{formatDuration(l.start)}</span>52              <span className={i === active ? "font-medium text-ink" : "text-muted"}><Highlight text={l.text} q={query} /></span>53            </button>54          </li>55        ))}56      </ol>57    </div>58  );59}60
Files main
Menu

Theme