forked from parseablehq/console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseHotTier.ts
72 lines (67 loc) · 2.49 KB
/
useHotTier.ts
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
import { useMutation, useQuery } from 'react-query';
import { notifyError, notifySuccess } from '@/utils/notification';
import { useStreamStore, streamStoreReducers } from '@/pages/Stream/providers/StreamProvider';
import { deleteHotTierInfo, getHotTierInfo, updateHotTierInfo } from '@/api/logStream';
import { AxiosError, isAxiosError } from 'axios';
const { setHotTier } = streamStoreReducers;
export const useHotTier = (streamName: string, hasSettingsAccess: boolean) => {
const [, setStreamStore] = useStreamStore((_store) => null);
const {
refetch: refetchHotTierInfo,
isError: getHotTierInfoError,
isLoading: getHotTierInfoLoading,
} = useQuery(['fetch-hot-tier-info', streamName], () => getHotTierInfo(streamName), {
retry: false,
enabled: streamName !== '' && hasSettingsAccess,
refetchOnWindowFocus: false,
onSuccess: (data) => {
setStreamStore((store) => setHotTier(store, data.data));
},
onError: () => setStreamStore((store) => setHotTier(store, {})),
});
const { mutate: updateHotTier, isLoading: isUpdating } = useMutation(
({ size }: { size: string; onSuccess?: () => void }) => updateHotTierInfo(streamName, { size }),
{
onSuccess: (_data, variables) => {
notifySuccess({ message: `Hot tier size modified successfully` });
refetchHotTierInfo();
variables.onSuccess && variables.onSuccess();
},
onError: (data: AxiosError) => {
if (isAxiosError(data) && data.response) {
const error = data.response?.data as string;
typeof error === 'string' && notifyError({ message: error });
} else if (data.message && typeof data.message === 'string') {
notifyError({ message: data.message, autoClose: 5000 });
}
},
},
);
const { mutate: deleteHotTier, isLoading: isDeleting } = useMutation(
(_opts: { onSuccess?: () => void }) => deleteHotTierInfo(streamName),
{
onSuccess: (_data, variables) => {
notifySuccess({ message: `Hot tier config deleted successfully` });
refetchHotTierInfo();
variables.onSuccess && variables.onSuccess();
},
onError: (data: AxiosError) => {
if (isAxiosError(data) && data.response) {
const error = data.response?.data as string;
typeof error === 'string' && notifyError({ message: error });
} else if (data.message && typeof data.message === 'string') {
notifyError({ message: data.message, autoClose: 5000 });
}
},
},
);
return {
getHotTierInfoError,
getHotTierInfoLoading,
refetchHotTierInfo,
updateHotTier,
deleteHotTier,
isDeleting,
isUpdating,
};
};