import { useCallback } from 'react'; import { useLocation, useNavigate } from 'react-router-dom'; /** What the Ask page reads from the navigation that brought the person there. */ export interface AskArrival { ask?: string | null; at: number } /** * Ask is a page (/assistant), not a panel that follows the person around. `ask()` goes there, carrying what the person * was looking at as context (the test run, or a business area), and optionally a question to send straight away. */ export function useAsk() { const nav = useNavigate(); const loc = useLocation(); const ask = useCallback((q?: string) => { const run = /^\/runs\/([^/]+)/.exec(loc.pathname)?.[1]; const here = /^\/assistant(\/|$)/.test(loc.pathname); const module = new URLSearchParams(loc.search).get('module'); const to = here ? `${loc.pathname}${loc.search}` : run && run !== 'new' ? `/assistant/${run}` : module ? `/assistant?module=${encodeURIComponent(module)}` : '/assistant'; const state: AskArrival = { ask: q ?? null, at: Date.now() }; nav(to, { state }); }, [nav, loc.pathname, loc.search]); return { ask }; } export type AskState = ReturnType;