umami/src/components/hooks/useConfig.ts

29 lines
575 B
TypeScript
Raw Normal View History

import { useEffect } from 'react';
import useStore, { setConfig } from 'store/app';
import useApi from 'components/hooks/useApi';
2022-10-31 19:02:37 +01:00
let loading = false;
2023-05-18 08:20:06 +02:00
export function useConfig() {
const { config } = useStore();
const { get } = useApi();
2023-08-28 04:56:44 +02:00
const configUrl = process.env.configUrl;
async function loadConfig() {
2023-08-28 04:56:44 +02:00
const data = await get(configUrl);
2022-10-31 19:02:37 +01:00
loading = false;
setConfig(data);
}
useEffect(() => {
2023-08-28 04:56:44 +02:00
if (!config && !loading && configUrl) {
2022-10-31 19:02:37 +01:00
loading = true;
loadConfig();
}
}, []);
2023-07-27 23:47:41 +02:00
return config;
}
2023-05-18 08:20:06 +02:00
export default useConfig;