Files
sorsfonalak/services/frontend/src/components/tarot/TarotLive.tsx
T
fecoandClaude Opus 5.5 b0fc547b73
CI / test (push) Successful in 1m29s
CI / build (push) Successful in 5m52s
CI / deploy (push) Skipped
Tarot readings: AI-written, drawn on the server, sold as units
A new Kártyajóslás page: choose a spread (1, 3 or 5 cards), ask a
question, pick the cards from the fanned deck and watch them turn over;
the reading is written by the detailed-analysis model from the cards,
their positions and base meanings, the question and the reader's Sun,
Moon and Ascendant. The server shuffles and draws, so nobody can draw
again for free. A reading is a 'tarot' analysis (worker, retries,
fallback, refunds and admin regeneration as for analyses), uses one
'tarot' unit, and survives recalculations; deleting the account
deletes it.

Packages carry 2/5/10 readings, the new +5 kártyajóslás addon costs
1 000 Ft. The card artwork comes from the tarot design studio
(celestial astrolabe style); the deck JSON is shared by the frontend
and the prompt. A public example is at /kartyajoslas/pelda.

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

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>
);
}