Files
nanobot/webui/src/components/settings/shared/SettingsFeature.tsx
T
chengyongru ce3f36983d feat(webui): stabilize session transitions and goal presentation
Keep the composer stable during interruptible session changes. Move active and blocked goals into the header with localized gallery previews, and preserve focus when overlays close. Include the reviewed navigation, layout and accessibility fixes.
2026-11-13 23:37:07 +08:00

92 lines
4.2 KiB
TypeScript

import { useEffect, useRef, useState, type ReactNode } from "react";
import { ChevronDown } from "lucide-react";
import { useTranslation } from "react-i18next";
import { ToggleButton } from "@/components/ui/toggle-button";
import { SettingsGroup, SettingsRow } from "@/components/settings/shared/SettingsControls";
import { Dialog, DialogContent, DialogTitle, DialogTrigger } from "@/components/ui/dialog";
import { Disclosure } from "@/components/ui/disclosure";
export function SettingsAdvancedOptions({ children, initialOpen = false }: { children: ReactNode; initialOpen?: boolean }) {
const { t } = useTranslation();
return (
<Disclosure
initialOpen={initialOpen}
summaryClassName="settings-list-inset flex min-h-12 cursor-pointer items-center justify-between gap-4 rounded-xl text-[13px] leading-5 text-muted-foreground settings-hover focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
summary={<>
{t("settings.runtimeConfig.advancedOptions")}
<ChevronDown aria-hidden className="h-3.5 w-3.5 shrink-0 transition-transform duration-200 group-data-[state=open]/disclosure:rotate-180 motion-reduce:transition-none" />
</>}
>
<div className="space-y-1">{children}</div>
</Disclosure>
);
}
export function SettingsFeature({
title, enabled, onChange, disabled, initialOpen = false, error, children, onClose,
}: {
title: string;
enabled: boolean;
onChange: (enabled: boolean) => void;
disabled?: boolean;
initialOpen?: boolean;
error?: string;
children?: ReactNode;
onClose?: () => void;
}) {
const { t } = useTranslation();
const [open, setOpen] = useState(initialOpen && enabled);
const section = useRef<HTMLElement>(null);
const opener = useRef<HTMLElement | null>(null);
const changeOpen = (next: boolean) => {
if (next) opener.current = document.activeElement instanceof HTMLElement ? document.activeElement : null;
setOpen(next);
};
useEffect(() => {
if (initialOpen && enabled) {
setOpen(true);
section.current?.scrollIntoView?.({ block: "nearest" });
}
}, [initialOpen, enabled]);
return (
<section ref={section} aria-label={title}>
<Dialog open={Boolean(children) && open} onOpenChange={(next) => {
changeOpen(next);
if (!next) onClose?.();
}}>
<SettingsGroup>
<SettingsRow title={children ? (
<DialogTrigger asChild>
<button type="button"
className="flex min-h-9 w-full items-center rounded-lg text-start focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring">
{title}
</button>
</DialogTrigger>
) : title}>
{children ? <DialogTrigger asChild>
<button type="button"
className="mr-3 shrink-0 rounded-lg px-2 py-1 text-[13px] leading-5 text-muted-foreground settings-hover hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring">
{t("settings.configure")}
</button>
</DialogTrigger> : null}
<ToggleButton checked={enabled} disabled={disabled} ariaLabel={title} label={title}
onChange={(next) => { changeOpen(next); onChange(next); }} />
</SettingsRow>
</SettingsGroup>
{children ? <DialogContent aria-describedby={undefined} onCloseAutoFocus={(event) => {
if (opener.current?.isConnected && !opener.current.matches(":disabled")) {
opener.current.focus({ preventScroll: true });
// Saving on close can disable the switch that opened this dialog.
// Let Radix restore its Configure trigger if the opener cannot focus.
if (document.activeElement === opener.current) event.preventDefault();
}
}} className="settings-grid max-h-[85dvh] w-[min(calc(100vw-2rem),40rem)] max-w-none overflow-y-auto p-0 [--settings-surface:var(--background)]">
<DialogTitle className="sr-only">{title}</DialogTitle>
<div className="pb-4 pt-8">{children}</div>
</DialogContent> : null}
</Dialog>
{error && !open ? <p role="alert" className="settings-list-inset pt-2 text-[13px] leading-5 text-destructive">{error}</p> : null}
</section>
);
}