File size: 3,313 Bytes
ba70497
 
 
 
 
 
 
 
 
 
d8d9ed1
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { Fragment, useEffect, useRef, useState } from "react";
import { Icon } from "../lib/icons.jsx";
import { useData } from "../lib/data.jsx";
import { useUi } from "../lib/ui.jsx";
import { Search } from "./Search.jsx";
import { tr } from "../lib/i18n.js";

const NAV = [
  ["overview", "home", "Overview"], ["events", "feed", "Event center"], ["add", "plus", "Add events"],
  ["tests", "check", "Tests"],
  ["ontology", "graph", "Knowledge graph"], ["records", "table", "Company records"], ["logs", "doc", "Logs"],
];

export function Sidebar({ page }) {
  const link = ([id, ic, label]) => (
    <a key={id} href={`#/${id}`} className={page === id ? "on" : ""}><span className="ico"><Icon name={ic} /></span>{tr(label)}</a>
  );
  return (
    <aside className="side">
      <div className="brand">
        <div className="brand-mark">W</div>
        <div className="brand-name">{tr("WACKY WHEELS")}<span>{tr("CONTROL TOWER")}</span></div>
      </div>
      <nav className="nav">{NAV.map(link)}</nav>
      <div className="nav-label">{tr("HELP")}</div>
      <nav className="nav">{link(["guide", "book", "Guide"])}</nav>
      <nav className="nav side-foot">{link(["settings", "gear", "Settings"])}</nav>
    </aside>
  );
}

// Shown only when the data can't be reached; otherwise the dashboard says nothing about loading.
export function LoadBanner() {
  const d = useData();
  if (!d.failed) return null;
  return (
    <div className="load-banner" role="status">
      <span>{tr("Couldn't load the latest data. Some pages may be empty.")}</span>
      <button className="btn" onClick={d.reload} disabled={d.busy}>{d.busy ? tr("Retrying…") : tr("Retry")}</button>
    </div>
  );
}

// title, and either a trail ([[label, href|null], ...]) or a subtitle (node) under it.
export function Topbar({ title, trail, subtitle }) {
  const { set } = useUi();
  const q = useRef(null);
  const [scrolled, setScrolled] = useState(false);

  useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 8);
    // "/" jumps to search from anywhere; Esc clears it.
    const onKey = (e) => {
      const typing = /^(INPUT|TEXTAREA|SELECT)$/.test(document.activeElement?.tagName || "");
      if (e.key === "/" && !typing) { e.preventDefault(); q.current?.focus(); }
      if (e.key === "Escape" && document.activeElement === q.current) { set({ q: "" }); q.current.blur(); }
    };
    window.addEventListener("scroll", onScroll, { passive: true });
    document.addEventListener("keydown", onKey);
    return () => { window.removeEventListener("scroll", onScroll); document.removeEventListener("keydown", onKey); };
  }, [set]);

  return (
    <header className={`topbar${scrolled ? " scrolled" : ""}`}>
      <div className="tb-left">
        {trail && (
          <nav className="trail" aria-label={tr("Breadcrumb")}>
            {trail.map(([label, href], i) => (
              <Fragment key={i}>
                {i > 0 && <span className="sep">›</span>}
                {href ? <a href={href}>{label}</a> : <span>{label}</span>}
              </Fragment>
            ))}
          </nav>
        )}
        <h1>{title}</h1>
        {!trail && <div className="tb-sub">{subtitle}</div>}
      </div>
      <div className="tb-right">
        <Search ref={q} />
      </div>
    </header>
  );
}