104 lines
3.0 KiB
TypeScript
104 lines
3.0 KiB
TypeScript
"use client";
|
|||
|
|
|
||
|
|
import { useEffect, useState } from "react";
|
||
|
|
|
||
|
|
import { api } from "@/lib/api";
|
||
|
|
import type { TarotReading } from "@/lib/tarot";
|
||
|
|
|
||
|
|
import { AnalysisLoading } from "../AnalysisLoading";
|
||
|
|
import { RegenerateButton } from "../RegenerateButton";
|
||
|
|
import { RetryNotice } from "../RetryNotice";
|
||
|
|
import { TarotResult } from "./TarotResult";
|
||
|
|
|
||
|
|
const POLL_MS = 3000;
|
||
|
|
const MAX_POLLS = 100; // 5 minutes
|
||
|
|
|
||
|
|
const open = (r: TarotReading) => r.status === "pending" || r.status === "running";
|
||
|
|
|
||
|
|
/**
|
||
|
|
* A reading while it is written (asks the backend until it is done), then the
|
||
|
|
* text. Give it a `key` of the reading id: a new reading starts afresh.
|
||
|
|
*/
|
||
|
|
export function TarotLive({
|
||
|
|
initial,
|
||
|
|
isAdmin,
|
||
|
|
onChange,
|
||
|
|
}: {
|
||
|
|
initial: TarotReading;
|
||
|
|
isAdmin: boolean;
|
||
|
|
/** Called when the reading changes (finished, failed). */
|
||
|
|
onChange?: (reading: TarotReading) => void;
|
||
|
|
}) {
|
||
|
|
const [reading, setReading] = useState(initial);
|
||
|
|
const [round, setRound] = useState(0);
|
||
|
|
|
||
|
|
const waiting = open(reading);
|
||
|
|
useEffect(() => {
|
||
|
|
if (!waiting) return;
|
||
|
|
let polls = 0;
|
||
|
|
let stopped = false;
|
||
|
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
||
|
|
const tick = async () => {
|
||
|
|
try {
|
||
|
|
const next = await api<TarotReading>(`/tarot/readings/${reading.id}`);
|
||
|
|
if (stopped) return;
|
||
|
|
setReading(next);
|
||
|
|
if (!open(next)) {
|
||
|
|
onChange?.(next);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
} catch {
|
||
|
|
// try again on the next tick
|
||
|
|
}
|
||
|
|
if (++polls < MAX_POLLS) timer = setTimeout(tick, POLL_MS);
|
||
|
|
};
|
||
|
|
timer = setTimeout(tick, POLL_MS);
|
||
|
|
return () => {
|
||
|
|
stopped = true;
|
||
|
|
clearTimeout(timer);
|
||
|
|
};
|
||
|
|
}, [waiting, reading.id, round, onChange]);
|
||
|
|
|
||
|
|
const regenerate = isAdmin && !waiting && (
|
||
|
|
<RegenerateButton<unknown>
|
||
|
|
url={`/admin/analyses/${reading.id}/regenerate`}
|
||
|
|
onStarted={() => {
|
||
|
|
setReading((r) => ({ ...r, status: "pending", content: null, error: null }));
|
||
|
|
setRound((n) => n + 1);
|
||
|
|
}}
|
||
|
|
/>
|
||
|
|
);
|
||
|
|
|
||
|
|
if (reading.status === "done" && reading.content) {
|
||
|
|
return (
|
||
|
|
<>
|
||
|
|
{regenerate}
|
||
|
|
<TarotResult cards={reading.cards} question={reading.question} content={reading.content} />
|
||
|
|
</>
|
||
|
|
);
|
||
|
|
}
|
||
|
|
if (reading.status === "failed") {
|
||
|
|
return (
|
||
|
|
<div className="field">
|
||
|
|
{regenerate}
|
||
|
|
<p className="msg msg-error" role="alert">
|
||
|
|
A jóslást többszöri próbálkozás után sem sikerült megírni
|
||
|
|
{reading.credits_charged > 0 ? ". A jóslást visszaadtuk, így ingyen újra húzhatsz" : ""}. Próbáld újra egy kicsit
|
||
|
|
később.
|
||
|
|
</p>
|
||
|
|
{isAdmin && reading.error && <p className="hint">Hiba: {reading.error}</p>}
|
||
|
|
</div>
|
||
|
|
);
|
||
|
|
}
|
||
|
|
return (
|
||
|
|
<div className="field">
|
||
|
|
<AnalysisLoading
|
||
|
|
compact
|
||
|
|
text="A lapok mellé készül a jóslásod, ez általában fél-egy perc…"
|
||
|
|
since={reading.created_at}
|
||
|
|
/>
|
||
|
|
<RetryNotice status={reading.status} error={reading.error} isAdmin={isAdmin} />
|
||
|
|
</div>
|
||
|
|
);
|
||
|
|
}
|