control-tower / tower /ui /web /src /components /Layout.jsx
Crypt0Maniac's picture
Deploy the control tower
d8d9ed1
Raw History Blame Contribute Delete
3.31 kB
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>
);
}