Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 4x 4x 4x 4x 4x 26x 26x 1x 26x 26x 26x 5x 5x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 1x 28x 28x 2x 2x 2x 2x 26x 26x | 'use client';
import type {
SystemSettings,
SystemSettingsUpdateParams,
} from '@shared/types/data/system-settings';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import {
getSystemSettings,
updateSystemSettings,
} from '@web/api/v1/super-agents/system-settings';
import { createContext, type ReactNode, useCallback, useContext } from 'react';
export const systemSettingsQueryKeys = {
all: ['system-settings'] as const,
detail: () => [...systemSettingsQueryKeys.all, 'detail'] as const,
};
interface SystemSettingsContextType {
settings: SystemSettings | null;
isLoading: boolean;
error: string | null;
refetch: () => Promise<void>;
update: (params: SystemSettingsUpdateParams) => Promise<SystemSettings>;
isUpdating: boolean;
}
const SystemSettingsContext = createContext<
SystemSettingsContextType | undefined
>(undefined);
interface SystemSettingsProviderProps {
children: ReactNode;
}
export function SystemSettingsProvider({
children,
}: SystemSettingsProviderProps) {
const queryClient = useQueryClient();
const {
data: settings = null,
isLoading,
error: queryError,
refetch: refetchQuery,
} = useQuery({
queryKey: systemSettingsQueryKeys.detail(),
queryFn: getSystemSettings,
});
const updateMutation = useMutation({
mutationFn: updateSystemSettings,
onSuccess: (updatedSettings) => {
queryClient.setQueryData(
systemSettingsQueryKeys.detail(),
updatedSettings,
);
},
});
const refetch = useCallback(async () => {
await refetchQuery();
}, [refetchQuery]);
const update = useCallback(
async (params: SystemSettingsUpdateParams) => {
return await updateMutation.mutateAsync(params);
},
[updateMutation],
);
const contextValue: SystemSettingsContextType = {
settings,
isLoading,
error: queryError ? (queryError as Error).message : null,
refetch,
update,
isUpdating: updateMutation.isPending,
};
return (
<SystemSettingsContext.Provider value={contextValue}>
{children}
</SystemSettingsContext.Provider>
);
}
export function useSystemSettings(): SystemSettingsContextType {
const context = useContext(SystemSettingsContext);
if (!context) {
throw new Error(
'useSystemSettings must be used within a SystemSettingsProvider',
);
}
return context;
}
|