File size: 1,215 Bytes
af1bee9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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<typeof useAsk>;