Files
sorsfonalak/services/frontend/src/components/tarot/TarotDemo.tsx
T

103 lines
3.4 KiB
TypeScript
Raw Normal View History

"use client";
import { useEffect, useState } from "react";
import type { TarotContent, TarotReading } from "@/lib/tarot";
import { TarotResult } from "./TarotResult";
import { TarotFan, TarotSlots } from "./TarotSpread";
export interface TarotExample {
id: string;
label: string;
signs: string;
spread_name: string;
question: string;
cards: TarotReading["cards"];
content: TarotContent;
}
/** The fan cards the demo "picks", in order. */
const PICKS = [4, 13, 8, 17, 2];
/**
* Plays a reading: the cards are picked from the fanned deck one by one,
* laid out, turned over, then the written reading appears below. The text is
* on the page from the start (hidden while the animation plays).
*/
function Demo({ example }: { example: TarotExample }) {
const n = example.cards.length;
const done = 3 * n + 2;
const [step, setStep] = useState(0);
// Without motion the end state shows at once.
useEffect(() => {
if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) setStep(done);
}, [done]);
useEffect(() => {
if (step >= done) return;
const picking = step <= 2 * n;
const delay = step === 0 ? 900 : picking ? (step % 2 === 1 ? 420 : 520) : 680;
const t = setTimeout(() => setStep((s) => s + 1), delay);
return () => clearTimeout(t);
}, [step, done, n]);
const pickedCount = Math.min(n, Math.floor(step / 2));
const lifted = step % 2 === 1 && step < 2 * n ? PICKS[(step - 1) / 2] : null;
const revealed = Math.max(0, Math.min(n, step - (2 * n + 1)));
const finished = step >= done;
return (
<div className="tarot-demo">
<div className="tarot-stage-head">
<span className="eyebrow">{example.spread_name}</span>
<p className="tarot-question">„{example.question}”</p>
<p className="hint">A kérdés és a képlet kitalált ({example.signs}).</p>
</div>
<TarotSlots count={n} filled={pickedCount} cards={example.cards} revealed={revealed} />
{!finished && <TarotFan picked={PICKS.slice(0, pickedCount)} lifted={lifted} />}
<p className="hint tarot-demo-status" aria-live="polite">
{finished ? (
<button type="button" className="btn btn-sm btn-ghost" onClick={() => setStep(0)}>
↻ Húzás újra
</button>
) : pickedCount < n ? (
`Lapválasztás a pakliból: ${pickedCount}/${n}`
) : (
"A lapok felfordulnak…"
)}
</p>
<div className={finished ? "tarot-demo-text is-shown" : "tarot-demo-text"}>
<TarotResult cards={example.cards} question={null} content={example.content} />
</div>
</div>
);
}
/** The example readings, one tab each. */
export function TarotDemo({ examples }: { examples: TarotExample[] }) {
const [active, setActive] = useState(examples[0]?.id);
const example = examples.find((e) => e.id === active) ?? examples[0];
if (!example) return null;
return (
<section className="card" aria-label="Példajóslások">
<div className="tab-list" role="tablist" aria-label="Példák">
{examples.map((e) => (
<button
key={e.id}
type="button"
role="tab"
className="tab"
aria-selected={e.id === example.id}
onClick={() => setActive(e.id)}
>
{e.label}
</button>
))}
</div>
<Demo key={example.id} example={example} />
</section>
);
}