159 lines
5.3 KiB
TypeScript
159 lines
5.3 KiB
TypeScript
"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>
|
|||
|
|
</>
|
|||
|
|
);
|
|||
|
|
}
|