voice-changer/client/demo/src/001_provider/001_AppRootProvider.tsx

40 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-04-04 21:35:16 +03:00
import React, { useContext, useEffect } from "react";
2023-02-16 18:09:56 +03:00
import { ReactNode } from "react";
2023-04-04 21:35:16 +03:00
import { AppGuiSettingStateAndMethod, userAppGuiSetting } from "../001_globalHooks/001_useAppGuiSetting";
2023-02-16 18:09:56 +03:00
import { AudioConfigState, useAudioConfig } from "../001_globalHooks/001_useAudioConfig";
type Props = {
children: ReactNode;
};
type AppRootValue = {
audioContextState: AudioConfigState
2023-04-04 21:35:16 +03:00
appGuiSettingState: AppGuiSettingStateAndMethod
2023-02-16 18:09:56 +03:00
}
const AppRootContext = React.createContext<AppRootValue | null>(null);
export const useAppRoot = (): AppRootValue => {
const state = useContext(AppRootContext);
if (!state) {
throw new Error("useAppState must be used within AppStateProvider");
}
return state;
};
export const AppRootProvider = ({ children }: Props) => {
const audioContextState = useAudioConfig()
2023-04-04 21:35:16 +03:00
const appGuiSettingState = userAppGuiSetting()
useEffect(() => {
const params = new URLSearchParams(window.location.search);
const modelType = params.get("modelType") || ""
appGuiSettingState.getAppSetting(`/assets/gui_settings/${modelType}.json`)
}, [])
2023-02-16 18:09:56 +03:00
const providerValue: AppRootValue = {
2023-02-22 01:35:26 +03:00
audioContextState,
2023-04-04 21:35:16 +03:00
appGuiSettingState
2023-02-16 18:09:56 +03:00
};
return <AppRootContext.Provider value={providerValue}>{children}</AppRootContext.Provider>;
};