Files
sorsfonalak/services/frontend/src/components/QuestionsDock.tsx
T
fecoandClaude Opus 5.5 e5c90b260d
CI / test (push) Successful in 2m5s
CI / build (push) Skipped
CI / deploy (push) Skipped
Questions in a chat panel behind a floating button
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
2026-11-23 10:57:08 +00:00

159 lines
5.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 { useEffect, useId, useRef, useState } from "react";
import type { PayMethod } from "@/lib/types";
import { AnalysisQuestions, QUESTION_TEXTS } from "./AnalysisQuestions";
type About = "natal" | "synastry" | "forecast";
/** The floating button's label and the line under the panel's title. */
const DOCK: Record<About, { button: string; lead: string; teaser: string }> = {
natal: {
button: "Kérdezz a képletedről",
lead: "A képleted és az elemzésed alapján válaszolunk.",
teaser: "Kérdésed van a képleteddel?",
},
synastry: {
button: "Kérdezz a kapcsolatotokról",
lead: "A két képlet és az elemzés alapján válaszolunk. A kérdéseket mindketten látjátok.",
teaser: "Kérdésed van a kapcsolatotokkal?",
},
forecast: {
button: "Kérdezz az évedről",
lead: "Az év adatai és az előrejelzésed alapján válaszolunk.",
teaser: "Kérdésed van az előrejelzéseddel?",
},
};
export const QUESTION_ICON = "/img/kerdezz.webp";
/**
* Questions about a finished analysis in a chat panel behind a floating button
* (bottom right), plus a short line at the end of the analysis that opens the
* same panel. With no questions left the panel offers the question package.
* The panel stays mounted while closed, so a pending answer keeps arriving.
*/
export function QuestionsDock({
analysisId,
left,
isAdmin,
canPay,
about,
}: {
analysisId: string;
left: number;
isAdmin: boolean;
canPay: PayMethod | null;
about: About;
}) {
const [open, setOpen] = useState(false);
const [unread, setUnread] = useState(false);
// The line at the end of the analysis is on screen: the floating button would only repeat it.
const [teaserInView, setTeaserInView] = useState(false);
const teaser = useRef<HTMLDivElement>(null);
const panel = useRef<HTMLElement>(null);
const fab = useRef<HTMLButtonElement>(null);
const panelId = useId();
const titleId = useId();
const t = DOCK[about];
function show() {
setOpen(true);
setUnread(false);
}
function hide() {
setOpen(false);
// The button is shown again on the next render; then it gets the focus back
// (or the line at the end of the analysis, while the button stays hidden).
requestAnimationFrame(() => {
const target = fab.current && !fab.current.hidden ? fab.current : teaser.current?.querySelector("button");
target?.focus();
});
}
useEffect(() => {
if (!open) return;
panel.current?.querySelector<HTMLTextAreaElement>("textarea")?.focus({ preventScroll: true });
const escape = (e: KeyboardEvent) => {
if (e.key === "Escape") hide();
};
document.addEventListener("keydown", escape);
// On phones the panel fills the screen: the page behind it does not scroll.
document.body.classList.add("qdock-open");
return () => {
document.removeEventListener("keydown", escape);
document.body.classList.remove("qdock-open");
};
}, [open]);
useEffect(() => {
const el = teaser.current;
if (!el || typeof IntersectionObserver === "undefined") return;
const io = new IntersectionObserver(([entry]) => setTeaserInView(entry.isIntersecting));
io.observe(el);
return () => io.disconnect();
}, []);
return (
<>
<div className="qa-teaser" ref={teaser}>
{/* eslint-disable-next-line @next/next/no-img-element */}
<img src={QUESTION_ICON} alt="" width={48} height={48} className="qa-teaser-icon" />
<div className="qa-teaser-text">
<strong>{t.teaser}</strong>
<span className="hint">{t.lead}</span>
</div>
<button type="button" className="btn btn-primary" onClick={show}>
Kérdezz
</button>
</div>
<button
ref={fab}
type="button"
className="qdock-fab"
hidden={open || (teaserInView && !unread)}
aria-controls={panelId}
aria-expanded={open}
aria-label={unread ? `${t.button} – új válasz érkezett` : t.button}
onClick={show}
>
{/* eslint-disable-next-line @next/next/no-img-element */}
<img src={QUESTION_ICON} alt="" width={44} height={44} />
<span className="qdock-fab-label">{t.button}</span>
{unread && <span className="qdock-dot" aria-hidden="true" />}
</button>
<aside ref={panel} id={panelId} className="qdock-panel" hidden={!open} aria-labelledby={titleId}>
<header className="qdock-head">
{/* eslint-disable-next-line @next/next/no-img-element */}
<img src={QUESTION_ICON} alt="" width={40} height={40} />
<div className="qdock-title">
<h2 id={titleId}>{QUESTION_TEXTS[about].title}</h2>
<span className="hint">{t.lead}</span>
</div>
<button type="button" className="modal-close" onClick={hide} aria-label="Bezárás">
<svg viewBox="0 0 24 24" aria-hidden="true">
<path d="M6 6l12 12M18 6L6 18" />
</svg>
</button>
</header>
<AnalysisQuestions
analysisId={analysisId}
left={left}
isAdmin={isAdmin}
canPay={canPay}
about={about}
inPanel
onAnswered={() => {
if (!panel.current || panel.current.hidden) setUnread(true);
}}
/>
</aside>
</>
);
}