mirror of
https://github.com/jakejarvis/sofa.git
synced 2026-08-29 01:35:39 -04:00
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:
@@ -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,
|
||||
};
|
||||
}
|
||||
@@ -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(),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user