"use client"; import { useEffect, useState } from "react"; import type { IntroPage } from "./IntroCardPlayer"; // The cut's INTRO page for a stage (Play's): composed by POST /api/report/chrome/intro -- the build's own page, Play's // seek appended -- again whenever the manifest moved (`token`), for the project's cut or a take's. Null while there // is no intro (or before it composed); `{ error }` when it did not compose, said where the placeholder stands. export type IntroPageState = IntroPage | { error: string } | null; export function useIntroPage(project: string, take: string | null, hasIntro: boolean, token: string | null | undefined): IntroPageState { const [page, setPage] = useState(null); useEffect(() => { if (!hasIntro) { setPage(null); return; } let live = true; void (async () => { try { const r = await fetch("/api/report/chrome/intro", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ project, ...(take ? { take } : {}) }), }); const j = (await r.json().catch(() => ({}))) as { intro?: IntroPage | null; error?: string }; if (!live) return; setPage(!r.ok ? { error: String(j.error ?? `HTTP ${r.status}`) } : (j.intro ?? null)); } catch (e) { if (live) setPage({ error: e instanceof Error ? e.message : String(e) }); } })(); return () => { live = false; }; }, [project, take, hasIntro, token]); return page; }