import { createContext, useContext, useEffect, useId, useRef, useState, type ReactNode, } from 'react'; import { CircleHelp } from 'lucide-react'; import { helpPreferenceKey, readHelpPreference, type InvestmentHelp, } from '@/lib/investment-step-help'; type GuideState = { steps: InvestmentHelp[]; active: string | null; automatic: boolean; choose: (code: string | null, reveal?: boolean) => void; }; const GuideContext = createContext(null); const helpButton = 'inline-flex min-h-11 items-center justify-center gap-2 rounded-lg border px-3 py-2 text-sm font-semibold focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-emerald-700'; export function InvestmentHelpGuide({ steps, user, flow, initialCode, children, }: { steps: InvestmentHelp[]; user: string | number; flow: string; initialCode?: string; children: ReactNode; }) { const root = useRef(null); const [active, setActive] = useState(null); const [automatic, setAutomatic] = useState(true); const paused = useRef(false); const key = helpPreferenceKey(user, flow); const firstCode = steps.some((step) => step.code === initialCode) ? initialCode : steps[0]?.code; useEffect(() => { let preference = readHelpPreference(null); try { preference = readHelpPreference(window.localStorage.getItem(key)); } catch { /* Help also works without browser storage. */ } setAutomatic(preference.automatic); setActive( preference.automatic && !preference.seen ? (firstCode ?? null) : null, ); paused.current = false; try { window.localStorage.setItem( key, JSON.stringify({ ...preference, seen: true }), ); } catch { /* Optional preference only. */ } }, [key]); // A new applicable step does not restart a dismissed guide. const visibleActive = steps.some((step) => step.code === active) ? active : null; useEffect(() => { if (!visibleActive || !root.current) return; const target = Array.from( root.current.querySelectorAll( '[data-investment-step]', ), ).find((node) => node.dataset.investmentStep === visibleActive); let ancestor: HTMLElement | null = target ?? null; while (ancestor && ancestor !== root.current) { if (ancestor.tagName === 'DETAILS') (ancestor as HTMLDetailsElement).open = true; ancestor = ancestor.parentElement; } }, [visibleActive]); function choose(code: string | null, reveal = false) { paused.current = code === null; if (code === null && visibleActive) { const panel = Array.from( root.current?.querySelectorAll( '[data-help-panel]', ) ?? [], ).find((node) => node.dataset.helpPanel === visibleActive); panel?.parentElement ?.querySelector('button') ?.focus({ preventScroll: true }); } setActive(code); if (code && reveal) { // Wait for the selected inline explanation to render before focusing it. requestAnimationFrame(() => { const target = Array.from( root.current?.querySelectorAll( '[data-help-panel]', ) ?? [], ).find((node) => node.dataset.helpPanel === code); target?.scrollIntoView({ block: 'nearest' }); target?.focus({ preventScroll: true }); }); } } function orient(target: EventTarget, disclosure = false) { if ( !automatic || paused.current || !(target instanceof Element) || target.closest('[data-help-controls]') || (!disclosure && target.closest('summary')) ) return; const code = target.closest('[data-investment-step]') ?.dataset.investmentStep; if (code && steps.some((step) => step.code === code)) setActive(code); } useEffect(() => { const node = root.current; if (!node) return; const onToggle = (event: Event) => { const target = event.target; // Native toggle follows the summary click. Observe the opened state // instead of changing it while focus/click is still being dispatched. if (target instanceof HTMLDetailsElement && target.open) { orient(target, true); } }; node.addEventListener('toggle', onToggle, true); return () => node.removeEventListener('toggle', onToggle, true); }, [automatic, steps]); return (
orient(event.target)} onClickCapture={(event) => orient(event.target)} onKeyDown={(event) => { if ( event.key === 'Escape' && visibleActive && (event.target as Element).closest('[data-help-panel]') ) { event.preventDefault(); choose(null); } }} >

Guia do Investemoz

Consulte o que fazer, veja um exemplo e avance ao seu ritmo.

{children}
); } export function HelpAtStep({ code }: { code: string }) { const guide = useContext(GuideContext); const id = useId(); const index = guide?.steps.findIndex((step) => step.code === code) ?? -1; if (!guide || index < 0) return null; const step = guide.steps[index]; const open = guide.active === code; return (
{open && (

Ajuda · Passo {index + 1} de {guide.steps.length}

{step.title}

O que fazer: {step.instruction}

Exemplo: {step.example}

Para que serve: {step.purpose}

{index < guide.steps.length - 1 ? ( ) : ( )}
)}
); }