feat(webui): redesign logs page with Apple-style UX

Rework the run-log viewer around progressive disclosure and a calmer,
content-first hierarchy, reusing existing UI tokens (no new deps):

- Toolbar: live status dot (red when disconnected) replaces the badge;
  real search field with inline icon + clear; icon-only actions with
  tooltips, grouped (toggles in a track · divider · data actions).
- One always-visible segmented level filter (iOS-style raised pill +
  colored dot) instead of strikethrough pills.
- Collapse the server-level switch + highlight-rule editor into a single
  animated "选项" panel; server level also surfaced in the subtitle. The
  control area drops from three rows to one.
- Log stream: subtle sticky column header (时间/级别/模块/消息) aligned to
  rows via a sm:contents trick, dot-style level indicators, hairline row
  separators, and distinct empty / no-match states.

a11y: radiogroup on color swatches, aria-pressed on level toggles,
unique remove-button labels, focus rings on all controls.
This commit is contained in:
DrSmoothl committed 2026-06-13 23:00:22 +08:00
1 parent a61e0a253d
commit 5b253ea1d5
1 file changed
+309 -150
+309 -150
View File
@@ -1,11 +1,11 @@
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { useCallback, useEffect, useMemo, useRef, useState, type ReactNode } from 'react';
import { motion } from 'motion/react'; import { motion, AnimatePresence } from 'motion/react';
import { ArrowDownToLine, Highlighter, Pause, Plus, RefreshCw, Trash2, WrapText, X } from 'lucide-react'; import { ArrowDownToLine, Filter, Highlighter, Inbox, Pause, Plus, RefreshCw, Search, SearchX, SlidersHorizontal, Trash2, WrapText, X } from 'lucide-react';
import { Button } from '@/components/ui/button'; import { Button } from '@/components/ui/button';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Badge } from '@/components/ui/badge';
import { Input } from '@/components/ui/input'; import { Input } from '@/components/ui/input';
import { ScrollArea } from '@/components/ui/scroll-area'; import { ScrollArea } from '@/components/ui/scroll-area';
import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip';
import { ConfirmDialog } from '@/components/confirm-dialog'; import { ConfirmDialog } from '@/components/confirm-dialog';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
import type { LogEntry, LogLevel, UiHighlightRule } from '@/types'; import type { LogEntry, LogLevel, UiHighlightRule } from '@/types';
@@ -43,6 +43,39 @@ function matchHighlight(message: string, rules: UiHighlightRule[]): string | nul
return null; return null;
} }
// Apple-style toolbar icon button: icon-only, tooltip-labelled, calm hover.
interface ToolButtonProps {
label: string;
onClick: () => void;
children: ReactNode;
active?: boolean;
danger?: boolean;
disabled?: boolean;
}
function ToolButton({ label, onClick, children, active, danger, disabled }: ToolButtonProps) {
return (
<Tooltip>
<TooltipTrigger asChild>
<button
type="button"
onClick={onClick}
disabled={disabled}
aria-label={label}
aria-pressed={active}
className={cn(
'inline-flex size-8 items-center justify-center rounded-lg transition-colors cursor-pointer outline-none focus-visible:ring-[3px] focus-visible:ring-ring/40 disabled:cursor-not-allowed disabled:opacity-50 [&_svg]:size-4',
active ? 'bg-primary/10 text-primary' : 'text-muted-foreground hover:bg-accent hover:text-foreground',
danger && !active && 'hover:bg-destructive/10 hover:text-destructive',
)}
>
{children}
</button>
</TooltipTrigger>
<TooltipContent>{label}</TooltipContent>
</Tooltip>
);
}
export function LogsPage() { export function LogsPage() {
const api = useApi(); const api = useApi();
const { formatClock } = useTheme(); const { formatClock } = useTheme();
@@ -55,7 +88,7 @@ export function LogsPage() {
const [confirmClear, setConfirmClear] = useState(false); const [confirmClear, setConfirmClear] = useState(false);
const [serverLevel, setServerLevel] = useState<LogLevel | null>(null); const [serverLevel, setServerLevel] = useState<LogLevel | null>(null);
const [levelBusy, setLevelBusy] = useState(false); const [levelBusy, setLevelBusy] = useState(false);
const [showHighlights, setShowHighlights] = useState(false); const [showOptions, setShowOptions] = useState(false);
const [newKeyword, setNewKeyword] = useState(''); const [newKeyword, setNewKeyword] = useState('');
const [newColor, setNewColor] = useState(HIGHLIGHT_COLORS[0].id); const [newColor, setNewColor] = useState(HIGHLIGHT_COLORS[0].id);
const endRef = useRef<HTMLDivElement | null>(null); const endRef = useRef<HTMLDivElement | null>(null);
@@ -137,6 +170,11 @@ export function LogsPage() {
setPages({ logs: { ...prefs, visibleLevels: LEVELS.filter((l) => next.has(l)) } }); setPages({ logs: { ...prefs, visibleLevels: LEVELS.filter((l) => next.has(l)) } });
}; };
const clearFilters = useCallback(() => {
setFilter('');
setPages({ logs: { ...prefs, visibleLevels: [...LEVELS] } });
}, [prefs, setPages]);
const addHighlight = () => { const addHighlight = () => {
const kw = newKeyword.trim(); const kw = newKeyword.trim();
if (!kw) return; if (!kw) return;
@@ -147,161 +185,282 @@ export function LogsPage() {
setPages({ logs: { ...prefs, highlightRules: prefs.highlightRules.filter((_, i) => i !== idx) } }); setPages({ logs: { ...prefs, highlightRules: prefs.highlightRules.filter((_, i) => i !== idx) } });
}; };
const live = streamStatus === '实时';
const connecting = streamStatus === '连接中' || streamStatus === '重连中';
const levelsFiltered = enabled.size < LEVELS.length;
return ( return (
<Card className="flex h-[calc(100vh-7rem)] min-h-[480px] flex-col"> <Card className="flex h-[calc(100vh-7rem)] min-h-[480px] flex-col overflow-hidden">
<CardHeader className="flex flex-col gap-3 space-y-0 sm:flex-row sm:items-center sm:justify-between"> {/* ── Toolbar ─────────────────────────────────────────────── */}
<div> <CardHeader className="gap-3 sm:flex-row sm:items-start sm:justify-between">
<CardTitle className="flex items-center gap-2"> <div className="flex min-w-0 flex-col gap-1">
运行日志 <div className="flex items-center gap-2.5">
<Badge variant={streamStatus === '实时' ? 'success' : 'secondary'}>{streamStatus}</Badge> <CardTitle>运行日志</CardTitle>
</CardTitle> <span className="inline-flex items-center gap-1.5 text-xs font-medium">
<CardDescription>最近 {filtered.length} / {logs.length} 条 · 通过 SSE 推送</CardDescription> <span
className={cn(
'size-1.5 rounded-full',
live ? 'bg-success animate-pulse' : connecting ? 'bg-warning' : 'bg-destructive',
)}
/>
<span className={cn(live ? 'text-success' : connecting ? 'text-warning' : 'text-destructive')}>
{streamStatus}
</span>
</span>
</div>
<CardDescription className="text-xs">
{filtered.length}/{logs.length} 条 · SSE 实时推送
{serverLevel && (
<>
{' · 服务端 '}
<span className={cn('font-medium', levelClass[serverLevel])}>{serverLevel.toUpperCase()}</span>
</>
)}
</CardDescription>
</div> </div>
<div className="flex flex-wrap items-center gap-2">
<Input <div className="flex flex-wrap items-center gap-1.5 sm:justify-end">
placeholder="搜索消息 / 模块 / 级别" <div className="relative">
value={filter} <Search className="pointer-events-none absolute left-2.5 top-1/2 size-3.5 -translate-y-1/2 text-muted-foreground" />
onChange={(e) => setFilter(e.target.value)} <Input
className="h-8 w-48" placeholder="搜索消息 / 模块 / 级别"
/> value={filter}
<Button variant="outline" size="sm" onClick={() => setPages({ logs: { ...prefs, autoScroll: !prefs.autoScroll } })} title="自动滚动到底部"> onChange={(e) => setFilter(e.target.value)}
{prefs.autoScroll ? <ArrowDownToLine className="size-3.5 text-primary" /> : <Pause className="size-3.5" />} className="h-8 w-44 rounded-lg pl-8 pr-7 sm:w-56"
{prefs.autoScroll ? '自动滚动' : '已暂停'} />
</Button> {filter && (
<Button variant="outline" size="sm" onClick={() => setPages({ logs: { ...prefs, wrap: !prefs.wrap } })} title="自动换行"> <button
<WrapText className={cn('size-3.5', prefs.wrap && 'text-primary')} /> 换行 type="button"
</Button> onClick={() => setFilter('')}
<Button variant="outline" size="sm" onClick={() => setShowHighlights((v) => !v)} title="高亮规则"> aria-label="清除搜索"
<Highlighter className={cn('size-3.5', prefs.highlightRules.length > 0 && 'text-primary')} /> 高亮 className="absolute right-1.5 top-1/2 -translate-y-1/2 rounded-full p-0.5 text-muted-foreground outline-none transition-colors hover:text-foreground focus-visible:ring-[3px] focus-visible:ring-ring/40 cursor-pointer"
</Button> >
<Button variant="outline" size="sm" onClick={() => void loadLogs()}> <X className="size-3.5" />
<RefreshCw className="size-3.5" /> 刷新 </button>
</Button> )}
<Button variant="outline" size="sm" onClick={() => setConfirmClear(true)} className="text-muted-foreground hover:text-destructive"> </div>
<Trash2 className="size-3.5" /> 清空视图
</Button> <div className="flex items-center gap-0.5 rounded-lg bg-muted/40 p-0.5">
<ToolButton
label={prefs.autoScroll ? '自动滚动已开启' : '自动滚动已暂停'}
active={prefs.autoScroll}
onClick={() => setPages({ logs: { ...prefs, autoScroll: !prefs.autoScroll } })}
>
{prefs.autoScroll ? <ArrowDownToLine /> : <Pause />}
</ToolButton>
<ToolButton
label="自动换行"
active={prefs.wrap}
onClick={() => setPages({ logs: { ...prefs, wrap: !prefs.wrap } })}
>
<WrapText />
</ToolButton>
<ToolButton label="显示选项" active={showOptions} onClick={() => setShowOptions((v) => !v)}>
<SlidersHorizontal />
</ToolButton>
</div>
<span className="mx-0.5 h-5 w-px bg-border/70" />
<ToolButton label="刷新" onClick={() => void loadLogs()}>
<RefreshCw />
</ToolButton>
<ToolButton label="清空视图" danger onClick={() => setConfirmClear(true)}>
<Trash2 />
</ToolButton>
</div> </div>
</CardHeader> </CardHeader>
{showHighlights && ( {/* ── Level filter (segmented, always visible) ────────────── */}
<div className="mx-5 mb-2 flex flex-col gap-2 rounded-lg border bg-muted/20 p-3"> <div className="flex items-center gap-2 px-5 pb-3">
<div className="flex flex-wrap items-center gap-2"> <Filter className={cn('size-3.5 shrink-0', levelsFiltered ? 'text-primary' : 'text-muted-foreground/60')} />
<Input <div className="flex flex-wrap items-center gap-1 rounded-lg bg-muted/50 p-1">
placeholder="高亮关键词" {LEVELS.map((lv) => {
value={newKeyword} const active = enabled.has(lv);
maxLength={50} return (
onChange={(e) => setNewKeyword(e.target.value)} <button
onKeyDown={(e) => { if (e.key === 'Enter') addHighlight(); }} key={lv}
className="h-8 w-44" type="button"
/> onClick={() => toggleLevel(lv)}
<div className="flex items-center gap-1"> aria-pressed={active}
{HIGHLIGHT_COLORS.map((c) => ( className={cn(
<button 'inline-flex items-center gap-1.5 rounded-md px-2.5 py-1 text-[11px] font-medium transition-all cursor-pointer',
key={c.id} active
type="button" ? cn('bg-card shadow-sm ring-1 ring-border/60', levelClass[lv])
onClick={() => setNewColor(c.id)} : 'text-muted-foreground/55 hover:text-foreground',
title={c.label} )}
aria-label={c.label} >
className={cn('size-6 rounded-full border transition-transform hover:scale-110 cursor-pointer', newColor === c.id ? 'ring-2 ring-offset-1 ring-foreground/40' : 'border-border')} <span className={cn('size-1.5 rounded-full', active ? 'bg-current' : 'bg-muted-foreground/30')} />
style={{ backgroundColor: c.swatch }} {lv.toUpperCase()}
/> </button>
))} );
</div> })}
<Button variant="outline" size="sm" onClick={addHighlight} disabled={!newKeyword.trim()}>
<Plus className="size-3.5" /> 添加
</Button>
</div>
{prefs.highlightRules.length > 0 && (
<div className="flex flex-wrap gap-1.5">
{prefs.highlightRules.map((r, i) => (
<span key={i} className="inline-flex items-center gap-1 rounded-full border px-2 py-0.5 text-[11px]" style={{ borderColor: colorSwatch(r.color) }}>
<span className="size-2 rounded-full" style={{ backgroundColor: colorSwatch(r.color) }} />
{r.keyword}
<button type="button" onClick={() => removeHighlight(i)} className="text-muted-foreground hover:text-destructive cursor-pointer" aria-label="移除">
<X className="size-3" />
</button>
</span>
))}
</div>
)}
</div> </div>
)}
<div className="flex flex-wrap items-center gap-1 px-5 pb-2">
<span className="mr-2 text-[11px] text-muted-foreground/80">视图过滤</span>
{LEVELS.map((lv) => {
const active = enabled.has(lv);
return (
<button
key={lv}
type="button"
onClick={() => toggleLevel(lv)}
className={cn(
'rounded-full border px-2.5 py-0.5 text-[11px] font-medium transition-colors cursor-pointer',
active ? cn('border-transparent', levelClass[lv], 'bg-muted') : 'border-border text-muted-foreground/60 line-through',
)}
>
{lv.toUpperCase()}
</button>
);
})}
</div> </div>
<div className="flex flex-wrap items-center gap-1 px-5 pb-3"> {/* ── Options panel (progressive disclosure) ──────────────── */}
<span className="mr-2 text-[11px] text-muted-foreground/80" title="文件落盘始终为 debug,此处仅控制控制台 / 实时流的级别"> <AnimatePresence initial={false}>
服务端级别 {showOptions && (
</span> <motion.div
{LEVELS.map((lv) => { key="options"
const active = serverLevel === lv; initial={{ height: 0, opacity: 0 }}
return ( animate={{ height: 'auto', opacity: 1 }}
<button exit={{ height: 0, opacity: 0 }}
key={lv} transition={{ duration: 0.2, ease: [0.16, 1, 0.3, 1] }}
type="button" className="overflow-hidden"
onClick={() => void changeServerLevel(lv)} >
disabled={levelBusy || serverLevel === null} <div className="mx-5 mb-3 flex flex-col gap-4 rounded-xl border bg-muted/20 p-4">
className={cn( {/* Server-side level */}
'rounded-full border px-2.5 py-0.5 text-[11px] font-medium transition-colors cursor-pointer disabled:cursor-not-allowed disabled:opacity-50', <div>
active ? cn('border-primary', levelClass[lv], 'bg-primary/10') : 'border-border text-muted-foreground/70 hover:text-foreground', <div className="mb-2 flex flex-wrap items-center gap-x-1.5 gap-y-0.5">
)} <span className="text-xs font-medium text-foreground">服务端日志级别</span>
> <span className="text-[10px] text-muted-foreground">· 仅影响控制台 / 实时流,文件始终落盘 debug</span>
{lv.toUpperCase()} </div>
</button> <div className="inline-flex flex-wrap gap-1 rounded-lg bg-muted/60 p-1">
); {LEVELS.map((lv) => {
})} const active = serverLevel === lv;
</div> return (
<button
<CardContent className="flex min-h-0 flex-1 flex-col pt-0"> key={lv}
<div className="flex min-h-0 flex-1 flex-col rounded-lg border bg-muted/30"> type="button"
<ScrollArea className="flex-1 min-h-0" viewportClassName="[&>div]:!block"> onClick={() => void changeServerLevel(lv)}
<div className="font-mono text-xs"> disabled={levelBusy || serverLevel === null}
{filtered.length === 0 ? ( aria-pressed={active}
<div className="flex h-40 items-center justify-center text-muted-foreground">暂无日志</div> className={cn(
) : ( 'inline-flex items-center gap-1.5 rounded-md px-2.5 py-1 text-[11px] font-medium transition-all cursor-pointer disabled:cursor-not-allowed disabled:opacity-50',
filtered.map((log) => { active
const hl = matchHighlight(log.message, prefs.highlightRules); ? cn('bg-card shadow-sm ring-1 ring-border/60', levelClass[lv])
return ( : 'text-muted-foreground/70 hover:text-foreground',
<motion.div
key={log.id}
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ duration: 0.12 }}
className="flex flex-col gap-0.5 px-3 py-1 leading-5 hover:bg-accent/30 sm:flex-row sm:gap-2 sm:py-0.5"
style={hl ? { boxShadow: `inset 3px 0 0 ${hl}`, backgroundColor: `color-mix(in oklab, ${hl} 8%, transparent)` } : undefined}
>
<div className="flex shrink-0 items-center gap-2">
<span className="shrink-0 text-muted-foreground tabular-nums">{formatClock(log.time)}</span>
<span className={cn('shrink-0 font-semibold sm:w-14', levelClass[log.level])}>{log.level.toUpperCase()}</span>
<span className="min-w-0 truncate text-muted-foreground sm:w-28">[{log.scope}]</span>
{log.req !== undefined && (
<span className="shrink-0 rounded bg-primary/10 px-1 text-[10px] text-primary tabular-nums" title="请求关联号">#{log.req}</span>
)} )}
</div> >
<span className={cn('min-w-0', prefs.wrap ? 'whitespace-pre-wrap break-all' : 'truncate')} title={prefs.wrap ? undefined : log.message}> <span className={cn('size-1.5 rounded-full', active ? 'bg-current' : 'bg-muted-foreground/30')} />
{log.message} {lv.toUpperCase()}
</button>
);
})}
</div>
</div>
{/* Highlight rules */}
<div>
<div className="mb-2 flex flex-wrap items-center gap-x-1.5 gap-y-0.5">
<Highlighter className="size-3.5 text-muted-foreground" />
<span className="text-xs font-medium text-foreground">高亮规则</span>
<span className="text-[10px] text-muted-foreground">· 命中关键词的行会被着色</span>
</div>
<div className="flex flex-wrap items-center gap-2">
<Input
placeholder="高亮关键词"
value={newKeyword}
maxLength={50}
onChange={(e) => setNewKeyword(e.target.value)}
onKeyDown={(e) => { if (e.key === 'Enter') addHighlight(); }}
className="h-8 w-44 rounded-lg"
/>
<div className="flex items-center gap-1" role="radiogroup" aria-label="高亮颜色">
{HIGHLIGHT_COLORS.map((c) => (
<button
key={c.id}
type="button"
role="radio"
aria-checked={newColor === c.id}
onClick={() => setNewColor(c.id)}
title={c.label}
aria-label={c.label}
className={cn(
'size-6 rounded-full border transition-transform hover:scale-110 cursor-pointer',
newColor === c.id ? 'ring-2 ring-offset-1 ring-foreground/40' : 'border-border',
)}
style={{ backgroundColor: c.swatch }}
/>
))}
</div>
<Button variant="outline" size="sm" onClick={addHighlight} disabled={!newKeyword.trim()} className="rounded-lg">
<Plus className="size-3.5" /> 添加
</Button>
</div>
{prefs.highlightRules.length > 0 && (
<div className="mt-2.5 flex flex-wrap gap-1.5">
{prefs.highlightRules.map((r, i) => (
<span key={i} className="inline-flex items-center gap-1 rounded-full border px-2 py-0.5 text-[11px]" style={{ borderColor: colorSwatch(r.color) }}>
<span className="size-2 rounded-full" style={{ backgroundColor: colorSwatch(r.color) }} />
{r.keyword}
<button type="button" onClick={() => removeHighlight(i)} className="text-muted-foreground hover:text-destructive cursor-pointer" aria-label={`移除高亮规则 ${r.keyword}`}>
<X className="size-3" />
</button>
</span> </span>
</motion.div> ))}
); </div>
}) )}
</div>
</div>
</motion.div>
)}
</AnimatePresence>
{/* ── Log stream ──────────────────────────────────────────── */}
<CardContent className="flex min-h-0 flex-1 flex-col pt-0">
<div className="flex min-h-0 flex-1 flex-col overflow-hidden rounded-xl border bg-muted/20">
<ScrollArea className="min-h-0 flex-1" viewportClassName="[&>div]:!block">
<div className="font-mono text-xs">
{filtered.length > 0 && (
<div className="sticky top-0 z-10 hidden items-center gap-3 border-b border-border/60 bg-card/75 px-3 py-2 text-[10px] font-medium uppercase tracking-wider text-muted-foreground/70 backdrop-blur-md sm:flex">
<span className="w-[104px] shrink-0">时间</span>
<span className="w-[76px] shrink-0">级别</span>
<span className="w-28 shrink-0">模块</span>
<span className="flex-1">消息</span>
</div>
)}
{filtered.length === 0 ? (
<div className="flex min-h-60 flex-col items-center justify-center gap-3 px-4 py-10 text-center font-sans text-muted-foreground">
{logs.length === 0 ? (
<>
<Inbox className="size-9 opacity-30" />
<span className="text-sm">暂无日志</span>
</>
) : (
<>
<SearchX className="size-9 opacity-30" />
<span className="text-sm">没有符合筛选条件的日志</span>
<Button variant="outline" size="sm" onClick={clearFilters} className="rounded-lg">清除筛选</Button>
</>
)}
</div>
) : (
<div className="divide-y divide-border/30">
{filtered.map((log) => {
const hl = matchHighlight(log.message, prefs.highlightRules);
return (
<motion.div
key={log.id}
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ duration: 0.12 }}
className="flex flex-col gap-0.5 px-3 py-1.5 transition-colors hover:bg-accent/40 sm:flex-row sm:items-start sm:gap-3 sm:py-1"
style={hl ? { boxShadow: `inset 3px 0 0 ${hl}`, backgroundColor: `color-mix(in oklab, ${hl} 8%, transparent)` } : undefined}
>
<div className="flex items-center gap-3 sm:contents">
<span className="w-[104px] shrink-0 tabular-nums text-muted-foreground">{formatClock(log.time)}</span>
<span className={cn('flex w-[76px] shrink-0 items-center gap-1.5 font-semibold', levelClass[log.level])}>
<span className="size-1.5 shrink-0 rounded-full bg-current" />
{log.level.toUpperCase()}
</span>
<span className="min-w-0 flex-1 truncate text-muted-foreground sm:w-28 sm:flex-none sm:shrink-0">[{log.scope}]</span>
</div>
<span
className={cn('min-w-0 flex-1 leading-5', prefs.wrap ? 'whitespace-pre-wrap break-all' : 'truncate')}
title={prefs.wrap ? undefined : log.message}
>
{log.req !== undefined && (
<span className="mr-1.5 rounded bg-primary/10 px-1 text-[10px] text-primary tabular-nums" title="请求关联号">#{log.req}</span>
)}
{log.message}
</span>
</motion.div>
);
})}
</div>
)} )}
<div ref={endRef} /> <div ref={endRef} />
</div> </div>