"use client"; import Link from "next/link"; import { useCallback, useEffect, useState } from "react"; import { api, ApiError, errorText } from "@/lib/api"; import { SPREADS, TAROT_QUESTION_MAX, TOPICS, tarotCard, type SpreadId, type TarotReading, type TopicId, } from "@/lib/tarot"; import type { PayMethod, UnitBalance } from "@/lib/types"; import { PackageChooser } from "../PackageCards"; import { TarotCard } from "./TarotCard"; import { TarotLive } from "./TarotLive"; import { TarotFan, TarotSlots } from "./TarotSpread"; /** Pause between two cards turning over. */ const REVEAL_MS = 650; type Phase = "setup" | "picking" | "drawing" | "revealing" | "done"; const day = (iso: string) => new Date(iso).toLocaleDateString("hu-HU", { timeZone: "Europe/Budapest", month: "long", day: "numeric" }); /** * The tarot table: choose a spread and ask a question, pick the cards from * the shuffled deck, watch them turn over, then read what they say. The * server draws the cards (picking only chooses the moment); each reading * uses one from the user's readings. */ export function TarotTable({ readings: initialReadings, left: initialLeft, isAdmin, canPay, }: { readings: TarotReading[]; left: number; isAdmin: boolean; canPay: PayMethod | null; }) { const [readings, setReadings] = useState(initialReadings); const [left, setLeft] = useState(initialLeft); const [spread, setSpread] = useState("trinity"); const [topic, setTopic] = useState("general"); const [question, setQuestion] = useState(""); const [phase, setPhase] = useState("setup"); const [picked, setPicked] = useState([]); const [current, setCurrent] = useState(null); const [revealed, setRevealed] = useState(0); const [error, setError] = useState(null); const [buying, setBuying] = useState(false); const chosen = SPREADS.find((s) => s.id === spread) ?? SPREADS[1]; const writing = readings.some((r) => r.status === "pending" || r.status === "running"); // The cards turn over one by one. useEffect(() => { if (phase !== "revealing" || !current) return; if (revealed >= current.cards.length) { const t = setTimeout(() => setPhase("done"), REVEAL_MS); return () => clearTimeout(t); } const t = setTimeout(() => setRevealed((n) => n + 1), revealed === 0 ? 350 : REVEAL_MS); return () => clearTimeout(t); }, [phase, revealed, current]); const draw = useCallback( async () => { setPhase("drawing"); setError(null); try { const reading = await api("/tarot/readings", { body: { spread, topic, question: question.trim() || null }, }); setCurrent(reading); setReadings((all) => [reading, ...all]); setLeft((n) => n - reading.credits_charged); setRevealed(0); setPhase("revealing"); } catch (err) { setPhase("setup"); setPicked([]); if (err instanceof ApiError && err.status === 402) { setLeft(0); setBuying(true); } else { setError(errorText(err)); } } }, [spread, topic, question], ); function pick(i: number) { if (phase !== "picking" || picked.includes(i) || picked.length >= chosen.cards) return; const next = [...picked, i]; setPicked(next); if (next.length === chosen.cards) void draw(); } function start(e: React.FormEvent) { e.preventDefault(); setError(null); if (left < 1) { setBuying(true); return; } setPicked([]); setCurrent(null); setPhase("picking"); } function again() { setPhase("setup"); setPicked([]); setCurrent(null); setQuestion(""); } const finished = useCallback((r: TarotReading) => { setReadings((all) => all.map((x) => (x.id === r.id ? r : x))); if (r.status === "failed" && r.credits_charged > 0) setLeft((n) => n + r.credits_charged); }, []); async function bought() { try { const me = await api<{ analyses_left: UnitBalance }>("/me"); setLeft(me.analyses_left.tarot); } catch { setLeft((n) => n + 5); } setBuying(false); } const slots = current?.cards.length ?? chosen.cards; const earlier = readings.filter((r) => r.id !== current?.id); return (
{phase === "setup" && (

Új jóslás

{buying ? (

Elfogytak a jóslásaid. A csomagokban is van néhány, vagy vegyél ötöt egyben, és a saját kérdéseidre húzhatsz újra.

Kíváncsi vagy, milyen egy jóslás? Nézd meg a példát →

) : (
Téma {TOPICS.map((t) => ( ))}
Kirakás {SPREADS.map((s) => ( ))}