Skip to content

Commit d18df34

Browse files
Refactored queries to use customMutationOptions wrapper function for mutations. Also put all queryOptions inside grouped files instead of a single one. Fixed imports.
1 parent f15f56b commit d18df34

29 files changed

Lines changed: 973 additions & 893 deletions

src/renderer/components/asset-diff.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useMemo } from 'react';
22

3+
import { AssetTeaser } from '@renderer/components/asset-teaser';
34
import {
45
DiffContainer,
56
DiffContainerSkeleton,
@@ -9,8 +10,6 @@ import { queryOptions } from '@renderer/queries';
910

1011
import { type GitCommit, type Project } from '@elek-io/core';
1112

12-
import { AssetTeaser } from './asset-teaser';
13-
1413
export function AssetDiff({
1514
project,
1615
commit,

src/renderer/components/asset-teaser.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { zodResolver } from '@hookform/resolvers/zod';
2-
import { useProject } from '@root/src/renderer/hooks/useProject';
32
import { useMutation } from '@tanstack/react-query';
43
import {
54
DownloadIcon,
@@ -62,6 +61,7 @@ import {
6261
TooltipProvider,
6362
TooltipTrigger,
6463
} from '@renderer/components/ui/tooltip';
64+
import { useProject } from '@renderer/hooks/useProject';
6565
import { cn, formatBytes } from '@renderer/lib/utils';
6666
import { queryOptions } from '@renderer/queries';
6767

src/renderer/components/commit-author.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use client';
22

3-
import { useProject } from '@root/src/renderer/hooks/useProject';
43
import type { ReactElement } from 'react';
54

65
import { Avatar } from '@renderer/components/ui/avatar';
6+
import { useProject } from '@renderer/hooks/useProject';
77

88
import { type GitCommit } from '@elek-io/core';
99

src/renderer/components/entry-diff.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
DiffContainer,
77
DiffContainerSkeleton,
88
} from '@renderer/components/diff-container';
9+
import { EntryForm } from '@renderer/components/forms/entry-form';
910
import { useQueryNoError } from '@renderer/hooks/useQueryNoError';
1011
import { queryOptions } from '@renderer/queries';
1112

@@ -16,8 +17,6 @@ import {
1617
type UpdateEntryProps,
1718
} from '@elek-io/core';
1819

19-
import { EntryForm } from './forms/entry-form';
20-
2120
export function EntryDiff({
2221
project,
2322
commit,

src/renderer/queries/client.ts

Lines changed: 1 addition & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,12 @@
11
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-
});
112

123
/**
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
215
*/
226
export const queryClient = new QueryClient({
237
defaultOptions: {
248
queries: {
259
staleTime: Infinity,
2610
},
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-
},
8911
},
9012
});

0 commit comments

Comments
 (0)