|
|
|
@ -1,9 +1,55 @@ |
|
|
|
"use client"; |
|
|
|
|
|
|
|
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; |
|
|
|
import { type ReactNode, useState } from "react"; |
|
|
|
import { QueryClient, QueryClientProvider, useQueryClient } from "@tanstack/react-query"; |
|
|
|
import { type ReactNode, useState, useEffect } from "react"; |
|
|
|
import { ViewPaddingsProvider } from "@/components/utils/view-paddings-provider"; |
|
|
|
|
|
|
|
function AppFocusReloader({ children }: { children: ReactNode }) { |
|
|
|
const queryClient = useQueryClient(); |
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
if (typeof window !== "undefined") { |
|
|
|
(window as any).__queryClient = queryClient; |
|
|
|
} |
|
|
|
|
|
|
|
const handleReload = () => { |
|
|
|
// Invalidate all active queries so fresh data is reloaded from backend
|
|
|
|
queryClient.invalidateQueries(); |
|
|
|
}; |
|
|
|
|
|
|
|
const handleVisibilityChange = () => { |
|
|
|
if (document.visibilityState === "visible") { |
|
|
|
handleReload(); |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
const handlePageShow = () => { |
|
|
|
handleReload(); |
|
|
|
}; |
|
|
|
|
|
|
|
window.addEventListener("focus", handleReload); |
|
|
|
window.addEventListener("pageshow", handlePageShow); |
|
|
|
document.addEventListener("visibilitychange", handleVisibilityChange); |
|
|
|
|
|
|
|
// Listen for Flutter response events if available
|
|
|
|
const win = window as any; |
|
|
|
const unbindFlutter = typeof win.addFlutterResponseListener === "function" |
|
|
|
? win.addFlutterResponseListener(() => handleReload()) |
|
|
|
: null; |
|
|
|
|
|
|
|
return () => { |
|
|
|
window.removeEventListener("focus", handleReload); |
|
|
|
window.removeEventListener("pageshow", handlePageShow); |
|
|
|
document.removeEventListener("visibilitychange", handleVisibilityChange); |
|
|
|
if (typeof unbindFlutter === "function") { |
|
|
|
unbindFlutter(); |
|
|
|
} |
|
|
|
}; |
|
|
|
}, [queryClient]); |
|
|
|
|
|
|
|
return <>{children}</>; |
|
|
|
} |
|
|
|
|
|
|
|
type ProvidersProps = { |
|
|
|
children: ReactNode; |
|
|
|
}; |
|
|
|
@ -14,10 +60,12 @@ export default function Providers({ children }: ProvidersProps) { |
|
|
|
new QueryClient({ |
|
|
|
defaultOptions: { |
|
|
|
queries: { |
|
|
|
refetchOnWindowFocus: false, |
|
|
|
refetchOnWindowFocus: "always", |
|
|
|
refetchOnMount: "always", |
|
|
|
refetchOnReconnect: "always", |
|
|
|
retry: 1, |
|
|
|
staleTime: 5 * 60 * 1000, // 5 minutes
|
|
|
|
gcTime: 10 * 60 * 1000, // 10 minutes (formerly cacheTime)
|
|
|
|
staleTime: 0, // Always consider queries stale to ensure fresh data from backend on every page load
|
|
|
|
gcTime: 10 * 60 * 1000, // 10 minutes
|
|
|
|
}, |
|
|
|
}, |
|
|
|
}), |
|
|
|
@ -25,8 +73,11 @@ export default function Providers({ children }: ProvidersProps) { |
|
|
|
|
|
|
|
return ( |
|
|
|
<QueryClientProvider client={queryClient}> |
|
|
|
<AppFocusReloader> |
|
|
|
<ViewPaddingsProvider /> |
|
|
|
{children} |
|
|
|
</AppFocusReloader> |
|
|
|
</QueryClientProvider> |
|
|
|
); |
|
|
|
} |
|
|
|
|