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

49 lines
1.5 KiB
TypeScript
Raw Normal View History

2023-04-10 18:21:17 +03:00
import { ClientType } from "@dannadori/voice-changer-client-js";
import React, { useContext, useEffect, useState } from "react";
2023-02-16 18:09:56 +03:00
import { ReactNode } from "react";
2023-05-26 11:56:41 +03:00
import { AppGuiSettingStateAndMethod, useAppGuiSetting } 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-04-10 18:21:17 +03:00
clientType: ClientType | null
setClientType: (val: ClientType | null) => void
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-05-26 11:56:41 +03:00
const appGuiSettingState = useAppGuiSetting()
2023-06-11 07:25:44 +03:00
2023-04-10 18:21:17 +03:00
const [clientType, setClientType] = useState<ClientType | null>(null)
2023-04-04 21:35:16 +03:00
2023-06-11 07:25:44 +03:00
2023-04-04 21:35:16 +03:00
useEffect(() => {
2023-04-10 18:21:17 +03:00
if (!clientType) {
return
}
appGuiSettingState.getAppGuiSetting(`/assets/gui_settings/${clientType}.json`)
}, [clientType])
2023-04-04 21:35:16 +03:00
2023-02-16 18:09:56 +03:00
const providerValue: AppRootValue = {
2023-02-22 01:35:26 +03:00
audioContextState,
2023-04-10 18:21:17 +03:00
appGuiSettingState,
clientType,
setClientType
2023-02-16 18:09:56 +03:00
};
return <AppRootContext.Provider value={providerValue}>{children}</AppRootContext.Provider>;
};