-
Notifications
You must be signed in to change notification settings - Fork 6.2k
/
Copy pathuse-get-config.ts
73 lines (68 loc) · 2.52 KB
/
use-get-config.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
73
import {
DEFAULT_POLLING_INTERVAL,
DEFAULT_TIMEOUT,
} from "@/constants/constants";
import { EventDeliveryType } from "@/constants/enums";
import useFlowsManagerStore from "@/stores/flowsManagerStore";
import { useUtilityStore } from "@/stores/utilityStore";
import axios from "axios";
import { useQueryFunctionType } from "../../../../types/api";
import { api } from "../../api";
import { getURL } from "../../helpers/constants";
import { UseRequestProcessor } from "../../services/request-processor";
export interface ConfigResponse {
frontend_timeout: number;
auto_saving: boolean;
auto_saving_interval: number;
health_check_max_retries: number;
max_file_size_upload: number;
feature_flags: Record<string, any>;
webhook_polling_interval: number;
event_delivery: EventDeliveryType;
}
export const useGetConfig: useQueryFunctionType<undefined, ConfigResponse> = (
options,
) => {
const setAutoSaving = useFlowsManagerStore((state) => state.setAutoSaving);
const setAutoSavingInterval = useFlowsManagerStore(
(state) => state.setAutoSavingInterval,
);
const setHealthCheckMaxRetries = useFlowsManagerStore(
(state) => state.setHealthCheckMaxRetries,
);
const setMaxFileSizeUpload = useUtilityStore(
(state) => state.setMaxFileSizeUpload,
);
const setFeatureFlags = useUtilityStore((state) => state.setFeatureFlags);
const setWebhookPollingInterval = useUtilityStore(
(state) => state.setWebhookPollingInterval,
);
const setEventDelivery = useUtilityStore((state) => state.setEventDelivery);
const { query } = UseRequestProcessor();
const getConfigFn = async () => {
const response = await api.get<ConfigResponse>(`${getURL("CONFIG")}`);
const data = response["data"];
if (data) {
const timeoutInMilliseconds = data.frontend_timeout
? data.frontend_timeout * 1000
: DEFAULT_TIMEOUT;
axios.defaults.baseURL = "";
axios.defaults.timeout = timeoutInMilliseconds;
setAutoSaving(data.auto_saving);
setAutoSavingInterval(data.auto_saving_interval);
setHealthCheckMaxRetries(data.health_check_max_retries);
setMaxFileSizeUpload(data.max_file_size_upload);
setFeatureFlags(data.feature_flags);
setWebhookPollingInterval(
data.webhook_polling_interval ?? DEFAULT_POLLING_INTERVAL,
);
setEventDelivery(data.event_delivery ?? EventDeliveryType.POLLING);
}
return data;
};
const queryResult = query(["useGetConfig"], getConfigFn, {
refetchOnWindowFocus: false,
...options,
});
return queryResult;
};