fix(webui): polish mobile settings and context interactions

This commit is contained in:
Xubin Ren committed 2026-09-13 23:35:16 +08:00
1 parent 827dbe137b
commit f5c6be2608
5 files changed
+553 -80

No files matched your search

+123 -60
View File
@@ -1,4 +1,4 @@
import { useRef } from "react";
import { useRef, useState } from "react";
import {
Activity,
Info,
@@ -23,12 +23,8 @@ import {
} from "@/components/SidebarSelectionHighlight";
import { isCapabilitySection, type SettingsSectionKey } from "@/components/settings/contracts";
import { Button } from "@/components/ui/button";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { Sheet, SheetContent, SheetTitle, SheetTrigger } from "@/components/ui/sheet";
import { useMediaQuery } from "@/hooks/useMediaQuery";
import { cn } from "@/lib/utils";
const SETTINGS_NAV_ITEMS: Array<{ key: SettingsSectionKey; icon: LucideIcon; fallback: string }> = [
@@ -49,17 +45,7 @@ export function standaloneSectionTitle(section: SettingsSectionKey): string {
return SETTINGS_NAV_ITEMS.find((item) => item.key === section)?.fallback ?? "Settings";
}
export function SettingsSidebar({
activeSection,
onSelectSection,
onBackToChat,
onLogout,
hostChromeInset,
onRestart,
isRestarting,
restartPending,
isNativeHost,
}: {
interface SettingsSidebarProps {
activeSection: SettingsSectionKey;
onSelectSection: (section: SettingsSectionKey) => void;
onBackToChat: () => void;
@@ -69,8 +55,22 @@ export function SettingsSidebar({
isRestarting?: boolean;
restartPending?: boolean;
isNativeHost?: boolean;
}) {
}
export function SettingsSidebar(props: SettingsSidebarProps) {
const {
activeSection,
onSelectSection,
onBackToChat,
onLogout,
hostChromeInset,
onRestart,
isRestarting,
restartPending,
isNativeHost,
} = props;
const { t } = useTranslation();
const mobile = useMediaQuery("(max-width: 1023px)");
const restartLabel = isRestarting
? t(isNativeHost ? "app.system.restartingEngine" : "app.system.restarting")
: t("app.system.restartAction");
@@ -78,11 +78,15 @@ export function SettingsSidebar({
const activeNavItemRef = useRef<HTMLButtonElement>(null);
const activeItem = SETTINGS_NAV_ITEMS.find((item) => item.key === navSection)
?? SETTINGS_NAV_ITEMS[0];
const ActiveIcon = activeItem.icon;
const activeLabel = t(`settings.nav.${activeItem.key}`, {
defaultValue: activeItem.fallback,
});
if (mobile) {
return <MobileSettingsNavigation {...props} navSection={navSection}
activeLabel={activeLabel} restartLabel={restartLabel} />;
}
return (
<aside
className={cn(
@@ -106,46 +110,6 @@ export function SettingsSidebar({
aria-label={t("settings.sidebar.ariaLabel")}
className="w-full"
>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<button
type="button"
aria-label={`${t("settings.sidebar.title")}: ${activeLabel}`}
className="touch-target flex h-11 w-full items-center gap-2.5 rounded-control bg-sidebar-accent px-3 text-left text-[13px] leading-5 font-normal text-foreground transition-colors settings-hover lg:hidden"
>
<ActiveIcon className="h-4 w-4 shrink-0" strokeWidth={2} aria-hidden />
<span className="min-w-0 flex-1 truncate">{activeLabel}</span>
<ChevronDown className="h-4 w-4 shrink-0 text-sidebar-content" aria-hidden />
</button>
</DropdownMenuTrigger>
<DropdownMenuContent
align="start"
sideOffset={6}
className="w-[var(--radix-dropdown-menu-trigger-width)] max-w-[calc(100vw-1.5rem)]"
>
{SETTINGS_NAV_ITEMS.map(({ key, icon: Icon, fallback }) => {
const active = key === navSection;
return (
<DropdownMenuItem
key={key}
aria-current={active ? "page" : undefined}
onSelect={() => onSelectSection(key)}
className={cn(
"flex h-10 cursor-default items-center gap-2.5 px-2.5 text-[13px] leading-5 font-normal",
active && "bg-sidebar-accent text-foreground focus:bg-sidebar-accent",
)}
>
<Icon className="h-4 w-4 shrink-0" strokeWidth={2} aria-hidden />
<span className="min-w-0 flex-1 truncate">
{t(`settings.nav.${key}`, { defaultValue: fallback })}
</span>
{active ? <Check className="h-4 w-4 shrink-0" aria-hidden /> : null}
</DropdownMenuItem>
);
})}
</DropdownMenuContent>
</DropdownMenu>
<SidebarSelectionHighlight
targetRef={activeNavItemRef}
activeId={navSection}
@@ -219,3 +183,102 @@ export function SettingsSidebar({
</aside>
);
}
// Mount only on narrow screens so switching to desktop also releases the sheet's focus/pointer lock.
function MobileSettingsNavigation({
navSection,
activeLabel,
restartLabel,
onSelectSection,
onBackToChat,
hostChromeInset,
onRestart,
isRestarting,
restartPending,
}: SettingsSidebarProps & {
navSection: SettingsSectionKey;
activeLabel: string;
restartLabel: string;
}) {
const { t } = useTranslation();
const [open, setOpen] = useState(false);
const restartAction = onRestart ? (
<Button type="button" variant="ghost" disabled={isRestarting}
onClick={() => { setOpen(false); onRestart(); }}
className={cn("h-11 shrink-0 gap-2 px-3 text-sm font-normal",
restartPending && !isRestarting
? "text-orange-600 hover:text-orange-700 dark:text-orange-400 dark:hover:text-orange-300"
: "text-muted-foreground hover:text-foreground")}
>
{isRestarting ? <Loader2 className="h-4 w-4 animate-spin" aria-hidden />
: <RotateCcw className="h-4 w-4" aria-hidden />}
{restartLabel}
</Button>
) : null;
return (
<aside className={cn("shrink-0 border-b border-border/50 bg-settings-canvas",
hostChromeInset ? "pt-10" : "pt-[env(safe-area-inset-top)]")}
>
<div className="grid h-14 grid-cols-[2.75rem_minmax(0,1fr)_2.75rem] items-center px-3">
<Button type="button" variant="ghost" onClick={onBackToChat}
aria-label={t("settings.backToChat")}
className="h-11 w-11 rounded-full p-0 text-sidebar-content"
>
<ChevronLeft className="h-5 w-5" aria-hidden />
</Button>
<Sheet open={open} onOpenChange={setOpen}>
<SheetTrigger asChild>
<Button type="button" variant="ghost"
aria-label={`${t("settings.sidebar.title")}: ${activeLabel}`}
className="h-11 min-w-0 max-w-full justify-self-center gap-1.5 px-2 text-base font-medium"
>
<span className="truncate">{activeLabel}</span>
<ChevronDown className="h-3.5 w-3.5 shrink-0 text-muted-foreground" aria-hidden />
</Button>
</SheetTrigger>
<SheetContent side="bottom" showCloseButton={false} aria-describedby={undefined}
className="mx-auto max-h-[85dvh] max-w-md gap-0 rounded-t-3xl px-3 pt-2 pb-[max(0.75rem,env(safe-area-inset-bottom))] data-[state=open]:duration-300 data-[state=closed]:duration-200"
>
<div className="flex shrink-0 items-center justify-between gap-3 px-3 pb-2">
<SheetTitle className="text-base font-medium">{t("settings.sidebar.title")}</SheetTitle>
<Button type="button" variant="ghost" onClick={() => setOpen(false)}
className="-mr-2 h-11 px-2 text-sm font-normal text-muted-foreground"
>{t("common.close")}</Button>
</div>
<div className="min-h-0 overflow-y-auto overscroll-contain">
<nav aria-label={t("settings.sidebar.ariaLabel")} className="space-y-0.5">
{SETTINGS_NAV_ITEMS.map(({ key, icon: Icon, fallback }) => {
const active = key === navSection;
return (
<Button key={key} type="button" variant="ghost"
aria-current={active ? "page" : undefined}
onClick={() => { setOpen(false); onSelectSection(key); }}
className={cn("h-11 w-full justify-start gap-3 rounded-xl px-3 text-sm font-normal",
active ? "bg-sidebar-accent text-foreground" : "text-sidebar-content")}
>
<Icon className="h-[18px] w-[18px] shrink-0" aria-hidden />
<span className="min-w-0 flex-1 truncate text-left">
{t(`settings.nav.${key}`, { defaultValue: fallback })}
</span>
{active ? <Check className="h-4 w-4 shrink-0" aria-hidden /> : null}
</Button>
);
})}
</nav>
{restartAction ? <div className="mt-2 border-t border-border/50 pt-2">{restartAction}</div> : null}
</div>
</SheetContent>
</Sheet>
</div>
{onRestart && (restartPending || isRestarting) ? (
<div className="flex min-h-11 items-center justify-between gap-2 px-4 pb-1">
<p role="status" className="text-xs leading-5 text-muted-foreground">
{isRestarting ? restartLabel : t("settings.status.savedRestartApply")}
</p>
{restartAction}
</div>
) : null}
</aside>
);
}
@@ -1,4 +1,4 @@
import { Fragment, useMemo, useRef, type ReactNode } from "react";
import { Fragment, useCallback, useMemo, useRef, useState, type PointerEvent, type ReactNode } from "react";
import { useTranslation } from "react-i18next";
import {
@@ -7,6 +7,7 @@ import {
PopoverTrigger,
} from "@/components/ui/popover";
import { Sheet, SheetContent, SheetTitle, SheetTrigger } from "@/components/ui/sheet";
import { FloatingPortalContext } from "@/components/ui/floating-portal";
import {
Tooltip,
TooltipContent,
@@ -65,6 +66,7 @@ export function ComposerUsagePopover({
bottomSheet?: boolean;
}) {
const { t, i18n } = useTranslation();
const [open, setOpen] = useState(false);
const normalizedRounds = useMemo(() => normalizeRounds(rounds), [rounds]);
const hasContext = !!context
&& Number.isFinite(context.contextTokens)
@@ -116,6 +118,10 @@ export function ComposerUsagePopover({
const Root = bottomSheet ? Sheet : Popover;
const Trigger = bottomSheet ? SheetTrigger : PopoverTrigger;
const RoundRoot = bottomSheet ? Popover : Tooltip;
const RoundTrigger = bottomSheet ? PopoverTrigger : TooltipTrigger;
const RoundContent = bottomSheet ? PopoverContent : TooltipContent;
const RoundBar = bottomSheet ? "button" : "span";
const trigger = (
<Trigger asChild>
<button
@@ -178,7 +184,7 @@ export function ComposerUsagePopover({
);
return (
<Root>
<Root open={open} onOpenChange={setOpen}>
<TooltipProvider>
{bottomSheet ? trigger : (
<Tooltip>
@@ -194,7 +200,7 @@ export function ComposerUsagePopover({
</Tooltip>
)}
<UsagePanel bottomSheet={bottomSheet}>
<UsagePanel bottomSheet={bottomSheet} onDismiss={() => setOpen(false)}>
<div className="px-4 pb-4 pt-3.5">
{contextPercentage !== null ? (
<>
@@ -330,16 +336,18 @@ export function ComposerUsagePopover({
index === normalizedRounds.length - 1 && "opacity-100",
)}
>
<Tooltip>
<TooltipTrigger asChild>
<span
role="img"
<RoundRoot>
<RoundTrigger asChild>
<RoundBar
type={bottomSheet ? "button" : undefined}
role={bottomSheet ? undefined : "img"}
tabIndex={0}
aria-label={detailLabel}
data-testid="round-usage-bar"
className={cn(
"flex w-full max-w-7 flex-col overflow-hidden rounded-t-[3px] bg-muted",
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
bottomSheet && "data-[state=open]:ring-2 data-[state=open]:ring-foreground/20",
)}
style={{ height: `${barHeight}px` }}
>
@@ -361,12 +369,16 @@ export function ComposerUsagePopover({
) : (
<span className="block h-full w-full bg-muted-foreground/25" />
)}
</span>
</TooltipTrigger>
<TooltipContent
</RoundBar>
</RoundTrigger>
<RoundContent
side="top"
align="center"
className="max-w-72 px-3 py-2 text-[11px]"
{...(bottomSheet ? {
"aria-label": timestampLabel,
onOpenAutoFocus: (event: Event) => event.preventDefault(),
} : {})}
>
<span
className="block font-medium text-foreground"
@@ -392,8 +404,8 @@ export function ComposerUsagePopover({
{detailNote}
</span>
) : null}
</TooltipContent>
</Tooltip>
</RoundContent>
</RoundRoot>
</span>
);
})}
@@ -408,34 +420,109 @@ export function ComposerUsagePopover({
);
}
function UsagePanel({ bottomSheet, children }: { bottomSheet: boolean; children: ReactNode }) {
function UsagePanel({ bottomSheet, onDismiss, children }: {
bottomSheet: boolean;
onDismiss: () => void;
children: ReactNode;
}) {
const { t } = useTranslation();
const panelRef = useRef<HTMLDivElement>(null);
const panelRef = useRef<HTMLDivElement | null>(null);
const [panelNode, setPanelNode] = useState<HTMLDivElement | null>(null);
const sheetRef = useCallback((node: HTMLDivElement | null) => {
panelRef.current = node;
setPanelNode(node);
}, []);
const gesture = useRef<{ pointerId: number; x: number; y: number; time: number } | null>(null);
const suppressClick = useRef(false);
const [offset, setOffset] = useState(0);
const [dragging, setDragging] = useState(false);
const title = t("thread.composer.context.panelTitle", { defaultValue: "Context usage" });
const focusPanel = (event: Event) => {
event.preventDefault();
gesture.current = null;
suppressClick.current = false;
setOffset(0);
setDragging(false);
panelRef.current?.focus({ preventScroll: true });
};
const cancelDrag = (event: PointerEvent<HTMLButtonElement>) => {
if (gesture.current?.pointerId !== event.pointerId) return;
gesture.current = null;
suppressClick.current = true;
setDragging(false);
setOffset(0);
};
if (bottomSheet) {
return (
<SheetContent
ref={panelRef}
ref={sheetRef}
side="bottom"
aria-describedby={undefined}
showCloseButton={false}
onOpenAutoFocus={focusPanel}
style={{ translate: `0 ${offset}px`, ...(dragging ? { transition: "none" } : {}) }}
className={cn(
"mx-auto max-h-[60dvh] w-full max-w-md gap-0 rounded-t-3xl border-t border-border/60 p-0 shadow-xl outline-none",
"transition-[translate]",
"data-[state=open]:duration-300 data-[state=open]:ease-out data-[state=closed]:duration-200 data-[state=closed]:ease-in",
)}
closeButtonClassName="right-3 top-5 flex size-11 items-center justify-center rounded-full"
>
<div aria-hidden="true" className="mx-auto mt-2.5 h-1 w-8 shrink-0 rounded-full bg-foreground/15" />
<div className="shrink-0 px-5 pb-2 pt-5 pr-16">
<button
type="button"
aria-label={t("common.close")}
className="mx-auto flex h-11 w-24 shrink-0 touch-none select-none items-center justify-center rounded-full cursor-grab active:cursor-grabbing focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
onPointerDown={(event) => {
if (event.button !== 0 || !event.isPrimary || gesture.current) return;
event.currentTarget.setPointerCapture(event.pointerId);
gesture.current = { pointerId: event.pointerId, x: event.clientX, y: event.clientY, time: event.timeStamp };
suppressClick.current = false;
setDragging(true);
}}
onPointerMove={(event) => {
const start = gesture.current;
if (!start || start.pointerId !== event.pointerId) return;
const dy = event.clientY - start.y;
const dx = event.clientX - start.x;
if (Math.abs(dy) > 5 || Math.abs(dx) > 5) suppressClick.current = true;
setOffset(Math.max(0, dy));
}}
onPointerUp={(event) => {
const start = gesture.current;
if (!start || start.pointerId !== event.pointerId) return;
gesture.current = null;
const dy = Math.max(0, event.clientY - start.y);
const dx = Math.abs(event.clientX - start.x);
const threshold = Math.max(48, Math.min(96, (panelRef.current?.offsetHeight ?? 320) * 0.3));
const elapsed = Math.max(1, event.timeStamp - start.time);
const dismiss = dy > dx && (dy >= threshold || (dy > 20 && dy / elapsed > 0.5));
suppressClick.current ||= dy > 5 || dx > 5;
event.currentTarget.releasePointerCapture(event.pointerId);
setDragging(false);
if (dismiss) onDismiss();
else setOffset(0);
}}
onPointerCancel={cancelDrag}
onLostPointerCapture={cancelDrag}
onClick={(event) => {
// A completed drag also emits a click; don't close after a snap-back.
if (suppressClick.current && event.detail !== 0) {
event.preventDefault();
suppressClick.current = false;
return;
}
onDismiss();
}}
>
<span aria-hidden="true" className="h-1 w-8 rounded-full bg-foreground/15" />
</button>
<div className="shrink-0 px-5 pb-2">
<SheetTitle className="text-base font-medium">{title}</SheetTitle>
</div>
<div className="min-h-0 overflow-y-auto overscroll-contain px-1 pb-[max(0.75rem,env(safe-area-inset-bottom))]">
{children}
<FloatingPortalContext.Provider value={panelNode}>
{children}
</FloatingPortalContext.Provider>
</div>
</SheetContent>
);