"use client"; import { useEffect, useState } from "react"; import type { Peaks } from "@/components/Waveform"; import type { BenchMedia } from "./benchMedia"; /** The waveform's envelope for a bench's media, on the source clock (benchMedia.ts). One request per media. */ export function usePeaks(media: BenchMedia): Peaks | null { const [got, setGot] = useState<{ url: string; peaks: Peaks } | null>(null); const url = media.peaksUrl; const toSource = media.peaksOnSource; useEffect(() => { if (!url) return; let live = true; void (async () => { const r = await fetch(url, { cache: "no-store" }); if (!r.ok || !live) return; const p = (await r.json()) as Peaks; if (live) setGot({ url, peaks: toSource(p) }); })(); return () => { live = false; }; // The URL names the media; its mapping comes with it. // eslint-disable-next-line react-hooks/exhaustive-deps }, [url]); // Until the new media's envelope arrives, the last one stays drawn (as the bench always did). return got?.peaks ?? null; }