// The hive UI's page chrome: `@hive/shared`'s `AppShell`, the same header, nav // row and settings menu the swarm UI has, with the hive's name for a brand and // the dashboard's three tabs. Routes are the URL hash (`#agents`, `#rebuild`, // `#kept`), so the gateway needs nothing beyond serving `index.html`. // // The tab panes stay plain DOM, built by `agents.js`, `builds.js` and // `kept.js`: `index.html` keeps them in a hidden store, and `Panes` moves them // into the shell's content column and shows the one the hash names. import { h, render } from "preact"; import { useEffect, useLayoutEffect, useRef, useState } from "preact/hooks"; import { AppShell } from "@hive/shared/app-shell.js"; import { LinksMenu } from "@hive/shared/links-menu.js"; import { UserMenu } from "@hive/shared/user-menu.js"; import { TABS, menuLinks, tabOfHash } from "./shell-routes.js"; // Storage keys of the shared settings, this page's own: the swarm UI is // another origin, so a choice made there is not seen here. const THEME_KEY = "hive-ui:theme-override"; const MOTION_KEY = "hive-ui:motion-override"; const SOLID_KEY = "hive-ui:solid-backgrounds"; function useHashTab() { const [tab, setTab] = useState(() => tabOfHash(location.hash)); useEffect(() => { const onHash = () => setTab(tabOfHash(location.hash)); window.addEventListener("hashchange", onHash); return () => window.removeEventListener("hashchange", onHash); }, []); return tab; } // A nav link for a hash route. function HashLink({ href, className, children, ...rest }) { return h("a", { href: "#" + href, class: className, ...rest }, children); } // The tab panes, moved out of the store into the content column. The shell // remounts its content on every navigation, which moves them in again. function Panes({ tab, panes, onShow }) { const box = useRef(null); useLayoutEffect(() => { box.current.append(...panes); for (const pane of panes) pane.hidden = pane.dataset.tabPane !== tab; onShow(tab); }, [tab]); return h("div", { ref: box }); } function Hive({ state, panes, onShow }) { const tab = useHashTab(); return h( AppShell, { brand: (state && state.hive_name) || "hive", brandHref: TABS[0].href, items: TABS, location: tab, LinkComponent: HashLink, themeKey: THEME_KEY, motionKey: MOTION_KEY, solidKey: SOLID_KEY, menus: [ h(LinksMenu, { links: menuLinks(state), label: "pages", key: "links" }), h(UserMenu, { whoamiUrl: "/api/whoami", autheliaUrl: (state && state.authelia_public_url) || null, key: "user", }), ], }, h(Panes, { tab, panes, onShow }), ); } /** (Re)render the shell into `#app-shell` for a state snapshot (`null` before * the first one). Safe to call on every refresh: Preact diffs the same tree, * so an open menu stays open. `onShow(tabId)` runs whenever a tab is shown. */ export function renderShell(state, onShow) { const root = document.getElementById("app-shell"); if (!root) return; if (!renderShell.panes) { renderShell.panes = Array.from( document.querySelectorAll("[data-tab-pane]"), ); } render(h(Hive, { state, panes: renderShell.panes, onShow }), root); }