- Under the summary, the detailed analyses, the forecast and tarot: "How tailored to you did it feel?" (1–5 stars); thumbs under answers. Only the owner rates, once (changeable); tags, a comment, and a permission to read the text; model and prompt version are kept with it - /visszajelzes: bug, idea, question, other (the page and the browser go with it, 10 a day); the state and the operator's reply show there, a dot in the account menu marks a new reply. No e-mail, no Telegram - One NPS question (0–10) in a bar under the header, after two finished contents; closing it counts - Admin → Visszajelzések: numbers by kind, week, tags, model and prompt, buyers and others, NPS; ratings with filters, the text only if allowed; messages with state, internal note and reply; a count in the admin menu - Nothing is public. Deleting an account keeps the stars without a name or comment; messages go. Privacy policy (hu, en) updated - The admin menu no longer shows a vertical scrollbar Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
255 lines
9.3 KiB
TypeScript
255 lines
9.3 KiB
TypeScript
"use client";
|
|
|
|
import Link from "next/link";
|
|
import { useCallback, useEffect, useRef, useState } from "react";
|
|
|
|
import { api, ApiError, errorText } from "@/lib/api";
|
|
import type { PayMethod, Question, UnitBalance } from "@/lib/types";
|
|
|
|
import { AnalysisLoading } from "./AnalysisLoading";
|
|
import { PackageChooser } from "./PackageCards";
|
|
import { RateContent } from "./RateContent";
|
|
import { ReportLink } from "./ReportLink";
|
|
import { RegenerateButton } from "./RegenerateButton";
|
|
import { RetryNotice } from "./RetryNotice";
|
|
import { Paragraphs } from "./Rich";
|
|
|
|
/** The longest question (the backend checks it too: QUESTION_MAX_CHARS). */
|
|
const MAX_CHARS = 300;
|
|
|
|
export const QUESTION_TEXTS = {
|
|
natal: {
|
|
title: "Kérdezz az elemzésedről",
|
|
hint: "Mit jelent ez a te életedben, hogyan kapcsolódik két rész egymáshoz? A válasz a képleted és az elemzés alapján készül, legfeljebb 250 szóban.",
|
|
example: "Pl. Hogyan hat a Hold helyzete a kapcsolataimra?",
|
|
},
|
|
synastry: {
|
|
title: "Kérdezz a kapcsolatotokról",
|
|
hint: "Hol erősítitek egymást, mire figyeljetek egy vitában, mit ad nektek a kapcsolat? A válasz a két képlet és az elemzés alapján készül, legfeljebb 250 szóban. A kérdéseket és a válaszokat mindketten látjátok.",
|
|
example: "Pl. Hogyan kezeljük jobban a nézeteltéréseinket?",
|
|
},
|
|
forecast: {
|
|
title: "Kérdezz az előrejelzésedről",
|
|
hint: "Mire figyelj egy időszakban, mit hozhat egy tranzit a munkádban vagy a kapcsolataidban? A válasz az év adatai és az előrejelzés alapján készül, legfeljebb 250 szóban.",
|
|
example: "Pl. Mire figyeljek a nyári időszakban a munkámban?",
|
|
},
|
|
};
|
|
const POLL_MS = 3000;
|
|
const MAX_POLLS = 80; // 4 minutes
|
|
|
|
const TEXTS = QUESTION_TEXTS;
|
|
|
|
/**
|
|
* Questions about a finished analysis or forecast, and their answers. Each
|
|
* question uses one from the user's questions (admins ask for free); the
|
|
* answer comes from the model that wrote the analysis. `inPanel`: shown in
|
|
* the chat panel (see QuestionsDock), which has its own heading.
|
|
*/
|
|
export function AnalysisQuestions({
|
|
analysisId,
|
|
left: initialLeft,
|
|
isAdmin,
|
|
canPay,
|
|
about,
|
|
inPanel = false,
|
|
onAnswered,
|
|
}: {
|
|
analysisId: string;
|
|
left: number;
|
|
isAdmin: boolean;
|
|
canPay: PayMethod | null;
|
|
/** What the analysis is about: the heading and the example question follow it. */
|
|
about: "natal" | "synastry" | "forecast";
|
|
inPanel?: boolean;
|
|
/** An answer has just arrived (for the panel's "new answer" dot). */
|
|
onAnswered?: () => void;
|
|
}) {
|
|
const [questions, setQuestions] = useState<Question[] | null>(null);
|
|
const [left, setLeft] = useState(initialLeft);
|
|
const [text, setText] = useState("");
|
|
const [busy, setBusy] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [buying, setBuying] = useState(false);
|
|
const waiting = questions?.some((q) => q.status === "pending" || q.status === "running") ?? false;
|
|
const fieldId = `question-${analysisId}`;
|
|
const thread = useRef<HTMLDivElement>(null);
|
|
const answered = useRef(onAnswered);
|
|
answered.current = onAnswered;
|
|
|
|
useEffect(() => {
|
|
api<{ questions: Question[] }>(`/analyses/${analysisId}/questions`)
|
|
.then((d) => setQuestions(d.questions))
|
|
.catch(() => setQuestions([]));
|
|
}, [analysisId]);
|
|
|
|
const replace = useCallback(
|
|
(next: Question) => setQuestions((all) => all?.map((q) => (q.id === next.id ? next : q)) ?? [next]),
|
|
[],
|
|
);
|
|
|
|
// Polls the open question until it is answered.
|
|
const openId = questions?.find((q) => q.status === "pending" || q.status === "running")?.id;
|
|
useEffect(() => {
|
|
if (!openId) return;
|
|
let polls = 0;
|
|
let stopped = false;
|
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
const tick = async () => {
|
|
try {
|
|
const next = await api<Question>(`/questions/${openId}`);
|
|
if (stopped) return;
|
|
replace(next);
|
|
if (next.status === "done" || next.status === "failed") {
|
|
if (next.status === "failed" && next.credits_charged > 0) setLeft((n) => n + next.credits_charged);
|
|
answered.current?.();
|
|
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);
|
|
};
|
|
}, [openId, replace]);
|
|
|
|
async function ask(e: React.FormEvent) {
|
|
e.preventDefault();
|
|
setBusy(true);
|
|
setError(null);
|
|
try {
|
|
const q = await api<Question>(`/analyses/${analysisId}/questions`, { body: { question: text } });
|
|
setQuestions((all) => [...(all ?? []), q]);
|
|
setLeft((n) => n - q.credits_charged);
|
|
setText("");
|
|
} catch (err) {
|
|
if (err instanceof ApiError && err.status === 402) {
|
|
setLeft(0);
|
|
setBuying(true);
|
|
} else {
|
|
setError(errorText(err));
|
|
}
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
}
|
|
|
|
async function bought() {
|
|
try {
|
|
const me = await api<{ analyses_left: UnitBalance }>("/me");
|
|
setLeft(me.analyses_left.question);
|
|
} catch {
|
|
setLeft((n) => n + 5);
|
|
}
|
|
setBuying(false);
|
|
}
|
|
|
|
// In the panel the newest message stays in view.
|
|
const last = questions?.at(-1);
|
|
useEffect(() => {
|
|
if (inPanel && thread.current) thread.current.scrollTop = thread.current.scrollHeight;
|
|
}, [inPanel, questions?.length, last?.status]);
|
|
|
|
const canAsk = left > 0;
|
|
return (
|
|
<section
|
|
className={inPanel ? "questions questions-panel" : "questions"}
|
|
aria-labelledby={inPanel ? undefined : `${fieldId}-title`}
|
|
aria-label={inPanel ? TEXTS[about].title : undefined}
|
|
>
|
|
{!inPanel && <h3 id={`${fieldId}-title`}>{TEXTS[about].title}</h3>}
|
|
<div className="qa-thread" ref={thread}>
|
|
<p className="hint">{TEXTS[about].hint}</p>
|
|
|
|
{questions?.map((q) => (
|
|
<article key={q.id} className="qa" aria-busy={q.status === "pending" || q.status === "running"}>
|
|
<p className="qa-question">
|
|
<span className="eyebrow">Kérdésed</span>
|
|
{q.question}
|
|
</p>
|
|
{q.status === "done" && q.answer ? (
|
|
<div className="qa-answer">
|
|
{isAdmin && (
|
|
<RegenerateButton<Question> url={`/admin/questions/${q.id}/regenerate`} onStarted={replace} />
|
|
)}
|
|
<Paragraphs text={q.answer} />
|
|
<RateContent questionId={q.id} thumbs />
|
|
</div>
|
|
) : q.status === "failed" ? (
|
|
<div className="qa-answer">
|
|
{isAdmin && (
|
|
<RegenerateButton<Question> url={`/admin/questions/${q.id}/regenerate`} onStarted={replace} />
|
|
)}
|
|
<p className="msg msg-error" role="alert">
|
|
Erre többszöri próbálkozás után sem sikerült válaszolni
|
|
{q.credits_charged > 0 ? ". A kérdést visszaadtuk, így ingyen újra felteheted" : ""}. Kérdezz újra egy
|
|
kicsit később. Ha újra nem sikerül: <ReportLink />.
|
|
</p>
|
|
{isAdmin && q.error && <p className="hint">Hiba: {q.error}</p>}
|
|
</div>
|
|
) : (
|
|
<>
|
|
<AnalysisLoading
|
|
compact
|
|
text="Készül a válasz, ez általában fél perc…"
|
|
since={q.requested_at ?? q.created_at}
|
|
/>
|
|
<RetryNotice status={q.status} error={q.error} nextAttemptAt={q.next_attempt_at} isAdmin={isAdmin} />
|
|
</>
|
|
)}
|
|
</article>
|
|
))}
|
|
</div>
|
|
|
|
{canAsk && !buying ? (
|
|
<form className="qa-form" onSubmit={ask}>
|
|
<label htmlFor={fieldId} className="sr-only">
|
|
A kérdésed
|
|
</label>
|
|
<textarea
|
|
id={fieldId}
|
|
className="inp"
|
|
rows={inPanel ? 2 : 3}
|
|
maxLength={MAX_CHARS}
|
|
value={text}
|
|
placeholder={TEXTS[about].example}
|
|
onChange={(e) => setText(e.target.value)}
|
|
disabled={busy || waiting}
|
|
/>
|
|
<div className="form-actions">
|
|
<button type="submit" className="btn btn-primary" disabled={busy || waiting || text.trim().length < 3}>
|
|
{busy ? "Küldés…" : "Kérdés küldése"}
|
|
</button>
|
|
<span className="hint">
|
|
{waiting ? "Várd meg az előző válaszát." : `Még ${left} kérdésed van.`}{" "}
|
|
<span className="num" aria-live="polite">
|
|
· {text.length}/{MAX_CHARS}
|
|
</span>
|
|
</span>
|
|
</div>
|
|
{error && (
|
|
<p className="msg msg-error" role="alert">
|
|
{error}
|
|
</p>
|
|
)}
|
|
</form>
|
|
) : canPay ? (
|
|
<div className="pay-box">
|
|
<p className="pay-lead">
|
|
Most nincs felhasználható kérdésed. Öt kérdés 1 000 Ft, és nem jár le: bármelyik elemzésedről vagy
|
|
előrejelzésedről kérdezhetsz vele.
|
|
</p>
|
|
<PackageChooser ids={["addon_questions"]} canPay={canPay} onBought={bought} row={inPanel} />
|
|
</div>
|
|
) : (
|
|
<p className="hint">
|
|
Most nincs felhasználható kérdésed. Ha kaptál promókódot, <Link href="/bevaltas">váltsd be</Link>.
|
|
</p>
|
|
)}
|
|
</section>
|
|
);
|
|
}
|