frigate/web-new/src/api/index.tsx
Josh Hawkins 696434b36d Initial framework for new UI with React/Typescript (#8885)
* Write a low resolution low fps stream from decoded frames (#8673)

* Generate low res low fps previews for recordings viewer

* Make sure previews end on the hour

* Fix durations and decrase keyframe interval to ensure smooth scrubbing

* Ensure minimized resolution is compatible with yuv

* Add ability to configure preview quality

* Fix

* Clean up previews more efficiently

* Use iterator

* Ensure final frame in preview is not duplicated

* initial react/ts framework

* fix gitignore glob excluding ts files

* ignore folders in web-new

* SWRConfig changes for swr 2.x

* use frigateConfig type in websocket handlers

---------

Co-authored-by: Nicolas Mowen <nickmowen213@gmail.com>
2024-01-31 12:56:11 +00:00

49 lines
1.1 KiB
TypeScript

import { baseUrl } from "./baseUrl";
import useSWR, { SWRConfig } from "swr";
import { WsProvider } from "./ws";
import axios from "axios";
import { ReactNode } from "react";
import { FrigateConfig } from "@/types/frigateConfig";
axios.defaults.baseURL = `${baseUrl}api/`;
type ApiProviderType = {
children?: ReactNode;
options?: Record<string, unknown>;
};
export function ApiProvider({ children, options }: ApiProviderType) {
axios.defaults.headers.common = {
"X-CSRF-TOKEN": 1,
"X-CACHE-BYPASS": 1,
};
return (
<SWRConfig
value={{
fetcher: (key) => {
const [path, params] = Array.isArray(key) ? key : [key, undefined];
return axios.get(path, { params }).then((res) => res.data);
},
...options,
}}
>
<WsWithConfig>{children}</WsWithConfig>
</SWRConfig>
);
}
type WsWithConfigType = {
children: ReactNode;
};
function WsWithConfig({ children }: WsWithConfigType) {
const { data } = useSWR<FrigateConfig>("config");
return data ? <WsProvider config={data}>{children}</WsProvider> : children;
}
export function useApiHost() {
return baseUrl;
}