Files
Sybil-2/web/src/components/settings/app-settings.tsx
T

120 lines
8.2 KiB
TypeScript

import { useLayoutEffect, useRef, useState } from "preact/hooks";
import { ArrowLeft, Bell, ChevronRight, Cpu, Keyboard, LoaderCircle, LogOut, Server, X } from "lucide-preact";
import { AgentNotifications } from "@/components/agent/agent-notifications";
import { ModelSettings, type ModelSettingsProps } from "@/components/settings/model-settings";
import { Button } from "@/components/ui/button";
import type { AuthMode } from "@/hooks/use-session-auth";
import { getApiBaseUrl } from "@/lib/api";
import { cn } from "@/lib/utils";
type Section = "notifications" | "models" | "connection";
const sections = [
{ id: "notifications", title: "Notifications", description: "Agent alerts and permissions", icon: Bell },
{ id: "models", title: "Models", description: "Chats and Quick Questions", icon: Cpu },
{ id: "connection", title: "Connection & session", description: "Server, sign-in, and shortcuts", icon: Server },
] as const;
function displayServerUrl() {
try {
const url = new URL(getApiBaseUrl(), window.location.href);
return `${url.origin}${url.pathname}`.replace(/\/$/, "");
} catch { return "Unavailable"; }
}
export function AppSettings({ authMode, onClose, onLogout, ...modelSettings }: ModelSettingsProps & {
authMode: AuthMode | null;
onClose: () => void;
onLogout: () => Promise<void>;
}) {
const dialogRef = useRef<HTMLDialogElement>(null);
const contentRef = useRef<HTMLDivElement>(null);
const navRef = useRef<HTMLElement>(null);
const lastSectionRef = useRef<Section | null>(null);
const [section, setSection] = useState<Section | null>(null);
const [signingOut, setSigningOut] = useState(false);
const activeSection = section ?? "notifications";
const modifier = /Mac|iPhone|iPad|iPod/i.test(navigator.platform) ? "⌘" : "Ctrl";
useLayoutEffect(() => {
const dialog = dialogRef.current;
const previouslyFocused = document.activeElement;
dialog?.showModal();
return () => {
dialog?.close();
if (previouslyFocused instanceof HTMLElement && previouslyFocused.isConnected) previouslyFocused.focus({ preventScroll: true });
};
}, []);
useLayoutEffect(() => {
if (section) {
lastSectionRef.current = section;
if (contentRef.current) contentRef.current.scrollTop = 0;
contentRef.current?.focus({ preventScroll: true });
} else if (lastSectionRef.current) {
navRef.current?.querySelector<HTMLButtonElement>(`[data-section="${lastSectionRef.current}"]`)?.focus({ preventScroll: true });
}
}, [section]);
return <dialog
ref={dialogRef}
aria-labelledby="app-settings-title"
className="fixed inset-0 m-0 flex h-dvh max-h-none w-full max-w-none items-center justify-center overflow-hidden bg-transparent p-0 text-foreground backdrop:bg-black/70 backdrop:backdrop-blur-md md:p-6"
onCancel={(event) => { event.preventDefault(); onClose(); }}
onClick={(event) => { if (event.target === event.currentTarget) onClose(); }}
>
<div className="glass-panel flex h-full min-h-0 w-full flex-col overflow-hidden md:h-[min(44rem,calc(100dvh-3rem))] md:max-w-[760px] md:rounded-2xl md:border md:border-violet-300/24">
<header className="app-settings-header flex shrink-0 items-center justify-between gap-4 border-b border-violet-300/15">
<div className="flex min-w-0 items-center gap-2">
{section ? <Button type="button" variant="ghost" size="icon" className="h-11 w-11 shrink-0 md:hidden" aria-label="Back to settings" onClick={() => setSection(null)}>
<ArrowLeft aria-hidden="true" className="h-5 w-5" />
</Button> : null}
<h2 id="app-settings-title" className="text-lg font-semibold text-violet-50">Settings</h2>
</div>
<Button type="button" size="icon" variant="ghost" className="h-11 w-11 shrink-0" aria-label="Close settings" onClick={onClose}>
<X aria-hidden="true" className="h-5 w-5" />
</Button>
</header>
<div className="flex min-h-0 flex-1 flex-col md:flex-row">
<nav ref={navRef} aria-label="Settings sections" className={cn("app-settings-nav shrink-0 space-y-2 overflow-y-auto overscroll-contain md:block md:w-44 md:border-r md:border-violet-300/15", section && "hidden")}>
{sections.map(({ id, title, description, icon: Icon }) => <button key={id} type="button" data-section={id} aria-label={title}
aria-current={activeSection === id ? "page" : undefined} aria-controls="app-settings-content" onClick={() => setSection(id)}
className={cn("flex min-h-11 w-full items-center gap-3 rounded-lg px-3 py-4 text-left text-violet-100/75 hover:bg-violet-400/10 hover:text-violet-50 focus-visible:outline focus-visible:outline-2 focus-visible:outline-ring md:items-start md:gap-2 md:py-3", activeSection === id && "md:bg-violet-400/15 md:text-violet-50")}>
<Icon aria-hidden="true" className="mt-0.5 h-4 w-4 shrink-0 text-violet-300" />
<span className="min-w-0 flex-1"><span className="block text-sm font-medium leading-5">{title}</span><span className="mt-1 block text-xs text-muted-foreground md:hidden">{description}</span></span>
<ChevronRight aria-hidden="true" className="h-4 w-4 shrink-0 md:hidden" />
</button>)}
</nav>
<div ref={contentRef} id="app-settings-content" role="group" aria-label={sections.find(item => item.id === activeSection)?.title} tabIndex={-1}
className={cn("app-settings-content min-h-0 min-w-0 flex-1 overflow-y-auto overscroll-contain outline-none md:block", !section && "hidden")}>
{activeSection === "notifications" ? authMode === "token" ? <AgentNotifications /> : (
<section aria-labelledby="notification-settings-title" className="space-y-3">
<h3 id="notification-settings-title" className="flex items-center gap-2 text-base font-semibold text-violet-50"><Bell aria-hidden="true" className="h-4 w-4 text-violet-300" />Notifications</h3>
<p className="text-sm leading-relaxed text-muted-foreground">Agent notifications require a server with token sign-in.</p>
</section>
) : activeSection === "models" ? <ModelSettings {...modelSettings} /> : (
<section aria-labelledby="connection-settings-title" className="space-y-6">
<h3 id="connection-settings-title" className="flex items-center gap-2 text-base font-semibold text-violet-50"><Server aria-hidden="true" className="h-4 w-4 text-violet-300" />Connection &amp; session</h3>
<dl className="space-y-5 text-sm">
<div className="space-y-1.5"><dt className="font-medium text-violet-100/80">Server</dt><dd className="break-all text-muted-foreground">{displayServerUrl()}</dd></div>
<div className="space-y-1.5"><dt className="font-medium text-violet-100/80">Sign-in</dt><dd className="text-muted-foreground">{authMode === "token" ? "Signed in on this browser." : "Open mode. No sign-in required."}</dd></div>
</dl>
{authMode === "token" ? <Button type="button" className="h-11" variant="secondary" disabled={signingOut} onClick={() => {
setSigningOut(true);
void onLogout().finally(() => setSigningOut(false));
}}>
{signingOut ? <LoaderCircle aria-hidden="true" className="mr-2 h-4 w-4 animate-spin" /> : <LogOut aria-hidden="true" className="mr-2 h-4 w-4" />}Sign out
</Button> : null}
<details className="border-t border-violet-300/15 pt-2">
<summary className="flex min-h-11 cursor-pointer items-center gap-2 py-3 text-sm text-violet-100/75 focus-visible:outline focus-visible:outline-2 focus-visible:outline-ring"><Keyboard aria-hidden="true" className="h-4 w-4" />Keyboard shortcuts</summary>
<dl className="space-y-3 pb-2 pt-2 text-xs text-muted-foreground">
{[ ["New chat", `${modifier} + J`], ["New search", `${modifier} + Shift + J`], ["Quick Question", `${modifier} + I`], ["Previous / next item", `${modifier} + ↑ / ↓`] ].map(([label, keys]) => <div key={label} className="flex flex-wrap justify-between gap-2"><dt>{label}</dt><dd><kbd>{keys}</kbd></dd></div>)}
</dl>
</details>
</section>
)}
</div>
</div>
</div>
</dialog>;
}