/* * The report's "How do I…?" help (`help`, `docsUrl`): where it is (its button after More, or only in * More), whether the reader took the button out of the toolbar (kept with the tips, tip-state.js), * the request of More's "How do I…?", the note on More the first time the button left, and the control "Show * me" marks with the step its card says. The help's list, its topics and "Show me" load on first use * (help-module.js); this hook is the part of the first load. */ import { useEffect, useRef, useState } from "react"; import { fullScreenSupported } from "./use-big-result.js"; /** * Where the help is: "on" its button in the toolbar (the default), "more" only in More, "off" none * (More → Help as without it). * @param {unknown} value the host's `help` * @returns {"on" | "more" | "off"} */ const helpMode = (value) => (value === false || value === "false" ? "off" : value === "more" ? "more" : "on"); /** * @typedef {Omit & { target?: string, text: string, lang?: string }} GuideStep * one control "Show me" marks, as help-show.js plans it, or the page's element of an own question by * its id (`target`); with what its card says (`{name}`: the words the control shows) and its language * where it is not the report's * @typedef {{ steps: GuideStep[], at: number, lang: "en" | "de" }} Guide "Show me" running: its steps and the one now * @typedef {object} HelpState * @property {Guide | null} guide "Show me" running: its steps and the one it marks now (help-guide.jsx) * @property {boolean} hint the note on More the first time the reader took the button out of the toolbar, until the next press or key * @property {number} request More's "How do I…?" with the button in the toolbar: a click on it * @property {boolean} atMore More's "How do I…?" without the button: the help at More */ /** * @param {import("./index.js").PivotReportProps} props * @param {ReturnType} dismissed */ export function useHelp(props, dismissed) { const mode = helpMode(props.help); const inToolbar = mode === "on" && dismissed.toolbar.helpButton.on; const [state, setState] = useState(/** @type {HelpState} */ ({ guide: null, hint: false, request: 0, atMore: false })); // the latest switch and place, for the callbacks made once below const latest = useRef({ set: dismissed.toolbar.helpButton.set, noted: dismissed.toolbar.helpButton.noted, inToolbar }); useEffect(() => { latest.current = { set: dismissed.toolbar.helpButton.set, noted: dismissed.toolbar.helpButton.noted, inToolbar }; }); // made once: the parts that take them (the note's listeners, the card) keep them const [act] = useState(() => { const set = (/** @type {Partial} */ patch) => setState((s) => ({ ...s, ...patch })); return { setGuide: (/** @type {Guide | null} */ guide) => set({ guide }), endGuide: () => set({ guide: null }), endHint: () => set({ hint: false }), closeAtMore: () => set({ atMore: false }), open: () => (latest.current.inToolbar ? setState((s) => ({ ...s, request: s.request + 1 })) : set({ atMore: true })), // the note only the first time the button goes (HELP_NOTED, kept with the tips) setInToolbar: (/** @type {boolean} */ on) => { const first = !on && !latest.current.noted; latest.current.set(on); set({ hint: first }); } }; }); return { ...state, ...act, mode, inToolbar, docsUrl: props.docsUrl, topics: props.helpTopics, drillDown: props.drillDown !== false, // the report offers Full screen (use-big-result.js): its question only then fullScreen: fullScreenSupported(), guideKey: state.guide?.steps[state.guide.at]?.key ?? null, // what More shows of the help: its "How do I…?", and the button's switch where it can be in the toolbar more: mode === "off" ? null : { inToolbar: mode === "on" ? inToolbar : null, open: act.open, setInToolbar: act.setInToolbar } }; }