perf(webui): virtualize the log viewer row list (#185)
The logs page rendered every filtered row, so a full 2000-line view mounted ~10k DOM nodes and cost ~66ms of layout per commit. Switch to a virtualized list (@tanstack/react-virtual) that mounts only the visible window plus a small overscan — a few dozen nodes regardless of how many lines the view holds. Rows are dynamically measured (measureElement) so wrap mode's variable-height lines need no fixed row-height assumption; the size cache is keyed by row id so the maxLines front-drop doesn't mis-attribute heights across the index shift. The column header moved just outside the scroll viewport so the virtual list is the sole scroll content (no sticky-offset math). Auto-scroll now follows the tail via scrollToIndex with a next-frame re-pin (freshly-appended rows carry the size estimate until measured, which otherwise left wrap-mode tails short), and re-runs on filter/level/wrap/maxLines changes, not just new logs. ScrollArea gained an optional viewportRef so the virtualizer can drive off the real Radix scroll element. The per-row entrance fade is dropped (rows mount/unmount on scroll); divide-y became a per-row border.
This commit is contained in:
4 files changed
+126
-30
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import { useCallback, useEffect, useMemo, useRef, useState, type ReactNode } from 'react';
|
||||
import { useVirtualizer } from '@tanstack/react-virtual';
|
||||
import { motion, AnimatePresence } from 'motion/react';
|
||||
import { ArrowDownToLine, Download, Filter, Highlighter, Inbox, Pause, Plus, RefreshCw, Search, SearchX, SlidersHorizontal, Trash2, WrapText, X } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
@@ -91,7 +92,7 @@ function ToolButton({ label, onClick, children, active, danger, disabled }: Tool
|
||||
|
||||
export function LogsPage() {
|
||||
const api = useApi();
|
||||
const { formatClock, appearance } = useTheme();
|
||||
const { formatClock } = useTheme();
|
||||
const { pages, setPages } = useLayout();
|
||||
const prefs = pages.logs;
|
||||
|
||||
@@ -104,7 +105,7 @@ export function LogsPage() {
|
||||
const [showOptions, setShowOptions] = useState(false);
|
||||
const [newKeyword, setNewKeyword] = useState('');
|
||||
const [newColor, setNewColor] = useState(HIGHLIGHT_COLORS[0].id);
|
||||
const endRef = useRef<HTMLDivElement | null>(null);
|
||||
const scrollRef = useRef<HTMLDivElement | null>(null);
|
||||
|
||||
// maxLines is read through a ref so changing it doesn't re-subscribe the SSE.
|
||||
const maxLines = prefs.maxLines;
|
||||
@@ -179,11 +180,6 @@ export function LogsPage() {
|
||||
};
|
||||
}, [api]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!prefs.autoScroll) return;
|
||||
endRef.current?.scrollIntoView({ block: 'end' });
|
||||
}, [logs, prefs.autoScroll]);
|
||||
|
||||
const filtered = useMemo(() => {
|
||||
const f = filter.trim().toLowerCase();
|
||||
const capped = logs.slice(-maxLines);
|
||||
@@ -199,6 +195,40 @@ export function LogsPage() {
|
||||
});
|
||||
}, [logs, filter, enabled, maxLines]);
|
||||
|
||||
// Virtualize the log rows: only the visible window (+overscan) is mounted, so
|
||||
// the DOM stays at a few dozen nodes regardless of how many lines the view
|
||||
// holds. Rows are dynamically measured (measureElement) so wrap mode's
|
||||
// variable-height lines are handled without a fixed row-height assumption.
|
||||
const rowVirtualizer = useVirtualizer({
|
||||
count: filtered.length,
|
||||
getScrollElement: () => scrollRef.current,
|
||||
estimateSize: () => 28,
|
||||
overscan: 12,
|
||||
// Key the size cache by row id, not index: the list is front-dropped at the
|
||||
// maxLines cap, so every surviving row's index shifts down each flush. Keying
|
||||
// by id keeps each measured height with its content (and matches the React
|
||||
// key), avoiding a one-frame height mis-attribution on off-screen rows.
|
||||
getItemKey: (index) => filtered[index]?.id ?? index,
|
||||
});
|
||||
|
||||
// Toggling wrap changes every row's height — force a clean re-measure so the
|
||||
// cached sizes don't leave gaps/overlaps.
|
||||
useEffect(() => { rowVirtualizer.measure(); }, [prefs.wrap, rowVirtualizer]);
|
||||
|
||||
// Follow the tail while auto-scroll is on. Re-run on anything that changes the
|
||||
// visible set or row heights (new logs, filter/level/maxLines → filtered.length,
|
||||
// wrap → heights), not just new logs. Freshly-appended rows still carry the
|
||||
// estimate on this pass, so a second pin on the next frame (after ResizeObserver
|
||||
// measures them) reaches the true bottom in wrap mode.
|
||||
useEffect(() => {
|
||||
if (!prefs.autoScroll || filtered.length === 0) return;
|
||||
const last = filtered.length - 1;
|
||||
rowVirtualizer.scrollToIndex(last, { align: 'end' });
|
||||
const raf = requestAnimationFrame(() => rowVirtualizer.scrollToIndex(last, { align: 'end' }));
|
||||
return () => cancelAnimationFrame(raf);
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [logs, filtered.length, prefs.autoScroll, prefs.wrap]);
|
||||
|
||||
// Any hand edit to the bundled prefs drops the active preset to 'custom'.
|
||||
const toggleLevel = (lv: LogLevel) => {
|
||||
const next = new Set(enabled);
|
||||
@@ -497,17 +527,18 @@ export function LogsPage() {
|
||||
{/* ── 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">
|
||||
{/* Column header lives OUTSIDE the scroll viewport so the virtualizer's
|
||||
scroll element contains only the rows (no sticky-offset math). */}
|
||||
{filtered.length > 0 && (
|
||||
<div className="hidden items-center gap-3 border-b border-border/60 bg-card/60 px-3 py-2 font-mono text-[10px] font-medium uppercase tracking-wider text-muted-foreground/70 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>
|
||||
)}
|
||||
<ScrollArea viewportRef={scrollRef} 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 ? (
|
||||
@@ -524,17 +555,21 @@ export function LogsPage() {
|
||||
)}
|
||||
</div>
|
||||
) : (
|
||||
<div className="divide-y divide-border/30">
|
||||
{filtered.map((log) => {
|
||||
<div className="relative w-full" style={{ height: rowVirtualizer.getTotalSize() }}>
|
||||
{rowVirtualizer.getVirtualItems().map((vRow) => {
|
||||
const log = filtered[vRow.index];
|
||||
if (!log) return null; // guard a transient count/measurement skew
|
||||
const hl = matchHighlight(log.message, prefs.highlightRules);
|
||||
return (
|
||||
<motion.div
|
||||
<div
|
||||
key={log.id}
|
||||
initial={appearance.disableMotion ? false : { opacity: 0 }}
|
||||
animate={{ opacity: 1 }}
|
||||
transition={appearance.disableMotion ? { duration: 0 } : { 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}
|
||||
data-index={vRow.index}
|
||||
ref={rowVirtualizer.measureElement}
|
||||
className="absolute left-0 top-0 flex w-full flex-col gap-0.5 border-b border-border/30 px-3 py-1.5 transition-colors hover:bg-accent/40 sm:flex-row sm:items-start sm:gap-3 sm:py-1"
|
||||
style={{
|
||||
transform: `translateY(${vRow.start}px)`,
|
||||
...(hl ? { boxShadow: `inset 3px 0 0 ${hl}`, backgroundColor: `color-mix(in oklab, ${hl} 8%, transparent)` } : {}),
|
||||
}}
|
||||
>
|
||||
<div className="flex items-center gap-3 sm:contents">
|
||||
<span className="w-[104px] shrink-0 tabular-nums text-muted-foreground">{formatClock(log.time)}</span>
|
||||
@@ -553,12 +588,11 @@ export function LogsPage() {
|
||||
)}
|
||||
{log.message}
|
||||
</span>
|
||||
</motion.div>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
<div ref={endRef} />
|
||||
</div>
|
||||
</ScrollArea>
|
||||
</div>
|
||||
|
||||
@@ -4,16 +4,20 @@ import { cn } from '@/lib/utils';
|
||||
|
||||
interface ScrollAreaProps extends React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.Root> {
|
||||
viewportClassName?: string;
|
||||
/** Ref to the scrollable viewport element — needed to drive a virtualizer
|
||||
* (TanStack Virtual's getScrollElement) off the actual scroll container. */
|
||||
viewportRef?: React.Ref<HTMLDivElement>;
|
||||
}
|
||||
|
||||
const ScrollArea = React.forwardRef<React.ElementRef<typeof ScrollAreaPrimitive.Root>, ScrollAreaProps>(
|
||||
({ className, children, viewportClassName, ...props }, ref) => (
|
||||
({ className, children, viewportClassName, viewportRef, ...props }, ref) => (
|
||||
<ScrollAreaPrimitive.Root
|
||||
ref={ref}
|
||||
className={cn('relative overflow-hidden', className)}
|
||||
{...props}
|
||||
>
|
||||
<ScrollAreaPrimitive.Viewport
|
||||
ref={viewportRef}
|
||||
className={cn('size-full rounded-[inherit] [&>div]:!block', viewportClassName)}
|
||||
>
|
||||
{children}
|
||||
|
||||
Reference in new issue
Block a user