Files
sorsfonalak/services/frontend/src/components/DetailedAnalysis.tsx
T
fecoandClaude Opus 5.5 e5cfbd6cdd
CI / test (push) Successful in 41s
CI / build (push) Successful in 4m11s
CI / deploy (push) Skipped
Detailed analysis button, loading wheel, birth data change limits
Backend:
- own birth data: three corrections within an hour of the first save,
  then one change per 24 hours (birth_data_changes); 429
  birth_data_change_limit with the user-facing message; /v1/me
  birth_change shows the notice; admins are not limited
- admins get detailed analyses free (no unit spent)
- POST /v1/payments/mock (MOCK_PAYMENTS): stand-in payment that adds a
  unit, until Stripe is connected; on in the compose files for now

Frontend:
- detailed analysis offer: admin starts it free, users with units
  confirm, others go through the (stand-in) payment step
- turning wheel with elapsed time while the summary or the analysis is
  being written
- no "adatok javítása" on the chart page (only in the profile), limit
  notice there; wheel hover highlighting removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
2026-11-17 06:45:33 +00:00

183 lines
6.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use client";
import Link from "next/link";
import { useEffect, useState } from "react";
import { api, errorText } from "@/lib/api";
import type { Analysis } from "@/lib/types";
import { AnalysisLoading } from "./AnalysisLoading";
import { AnalysisView } from "./AnalysisView";
const POLL_MS = 5000;
const MAX_POLLS = 120; // 10 minutes
type Step = "offer" | "confirm" | "pay";
/**
* The detailed analysis of a chart. A saved one is shown at once; otherwise
* the offer: admins start it free, users with units confirm, others pay
* (a stand-in payment for now). While the worker writes it, a loading wheel.
*/
export function DetailedAnalysis({
chartId,
initial,
left,
isAdmin,
canPay,
}: {
chartId: string;
initial: Analysis | null;
left: number;
isAdmin: boolean;
/** The stand-in payment is on (MOCK_PAYMENTS). */
canPay: boolean;
}) {
const [analysis, setAnalysis] = useState<Analysis | null>(initial);
const [step, setStep] = useState<Step>("offer");
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
const waiting = analysis?.status === "pending" || analysis?.status === "running";
useEffect(() => {
if (!waiting || !analysis) return;
let polls = 0;
let stopped = false;
let timer: ReturnType<typeof setTimeout> | undefined;
const tick = async () => {
try {
const next = await api<Analysis>(`/analyses/${analysis.id}`);
if (stopped) return;
setAnalysis(next);
if (next.status === "done" || next.status === "failed") 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, analysis]);
async function start(pay: boolean) {
setBusy(true);
setError(null);
try {
if (pay) await api("/payments/mock", { body: { unit: "natal" } });
setAnalysis(await api<Analysis>(`/charts/${chartId}/analyses`, { method: "POST" }));
setStep("offer");
} catch (e) {
setError(errorText(e));
} finally {
setBusy(false);
}
}
if (analysis?.status === "done" && analysis.content) {
return (
<section className="card analysis-card" aria-label="Részletes elemzés">
<span className="eyebrow">Részletes elemzés</span>
<AnalysisView content={analysis.content} />
</section>
);
}
if (waiting) {
return (
<section className="card analysis-card" aria-label="Részletes elemzés" aria-busy="true">
<span className="eyebrow">Részletes elemzés</span>
<AnalysisLoading
title="Készül az elemzésed"
text="Most íródik a hat fejezet a képleted alapján. Ez általában 1–3 perc; nyugodtan elhagyhatod az oldalt, az elemzés elkészül és itt vár."
/>
</section>
);
}
return (
<section className="card analysis-card" aria-labelledby="detailed-title">
<span className="eyebrow">Részletes elemzés</span>
<h2 id="detailed-title">Az egész képleted, fejezetekre bontva</h2>
<p className="muted">
Kb. 1500 szó hat fejezetben: személyiség, érzelmek, gondolkodás, szerelem és kapcsolatok, hivatás, fejlődési út.
A végén erősségek, kihívások és útravaló. Csak a képleted pozíciói alapján készül.
</p>
{analysis?.status === "failed" && (
<p className="msg msg-error" role="alert">
Az előző kérés nem sikerült{isAdmin ? "" : ", az elemzést visszaadtuk a keretedbe"}. Próbáld újra.
</p>
)}
{error && (
<p className="msg msg-error" role="alert">
{error}
</p>
)}
{isAdmin ? (
<div className="form-actions">
<button type="button" className="btn btn-primary" onClick={() => start(false)} disabled={busy}>
{busy ? "Indítás…" : "Részletes elemzés készítése"}
</button>
<span className="hint">Adminként ingyenes.</span>
</div>
) : left > 0 ? (
step === "confirm" ? (
<div className="form-actions">
<button type="button" className="btn btn-primary" onClick={() => start(false)} disabled={busy}>
{busy ? "Indítás…" : "Igen, kérem az elemzést"}
</button>
<button type="button" className="btn btn-ghost" onClick={() => setStep("offer")} disabled={busy}>
Mégse
</button>
<span className="hint">Ez felhasznál 1-et a {left} képletelemzésedből.</span>
</div>
) : (
<div className="form-actions">
<button type="button" className="btn btn-primary" onClick={() => setStep("confirm")}>
Részletes elemzés kérése
</button>
<span className="hint">Még {left} képletelemzésed van.</span>
</div>
)
) : canPay ? (
step === "pay" ? (
<div className="pay-box">
<div className="pay-line">
<span>Részletes képletelemzés</span>
<strong>1 db</strong>
</div>
<div className="form-actions">
<button type="button" className="btn btn-primary" onClick={() => start(true)} disabled={busy}>
{busy ? "Fizetés…" : "Fizetés"}
</button>
<button type="button" className="btn btn-ghost" onClick={() => setStep("offer")} disabled={busy}>
Mégse
</button>
</div>
<p className="hint">Tesztüzem: a fizetés még nincs bekötve, a gomb nem terhel semmit.</p>
</div>
) : (
<div className="form-actions">
<button type="button" className="btn btn-primary" onClick={() => setStep("pay")}>
Részletes elemzés kérése
</button>
<Link href="/pelda" className="btn btn-ghost">
Példa megtekintése
</Link>
</div>
)
) : (
<div className="form-actions">
<Link href="/pelda" className="btn">
Példa megtekintése
</Link>
<span className="hint">A részletes elemzés fizetős; a csomagok hamarosan elérhetők.</span>
</div>
)}
</section>
);
}