From 8d61659c6016c980e6ce89bd6f49e12468c44f43 Mon Sep 17 00:00:00 2001 From: Will Chen Date: Thu, 8 May 2025 22:32:50 -0700 Subject: [PATCH] Show error toast in centralized way for query (#117) --- src/hooks/useCurrentBranch.ts | 2 +- src/hooks/useVersions.ts | 1 + src/renderer.tsx | 33 +++++++++++++++++++++++++++++++-- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/hooks/useCurrentBranch.ts b/src/hooks/useCurrentBranch.ts index 41f230a..54a7867 100644 --- a/src/hooks/useCurrentBranch.ts +++ b/src/hooks/useCurrentBranch.ts @@ -19,7 +19,7 @@ export function useCurrentBranch(appId: number | null) { return ipcClient.getCurrentBranch(appId); }, enabled: appId !== null, - meta: { showErrorToast: false }, + meta: { showErrorToast: true }, }); return { diff --git a/src/hooks/useVersions.ts b/src/hooks/useVersions.ts index 785abb4..42f6d7c 100644 --- a/src/hooks/useVersions.ts +++ b/src/hooks/useVersions.ts @@ -29,6 +29,7 @@ export function useVersions(appId: number | null) { }, enabled: appId !== null, initialData: [], + meta: { showErrorToast: true }, }); useEffect(() => { diff --git a/src/renderer.tsx b/src/renderer.tsx index d746f3a..08e2883 100644 --- a/src/renderer.tsx +++ b/src/renderer.tsx @@ -5,12 +5,41 @@ import { RouterProvider } from "@tanstack/react-router"; import { PostHogProvider } from "posthog-js/react"; import posthog from "posthog-js"; import { getTelemetryUserId, isTelemetryOptedIn } from "./hooks/useSettings"; -import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; +import { + QueryCache, + QueryClient, + QueryClientProvider, +} from "@tanstack/react-query"; +import { showError } from "./lib/toast"; // @ts-ignore console.log("Running in mode:", import.meta.env.MODE); -const queryClient = new QueryClient(); +interface MyMeta extends Record { + showErrorToast: boolean; +} + +declare module "@tanstack/react-query" { + interface Register { + queryMeta: MyMeta; + mutationMeta: MyMeta; + } +} + +const queryClient = new QueryClient({ + defaultOptions: { + queries: { + retry: false, + }, + }, + queryCache: new QueryCache({ + onError: (error, query) => { + if (query.meta?.showErrorToast) { + showError(error); + } + }, + }), +}); const posthogClient = posthog.init( "phc_5Vxx0XT8Ug3eWROhP6mm4D6D2DgIIKT232q4AKxC2ab",