2023-02-16 18:09:56 +03:00
|
|
|
import { ClientState } from "@dannadori/voice-changer-client-js";
|
2023-02-22 01:35:26 +03:00
|
|
|
import React, { useContext, useEffect, useRef } from "react";
|
2023-02-16 18:09:56 +03:00
|
|
|
import { ReactNode } from "react";
|
|
|
|
import { useVCClient, VCClientState } from "../001_globalHooks/001_useVCClient";
|
|
|
|
import { FrontendManagerStateAndMethod, useFrontendManager } from "../001_globalHooks/010_useFrontendManager";
|
|
|
|
import { useAppRoot } from "./001_AppRootProvider";
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
children: ReactNode;
|
|
|
|
};
|
|
|
|
|
|
|
|
type AppStateValue = ClientState & {
|
|
|
|
audioContext: AudioContext
|
|
|
|
frontendManagerState: FrontendManagerStateAndMethod;
|
2023-02-22 01:35:26 +03:00
|
|
|
initializedRef: React.MutableRefObject<boolean>
|
2023-02-16 18:09:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const AppStateContext = React.createContext<AppStateValue | null>(null);
|
|
|
|
export const useAppState = (): AppStateValue => {
|
|
|
|
const state = useContext(AppStateContext);
|
|
|
|
if (!state) {
|
|
|
|
throw new Error("useAppState must be used within AppStateProvider");
|
|
|
|
}
|
|
|
|
return state;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const AppStateProvider = ({ children }: Props) => {
|
|
|
|
const appRoot = useAppRoot()
|
2023-02-16 20:11:03 +03:00
|
|
|
const clientState = useVCClient({ audioContext: appRoot.audioContextState.audioContext })
|
2023-02-16 18:09:56 +03:00
|
|
|
const frontendManagerState = useFrontendManager();
|
|
|
|
|
2023-02-22 01:35:26 +03:00
|
|
|
const initializedRef = useRef<boolean>(false)
|
|
|
|
useEffect(() => {
|
|
|
|
if (clientState.clientState.initialized) {
|
|
|
|
initializedRef.current = true
|
2023-03-07 20:26:17 +03:00
|
|
|
|
|
|
|
clientState.clientState.clientSetting.updateClientSetting({
|
|
|
|
...clientState.clientState.clientSetting.clientSetting, speakers: [
|
|
|
|
{
|
|
|
|
"id": 107,
|
|
|
|
"name": "user"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 100,
|
|
|
|
"name": "ずんだもん"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 101,
|
|
|
|
"name": "そら"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 102,
|
|
|
|
"name": "めたん"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 103,
|
|
|
|
"name": "つむぎ"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
2023-02-22 01:35:26 +03:00
|
|
|
}
|
|
|
|
}, [clientState.clientState.initialized])
|
|
|
|
|
|
|
|
|
2023-02-16 18:09:56 +03:00
|
|
|
const providerValue: AppStateValue = {
|
|
|
|
audioContext: appRoot.audioContextState.audioContext!,
|
|
|
|
...clientState.clientState,
|
2023-02-22 01:35:26 +03:00
|
|
|
frontendManagerState,
|
|
|
|
initializedRef
|
|
|
|
|
|
|
|
|
2023-02-16 18:09:56 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return <AppStateContext.Provider value={providerValue}>{children}</AppStateContext.Provider>;
|
|
|
|
};
|