|
1 | 1 | import { QueryClient } from '@tanstack/react-query'; |
2 | | -import { toast } from 'sonner'; |
3 | | -import z from 'zod'; |
4 | | - |
5 | | -import { objectTypeSchema } from '@elek-io/core'; |
6 | | - |
7 | | -const logableMutationMetaSchema = z.object({ |
8 | | - method: z.enum(['create', 'update', 'delete', 'clone']), |
9 | | - objectType: objectTypeSchema, |
10 | | -}); |
11 | 2 |
|
12 | 3 | /** |
13 | | - * Tanstack Query instance with agressive caching and |
14 | | - * automatic display of a toast notification whenever a mutation succeeds or fails |
15 | | - * |
16 | | - * The display of a notification depends on the availability of the meta object with |
17 | | - * method and objectType keys, which needs to be set on all mutationOptions inside |
18 | | - * the `./options.ts` file. |
19 | | - * |
20 | | - * Additionally it logs all mutations with Core to enable full E2E debugging. |
| 4 | + * Tanstack Query instance with agressive caching |
21 | 5 | */ |
22 | 6 | export const queryClient = new QueryClient({ |
23 | 7 | defaultOptions: { |
24 | 8 | queries: { |
25 | 9 | staleTime: Infinity, |
26 | 10 | }, |
27 | | - mutations: { |
28 | | - onSuccess: async (data, variables, result, context) => { |
29 | | - const logableMutationMeta = logableMutationMetaSchema.safeParse( |
30 | | - context.meta |
31 | | - ); |
32 | | - |
33 | | - if (logableMutationMeta.success) { |
34 | | - toast.success( |
35 | | - `${logableMutationMeta.data.method} ${logableMutationMeta.data.objectType}` |
36 | | - ); |
37 | | - |
38 | | - await window.ipc.core.logger.info({ |
39 | | - source: 'desktop', |
40 | | - message: `Successfully ${logableMutationMeta.data.method}ed ${logableMutationMeta.data.objectType}`, |
41 | | - meta: { |
42 | | - ...logableMutationMeta.data, |
43 | | - }, |
44 | | - }); |
45 | | - } else { |
46 | | - await window.ipc.core.logger.error({ |
47 | | - source: 'desktop', |
48 | | - message: 'Detected mutation without meta', |
49 | | - meta: { |
50 | | - data, |
51 | | - variables, |
52 | | - result, |
53 | | - context, |
54 | | - }, |
55 | | - }); |
56 | | - } |
57 | | - }, |
58 | | - onError: async (error, variables, result, context) => { |
59 | | - const logableMutationMeta = logableMutationMetaSchema.safeParse( |
60 | | - context.meta |
61 | | - ); |
62 | | - |
63 | | - if (logableMutationMeta.success) { |
64 | | - toast.error( |
65 | | - `${logableMutationMeta.data.method} ${logableMutationMeta.data.objectType}` |
66 | | - ); |
67 | | - |
68 | | - await window.ipc.core.logger.error({ |
69 | | - source: 'desktop', |
70 | | - message: `Failed to ${logableMutationMeta.data.method} ${logableMutationMeta.data.objectType}`, |
71 | | - meta: { |
72 | | - ...logableMutationMeta.data, |
73 | | - }, |
74 | | - }); |
75 | | - } else { |
76 | | - await window.ipc.core.logger.error({ |
77 | | - source: 'desktop', |
78 | | - message: 'Detected mutation without meta', |
79 | | - meta: { |
80 | | - error, |
81 | | - variables, |
82 | | - result, |
83 | | - context, |
84 | | - }, |
85 | | - }); |
86 | | - } |
87 | | - }, |
88 | | - }, |
89 | 11 | }, |
90 | 12 | }); |
0 commit comments