Add SWR for system health and command palette search fetching

Replace manual fetch/useState/useEffect patterns with SWR hooks in
system health section and command palette search, gaining automatic
caching, request deduplication, and stale-while-revalidate.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-05 12:24:23 -05:00
co-authored by Claude Opus 4.6
parent fb6c88e8f5
commit 59e9dd6775
7 changed files with 87 additions and 66 deletions
+30
View File
@@ -0,0 +1,30 @@
import useSWR from "swr";
import { fetcher } from "@/lib/swr/fetcher";
interface SearchResponse {
results: {
tmdbId: number;
type: "movie" | "tv";
title: string;
posterPath: string | null;
releaseDate: string | null;
voteAverage: number;
}[];
}
export function useSearch(debouncedQuery: string) {
const trimmed = debouncedQuery.trim();
const { data, isLoading } = useSWR<SearchResponse>(
trimmed ? `/api/search?query=${encodeURIComponent(trimmed)}` : null,
fetcher,
{
revalidateOnFocus: false,
dedupingInterval: 2_000,
},
);
return {
results: data?.results?.slice(0, 8) ?? [],
isLoading,
};
}
+19
View File
@@ -0,0 +1,19 @@
import useSWR from "swr";
import type { SystemHealthData } from "@/lib/services/system-health";
import { fetcher } from "@/lib/swr/fetcher";
export function useSystemHealth() {
const { data, error, isLoading, isValidating, mutate } =
useSWR<SystemHealthData>("/api/admin/system-health", fetcher, {
revalidateOnFocus: true,
dedupingInterval: 10_000,
});
return {
data: data ?? null,
error,
isLoading,
isValidating,
refresh: () => mutate(),
};
}