"use client"; import { useEffect, useState } from "react"; import { api } from "@/lib/api"; import type { Analysis } from "@/lib/types"; import { AnalysisLoading } from "./AnalysisLoading"; import { Rich } from "./Rich"; import { openTab } from "./Tabs"; const POLL_MS = 3000; const MAX_POLLS = 60; /** * The free short summary of a chart. The backend queues it when the chart is * saved; this asks for it (queueing it again if it failed) and polls until the * worker has written it. */ export function ChartSummary({ chartId, initial, detailedDone = false, }: { chartId: string; initial: Analysis | null; /** The detailed analysis is ready: the link says so instead of offering it. */ detailedDone?: boolean; }) { const [summary, setSummary] = useState(initial); const [gaveUp, setGaveUp] = useState(false); const done = summary?.status === "done"; useEffect(() => { if (done) return; let polls = 0; let stopped = false; let timer: ReturnType | undefined; const tick = async () => { try { const next = await api(`/charts/${chartId}/summary`, { method: "POST" }); if (stopped) return; setSummary(next); if (next.status === "done") return; } catch { // try again on the next tick } if (++polls >= MAX_POLLS) return setGaveUp(true); timer = setTimeout(tick, POLL_MS); }; tick(); return () => { stopped = true; clearTimeout(timer); }; }, [chartId, done]); const content = done ? summary?.content : null; return (
Röviden {content ? ( <>

{content.title}

{ e.preventDefault(); openTab("elemzes"); }} > {detailedDone ? "Elolvasom a részletes elemzést" : "Kérj részletes elemzést: hat fejezet a képletedről"} ) : ( <>

Rövid összefoglaló

{gaveUp || summary?.status === "failed" ? (

Az összefoglaló most nem készült el. Töltsd újra az oldalt egy kicsit később.

) : ( )} )}
); }