frigate/web/src/api/index.jsx

30 lines
722 B
React
Raw Normal View History

2022-02-26 22:11:00 +03:00
import { h } from 'preact';
2021-02-11 18:13:21 +03:00
import { baseUrl } from './baseUrl';
2022-02-26 22:11:00 +03:00
import useSWR, { SWRConfig } from 'swr';
2021-02-16 07:10:20 +03:00
import { MqttProvider } from './mqtt';
2022-02-26 22:11:00 +03:00
import axios from 'axios';
2021-01-26 18:04:03 +03:00
2022-05-19 15:31:02 +03:00
axios.defaults.baseURL = `${baseUrl}api/`;
2021-01-26 18:04:03 +03:00
2022-03-06 07:16:31 +03:00
export function ApiProvider({ children, options }) {
2021-02-16 07:10:20 +03:00
return (
2022-02-26 22:11:00 +03:00
<SWRConfig
value={{
2022-05-12 20:05:34 +03:00
fetcher: (path, params) => axios.get(path, { params }).then((res) => res.data),
2022-03-06 07:16:31 +03:00
...options,
2022-02-26 22:11:00 +03:00
}}
>
<MqttWithConfig>{children}</MqttWithConfig>
2022-02-26 22:11:00 +03:00
</SWRConfig>
2021-02-16 07:10:20 +03:00
);
}
function MqttWithConfig({ children }) {
2022-02-26 22:11:00 +03:00
const { data } = useSWR('config');
return data ? <MqttProvider config={data}>{children}</MqttProvider> : children;
}
2021-01-26 18:04:03 +03:00
export function useApiHost() {
2022-02-26 22:11:00 +03:00
return baseUrl;
2021-01-26 18:04:03 +03:00
}