feat: add sidebar

This commit is contained in:
Austin Pickett 2026-04-22 23:25:17 -04:00
parent 7db2703b33
commit e5d2815b41
41 changed files with 2469 additions and 1391 deletions

View file

@ -1,13 +1,14 @@
import { useEffect, useState, useCallback, useRef } from "react";
import { FileText, RefreshCw, ChevronRight } from "lucide-react";
import { H2 } from "@nous-research/ui";
import { useEffect, useLayoutEffect, useState, useCallback, useRef } from "react";
import { FileText, RefreshCw } from "lucide-react";
import { api } from "@/lib/api";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import { Switch } from "@/components/ui/switch";
import { Label } from "@/components/ui/label";
import { FilterGroup, Segmented } from "@/components/ui/segmented";
import { useI18n } from "@/i18n";
import { usePageHeader } from "@/contexts/usePageHeader";
const FILES = ["agent", "errors", "gateway"] as const;
const LEVELS = ["ALL", "DEBUG", "INFO", "WARNING", "ERROR"] as const;
@ -34,38 +35,8 @@ const LINE_COLORS: Record<string, string> = {
debug: "text-muted-foreground/60",
};
function SidebarHeading({ children }: { children: React.ReactNode }) {
return (
<span className="text-[10px] font-semibold uppercase tracking-wider text-muted-foreground/60 px-2.5 pt-3 pb-1">
{children}
</span>
);
}
function SidebarItem<T extends string>({
label,
value,
current,
onChange,
}: SidebarItemProps<T>) {
const isActive = current === value;
return (
<button
type="button"
onClick={() => onChange(value)}
className={`group flex items-center gap-2 px-2.5 py-1 text-left text-xs transition-colors cursor-pointer ${
isActive
? "bg-primary/10 text-primary font-medium"
: "text-muted-foreground hover:text-foreground hover:bg-muted/50"
}`}
>
<span className="flex-1 truncate">{label}</span>
{isActive && (
<ChevronRight className="h-3 w-3 text-primary/50 shrink-0" />
)}
</button>
);
}
const toOptions = <T extends string>(values: readonly T[]) =>
values.map((v) => ({ value: v, label: v }));
export default function LogsPage() {
const [file, setFile] = useState<(typeof FILES)[number]>("agent");
@ -79,6 +50,7 @@ export default function LogsPage() {
const [error, setError] = useState<string | null>(null);
const scrollRef = useRef<HTMLDivElement>(null);
const { t } = useI18n();
const { setAfterTitle, setEnd } = usePageHeader();
const fetchLogs = useCallback(() => {
setLoading(true);
@ -97,6 +69,66 @@ export default function LogsPage() {
.finally(() => setLoading(false));
}, [file, lineCount, level, component]);
useLayoutEffect(() => {
setAfterTitle(
<span className="flex items-center gap-2">
{loading && (
<div className="h-4 w-4 shrink-0 animate-spin rounded-full border-2 border-primary border-t-transparent" />
)}
<Badge variant="secondary" className="text-[10px]">
{file} · {level} · {component}
</Badge>
</span>,
);
setEnd(
<div className="flex w-full min-w-0 flex-wrap items-center justify-end gap-2 sm:gap-3">
<div className="flex items-center gap-2">
<Switch
checked={autoRefresh}
onCheckedChange={setAutoRefresh}
id="logs-auto-refresh"
/>
<Label htmlFor="logs-auto-refresh" className="text-xs cursor-pointer">
{t.logs.autoRefresh}
</Label>
{autoRefresh && (
<Badge variant="success" className="text-[10px]">
<span className="mr-1 inline-block h-1.5 w-1.5 animate-pulse rounded-full bg-current" />
{t.common.live}
</Badge>
)}
</div>
<Button
type="button"
variant="outline"
size="sm"
onClick={fetchLogs}
disabled={loading}
className="h-7 text-xs"
>
<RefreshCw className="mr-1 h-3 w-3" />
{t.common.refresh}
</Button>
</div>,
);
return () => {
setAfterTitle(null);
setEnd(null);
};
}, [
autoRefresh,
component,
file,
level,
loading,
setAfterTitle,
setEnd,
t.common.live,
t.common.refresh,
t.logs.autoRefresh,
fetchLogs,
]);
useEffect(() => {
fetchLogs();
}, [fetchLogs]);
@ -109,145 +141,80 @@ export default function LogsPage() {
return (
<div className="flex flex-col gap-4">
{/* ═══════════════ Header ═══════════════ */}
<div className="flex items-center justify-between gap-4">
<div className="flex items-center gap-2">
<FileText className="h-5 w-5 text-muted-foreground" />
<H2 variant="sm">{t.logs.title}</H2>
{loading && (
<div className="h-4 w-4 animate-spin rounded-full border-2 border-primary border-t-transparent" />
)}
<Badge variant="secondary" className="text-[10px]">
{file} · {level} · {component}
</Badge>
</div>
<div className="flex items-center gap-3">
<div className="flex items-center gap-2">
<Switch checked={autoRefresh} onCheckedChange={setAutoRefresh} />
<Label className="text-xs">{t.logs.autoRefresh}</Label>
{autoRefresh && (
<Badge variant="success" className="text-[10px]">
<span className="mr-1 inline-block h-1.5 w-1.5 animate-pulse rounded-full bg-current" />
{t.common.live}
</Badge>
)}
</div>
<Button
variant="outline"
size="sm"
onClick={fetchLogs}
className="text-xs h-7"
>
<RefreshCw className="h-3 w-3 mr-1" />
{t.common.refresh}
</Button>
</div>
</div>
{/* ═══════════════ Sidebar + Content ═══════════════ */}
{/* ═══════════════ Filter toolbar ═══════════════ */}
<div
className="flex flex-col sm:flex-row gap-4"
style={{ minHeight: "calc(100vh - 180px)" }}
role="toolbar"
aria-label={t.logs.title}
className="flex flex-wrap items-center gap-x-6 gap-y-2"
>
{/* ---- Sidebar ---- */}
<div className="sm:w-44 sm:shrink-0">
<div className="sm:sticky sm:top-[72px] flex flex-col gap-0.5">
<SidebarHeading>{t.logs.file}</SidebarHeading>
{FILES.map((f) => (
<SidebarItem
key={f}
label={f}
value={f}
current={file}
onChange={setFile}
/>
))}
<FilterGroup label={t.logs.file}>
<Segmented value={file} onChange={setFile} options={toOptions(FILES)} />
</FilterGroup>
<SidebarHeading>{t.logs.level}</SidebarHeading>
{LEVELS.map((l) => (
<SidebarItem
key={l}
label={l}
value={l}
current={level}
onChange={setLevel}
/>
))}
<FilterGroup label={t.logs.level}>
<Segmented value={level} onChange={setLevel} options={toOptions(LEVELS)} />
</FilterGroup>
<SidebarHeading>{t.logs.component}</SidebarHeading>
{COMPONENTS.map((c) => (
<SidebarItem
key={c}
label={c}
value={c}
current={component}
onChange={setComponent}
/>
))}
<FilterGroup label={t.logs.component}>
<Segmented
value={component}
onChange={setComponent}
options={toOptions(COMPONENTS)}
/>
</FilterGroup>
<SidebarHeading>{t.logs.lines}</SidebarHeading>
{LINE_COUNTS.map((n) => (
<SidebarItem
key={n}
label={String(n)}
value={String(n)}
current={String(lineCount)}
onChange={(v) =>
setLineCount(Number(v) as (typeof LINE_COUNTS)[number])
}
/>
))}
</div>
</div>
{/* ---- Content ---- */}
<div className="flex-1 min-w-0">
<Card>
<CardHeader className="py-3 px-4">
<CardTitle className="text-sm flex items-center gap-2">
<FileText className="h-4 w-4" />
{file}.log
</CardTitle>
</CardHeader>
<CardContent className="p-0">
{error && (
<div className="bg-destructive/10 border-b border-destructive/20 p-3">
<p className="text-sm text-destructive">{error}</p>
</div>
)}
<div
ref={scrollRef}
className="p-4 font-mono-ui text-xs leading-5 overflow-auto max-h-[600px] min-h-[200px]"
>
{lines.length === 0 && !loading && (
<p className="text-muted-foreground text-center py-8">
{t.logs.noLogLines}
</p>
)}
{lines.map((line, i) => {
const cls = classifyLine(line);
return (
<div
key={i}
className={`${LINE_COLORS[cls]} hover:bg-secondary/20 px-1 -mx-1`}
>
{line}
</div>
);
})}
</div>
</CardContent>
</Card>
</div>
<FilterGroup label={t.logs.lines}>
<Segmented
value={String(lineCount)}
onChange={(v) =>
setLineCount(Number(v) as (typeof LINE_COUNTS)[number])
}
options={LINE_COUNTS.map((n) => ({
value: String(n),
label: String(n),
}))}
/>
</FilterGroup>
</div>
{/* ═══════════════ Log viewer ═══════════════ */}
<Card>
<CardHeader className="py-3 px-4">
<CardTitle className="text-sm flex items-center gap-2">
<FileText className="h-4 w-4" />
{file}.log
</CardTitle>
</CardHeader>
<CardContent className="p-0">
{error && (
<div className="bg-destructive/10 border-b border-destructive/20 p-3">
<p className="text-sm text-destructive">{error}</p>
</div>
)}
<div
ref={scrollRef}
className="p-4 font-mono-ui text-xs leading-5 overflow-auto min-h-[400px] max-h-[calc(100vh-220px)]"
>
{lines.length === 0 && !loading && (
<p className="text-muted-foreground text-center py-8">
{t.logs.noLogLines}
</p>
)}
{lines.map((line, i) => {
const cls = classifyLine(line);
return (
<div
key={i}
className={`${LINE_COLORS[cls]} hover:bg-secondary/20 px-1 -mx-1`}
>
{line}
</div>
);
})}
</div>
</CardContent>
</Card>
</div>
);
}
interface SidebarItemProps<T extends string> {
label: string;
value: T;
current: T;
onChange: (v: T) => void;
}