|
1 |
| -import { QueryClient } from "react-query"; |
| 1 | +import { QueryClient, type UseQueryOptions } from "react-query"; |
2 | 2 | import * as API from "api/api";
|
3 |
| -import { AppearanceConfig } from "api/typesGenerated"; |
| 3 | +import { type AppearanceConfig } from "api/typesGenerated"; |
4 | 4 | import { getMetadataAsJSON } from "utils/metadata";
|
5 | 5 |
|
6 |
| -export const appearance = () => { |
| 6 | +const initialAppearanceData = getMetadataAsJSON<AppearanceConfig>("appearance"); |
| 7 | +const appearanceConfigKey = ["appearance"] as const; |
| 8 | + |
| 9 | +export const appearance = (queryClient: QueryClient) => { |
7 | 10 | return {
|
8 |
| - queryKey: ["appearance"], |
9 |
| - queryFn: async () => |
10 |
| - getMetadataAsJSON<AppearanceConfig>("appearance") ?? API.getAppearance(), |
11 |
| - }; |
| 11 | + queryKey: appearanceConfigKey, |
| 12 | + queryFn: async () => { |
| 13 | + const cachedData = queryClient.getQueryData(appearanceConfigKey); |
| 14 | + if (cachedData === undefined && initialAppearanceData !== undefined) { |
| 15 | + return initialAppearanceData; |
| 16 | + } |
| 17 | + |
| 18 | + return API.getAppearance(); |
| 19 | + }, |
| 20 | + } satisfies UseQueryOptions<AppearanceConfig>; |
12 | 21 | };
|
13 | 22 |
|
14 | 23 | export const updateAppearance = (queryClient: QueryClient) => {
|
15 | 24 | return {
|
16 | 25 | mutationFn: API.updateAppearance,
|
17 | 26 | onSuccess: (newConfig: AppearanceConfig) => {
|
18 |
| - queryClient.setQueryData(["appearance"], newConfig); |
| 27 | + queryClient.setQueryData(appearanceConfigKey, newConfig); |
19 | 28 | },
|
20 | 29 | };
|
21 | 30 | };
|
0 commit comments