umami/src/components/hooks/useConfig.js

29 lines
575 B
JavaScript
Raw Normal View History

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