Files
sorsfonalak/services/frontend/src/components/ChartSummary.tsx
T
fecoandClaude Opus 5.5 182d71fe65
CI / test (push) Successful in 1m5s
CI / build (push) Successful in 4m56s
CI / deploy (push) Skipped
Questions about an analysis or forecast, answered by the model that wrote it
- 3 questions in every package, +5 questions add-on (1 000 Ft); question units, refund on failure
- The answer sees the chart data, the analysis and earlier questions; at most 250 words
- Editable question prompt (Promptok → Kérdés-válasz); admins regenerate answers
- Package texts and FAQ updated

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
2026-11-18 18:44:20 +00:00

112 lines
3.3 KiB
TypeScript

"use client";
import { useEffect, useState } from "react";
import { api } from "@/lib/api";
import type { Analysis } from "@/lib/types";
import { AnalysisLoading } from "./AnalysisLoading";
import { RegenerateButton } from "./RegenerateButton";
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,
isAdmin = false,
}: {
chartId: string;
initial: Analysis | null;
/** The detailed analysis is ready: the link says so instead of offering it. */
detailedDone?: boolean;
/** Admins can write the summary again. */
isAdmin?: boolean;
}) {
const [summary, setSummary] = useState<Analysis | null>(initial);
const [gaveUp, setGaveUp] = useState(false);
const done = summary?.status === "done";
useEffect(() => {
if (done) return;
let polls = 0;
let stopped = false;
let timer: ReturnType<typeof setTimeout> | undefined;
const tick = async () => {
try {
const next = await api<Analysis>(`/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 (
<section className="card summary-card" aria-labelledby="summary-title" aria-busy={!done}>
<span className="eyebrow">Röviden</span>
{content ? (
<>
{isAdmin && summary && (
<RegenerateButton<Analysis>
url={`/admin/analyses/${summary.id}/regenerate`}
onStarted={(a) => {
setGaveUp(false);
setSummary(a);
}}
/>
)}
<h2 id="summary-title" className="summary-title">
{content.title}
</h2>
<p className="summary-text">
<Rich text={content.summary ?? ""} />
</p>
<a
href="#elemzes"
className="summary-more"
onClick={(e) => {
e.preventDefault();
openTab("elemzes");
}}
>
{detailedDone ? "Elolvasom a részletes elemzést" : "Kérj részletes elemzést: hat fejezet a képletedről"}
<span aria-hidden="true"> →</span>
</a>
</>
) : (
<>
<h2 id="summary-title" className="sr-only">
Rövid összefoglaló
</h2>
{gaveUp || summary?.status === "failed" ? (
<p className="muted" role="status">
Az összefoglaló most nem készült el. Töltsd újra az oldalt egy kicsit később.
</p>
) : (
<AnalysisLoading compact text="Az összefoglalód készül, ez néhány másodperc…" />
)}
</>
)}
</section>
);
}