"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 = { 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(null); const panel = useRef(null); const fab = useRef(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("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 ( <>
{/* eslint-disable-next-line @next/next/no-img-element */}
{t.teaser} {t.lead}
); }