From 91eabf336912fc5cc9c42523bd5ada73ddbe00a8 Mon Sep 17 00:00:00 2001 From: Willie Date: Thu, 9 Apr 2026 15:02:46 +0800 Subject: [PATCH] feat(ui): cache audit results with React Query SSE scan results are written to query cache on completion. Navigating away and back shows cached results instead of requiring a re-scan. Cache expires after 5 minutes (staleTimes.audit). Per-kind cache keys ensure skills and agents results are independent. --- ui/src/lib/queryKeys.ts | 3 ++- ui/src/pages/AuditPage.tsx | 25 +++++++++++++++++++------ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/ui/src/lib/queryKeys.ts b/ui/src/lib/queryKeys.ts index 603e49c3f..6aeea7675 100644 --- a/ui/src/lib/queryKeys.ts +++ b/ui/src/lib/queryKeys.ts @@ -21,7 +21,7 @@ export const queryKeys = { gitBranches: ['git-branches'] as const, audit: { - all: ['audit'] as const, + all: (kind?: string) => ['audit', kind ?? 'skills'] as const, skill: (name: string) => ['audit', 'skill', name] as const, rules: ['audit', 'rules'] as const, compiled: ['audit', 'rules', 'compiled'] as const, @@ -56,6 +56,7 @@ export const staleTimes = { auditRules: 5 * 60 * 1000, // 5min backups: 2 * 60 * 1000, // 2min trash: 2 * 60 * 1000, // 2min + audit: 5 * 60 * 1000, // 5min — full audit scan, expensive auditSkill: 5 * 60 * 1000, // 5min — per-skill audit, rarely changes check: 60 * 1000, // 1min syncMatrix: 30 * 1000, // 30s — changes after filter edits diff --git a/ui/src/pages/AuditPage.tsx b/ui/src/pages/AuditPage.tsx index f1ae663bc..7cc46bde9 100644 --- a/ui/src/pages/AuditPage.tsx +++ b/ui/src/pages/AuditPage.tsx @@ -14,6 +14,7 @@ import { Puzzle, Bot, } from 'lucide-react'; +import { useQueryClient } from '@tanstack/react-query'; import { api } from '../api/client'; import type { AuditAllResponse, AuditResult, AuditFinding } from '../api/client'; import Card from '../components/Card'; @@ -29,6 +30,7 @@ import { severityBadgeVariant } from '../lib/severity'; import { BlockStamp, RiskMeter, riskColor, riskBgColor } from '../components/audit'; import ScrollToTop from '../components/ScrollToTop'; import KindBadge from '../components/KindBadge'; +import { queryKeys, staleTimes } from '../lib/queryKeys'; type SeverityFilter = 'CRITICAL' | 'HIGH' | 'MEDIUM' | 'LOW' | 'INFO'; type AuditKind = 'skills' | 'agents'; @@ -43,18 +45,28 @@ const severityFilterOptions: { value: SeverityFilter; label: string }[] = [ export default function AuditPage() { const { toast } = useToast(); + const queryClient = useQueryClient(); const [activeKind, setActiveKind] = useState('skills'); - const [dataCache, setDataCache] = useState>({ - skills: null, - agents: null, - }); - const data = dataCache[activeKind]; const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [minSeverity, setMinSeverity] = useState('MEDIUM'); const [progress, setProgress] = useState<{ scanned: number; total: number } | null>(null); const esRef = useRef(null); const startTimeRef = useRef(0); + // Bump to trigger re-render after writing to query cache + const [, setCacheTick] = useState(0); + + // Read cached audit results per kind from React Query cache. + // Cache survives page navigation; stale after staleTimes.audit (5min). + const getCached = (kind: AuditKind): AuditAllResponse | null => { + const state = queryClient.getQueryState(queryKeys.audit.all(kind)); + if (!state || state.dataUpdatedAt === 0) return null; + // Respect stale time — don't show data older than threshold + if (Date.now() - state.dataUpdatedAt > staleTimes.audit) return null; + return queryClient.getQueryData(queryKeys.audit.all(kind)) ?? null; + }; + const dataCache = { skills: getCached('skills'), agents: getCached('agents') }; + const data = dataCache[activeKind]; // Clean up EventSource on unmount useEffect(() => { @@ -113,7 +125,8 @@ export default function AuditPage() { (total) => setProgress({ scanned: 0, total }), (scanned) => setProgress((p) => p ? { ...p, scanned } : null), (res) => { - setDataCache((prev) => ({ ...prev, [activeKind]: res })); + queryClient.setQueryData(queryKeys.audit.all(activeKind), res); + setCacheTick((n) => n + 1); setLoading(false); setProgress(null); showAuditToast(res);