File size: 2,869 Bytes
ba70497
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// UI state that outlives a page (filters, selections, search), the hash route, the hover tip and toasts.
import { createContext, useCallback, useContext, useEffect, useRef, useState } from "react";

const INITIAL = {
  q: "", sel: null, evFilter: { outcome: "all", kind: "all" },
  evView: "results", resultOpen: false, openEvent: null, evVisit: null, evPageSize: 5, evPage: 0,
  testReader: null, logFilter: "reader", caseFilter: { verdict: "all", cat: "all" },
  logTab: "calls", logFile: null, callEvent: "", callFailed: false,
  chartView: "chart", confirmDelete: null,
  orb: { view: "network", event: null, run: "" },
};

const UiCtx = createContext(null);

function readHash() { return location.hash.replace(/^#\/?/, "").split("?")[0].split("/"); }

export function UiProvider({ children }) {
  const [ui, setState] = useState(INITIAL);
  const set = useCallback((patch) => setState((s) => ({ ...s, ...(typeof patch === "function" ? patch(s) : patch) })), []);

  const [segs, setSegs] = useState(readHash);
  // Goes up on every move to another page; a redraw (language change) leaves it as it is.
  const [visit, setVisit] = useState(0);
  useEffect(() => {
    const on = () => {
      const s = readHash();
      // Old links: #/reports and #/writeups are now the Tests page.
      if (s[0] === "reports" || s[0] === "writeups") { location.replace("#/tests"); return; }
      setSegs(s);
      setVisit((n) => n + 1);
    };
    window.addEventListener("hashchange", on);
    return () => window.removeEventListener("hashchange", on);
  }, []);

  // Toast
  const [toastMsg, setToastMsg] = useState({ text: "", on: false });
  const timer = useRef();
  const toast = useCallback((text) => {
    setToastMsg({ text, on: true });
    clearTimeout(timer.current);
    timer.current = setTimeout(() => setToastMsg((t) => ({ ...t, on: false })), 3200);
  }, []);

  // Hover tip: any element calls tip.show(event, content) / tip.hide().
  const [tipState, setTip] = useState({ on: false, x: 0, y: 0, content: null });
  const tip = useRef({
    show: (e, content) => setTip({ on: true, x: e.pageX, y: e.pageY, content }),
    hide: () => setTip((t) => ({ ...t, on: false })),
  }).current;

  return (
    <UiCtx.Provider value={{ ui, set, segs, visit, toast, tip }}>
      {children}
      <div className={`tip${tipState.on ? " on" : ""}`} style={{ left: tipState.x, top: tipState.y }}>{tipState.content}</div>
      <div className={`toast${toastMsg.on ? " on" : ""}`} role="status" aria-live="polite">{toastMsg.text}</div>
    </UiCtx.Provider>
  );
}

export const useUi = () => useContext(UiCtx);
export const go = (hash) => { location.hash = hash; };

// Props that show a plain-text tip on hover.
export function useTipProps() {
  const { tip } = useUi();
  return (text) => ({ onMouseMove: (e) => tip.show(e, <b>{text}</b>), onMouseLeave: tip.hide });
}