Questions in a chat panel behind a floating button
CI / test (push) Successful in 2m5s
CI / build (push) Skipped
CI / deploy (push) Skipped

Where a finished detailed analysis can be asked about (chart, comparison,
forecast), a floating button with the silhouette icon opens a chat panel
(a side panel on desktop, full screen on phones): earlier questions and
answers as bubbles, the question box at the bottom, or a one-line package
offer when no questions are left. The end of the analysis keeps a short
line that opens the same panel; the button hides while that line is in
view. The panel stays mounted when closed, so an answer keeps arriving and
the button shows a dot for it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
feco
2026-11-23 10:57:08 +00:00
co-authored by Claude Opus 5.5
parent 6a147aa253
commit e5c90b260d
6 changed files with 374 additions and 99 deletions
@@ -1,7 +1,7 @@
"use client";
import Link from "next/link";
import { useCallback, useEffect, useState } from "react";
import { useCallback, useEffect, useRef, useState } from "react";
import { api, ApiError, errorText } from "@/lib/api";
import type { PayMethod, Question, UnitBalance } from "@/lib/types";
@@ -14,7 +14,7 @@ import { Paragraphs } from "./Rich";
const MAX_CHARS = 600;
const TEXTS = {
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.",
@@ -34,10 +34,13 @@ const TEXTS = {
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.
* 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,
@@ -45,6 +48,8 @@ export function AnalysisQuestions({
isAdmin,
canPay,
about,
inPanel = false,
onAnswered,
}: {
analysisId: string;
left: number;
@@ -52,6 +57,9 @@ export function AnalysisQuestions({
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);
@@ -61,6 +69,9 @@ export function AnalysisQuestions({
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`)
@@ -87,6 +98,7 @@ export function AnalysisQuestions({
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 {
@@ -132,49 +144,61 @@ export function AnalysisQuestions({
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="questions" aria-labelledby={`${fieldId}-title`}>
<h3 id={`${fieldId}-title`}>{TEXTS[about].title}</h3>
<p className="hint">{TEXTS[about].hint}</p>
<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} />
</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.
</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>
))}
{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} />
</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.
</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}>
@@ -184,7 +208,7 @@ export function AnalysisQuestions({
<textarea
id={fieldId}
className="inp"
rows={3}
rows={inPanel ? 2 : 3}
maxLength={MAX_CHARS}
value={text}
placeholder={TEXTS[about].example}
@@ -212,7 +236,7 @@ export function AnalysisQuestions({
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} />
<PackageChooser ids={["addon_questions"]} canPay={canPay} onBought={bought} row={inPanel} />
</div>
) : (
<p className="hint">