usePolled takes a cache key: it saves each answer in localStorage and starts from the saved one, so opening the agents, alerts, builds, review runs or hives view shows the last data at once and the refresh spinner marks the update. A saved answer is dropped after a day, when it was saved by another build of the UI, at log out (a new onLogout of the shared user menu) and when the gateway ends the login (a new onSessionEnded of the session guard). Pages with an editor (schedules, notification flows, broadcast) do not use it.
43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
// <UserMenu> in the swarm UI's header: the shared profile menu
|
|
// (`@hive/shared/user-menu.js`) with authelia's URL filled in from
|
|
// `GET /api/links`, the same source `LinksMenu` shows its "Authelia" entry
|
|
// from. The name comes from `/api/whoami`, which `swarm-ui.nix` proxies to
|
|
// authelia's own user info.
|
|
import { useEffect, useState } from "preact/hooks";
|
|
import { UserMenu as SharedUserMenu } from "@hive/shared/user-menu.js";
|
|
import "@hive/shared/user-menu.css";
|
|
import { clearAnswers } from "../ui/polled/answer-cache.js";
|
|
|
|
interface ServiceLink {
|
|
label: string;
|
|
icon: string;
|
|
url: string;
|
|
}
|
|
|
|
export function UserMenu() {
|
|
const [autheliaUrl, setAutheliaUrl] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
fetch("/api/links")
|
|
.then((r) =>
|
|
r.ok
|
|
? (r.json() as Promise<ServiceLink[]>)
|
|
: Promise.reject(new Error(`http ${r.status}`)),
|
|
)
|
|
.then((links) => {
|
|
const authelia = links.find((l) => l.label === "Authelia");
|
|
if (authelia) setAutheliaUrl(authelia.url);
|
|
})
|
|
.catch(() => {
|
|
/* cosmetic only: the menu then shows the name without the links */
|
|
});
|
|
}, []);
|
|
|
|
return (
|
|
<SharedUserMenu
|
|
whoamiUrl="/api/whoami"
|
|
autheliaUrl={autheliaUrl}
|
|
onLogout={() => clearAnswers()}
|
|
/>
|
|
);
|
|
}
|