File size: 7,676 Bytes
e94832e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/** The frame every page sits in: a left rail with the wordmark, the sections,
 *  an on-this-page list, and the theme toggle. Content to the right. */
import { useEffect, useState, type ReactNode } from "react";
import { Link, NavLink, useLocation, useParams } from "react-router-dom";
import { useBuildCheck } from "../lib/useBuildCheck";
import { useTheme } from "../lib/theme";
import { forgetTokens } from "../lib/tokens";
import { CAPABILITIES, stageFor } from "../content/capabilities";
import { CommandPalette } from "./CommandPalette";
import { DotArt } from "./DotArt";

/** ⌘ on a Mac, Ctrl everywhere else. Showing the wrong one teaches the wrong
 *  shortcut, which is worse than showing none. */
const MOD = typeof navigator !== "undefined" && /Mac|iP(hone|ad)/.test(navigator.platform)
  ? "\u2318K"
  : "Ctrl K";

const PAGES = [
  { to: "/", label: "Overview" },
  { to: "/case", label: "This case" },
  { to: "/monitor", label: "Live monitor" },
  { to: "/alerts", label: "Alert queue" },
  { to: "/investigate", label: "Investigation graph" },
  { to: "/peers", label: "Peer lookup" },
  { to: "/redteam", label: "Red team" },
  { to: "/custody", label: "Chain of custody" },
];

export interface Anchor {
  id: string;
  label: string;
}

export function Shell({
  anchors,
  children,
  variant = "rail",
}: {
  anchors?: Anchor[];
  children: ReactNode;
  /** "top" puts the navigation in a bar across the page, for the showcase. */
  variant?: "rail" | "top";
}) {
  const [theme, toggleTheme] = useTheme();
  const build = useBuildCheck();
  const [paletteOpen, setPaletteOpen] = useState(false);
  const [activeAnchor, setActiveAnchor] = useState<string | null>(null);
  const { pathname } = useLocation();
  const params = useParams();
  const stage = variant === "rail" ? stageFor(pathname) : null;

  // A case and a transaction are places too. Showing the open one in the rail
  // is how a reader learns the console has more than two pages.
  const open = params.id
    ? { label: `Case ${params.id.slice(0, 10)}…`, to: pathname }
    : params.txid
      ? { label: `Transaction ${params.txid.slice(0, 8)}…`, to: pathname }
      : params.peer
        ? { label: `Peer ${params.peer.slice(0, 15)}`, to: pathname }
        : null;

  useEffect(() => {
    const onKey = (event: KeyboardEvent) => {
      if ((event.metaKey || event.ctrlKey) && event.key.toLowerCase() === "k") {
        event.preventDefault();
        setPaletteOpen(true);
      }
    };
    addEventListener("keydown", onKey);
    return () => removeEventListener("keydown", onKey);
  }, []);

  // Which section the reader is in. Observed, not computed on scroll, so the
  // rail never costs a frame while the table is being worked.
  useEffect(() => {
    if (!anchors?.length) return;
    setActiveAnchor(anchors[0].id);
    const observer = new IntersectionObserver(
      (entries) => {
        const seen = entries.filter((e) => e.isIntersecting);
        if (seen.length) setActiveAnchor(seen[0].target.id);
      },
      { rootMargin: "-10% 0px -70% 0px" },
    );
    for (const anchor of anchors) {
      const node = document.getElementById(anchor.id);
      if (node) observer.observe(node);
    }
    return () => observer.disconnect();
  }, [anchors, pathname]);

  return (
    <div className={variant === "top" ? "shell shell-top" : "shell"}>
      <header className={variant === "top" ? "topbar" : "rail"}>
        <NavLink to="/" className="wordmark">
          <span className="wordmark-mark" aria-hidden="true" />
          btc-intel
        </NavLink>

        <nav className="rail-nav" aria-label="Sections">
          {PAGES.map((page) => (
            <NavLink key={page.to} to={page.to} end={page.to === "/"} className="rail-link">
              {page.label}
            </NavLink>
          ))}
          {open && (
            <NavLink to={open.to} className="rail-link" title={params.id ?? params.txid ?? params.peer}>
              <span className="mono" style={{ fontSize: "var(--fs-small)" }}>{open.label}</span>
            </NavLink>
          )}
        </nav>

        {variant === "rail" && anchors?.length ? (
          <>
            <hr className="rail-divider" />
            <p className="label">On this page</p>
            <nav className="subnav" aria-label="On this page">
              {anchors.map((anchor) => (
                <a
                  key={anchor.id}
                  href={`#${anchor.id}`}
                  aria-current={activeAnchor === anchor.id}
                >
                  {anchor.label}
                </a>
              ))}
            </nav>
          </>
        ) : null}

        <div className="rail-foot">
          <button type="button" className="btn btn-ghost bracket" onClick={() => setPaletteOpen(true)}>
            Search <span className="muted">{MOD}</span>
          </button>
          <button
            type="button"
            className="btn btn-quiet"
            onClick={() => {
              forgetTokens();
              toggleTheme();
            }}
            aria-label={`Switch to ${theme === "dark" ? "light" : "dark"} theme`}
          >
            <span className="theme-swatch" aria-hidden="true" />
            {theme === "dark" ? "light" : "dark"}
          </button>
          {variant === "rail" && (
            <p className="label" style={{ marginTop: "var(--sp-2)" }}>
              Offline, synthetic data
            </p>
          )}
        </div>
      </header>

      <main className="content">
        {(build.mismatch || build.unreachable) && (
          <div className="notice build-notice" role="alert">
            <span aria-hidden="true">⚠</span>
            <div>
              <p className="label">
                {build.mismatch ? "The API is a different build" : "The API is not answering"}
              </p>
              <p className="soft" style={{ marginTop: "var(--sp-1)" }}>
                {build.mismatch ? (
                  <>
                    This page was built from <code className="mono">{build.ours}</code>; the
                    server at <code className="mono">{"/version"}</code> reports{" "}
                    <code className="mono">{build.theirs}</code>, started{" "}
                    {build.startedAt?.replace("T", " ").replace("+00:00", " UTC")}. Restart it
                    with <code className="mono">offline/run_offline.sh</code> before trusting
                    anything on screen.
                  </>
                ) : (
                  <>
                    Nothing answered <code className="mono">/version</code>. Either the API is
                    not running, or it is an older build without that endpoint — start it with{" "}
                    <code className="mono">offline/run_offline.sh</code>.
                  </>
                )}
              </p>
            </div>
          </div>
        )}
        {stage && (
          <Link to={`/#${stage.id}`} className="stage-banner" viewTransition>
            <DotArt name={stage.art} mode={stage.mode} cell={4} className="stage-banner-art" />
            <span className="stage-banner-copy">
              <span className="stage-num">
                stage {String(CAPABILITIES.indexOf(stage) + 1).padStart(2, "0")} of{" "}
                {CAPABILITIES.length}
              </span>
              <span className="stage-banner-name">{stage.name}</span>
              <span className="stage-banner-title">{stage.title}</span>
            </span>
          </Link>
        )}
        {children}
      </main>
      <CommandPalette open={paletteOpen} onClose={() => setPaletteOpen(false)} />
    </div>
  );
}