SPB Git

Repository Public

AudioView.tsx 35.6 KB 436 lines tsx

1"use client";2/**3 * file: src/components/editor/nodes/AudioView.tsx4 * description: node view of the `audio` block (voice note, SPEC §2 and §4.13). Without an attachment it is a recorder (microphone →5 *   MediaRecorder → upload → attrs; the microphone tracks are always released, a failed upload keeps the recording for a retry) or an6 *   audio-file picker. With an attachment it is a compact player (play/pause, keyboard-friendly scrubber, rate, editable title), a7 *   waveform decoded lazily and only for small files, the transcript as seek buttons, the transcription request (feature-detected:8 *   an older server simply shows "not available") and the AI summary, always labelled as AI and never produced or changed here.9 *   Nothing touches the microphone, an AudioContext or the network before the user asks.10 *   Voice flows (SPEC §4.13): recording with pause/resume; "Transcribe" (optionally with an AI summary) queues the server job, the11 *   block then polls `GET /api/audio/<id>` while it is pending and writes the result into the node itself (the server never edits12 *   the page); failures say why and offer a retry; the 25 MB limit and a missing configuration are explained; the summary offers13 *   "Insert summary" / "Insert as tasks" (AI blocks with their source); search words (`?hl=`) are marked in the transcript; once14 *   transcribed, the original audio can be kept or deleted (the transcript stays).15 * author: Simon-Pierre Boucher — contact@spboucher.ai16 */17import * as React from "react";18import { AudioLines, Captions, FileAudio, ListTodo, ListPlus, Mic, Pause, Play, RotateCw, Sparkles, Square, Trash2, Upload, X } from "lucide-react";19import type { AudioAttrs, AudioSummary } from "@/lib/editor/next-node-defs";20import { activeLineIndex, computePeaks, extensionForMime, formatTime, pickRecordingMime, transcriptLines, transcriptStamp } from "@/lib/editor/audio";21import { fileUrl, uploadFile } from "@/lib/client/upload";22import { api, ApiError } from "@/lib/client/api";23import { toast } from "@/components/ui/toast";24import { cn } from "@/lib/utils";25import { clockSeconds, matchingLines, patchFromServer, pauseClock, resumeClock, searchTerms, startClock, summaryBlock, tasksBlock, transcribeProblem, type RecClock, type TranscriptView, type VoiceSource } from "@/lib/editor/voice";26import { NodeButton, NodeFrame, NodeState, useDebounced, type LeafViewProps } from "./frame";2728/** The waveform needs the whole file decoded in memory: only for attachments up to 25 MB, and (when the length is known) 45 minutes. */29const MAX_WAVE_BYTES = 25 * 1024 * 1024;30const MAX_WAVE_SECONDS = 45 * 60;31const WAVE_BUCKETS = 160;32const RATES = [1, 1.25, 1.5, 2];33const MAX_AUDIO_BYTES = 200 * 1024 * 1024;34const reducedMotion = () => typeof window !== "undefined" && !!window.matchMedia?.("(prefers-reduced-motion: reduce)").matches;35const stopTracks = (stream: MediaStream | null | undefined) => { stream?.getTracks().forEach((t) => { try { t.stop(); } catch { /* already ended */ } }); };36function microphoneError(e: unknown): string {37  const name = (e as { name?: string })?.name;38  if (name === "NotAllowedError" || name === "SecurityError") return "Microphone access was refused. Allow the microphone for this site in the browser settings, then try again.";39  if (name === "NotFoundError" || name === "OverconstrainedError") return "No microphone was found on this device.";40  if (name === "NotReadableError" || name === "AbortError") return "The microphone is in use by another application or could not be started.";41  return "The microphone could not be opened.";42}43const stamp = (d: Date) => `${d.getFullYear()}${String(d.getMonth() + 1).padStart(2, "0")}${String(d.getDate()).padStart(2, "0")}-${String(d.getHours()).padStart(2, "0")}${String(d.getMinutes()).padStart(2, "0")}${String(d.getSeconds()).padStart(2, "0")}`;4445export function AudioView(props: LeafViewProps<AudioAttrs>) {46  const { attrs, editable, selected } = props;47  const title = typeof attrs.title === "string" ? attrs.title : "";48  return (49    <NodeFrame kind="audio" label="Voice note" icon={<Mic />} title={attrs.attachmentId && editable ? undefined : title} selected={selected}>50      {attrs.attachmentId ? <Player key={attrs.attachmentId} {...props} attachmentId={attrs.attachmentId} />51        : (Array.isArray(attrs.transcript) && attrs.transcript.length > 0) || attrs.summary ? <Player key="transcript-only" {...props} attachmentId={null} /> // the audio was deleted, the transcript kept52        : editable ? <Recorder {...props} />53        : <NodeState icon={<Mic />} title="No recording yet" />}54    </NodeFrame>55  );56}5758interface Session { stream: MediaStream; recorder: MediaRecorder; chunks: Blob[]; clock: RecClock; startedIso: string; mime: string; discard: boolean; context: AudioContext | null; raf: number; timer: ReturnType<typeof setInterval> | null }59interface Take { blob: Blob; duration: number; recordedAt: string }6061function Recorder({ attrs, update, pageId }: LeafViewProps<AudioAttrs>) {62  const [phase, setPhase] = React.useState<"idle" | "asking" | "recording" | "uploading">("idle");63  const [problem, setProblem] = React.useState<string | null>(null);64  const [elapsed, setElapsed] = React.useState(0);65  const [paused, setPaused] = React.useState(false);66  const [progress, setProgress] = React.useState(0);67  /** A recording whose upload failed: kept in memory so it is not lost. */68  const [kept, setKept] = React.useState<Take | null>(null);69  const session = React.useRef<Session | null>(null); const meter = React.useRef<HTMLSpanElement>(null); const input = React.useRef<HTMLInputElement>(null);70  const abort = React.useRef<AbortController | null>(null); const alive = React.useRef(true);71  const titleRef = React.useRef(attrs.title); titleRef.current = attrs.title;7273  const release = (s: Session) => {74    stopTracks(s.stream);75    if (s.timer) { clearInterval(s.timer); s.timer = null; }76    if (s.raf) { cancelAnimationFrame(s.raf); s.raf = 0; }77    if (s.context) { void s.context.close().catch(() => {}); s.context = null; }78    if (session.current === s) session.current = null;79  };80  React.useEffect(() => {81    alive.current = true;82    return () => { // unmount: the microphone is released whatever the state, nothing is uploaded83      alive.current = false; abort.current?.abort();84      const s = session.current; if (s) { s.discard = true; try { if (s.recorder.state !== "inactive") s.recorder.stop(); } catch { /* not started */ } release(s); }85    };86  }, []);8788  const send = async (take: Take | { file: File }) => {89    const controller = new AbortController(); abort.current = controller; setPhase("uploading"); setProgress(0); setProblem(null);90    try {91      if ("file" in take) {92        const up = await uploadFile(take.file, pageId, { onProgress: (f) => { if (alive.current) setProgress(f); }, signal: controller.signal });93        if (alive.current) update({ attachmentId: up.id, title: titleRef.current || take.file.name.replace(/\.[^.]+$/, "") });94      } else {95        const when = new Date(take.recordedAt); const type = (take.blob.type || "audio/webm").split(";")[0];96        const file = new File([take.blob], `voice-note-${stamp(when)}.${extensionForMime(type)}`, { type });97        const up = await uploadFile(file, pageId, { onProgress: (f) => { if (alive.current) setProgress(f); }, signal: controller.signal });98        if (alive.current) { setKept(null); update({ attachmentId: up.id, duration: take.duration, recordedAt: take.recordedAt, title: titleRef.current || `Voice note ${when.toLocaleDateString(undefined, { dateStyle: "medium" })}` }); }99      }100    } catch (e) {101      if (!alive.current) return;102      if (!("file" in take)) setKept(take);103      setProblem((e as Error)?.name === "AbortError" ? "Upload cancelled." : (e as Error)?.message || "Upload failed");104    } finally { if (abort.current === controller) abort.current = null; if (alive.current) setPhase("idle"); }105  };106107  const start = async () => {108    if (phase !== "idle") return;109    if (typeof navigator === "undefined" || !navigator.mediaDevices?.getUserMedia || typeof MediaRecorder === "undefined") { setProblem(typeof window !== "undefined" && !window.isSecureContext ? "The microphone needs a secure (https) page. You can still upload an audio file." : "Recording is not available in this browser. You can still upload an audio file."); return; }110    setPhase("asking"); setProblem(null);111    let stream: MediaStream;112    try { stream = await navigator.mediaDevices.getUserMedia({ audio: true }); }113    catch (e) { if (alive.current) { setPhase("idle"); setProblem(microphoneError(e)); } return; }114    if (!alive.current) { stopTracks(stream); return; }115    const format = pickRecordingMime((m) => MediaRecorder.isTypeSupported(m));116    let recorder: MediaRecorder;117    try { recorder = format.mime ? new MediaRecorder(stream, { mimeType: format.mime }) : new MediaRecorder(stream); }118    catch { try { recorder = new MediaRecorder(stream); } catch { stopTracks(stream); setPhase("idle"); setProblem("This browser cannot record audio."); return; } }119    const s: Session = { stream, recorder, chunks: [], clock: startClock(performance.now()), startedIso: new Date().toISOString(), mime: format.mime, discard: false, context: null, raf: 0, timer: null };120    session.current = s;121    recorder.ondataavailable = (e) => { if (e.data && e.data.size > 0) s.chunks.push(e.data); };122    recorder.onerror = () => { s.discard = true; try { if (recorder.state !== "inactive") recorder.stop(); } catch { /* ignore */ } release(s); if (alive.current) { setPhase("idle"); setProblem("The recording was interrupted."); } };123    recorder.onstop = () => {124      const duration = Math.round(clockSeconds(s.clock, performance.now()) * 10) / 10; // pauses are not part of the recording125      release(s);126      if (s.discard || !alive.current) { if (alive.current) setPhase("idle"); return; }127      const blob = new Blob(s.chunks, { type: recorder.mimeType || s.mime || "audio/webm" });128      if (blob.size === 0) { setPhase("idle"); setProblem("Nothing was recorded."); return; }129      void send({ blob, duration, recordedAt: s.startedIso });130    };131    // The track can end on its own (device unplugged, permission revoked): finish with what was captured.132    stream.getAudioTracks().forEach((t) => t.addEventListener("ended", () => { try { if (recorder.state !== "inactive") recorder.stop(); } catch { /* ignore */ } }));133    try { recorder.start(1000); } catch { release(s); setPhase("idle"); setProblem("This browser cannot record audio."); return; }134    s.clock = startClock(performance.now()); setElapsed(0); setPaused(false);135    s.timer = setInterval(() => { if (alive.current) setElapsed(clockSeconds(s.clock, performance.now())); }, 250);136    if (!reducedMotion()) { // live level meter: written straight to the element, no React render per frame137      try {138        const Ctx: typeof AudioContext | undefined = window.AudioContext ?? (window as unknown as { webkitAudioContext?: typeof AudioContext }).webkitAudioContext;139        if (Ctx) {140          const context = new Ctx(); s.context = context;141          const analyser = context.createAnalyser(); analyser.fftSize = 256; context.createMediaStreamSource(stream).connect(analyser);142          const data = new Uint8Array(analyser.fftSize);143          const tick = () => {144            analyser.getByteTimeDomainData(data); let sum = 0;145            for (let i = 0; i < data.length; i++) { const v = (data[i] - 128) / 128; sum += v * v; }146            if (meter.current) meter.current.style.transform = `scaleX(${Math.min(1, Math.sqrt(sum / data.length) * 3.2).toFixed(3)})`;147            s.raf = requestAnimationFrame(tick);148          };149          s.raf = requestAnimationFrame(tick);150        }151      } catch { /* the meter is optional */ }152    }153    setPhase("recording");154  };155  const stop = (discard: boolean) => { const s = session.current; if (!s) return; s.discard = discard; try { if (s.recorder.state !== "inactive") s.recorder.stop(); else { release(s); setPhase("idle"); } } catch { release(s); setPhase("idle"); } };156  /** Pause keeps the microphone open and records nothing; resume continues the same recording (one file). */157  const togglePause = () => {158    const s = session.current; if (!s) return;159    try {160      if (s.recorder.state === "recording") { s.recorder.pause(); s.clock = pauseClock(s.clock, performance.now()); setPaused(true); }161      else if (s.recorder.state === "paused") { s.recorder.resume(); s.clock = resumeClock(s.clock, performance.now()); setPaused(false); }162    } catch { setProblem("This browser cannot pause a recording."); }163  };164  const pick = (file: File | undefined) => {165    if (!file || phase !== "idle") return;166    if (!file.type.startsWith("audio/") && !/\.(m4a|mp3|wav|ogg|oga|opus|webm|aac|flac|aiff?|caf)$/i.test(file.name)) { setProblem("Choose an audio file."); return; }167    if (file.size > MAX_AUDIO_BYTES) { setProblem("File too large (200 MB max)."); return; }168    if (file.size > MAX_WAVE_BYTES) toast("Large recording", { description: "It is kept and playable, but it is over the 25 MB a transcription accepts." });169    void send({ file });170  };171172  if (phase === "recording") return (173    <div className="au-rec" role="group" aria-label="Recording" data-paused={paused ? "true" : undefined}>174      <span className="au-rec-dot" aria-hidden />175      <span className="au-rec-time" role="timer" aria-label="Elapsed time">{formatTime(elapsed)}</span>176      {paused ? <span className="nb-meta au-paused">Paused</span> : <span className="au-meter" aria-hidden><span ref={meter} className="au-meter-fill" /></span>}177      <span className="sr-only" role="status">{paused ? "Recording paused" : "Recording in progress"}</span>178      <span className="au-rec-actions">179        <NodeButton label="Discard recording" tone="danger" onClick={() => stop(true)}><Trash2 /></NodeButton>180        <NodeButton label={paused ? "Resume recording" : "Pause recording"} text onClick={togglePause}>{paused ? <Mic /> : <Pause />}</NodeButton>181        <NodeButton label="Stop recording" tone="accent" text onClick={() => stop(false)}><Square /></NodeButton>182      </span>183    </div>184  );185  if (phase === "uploading") return (186    <NodeState icon={<Upload />} title={`Uploading… ${Math.round(progress * 100)} %`} hint={<progress className="au-progress" max={1} value={progress} aria-label="Upload progress" />}>187      <NodeButton label="Cancel upload" text onClick={() => abort.current?.abort()}><X /></NodeButton>188    </NodeState>189  );190  return (191    <NodeState icon={<Mic />} tone={problem ? "danger" : "neutral"} title={phase === "asking" ? "Waiting for the microphone…" : problem ?? "Record a voice note"}192      hint={phase === "asking" ? "Answer the browser's permission prompt." : kept ? `Your recording (${formatTime(kept.duration)}) is still here — nothing was lost.` : problem ? undefined : "The microphone is used only while you record."}>193      <input ref={input} type="file" accept="audio/*" hidden aria-hidden tabIndex={-1} onChange={(e) => { const f = e.target.files?.[0]; e.target.value = ""; pick(f); }} />194      {kept ? (195        <>196          <NodeButton label="Retry upload" tone="accent" text onClick={() => void send(kept)}><RotateCw /></NodeButton>197          <NodeButton label="Discard recording" tone="danger" text onClick={() => { setKept(null); setProblem(null); }}><Trash2 /></NodeButton>198        </>199      ) : (200        <>201          <NodeButton label="Start recording" tone="accent" text disabled={phase !== "idle"} onClick={() => void start()}><Mic /></NodeButton>202          <NodeButton label="Upload audio file" text disabled={phase !== "idle"} onClick={() => input.current?.click()}><FileAudio /></NodeButton>203        </>204      )}205    </NodeState>206  );207}208209function Player({ attrs, update, editable, attachmentId, editor, getPos, pageId }: LeafViewProps<AudioAttrs> & { /** null = the original audio was deleted: transcript and summary only */ attachmentId: string | null }) {210  const audio = React.useRef<HTMLAudioElement>(null);211  const [playing, setPlaying] = React.useState(false);212  const [time, setTime] = React.useState(0);213  const [mediaDuration, setMediaDuration] = React.useState(0);214  const [rate, setRate] = React.useState(1);215  const [mediaError, setMediaError] = React.useState<string | null>(null);216  const storedDuration = Number.isFinite(attrs.duration) && attrs.duration > 0 ? attrs.duration : 0;217  const total = mediaDuration > 0 ? mediaDuration : storedDuration;218  const alive = React.useRef(true);219  const waveAbort = React.useRef<AbortController | null>(null);220  React.useEffect(() => { alive.current = true; return () => { alive.current = false; waveAbort.current?.abort(); audio.current?.pause(); }; }, []);221222  // ---- duration: filled once from the media metadata when the node does not know it ----223  const filled = React.useRef(false);224  const onDuration = () => {225    const d = audio.current?.duration ?? 0; if (!Number.isFinite(d) || d <= 0) return;226    setMediaDuration(d);227    if (editable && storedDuration === 0 && !filled.current) { filled.current = true; update({ duration: Math.round(d * 10) / 10 }); }228  };229230  // ---- waveform: fetched + decoded on first play or on request, small files only ----231  const [wave, setWave] = React.useState<"idle" | "loading" | "ready" | "unavailable">("idle");232  const [peaks, setPeaks] = React.useState<number[] | null>(null);233  const loadWave = React.useCallback(async () => {234    if (wave !== "idle") return;235    if (total > MAX_WAVE_SECONDS) { setWave("unavailable"); return; }236    const controller = new AbortController(); waveAbort.current = controller; setWave("loading");237    try {238      if (!attachmentId) { setWave("unavailable"); return; }239      const res = await fetch(fileUrl(attachmentId), { credentials: "same-origin", signal: controller.signal });240      const length = Number(res.headers.get("content-length") ?? NaN);241      if (!res.ok || !Number.isFinite(length) || length <= 0 || length > MAX_WAVE_BYTES) { controller.abort(); if (alive.current) setWave("unavailable"); return; }242      const data = await res.arrayBuffer();243      if (data.byteLength > MAX_WAVE_BYTES) throw new Error("too large");244      const Offline: typeof OfflineAudioContext | undefined = window.OfflineAudioContext ?? (window as unknown as { webkitOfflineAudioContext?: typeof OfflineAudioContext }).webkitOfflineAudioContext;245      if (!Offline) throw new Error("no decoder");246      // An offline context decodes without an audio device or autoplay permission; a low sample rate keeps the decoded copy small.247      let context: OfflineAudioContext | null = null;248      for (const sampleRate of [8000, 22050, 44100]) { try { context = new Offline(1, 1, sampleRate); break; } catch { /* rate not supported: try the next */ } }249      if (!context) throw new Error("no decoder");250      const decoded = await context.decodeAudioData(data);251      const result = computePeaks(decoded.getChannelData(0), WAVE_BUCKETS);252      if (alive.current) { setPeaks(result); setWave("ready"); }253    } catch { if (alive.current) setWave("unavailable"); }254  }, [wave, total, attachmentId]);255256  const canvas = React.useRef<HTMLCanvasElement>(null);257  const [waveWidth, setWaveWidth] = React.useState(0);258  React.useEffect(() => {259    const el = canvas.current; if (!el || wave !== "ready") return;260    const measure = () => setWaveWidth(Math.floor(el.clientWidth)); measure();261    if (typeof ResizeObserver === "undefined") return;262    const ro = new ResizeObserver(measure); ro.observe(el); return () => ro.disconnect();263  }, [wave]);264  const fraction = total > 0 ? Math.min(1, Math.max(0, time / total)) : 0;265  React.useEffect(() => {266    const el = canvas.current; if (!el || !peaks || waveWidth <= 0) return;267    const dpr = Math.min(window.devicePixelRatio || 1, 3); const h = el.clientHeight || 48;268    const w = Math.floor(waveWidth * dpr), hh = Math.floor(h * dpr); if (el.width !== w) el.width = w; if (el.height !== hh) el.height = hh;269    const ctx = el.getContext("2d"); if (!ctx) return;270    const css = getComputedStyle(el); const played = css.getPropertyValue("--accent").trim() || "#3b5bdb"; const rest = css.getPropertyValue("--line-strong").trim() || "#9aa0a6";271    ctx.clearRect(0, 0, w, hh);272    const n = peaks.length; const step = w / n; const bar = Math.max(1, Math.floor(step * 0.62)); const mid = hh / 2;273    for (let i = 0; i < n; i++) {274      const amp = Math.max(1 * dpr, peaks[i] * (hh - 2 * dpr)); const x = Math.floor(i * step + (step - bar) / 2);275      ctx.fillStyle = (i + 0.5) / n <= fraction ? played : rest; ctx.fillRect(x, Math.round(mid - amp / 2), bar, Math.round(amp));276    }277  }, [peaks, waveWidth, fraction]);278279  // ---- transport ----280  const seek = (t: number) => { const el = audio.current; if (!el) return; const to = Math.max(0, total > 0 ? Math.min(t, total) : t); try { el.currentTime = to; } catch { /* not seekable yet */ } setTime(to); };281  const play = () => { const el = audio.current; if (!el) return; el.playbackRate = rate; setMediaError(null); void loadWave(); el.play().catch((e) => { if ((e as Error)?.name !== "AbortError") setMediaError("This recording cannot be played here."); }); };282  const toggle = () => { const el = audio.current; if (!el) return; if (el.paused) play(); else el.pause(); };283  const changeRate = (r: number) => { setRate(r); if (audio.current) audio.current.playbackRate = r; };284  const onWavePointer = (e: React.PointerEvent<HTMLCanvasElement>) => { const r = e.currentTarget.getBoundingClientRect(); if (r.width > 0 && total > 0) seek(((e.clientX - r.left) / r.width) * total); };285286  // ---- title ----287  const storedTitle = typeof attrs.title === "string" ? attrs.title : "";288  const [title, setTitle] = React.useState(storedTitle);289  const writtenTitle = React.useRef(storedTitle);290  React.useEffect(() => { if (storedTitle !== writtenTitle.current) { writtenTitle.current = storedTitle; setTitle(storedTitle); } }, [storedTitle]);291  const saveTitle = useDebounced((v: string) => { if (v !== writtenTitle.current) { writtenTitle.current = v; update({ title: v }); } }, 400);292293  // ---- transcript / transcription ----294  const lines = React.useMemo(() => transcriptLines(attrs.transcript), [attrs.transcript]);295  const activeLine = playing ? activeLineIndex(lines, time) : -1;296  const status = attrs.transcriptStatus === "pending" || attrs.transcriptStatus === "done" || attrs.transcriptStatus === "failed" ? attrs.transcriptStatus : "none";297  const [unavailable, setUnavailable] = React.useState(false);298  const [asking, setAsking] = React.useState(false);299  /** Why the last request was refused or failed (limit, configuration, engine error): shown in the block until the next attempt. */300  const [problem, setProblem] = React.useState<{ message: string; retry: boolean } | null>(null);301  const attrsRef = React.useRef(attrs); attrsRef.current = attrs; const updateRef = React.useRef(update); updateRef.current = update;302  /** Write the server's state into the node — only what differs, so a poll that changes nothing creates no transaction. */303  const applyServer = React.useCallback((v: TranscriptView) => {304    const patch = patchFromServer(attrsRef.current, v); if (patch) updateRef.current(patch);305    if (v.status === "failed") setProblem({ message: v.error || "The transcription engine failed.", retry: true });306    else if (v.status === "done") setProblem(v.error ? { message: v.error, retry: false } : null); // done + error = the summary could not be produced307  }, []);308  const transcribe = async (opts: { summarize?: boolean; force?: boolean } = {}) => {309    if (!editable || asking || !attachmentId) return; setAsking(true); setProblem(null);310    try {311      const r = await api<TranscriptView>(`/api/audio/${encodeURIComponent(attachmentId)}/transcribe`, { method: "POST", json: opts });312      if (!alive.current) return;313      if (r && typeof r === "object" && typeof r.status === "string") applyServer(r); else update({ transcriptStatus: "pending" });314    } catch (e) {315      if (!alive.current) return;316      const p = transcribeProblem(e as ApiError);317      if (p.unavailable) setUnavailable(true); // older server: nothing is changed in the node318      else setProblem({ message: p.message, retry: p.retry });319    } finally { if (alive.current) setAsking(false); }320  };321  /** Ask the server where the job stands; the result is written into the node (the server never edits the page, docs/next/API-NEXT-AI.md §6.2). */322  const check = React.useCallback(async (announce: boolean) => {323    if (!attachmentId) return;324    try {325      const r = await api<TranscriptView>(`/api/audio/${encodeURIComponent(attachmentId)}`);326      if (!alive.current) return; applyServer(r);327      if (announce && r.status === "pending") toast("Transcription still in progress");328    } catch (e) { if (alive.current && announce) { const p = transcribeProblem(e as ApiError); if (p.unavailable) setUnavailable(true); else toast("Could not check the transcription", { description: (e as Error)?.message, tone: "danger" }); } }329  }, [attachmentId, applyServer]);330  // While the node says "pending" the block polls (the request was the user's; reopening the page resumes the wait). Read-only pages never poll: they could not write the result.331  React.useEffect(() => {332    if (status !== "pending" || !editable || !attachmentId || unavailable) return;333    void check(false); const t = setInterval(() => { if (document.visibilityState === "visible") void check(false); }, 2500);334    return () => clearInterval(t);335  }, [status, editable, attachmentId, unavailable, check]);336  const refresh = async () => { if (asking) return; setAsking(true); try { await check(true); } finally { if (alive.current) setAsking(false); } };337  // ---- search hits: words of the result that opened the page (`?hl=`) are marked in the transcript and the first one is brought into view338  const terms = React.useMemo(() => (typeof window === "undefined" ? [] : searchTerms(new URLSearchParams(window.location.search).get("hl"))), []);339  const hits = React.useMemo(() => matchingLines(lines, terms), [lines, terms]);340  const listRef = React.useRef<HTMLOListElement>(null);341  React.useEffect(() => { const list = listRef.current; if (!list || !hits.length) return; const el = list.children[hits[0]] as HTMLElement | undefined; if (el) list.scrollTop = Math.max(0, el.offsetTop - list.offsetTop - 8); }, [hits]);342  // ---- AI summary → page content, only on request, always as an AI block that names its source343  const source = (): VoiceSource => ({ pageId, blockId: (attrs.id as string | null | undefined) ?? null, title: storedTitle || "Voice note" });344  const insertAfter = (block: ReturnType<typeof summaryBlock>, done: string) => {345    if (!block || !editable) return; const pos = getPos(); if (typeof pos !== "number") return;346    const node = editor.state.doc.nodeAt(pos); const at = pos + (node?.nodeSize ?? 1);347    if (editor.chain().insertContentAt(at, block as never).run()) toast(done, { description: "Marked as AI, with this voice note as its source.", tone: "ai" });348  };349  // ---- original audio: kept by default; deleting it is an explicit, confirmed choice and leaves the transcript350  const [confirmDelete, setConfirmDelete] = React.useState(false); const [deleting, setDeleting] = React.useState(false);351  const deleteAudio = async () => {352    if (!attachmentId || deleting) return; setDeleting(true);353    try { audio.current?.pause(); await api(`/api/attachments?id=${encodeURIComponent(attachmentId)}`, { method: "DELETE" }); if (alive.current) { update({ attachmentId: null }); toast("Original audio deleted", { description: "The transcript stays in the page." }); } }354    catch (e) { if (alive.current) toast("The audio could not be deleted", { description: (e as Error)?.message, tone: "danger" }); }355    finally { if (alive.current) { setDeleting(false); setConfirmDelete(false); } }356  };357  const summary = attrs.summary && typeof attrs.summary === "object" ? attrs.summary : null;358359  return (360    <div className="au">361      {/* eslint-disable-next-line jsx-a11y/media-has-caption -- the transcript below is the text alternative */}362      {attachmentId && <audio ref={audio} src={fileUrl(attachmentId)} preload={storedDuration > 0 ? "none" : "metadata"} hidden363        onLoadedMetadata={onDuration} onDurationChange={onDuration} onTimeUpdate={(e) => setTime(e.currentTarget.currentTime)}364        onPlay={() => setPlaying(true)} onPause={() => setPlaying(false)} onEnded={() => setPlaying(false)}365        onError={() => { setPlaying(false); setMediaError("The audio file could not be loaded — it may have been deleted or its format is not supported by this browser."); }} />}366      {editable && <input className="nb-field au-title" aria-label="Title" placeholder="Title" value={title} maxLength={200} onChange={(e) => { setTitle(e.target.value); saveTitle(e.target.value); }} onBlur={() => saveTitle.flush()} />}367      {!attachmentId && <p className="au-note nb-meta" role="note"><FileAudio aria-hidden />The original audio was deleted — the transcript is kept.</p>}368      {attachmentId && <div className="au-player" role="group" aria-label="Audio player">369        <NodeButton label={playing ? "Pause" : "Play"} tone="accent" className="au-play" onClick={toggle}>{playing ? <Pause /> : <Play />}</NodeButton>370        <span className="au-time nb-meta" aria-hidden>{formatTime(time)}</span>371        <input className="au-seek" type="range" aria-label="Seek" min={0} max={total > 0 ? total : 1} step={total > 60 ? 1 : 0.1} value={Math.min(time, total > 0 ? total : 1)} disabled={total <= 0}372          aria-valuetext={`${formatTime(time)} of ${formatTime(total)}`} style={{ "--au-p": `${fraction * 100}%` } as React.CSSProperties} onChange={(e) => seek(Number(e.target.value))} />373        <span className="au-time nb-meta" aria-label="Duration">{formatTime(total)}</span>374        <select className="nb-field au-rate" aria-label="Playback speed" value={rate} onChange={(e) => changeRate(Number(e.target.value))}>375          {RATES.map((r) => <option key={r} value={r}>{r}×</option>)}376        </select>377      </div>}378      {mediaError && <NodeState tone="danger" icon={<FileAudio />} title="Playback problem" hint={mediaError} />}379      {wave === "ready" && <canvas ref={canvas} className="au-wave" aria-hidden onPointerDown={onWavePointer} />}380      {attachmentId && wave === "idle" && !mediaError && total <= MAX_WAVE_SECONDS && <button type="button" className="au-wave-load nb-meta" onMouseDown={(e) => e.preventDefault()} onClick={() => void loadWave()}><AudioLines aria-hidden />Show waveform</button>}381      {wave === "loading" && <div className="au-wave-load nb-meta" role="status"><AudioLines aria-hidden />Loading waveform…</div>}382383      {hits.length > 0 && <p className="au-note nb-meta" role="status" data-testid="au-hits"><Captions aria-hidden />{hits.length} transcript line{hits.length > 1 ? "s" : ""} match{hits.length > 1 ? "" : "es"} your search</p>}384      {lines.length > 0 && (385        <ol ref={listRef} className="au-transcript" aria-label="Transcript">386          {lines.map((l, i) => (387            <li key={`${l.t}-${i}`}>388              <button type="button" className={cn("au-line", i === activeLine && "is-active", hits.includes(i) && "is-hit")} aria-current={i === activeLine ? "true" : undefined} data-hit={hits.includes(i) ? "true" : undefined} disabled={!attachmentId} title={attachmentId ? `Play from ${formatTime(l.t)}` : undefined} onMouseDown={(e) => e.preventDefault()} onClick={() => { seek(l.t); play(); }}>389                <span className="au-stamp">{transcriptStamp(l.t)}</span><span className="au-line-text">{l.text}</span>390              </button>391            </li>392          ))}393        </ol>394      )}395      {attachmentId && (editable || status === "done" || status === "pending" || status === "failed") && (396        <div className="au-tr nb-meta" data-status={status}>397          <Captions aria-hidden />398          {unavailable ? <span role="status">Transcription is not available on this server yet</span>399            : status === "none" ? (editable && <><NodeButton label="Transcribe" text disabled={asking} onClick={() => void transcribe()} /><NodeButton label="Transcript + AI summary" tone="ai" text disabled={asking} onClick={() => void transcribe({ summarize: true })}><Sparkles /></NodeButton><span className="au-hint">Sends the recording to the transcription engine (25 MB max).</span></>)400            : status === "pending" ? <><span role="status" className="au-pending"><span className="au-spin" aria-hidden />Transcription in progress…</span>{editable && <NodeButton label="Refresh" text disabled={asking} onClick={() => void refresh()}><RotateCw /></NodeButton>}</>401            : status === "failed" ? <><span role="status" className="au-failed">Transcription failed</span>{editable && <NodeButton label="Retry transcription" text disabled={asking} onClick={() => void transcribe({ force: true })}><RotateCw /></NodeButton>}</>402            : <><span>Transcribed{attrs.transcriptEngine ? ` with ${attrs.transcriptEngine}` : ""}{lines.length === 0 ? " — no speech detected" : ""}</span>{editable && !summary && lines.length > 0 && <NodeButton label="Summarise" tone="ai" text disabled={asking} onClick={() => void transcribe({ summarize: true })}><Sparkles /></NodeButton>}</>}403        </div>404      )}405      {problem && !unavailable && <p className="au-problem" role="alert">{problem.message}</p>}406      {attachmentId && editable && status === "done" && (407        <div className="au-keep nb-meta" role="group" aria-label="Original audio">408          <FileAudio aria-hidden />409          {confirmDelete ? <><span>Delete the audio file? The transcript and the summary stay.</span><NodeButton label="Delete audio" tone="danger" text disabled={deleting} onClick={() => void deleteAudio()}><Trash2 /></NodeButton><NodeButton label="Keep audio" text disabled={deleting} onClick={() => setConfirmDelete(false)} /></>410            : <><span>The original audio is kept.</span><NodeButton label="Delete original audio…" text onClick={() => setConfirmDelete(true)}><Trash2 /></NodeButton></>}411        </div>412      )}413      {summary && <SummaryBox summary={summary} onInsertSummary={editable ? () => insertAfter(summaryBlock(summary, source()), "Summary inserted below") : undefined} onInsertTasks={editable ? () => insertAfter(tasksBlock(summary, source()), "Tasks inserted below") : undefined} />}414    </div>415  );416}417418/** AI-made summary: displayed as stored, always labelled, never generated or edited by this component (SPEC §0 rule 10). */419function SummaryBox({ summary, onInsertSummary, onInsertTasks }: { summary: AudioSummary; /** undefined in read-only pages */ onInsertSummary?: () => void; onInsertTasks?: () => void }) {420  const list = (v: unknown) => (Array.isArray(v) ? v.filter((x): x is string => typeof x === "string" && x.trim() !== "") : []);421  const decisions = list(summary.decisions), tasks = list(summary.tasks);422  const at = typeof summary.at === "string" ? new Date(summary.at) : null; const when = at && !Number.isNaN(at.getTime()) ? at.toLocaleString(undefined, { dateStyle: "medium", timeStyle: "short" }) : "";423  return (424    <section className="au-summary" aria-label="AI summary">425      <header className="au-summary-head"><span className="au-ai-mark"><Sparkles aria-hidden />AI summary</span><span className="nb-meta">{[typeof summary.model === "string" ? summary.model : "", when].filter(Boolean).join(" · ")}</span></header>426      {typeof summary.text === "string" && summary.text && <p className="au-summary-text">{summary.text}</p>}427      {decisions.length > 0 && <><h4 className="au-summary-h">Decisions</h4><ul className="au-summary-list">{decisions.map((d, i) => <li key={i}>{d}</li>)}</ul></>}428      {tasks.length > 0 && <><h4 className="au-summary-h">Tasks</h4><ul className="au-summary-list">{tasks.map((t, i) => <li key={i}>{t}</li>)}</ul></>}429      {(onInsertSummary || onInsertTasks) && <div className="au-summary-actions">430        {onInsertSummary && typeof summary.text === "string" && summary.text.trim() !== "" && <NodeButton label="Insert summary" tone="ai" text onClick={onInsertSummary}><ListPlus /></NodeButton>}431        {onInsertTasks && tasks.length > 0 && <NodeButton label="Insert as tasks" tone="ai" text onClick={onInsertTasks}><ListTodo /></NodeButton>}432      </div>}433    </section>434  );435}436
Files main
Menu

Theme