2025-09-20 11:35:40 +09:00
|
|
|
/**
|
|
|
|
|
* React Query Provider
|
2025-12-11 18:47:24 +09:00
|
|
|
* Simple provider setup for TanStack Query with CSP nonce support
|
2025-09-20 11:35:40 +09:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
|
|
|
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
|
|
|
|
|
import { useState } from "react";
|
2025-10-22 10:58:16 +09:00
|
|
|
import { isApiError } from "@/lib/api/runtime/client";
|
2025-09-20 11:35:40 +09:00
|
|
|
|
2025-12-11 18:47:24 +09:00
|
|
|
interface QueryProviderProps {
|
|
|
|
|
children: React.ReactNode;
|
|
|
|
|
nonce?: string;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-12 11:47:17 +09:00
|
|
|
export function QueryProvider({ children }: QueryProviderProps) {
|
2025-09-20 11:35:40 +09:00
|
|
|
const [queryClient] = useState(
|
|
|
|
|
() =>
|
|
|
|
|
new QueryClient({
|
|
|
|
|
defaultOptions: {
|
|
|
|
|
queries: {
|
|
|
|
|
staleTime: 5 * 60 * 1000, // 5 minutes
|
|
|
|
|
gcTime: 10 * 60 * 1000, // 10 minutes
|
2025-10-29 13:29:28 +09:00
|
|
|
refetchOnWindowFocus: false, // Prevent excessive refetches in development
|
|
|
|
|
refetchOnMount: true, // Only refetch if data is stale (>5 min old)
|
|
|
|
|
refetchOnReconnect: true, // Only refetch on reconnect if stale
|
2025-09-24 18:00:49 +09:00
|
|
|
retry: (failureCount, error: unknown) => {
|
|
|
|
|
if (isApiError(error)) {
|
|
|
|
|
const status = error.response?.status;
|
2025-10-29 13:29:28 +09:00
|
|
|
// Don't retry on 4xx errors (client errors)
|
2025-09-24 18:00:49 +09:00
|
|
|
if (status && status >= 400 && status < 500) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
const body = error.body as Record<string, unknown> | undefined;
|
|
|
|
|
const code = typeof body?.code === "string" ? body.code : undefined;
|
2025-10-29 13:29:28 +09:00
|
|
|
// Don't retry on auth errors or rate limits
|
2025-09-24 18:00:49 +09:00
|
|
|
if (code === "AUTHENTICATION_REQUIRED" || code === "FORBIDDEN") {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-09-20 11:35:40 +09:00
|
|
|
}
|
|
|
|
|
return failureCount < 3;
|
|
|
|
|
},
|
2025-10-29 13:29:28 +09:00
|
|
|
retryDelay: attemptIndex => Math.min(1000 * 2 ** attemptIndex, 30000), // Exponential backoff
|
2025-09-20 11:35:40 +09:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<QueryClientProvider client={queryClient}>
|
|
|
|
|
{children}
|
2025-12-11 18:51:13 +09:00
|
|
|
{process.env.NODE_ENV === "development" && <ReactQueryDevtools />}
|
2025-09-20 11:35:40 +09:00
|
|
|
</QueryClientProvider>
|
|
|
|
|
);
|
|
|
|
|
}
|