2026-11-16 17:12:17 +00:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
|
|
|
|
|
|
import { api } from "@/lib/api";
|
|
|
|
|
import type { Analysis } from "@/lib/types";
|
|
|
|
|
|
2026-11-17 06:45:33 +00:00
|
|
|
import { AnalysisLoading } from "./AnalysisLoading";
|
2026-11-18 13:15:57 +00:00
|
|
|
import { RegenerateButton } from "./RegenerateButton";
|
2026-11-18 21:47:37 +00:00
|
|
|
import { RetryNotice } from "./RetryNotice";
|
2026-11-16 17:39:15 +00:00
|
|
|
import { Rich } from "./Rich";
|
2026-11-19 07:37:06 +00:00
|
|
|
import { ShareChart } from "./ShareChart";
|
2026-11-17 10:43:03 +00:00
|
|
|
import { openTab } from "./Tabs";
|
2026-11-16 17:39:15 +00:00
|
|
|
|
2026-11-16 17:12:17 +00:00
|
|
|
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.
|
|
|
|
|
*/
|
2026-11-17 10:43:03 +00:00
|
|
|
export function ChartSummary({
|
|
|
|
|
chartId,
|
|
|
|
|
initial,
|
|
|
|
|
detailedDone = false,
|
2026-11-18 13:15:57 +00:00
|
|
|
isAdmin = false,
|
2026-11-19 09:26:06 +00:00
|
|
|
shareRewardGranted = false,
|
2026-11-19 11:24:49 +00:00
|
|
|
referralOpen = false,
|
|
|
|
|
referralsLeft = 0,
|
2026-11-17 10:43:03 +00:00
|
|
|
}: {
|
|
|
|
|
chartId: string;
|
|
|
|
|
initial: Analysis | null;
|
|
|
|
|
/** The detailed analysis is ready: the link says so instead of offering it. */
|
|
|
|
|
detailedDone?: boolean;
|
2026-11-18 13:15:57 +00:00
|
|
|
/** Admins can write the summary again. */
|
|
|
|
|
isAdmin?: boolean;
|
2026-11-19 11:24:49 +00:00
|
|
|
/** The free question for sharing is used up. */
|
2026-11-19 09:26:06 +00:00
|
|
|
shareRewardGranted?: boolean;
|
2026-11-19 11:24:49 +00:00
|
|
|
/** New users from shared links earn analyses, and how many more can be. */
|
|
|
|
|
referralOpen?: boolean;
|
|
|
|
|
referralsLeft?: number;
|
2026-11-17 10:43:03 +00:00
|
|
|
}) {
|
2026-11-16 17:12:17 +00:00
|
|
|
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 ? (
|
|
|
|
|
<>
|
2026-11-18 13:15:57 +00:00
|
|
|
{isAdmin && summary && (
|
2026-11-18 18:44:20 +00:00
|
|
|
<RegenerateButton<Analysis>
|
|
|
|
|
url={`/admin/analyses/${summary.id}/regenerate`}
|
2026-11-18 13:15:57 +00:00
|
|
|
onStarted={(a) => {
|
|
|
|
|
setGaveUp(false);
|
|
|
|
|
setSummary(a);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2026-11-16 17:12:17 +00:00
|
|
|
<h2 id="summary-title" className="summary-title">
|
|
|
|
|
{content.title}
|
|
|
|
|
</h2>
|
2026-11-19 06:10:38 +00:00
|
|
|
<p className="summary-text" lang={summary?.language}>
|
2026-11-16 17:12:17 +00:00
|
|
|
<Rich text={content.summary ?? ""} />
|
|
|
|
|
</p>
|
2026-11-19 11:24:49 +00:00
|
|
|
<ShareChart
|
|
|
|
|
chartId={chartId}
|
|
|
|
|
rewardGranted={shareRewardGranted}
|
|
|
|
|
referralOpen={referralOpen}
|
|
|
|
|
referralsLeft={referralsLeft}
|
|
|
|
|
/>
|
2026-11-17 10:43:03 +00:00
|
|
|
<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>
|
2026-11-16 17:12:17 +00:00
|
|
|
</>
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
<h2 id="summary-title" className="sr-only">
|
|
|
|
|
Rövid összefoglaló
|
|
|
|
|
</h2>
|
2026-11-17 06:45:33 +00:00
|
|
|
{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>
|
|
|
|
|
) : (
|
2026-11-18 21:47:37 +00:00
|
|
|
<>
|
|
|
|
|
<AnalysisLoading
|
|
|
|
|
compact
|
|
|
|
|
text="Az összefoglalód készül, ez néhány másodperc…"
|
|
|
|
|
since={summary?.requested_at ?? summary?.created_at}
|
|
|
|
|
/>
|
|
|
|
|
{summary && (
|
|
|
|
|
<RetryNotice
|
|
|
|
|
status={summary.status}
|
|
|
|
|
error={summary.error}
|
|
|
|
|
nextAttemptAt={summary.next_attempt_at}
|
|
|
|
|
isAdmin={isAdmin}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
2026-11-16 17:12:17 +00:00
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</section>
|
|
|
|
|
);
|
|
|
|
|
}
|