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

77 lines
3.1 KiB
TypeScript
Raw Normal View History

2023-02-16 18:09:56 +03:00
import { ClientState } from "@dannadori/voice-changer-client-js";
2023-11-08 13:54:13 +03:00
import { VoiceChangerJSClient } from "@dannadori/voice-changer-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";
import { WebInfoStateAndMethod, useWebInfo } from "../001_globalHooks/100_useWebInfo";
2023-02-16 18:09:56 +03:00
type Props = {
children: ReactNode;
};
type AppStateValue = ClientState & {
2023-09-27 22:19:41 +03:00
audioContext: AudioContext;
initializedRef: React.MutableRefObject<boolean>;
webInfoState: WebInfoStateAndMethod;
2023-09-27 22:19:41 +03:00
};
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) => {
2023-09-27 22:19:41 +03:00
const appRoot = useAppRoot();
const clientState = useVCClient({ audioContext: appRoot.audioContextState.audioContext });
const messageBuilderState = useMessageBuilder();
const webInfoState = useWebInfo(clientState);
// const voiceChangerJSClient = useRef<VoiceChangerJSClient>();
2023-04-04 21:35:16 +03:00
2023-06-11 07:25:44 +03:00
useEffect(() => {
messageBuilderState.setMessage(__filename, "ioError", {
2023-09-27 22:19:41 +03:00
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-09-27 22:19:41 +03:00
const initializedRef = useRef<boolean>(false);
2023-06-25 20:40:44 +03:00
useEffect(() => {
if (clientState.clientState.initialized) {
2023-09-27 22:19:41 +03:00
initializedRef.current = true;
clientState.clientState.getInfo();
2023-06-25 20:40:44 +03:00
// clientState.clientState.setVoiceChangerClientSetting({
// ...clientState.clientState.setting.voiceChangerClientSetting
// })
}
2023-09-27 22:19:41 +03:00
}, [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-09-27 22:19:41 +03:00
alert(messageBuilderState.getMessage(__filename, "ioError"));
clientState.clientState.resetIoErrorCount();
2023-06-11 04:55:26 +03:00
}
2023-09-27 22:19:41 +03:00
}, [clientState.clientState.ioErrorCount]);
2023-06-11 04:55:26 +03:00
2023-11-14 02:17:27 +03:00
useEffect(() => {
if (appRoot.appGuiSettingState.edition.indexOf("web") >= 0 && clientState.clientState.initialized) {
clientState.clientState.setWorkletNodeSetting({ ...clientState.clientState.setting.workletNodeSetting, protocol: "internal" });
webInfoState.loadVoiceChanagerModel();
2023-11-14 02:17:27 +03:00
}
}, [clientState.clientState.initialized]);
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,
webInfoState,
2023-02-16 18:09:56 +03:00
};
return <AppStateContext.Provider value={providerValue}>{children}</AppStateContext.Provider>;
};