forked from parseablehq/console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseAlertsEditor.tsx
62 lines (56 loc) · 1.68 KB
/
useAlertsEditor.tsx
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
import { useMutation, useQuery } from 'react-query';
import { getLogStreamAlerts, putLogStreamAlerts } from '@/api/logStream';
import { IconFileAlert } from '@tabler/icons-react';
import { notifications } from '@mantine/notifications';
import useMountedState from './useMountedState';
export const useAlertsEditor = (streamName: string) => {
const [alertQuery, setAlertQuery] = useMountedState<string | undefined>('');
const {
mutate: updateLogStreamAlerts,
data: alertQueryData,
isSuccess: updateLogStreamIsSuccess,
} = useMutation((data: string) => putLogStreamAlerts(streamName, data), {});
const {
data: getLogAlertData,
isError: getLogAlertIsError,
isSuccess: getLogAlertIsSuccess,
isLoading: getLogAlertIsLoading,
} = useQuery(['fetch-log-stream-alert', streamName, updateLogStreamIsSuccess], () => getLogStreamAlerts(streamName), {
onSuccess: (data) => {
setAlertQuery(JSON.stringify(data?.data));
},
retry: false,
enabled: streamName !== '',
});
const handleAlertQueryChange = (value: string | undefined) => setAlertQuery(value);
const submitAlertQuery = () => {
try {
if (alertQuery) {
JSON.parse(alertQuery);
updateLogStreamAlerts(alertQuery);
} else {
throw new Error('Invalid JSON');
}
} catch (e) {
notifications.show({
id: 'load-data',
loading: false,
color: 'red',
title: 'Error occurred',
message: `Error occurred, please check your query and try again ${e}`,
icon: <IconFileAlert size="1rem" />,
autoClose: 3000,
});
}
};
return {
alertQuery,
handleAlertQueryChange,
submitAlertQuery,
alertQueryData,
getLogAlertData,
getLogAlertIsError,
getLogAlertIsSuccess,
getLogAlertIsLoading,
};
};