mirror of
https://github.com/blakeblackshear/frigate.git
synced 2025-09-26 19:41:29 +08:00
30 lines
703 B
JavaScript
30 lines
703 B
JavaScript
import { h } from 'preact';
|
|
import { baseUrl } from './baseUrl';
|
|
import useSWR, { SWRConfig } from 'swr';
|
|
import { MqttProvider } from './mqtt';
|
|
import axios from 'axios';
|
|
|
|
axios.defaults.baseURL = `${baseUrl}/api/`;
|
|
|
|
export function ApiProvider({ children, options }) {
|
|
return (
|
|
<SWRConfig
|
|
value={{
|
|
fetcher: (path) => axios.get(path).then((res) => res.data),
|
|
...options,
|
|
}}
|
|
>
|
|
<MqttWithConfig>{children}</MqttWithConfig>
|
|
</SWRConfig>
|
|
);
|
|
}
|
|
|
|
function MqttWithConfig({ children }) {
|
|
const { data } = useSWR('config');
|
|
return data ? <MqttProvider config={data}>{children}</MqttProvider> : children;
|
|
}
|
|
|
|
export function useApiHost() {
|
|
return baseUrl;
|
|
}
|