2026-11-20 14:35:50 +00:00
|
|
|
|
"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, tarotCard, type SpreadId, type TarotReading } from "@/lib/tarot";
|
|
|
|
|
|
import type { PayMethod, UnitBalance } from "@/lib/types";
|
|
|
|
|
|
|
|
|
|
|
|
import { PackageChooser } from "../PackageCards";
|
2026-11-20 15:51:23 +00:00
|
|
|
|
import { TarotCard } from "./TarotCard";
|
2026-11-20 14:35:50 +00:00
|
|
|
|
import { TarotLive } from "./TarotLive";
|
2026-11-20 15:51:23 +00:00
|
|
|
|
import { TarotFan, TarotSlots } from "./TarotSpread";
|
2026-11-20 14:35:50 +00:00
|
|
|
|
|
|
|
|
|
|
/** 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<SpreadId>("trinity");
|
|
|
|
|
|
const [question, setQuestion] = useState("");
|
|
|
|
|
|
const [phase, setPhase] = useState<Phase>("setup");
|
|
|
|
|
|
const [picked, setPicked] = useState<number[]>([]);
|
|
|
|
|
|
const [current, setCurrent] = useState<TarotReading | null>(null);
|
|
|
|
|
|
const [revealed, setRevealed] = useState(0);
|
|
|
|
|
|
const [error, setError] = useState<string | null>(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<TarotReading>("/tarot/readings", {
|
|
|
|
|
|
body: { spread, 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, 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 (
|
|
|
|
|
|
<div className="tarot">
|
|
|
|
|
|
{phase === "setup" && (
|
|
|
|
|
|
<section className="card" aria-labelledby="tarot-setup-title">
|
|
|
|
|
|
<h2 id="tarot-setup-title">Új jóslás</h2>
|
|
|
|
|
|
{buying ? (
|
|
|
|
|
|
<div className="field">
|
|
|
|
|
|
<p>
|
|
|
|
|
|
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.
|
|
|
|
|
|
</p>
|
|
|
|
|
|
<PackageChooser ids={["addon_tarot", "single", "couple"]} canPay={canPay} onBought={bought} />
|
|
|
|
|
|
<p className="hint">
|
|
|
|
|
|
Kíváncsi vagy, milyen egy jóslás? <Link href="/kartyajoslas/pelda">Nézd meg a példát →</Link>
|
|
|
|
|
|
</p>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<form className="field" style={{ gap: 18 }} onSubmit={start}>
|
|
|
|
|
|
<fieldset className="tarot-spreads">
|
|
|
|
|
|
<legend className="label">Kirakás</legend>
|
|
|
|
|
|
{SPREADS.map((s) => (
|
|
|
|
|
|
<label key={s.id} className={s.id === spread ? "tarot-spread is-active" : "tarot-spread"}>
|
|
|
|
|
|
<input
|
|
|
|
|
|
type="radio"
|
|
|
|
|
|
name="spread"
|
|
|
|
|
|
value={s.id}
|
|
|
|
|
|
checked={s.id === spread}
|
|
|
|
|
|
onChange={() => setSpread(s.id)}
|
|
|
|
|
|
className="sr-only"
|
|
|
|
|
|
/>
|
|
|
|
|
|
<span className="tarot-spread-name">{s.name}</span>
|
|
|
|
|
|
<span className="hint">{s.about}</span>
|
|
|
|
|
|
</label>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</fieldset>
|
|
|
|
|
|
<div className="field">
|
|
|
|
|
|
<label htmlFor="tarot-question">Kérdésed (nem kötelező)</label>
|
|
|
|
|
|
<textarea
|
|
|
|
|
|
id="tarot-question"
|
|
|
|
|
|
className="inp"
|
|
|
|
|
|
rows={2}
|
|
|
|
|
|
maxLength={TAROT_QUESTION_MAX}
|
|
|
|
|
|
value={question}
|
|
|
|
|
|
placeholder="Pl. Mire figyeljek, ha munkahelyet váltanék? Kérdés nélkül általános útmutatást kapsz."
|
|
|
|
|
|
onChange={(e) => setQuestion(e.target.value)}
|
|
|
|
|
|
/>
|
|
|
|
|
|
<span className="hint">
|
|
|
|
|
|
Nyitott kérdés működik a legjobban („mire figyeljek”, „mit tanulhatok”), nem igen–nem kérdés.
|
|
|
|
|
|
</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className="form-actions">
|
|
|
|
|
|
<button type="submit" className="btn btn-primary" disabled={writing}>
|
|
|
|
|
|
Keverés és húzás
|
|
|
|
|
|
</button>
|
|
|
|
|
|
<span className="hint">
|
|
|
|
|
|
{writing
|
|
|
|
|
|
? "Várd meg, amíg az előző jóslásod elkészül."
|
|
|
|
|
|
: left > 0
|
|
|
|
|
|
? `Még ${left} jóslásod van.`
|
|
|
|
|
|
: "Nincs több jóslásod."}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
{error && (
|
|
|
|
|
|
<p className="msg msg-error" role="alert">
|
|
|
|
|
|
{error}
|
|
|
|
|
|
</p>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</form>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</section>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
|
|
{phase !== "setup" && (
|
|
|
|
|
|
<section className="card tarot-stage" aria-labelledby="tarot-stage-title">
|
|
|
|
|
|
<div className="tarot-stage-head">
|
|
|
|
|
|
<span className="eyebrow">{current?.spread_name ?? chosen.name}</span>
|
|
|
|
|
|
<h2 id="tarot-stage-title">
|
|
|
|
|
|
{phase === "picking"
|
|
|
|
|
|
? `Válassz ${chosen.cards} lapot a pakliból`
|
|
|
|
|
|
: phase === "drawing"
|
|
|
|
|
|
? "A lapok…"
|
|
|
|
|
|
: "A kirakásod"}
|
|
|
|
|
|
</h2>
|
|
|
|
|
|
{question.trim() && <p className="tarot-question">„{question.trim()}”</p>}
|
|
|
|
|
|
<p className="hint" role="status">
|
|
|
|
|
|
{phase === "picking" &&
|
|
|
|
|
|
`Hangolódj a kérdésedre, és válassz ösztönösen. ${picked.length}/${chosen.cards}`}
|
|
|
|
|
|
</p>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-11-20 15:51:23 +00:00
|
|
|
|
<TarotSlots count={slots} filled={current ? slots : picked.length} cards={current?.cards} revealed={revealed} />
|
2026-11-20 14:35:50 +00:00
|
|
|
|
|
2026-11-20 15:51:23 +00:00
|
|
|
|
{phase === "picking" && <TarotFan picked={picked} onPick={pick} />}
|
2026-11-20 14:35:50 +00:00
|
|
|
|
{phase === "picking" && (
|
|
|
|
|
|
<div className="form-actions">
|
|
|
|
|
|
<button type="button" className="btn btn-ghost" onClick={again}>
|
|
|
|
|
|
Mégsem
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
|
|
{phase === "done" && current && (
|
|
|
|
|
|
<>
|
|
|
|
|
|
<TarotLive key={current.id} initial={current} isAdmin={isAdmin} onChange={finished} />
|
|
|
|
|
|
<div className="form-actions">
|
|
|
|
|
|
<button type="button" className="btn" onClick={again}>
|
|
|
|
|
|
Új jóslás
|
|
|
|
|
|
</button>
|
|
|
|
|
|
<Link href={`/kartyajoslas/${current.id}`} className="btn btn-ghost">
|
|
|
|
|
|
Külön oldalon
|
|
|
|
|
|
</Link>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</section>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
|
|
{earlier.length > 0 && (
|
|
|
|
|
|
<section className="card" aria-labelledby="tarot-history-title">
|
|
|
|
|
|
<h2 id="tarot-history-title">Korábbi jóslásaim</h2>
|
|
|
|
|
|
<ul className="tarot-history">
|
|
|
|
|
|
{earlier.map((r) => (
|
|
|
|
|
|
<li key={r.id}>
|
|
|
|
|
|
<Link href={`/kartyajoslas/${r.id}`} className="tarot-history-row">
|
|
|
|
|
|
<span className="tarot-history-cards" aria-hidden="true">
|
|
|
|
|
|
{r.cards.slice(0, 5).map((c, i) => {
|
|
|
|
|
|
const card = tarotCard(c.card);
|
|
|
|
|
|
return card ? <TarotCard key={i} card={card} reversed={c.reversed} className="tarot-card-xs" /> : null;
|
|
|
|
|
|
})}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
<span className="tarot-history-text">
|
|
|
|
|
|
<strong>{r.content?.title ?? r.question ?? r.spread_name}</strong>
|
|
|
|
|
|
<span className="hint">
|
|
|
|
|
|
{day(r.created_at)} · {r.spread_name}
|
|
|
|
|
|
{r.question && r.content ? ` · „${r.question}”` : ""}
|
|
|
|
|
|
{r.status === "pending" || r.status === "running" ? " · készül" : ""}
|
|
|
|
|
|
{r.status === "failed" ? " · nem sikerült" : ""}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
</span>
|
|
|
|
|
|
</Link>
|
|
|
|
|
|
</li>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</ul>
|
|
|
|
|
|
</section>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|