the hive dashboard uses the swarm UI's links menu, beside the profile avatar
The links menu moves into @hive/shared and takes its links as a prop. The swarm UI feeds it /api/links as before; the dashboard feeds it stats, API docs and the swarm, and mounts it next to the profile menu in place of its own hamburger on the left. A link opens in a new tab unless its entry says it is a page of the same site.
This commit is contained in:
parent
b2e3b05198
commit
464b52004b
1 changed files with 192 additions and 239 deletions
81
frontend/packages/shared/src/links-menu/LinksMenu.tsx
Normal file
81
frontend/packages/shared/src/links-menu/LinksMenu.tsx
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
// <LinksMenu> — a single header icon-button opening a popover of links, next
|
||||
// to the profile avatar. The swarm UI hands it the swarm's service list and
|
||||
// the hive dashboard its own pages; both look and behave the same, which is
|
||||
// why it lives here (it was the swarm UI's, and the dashboard had its own
|
||||
// copy with a hamburger on the left).
|
||||
//
|
||||
// A menu with no links renders nothing: no button beats a dead one.
|
||||
//
|
||||
// Styles live in `@hive/shared/links-menu.css`, which the mounting page
|
||||
// imports or `@import`s itself: the dashboard's JS bundle loads `.css`
|
||||
// imports as text, so a component that imported it would not style there
|
||||
// (the same split as `UserMenu`).
|
||||
import { useEffect, useRef, useState } from "preact/hooks";
|
||||
import { LinkIcon } from "../icons.js";
|
||||
import { linkAttrs, type MenuLink } from "./links.js";
|
||||
|
||||
export type { MenuLink };
|
||||
|
||||
export function LinksMenu({
|
||||
links,
|
||||
label = "links",
|
||||
}: {
|
||||
links: MenuLink[];
|
||||
/** The button's accessible name. */
|
||||
label?: string;
|
||||
}) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const rootRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return;
|
||||
function onPointerDown(e: MouseEvent) {
|
||||
if (rootRef.current && !rootRef.current.contains(e.target as Node))
|
||||
setOpen(false);
|
||||
}
|
||||
function onKeyDown(e: KeyboardEvent) {
|
||||
if (e.key === "Escape") setOpen(false);
|
||||
}
|
||||
document.addEventListener("pointerdown", onPointerDown);
|
||||
document.addEventListener("keydown", onKeyDown);
|
||||
return () => {
|
||||
document.removeEventListener("pointerdown", onPointerDown);
|
||||
document.removeEventListener("keydown", onKeyDown);
|
||||
};
|
||||
}, [open]);
|
||||
|
||||
if (links.length === 0) return null;
|
||||
|
||||
return (
|
||||
<div class="links-menu" ref={rootRef}>
|
||||
<button
|
||||
type="button"
|
||||
class="links-menu-button"
|
||||
aria-haspopup="true"
|
||||
aria-expanded={open}
|
||||
aria-label={label}
|
||||
title={label}
|
||||
onClick={() => setOpen((v) => !v)}
|
||||
>
|
||||
<LinkIcon />
|
||||
</button>
|
||||
{open ? (
|
||||
<div class="links-menu-popover" role="menu">
|
||||
{links.map((link) => (
|
||||
<a
|
||||
key={link.url}
|
||||
class="links-menu-item"
|
||||
href={link.url}
|
||||
{...linkAttrs(link)}
|
||||
role="menuitem"
|
||||
onClick={() => setOpen(false)}
|
||||
>
|
||||
{link.icon ? <span aria-hidden="true">{link.icon}</span> : null}
|
||||
{link.label}
|
||||
</a>
|
||||
))}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Loading…
Reference in a new issue