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";
|
2023-03-09 00:47:11 +03:00
|
|
|
import { useVCClient } from "../001_globalHooks/001_useVCClient";
|
2023-02-16 18:09:56 +03:00
|
|
|
import { useAppRoot } from "./001_AppRootProvider";
|
2023-06-11 07:25:44 +03:00
|
|
|
import { useMessageBuilder } from "../hooks/useMessageBuilder";
|
2023-02-16 18:09:56 +03:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
children: ReactNode;
|
|
|
|
};
|
|
|
|
|
|
|
|
type AppStateValue = ClientState & {
|
|
|
|
audioContext: AudioContext
|
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-06-24 03:52:21 +03:00
|
|
|
const clientState = useVCClient({ audioContext: appRoot.audioContextState.audioContext })
|
2023-06-11 07:25:44 +03:00
|
|
|
const messageBuilderState = useMessageBuilder()
|
2023-04-04 21:35:16 +03:00
|
|
|
|
2023-06-11 07:25:44 +03:00
|
|
|
useEffect(() => {
|
|
|
|
messageBuilderState.setMessage(__filename, "ioError", {
|
|
|
|
"ja": "エラーが頻発しています。対象としているフレームワークのモデルがロードされているか確認してください。",
|
|
|
|
"en": "Frequent errors occur. Please check if the model of the framework being targeted is loaded."
|
|
|
|
})
|
|
|
|
}, [])
|
2023-02-16 18:09:56 +03:00
|
|
|
|
2023-02-22 01:35:26 +03:00
|
|
|
const initializedRef = useRef<boolean>(false)
|
2023-06-25 16:38:21 +03:00
|
|
|
// useEffect(() => {
|
|
|
|
// if (clientState.clientState.initialized) {
|
|
|
|
// initializedRef.current = true
|
|
|
|
// clientState.clientState.setVoiceChangerClientSetting({
|
|
|
|
// ...clientState.clientState.setting.voiceChangerClientSetting
|
|
|
|
// })
|
|
|
|
// }
|
|
|
|
// }, [clientState.clientState.initialized])
|
2023-02-22 01:35:26 +03:00
|
|
|
|
2023-06-11 04:55:26 +03:00
|
|
|
useEffect(() => {
|
|
|
|
if (clientState.clientState.ioErrorCount > 100) {
|
2023-06-11 07:25:44 +03:00
|
|
|
alert(messageBuilderState.getMessage(__filename, "ioError"))
|
2023-06-11 04:55:26 +03:00
|
|
|
clientState.clientState.resetIoErrorCount()
|
|
|
|
}
|
|
|
|
|
|
|
|
}, [clientState.clientState.ioErrorCount])
|
2023-04-04 21:35:16 +03:00
|
|
|
|
2023-02-22 01:35:26 +03:00
|
|
|
|
2023-02-16 18:09:56 +03:00
|
|
|
const providerValue: AppStateValue = {
|
|
|
|
audioContext: appRoot.audioContextState.audioContext!,
|
|
|
|
...clientState.clientState,
|
2023-04-04 10:38:30 +03:00
|
|
|
initializedRef,
|
2023-02-16 18:09:56 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return <AppStateContext.Provider value={providerValue}>{children}</AppStateContext.Provider>;
|
|
|
|
};
|