91 lines
2.7 KiB
TypeScript
91 lines
2.7 KiB
TypeScript
"use client";
|
|||
|
|
|
||
|
|
import { useEffect, useState } from "react";
|
||
|
|
|
||
|
|
import { api } from "@/lib/api";
|
||
|
|
import type { Analysis } from "@/lib/types";
|
||
|
|
|
||
|
|
const POLL_MS = 3000;
|
||
|
|
const MAX_POLLS = 60;
|
||
|
|
|
||
|
|
/** `**bold**` → <strong>; the rest is plain text. */
|
||
|
|
function Rich({ text }: { text: string }) {
|
||
|
|
return (
|
||
|
|
<>
|
||
|
|
{text.split(/(\*\*[^*]+\*\*)/g).map((part, i) =>
|
||
|
|
part.startsWith("**") && part.endsWith("**") ? <strong key={i}>{part.slice(2, -2)}</strong> : part,
|
||
|
|
)}
|
||
|
|
</>
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 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 }: { chartId: string; initial: Analysis | null }) {
|
||
|
|
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 ? (
|
||
|
|
<>
|
||
|
|
<h2 id="summary-title" className="summary-title">
|
||
|
|
{content.title}
|
||
|
|
</h2>
|
||
|
|
<p className="summary-text">
|
||
|
|
<Rich text={content.summary ?? ""} />
|
||
|
|
</p>
|
||
|
|
</>
|
||
|
|
) : (
|
||
|
|
<>
|
||
|
|
<h2 id="summary-title" className="sr-only">
|
||
|
|
Rövid összefoglaló
|
||
|
|
</h2>
|
||
|
|
<p className="muted" role="status">
|
||
|
|
{gaveUp || summary?.status === "failed"
|
||
|
|
? "Az összefoglaló most nem készült el. Töltsd újra az oldalt egy kicsit később."
|
||
|
|
: "Az összefoglalód készül, ez néhány másodperc…"}
|
||
|
|
</p>
|
||
|
|
{!gaveUp && summary?.status !== "failed" && (
|
||
|
|
<div className="skeleton" aria-hidden="true">
|
||
|
|
<span />
|
||
|
|
<span />
|
||
|
|
<span />
|
||
|
|
</div>
|
||
|
|
)}
|
||
|
|
</>
|
||
|
|
)}
|
||
|
|
</section>
|
||
|
|
);
|
||
|
|
}
|