2023-01-07 14:07:39 +03:00
import * as React from "react" ;
import { createRoot } from "react-dom/client" ;
2023-02-16 18:09:56 +03:00
import { library } from "@fortawesome/fontawesome-svg-core" ;
import { fas } from "@fortawesome/free-solid-svg-icons" ;
import { far } from "@fortawesome/free-regular-svg-icons" ;
import { fab } from "@fortawesome/free-brands-svg-icons" ;
2023-06-24 02:52:02 +03:00
import { ErrorInfo , useEffect , useMemo , useState , } from "react" ;
import "./css/App.css"
2023-02-22 01:35:26 +03:00
import ErrorBoundary from "./001_provider/900_ErrorBoundary" ;
2023-06-24 02:52:02 +03:00
import { AppStateProvider } from "./001_provider/001_AppStateProvider" ;
import { AppRootProvider , useAppRoot } from "./001_provider/001_AppRootProvider" ;
import { useIndexedDB } from "@dannadori/voice-changer-client-js" ;
2023-04-04 10:38:30 +03:00
import { Demo } from "./components/demo/010_Demo" ;
2023-06-11 07:25:44 +03:00
import { useMessageBuilder } from "./hooks/useMessageBuilder" ;
2023-02-16 18:09:56 +03:00
library . add ( fas , far , fab ) ;
2023-01-07 14:07:39 +03:00
const container = document . getElementById ( "app" ) ! ;
const root = createRoot ( container ) ;
const App = ( ) = > {
2023-04-10 21:07:14 +03:00
const { appGuiSettingState } = useAppRoot ( )
2023-04-04 10:38:30 +03:00
const front = useMemo ( ( ) = > {
2023-04-04 21:35:16 +03:00
if ( appGuiSettingState . appGuiSetting . type == "demo" ) {
2023-04-04 10:38:30 +03:00
return < Demo > < / Demo >
} else {
2023-04-04 21:35:16 +03:00
return < > unknown gui type . { appGuiSettingState . appGuiSetting . type } < / >
2023-02-16 18:09:56 +03:00
}
2023-04-04 21:35:16 +03:00
} , [ appGuiSettingState . appGuiSetting . type ] )
2023-01-07 14:07:39 +03:00
return (
2023-02-16 18:09:56 +03:00
< >
2023-04-04 10:38:30 +03:00
{ front }
2023-02-16 18:09:56 +03:00
< / >
)
}
const AppStateWrapper = ( ) = > {
2023-06-24 03:52:21 +03:00
const { appGuiSettingState , getGUISetting } = useAppRoot ( )
2023-06-11 07:25:44 +03:00
const messageBuilderState = useMessageBuilder ( )
// エラーメッセージ登録
useMemo ( ( ) = > {
messageBuilderState . setMessage ( __filename , "Problem" , { "ja" : "ちょっと問題が起きたみたいです。" , "en" : "Looks like there's a bit of a problem." } )
messageBuilderState . setMessage ( __filename , "Problem-sub1" , { "ja" : "このアプリで管理している情報をクリアすると回復する場合があります。" , "en" : "" } )
messageBuilderState . setMessage ( __filename , "Problem-sub2" , { "ja" : "下記のボタンを押して情報をクリアします。" , "en" : "If you clear the information being managed by this app, it may be recoverable." } )
messageBuilderState . setMessage ( __filename , "Problem-action1" , { "ja" : "アプリを初期化" , "en" : "Initialize" } )
messageBuilderState . setMessage ( __filename , "Problem-action2" , { "ja" : "初期化せずリロード" , "en" : "Reload without initialize" } )
} , [ ] )
2023-02-22 01:35:26 +03:00
// エラーバウンダリー設定
const [ error , setError ] = useState < { error : Error , errorInfo : ErrorInfo } > ( )
2023-06-24 02:52:02 +03:00
const { removeDB } = useIndexedDB ( { clientType : null } )
2023-06-11 07:25:44 +03:00
2023-02-22 01:35:26 +03:00
const errorComponent = useMemo ( ( ) = > {
const errorName = error ? . error . name || "no error name"
const errorMessage = error ? . error . message || "no error message"
const errorInfos = ( error ? . errorInfo . componentStack || "no error stack" ) . split ( "\n" )
2023-02-28 05:54:40 +03:00
const onClearCacheClicked = async ( ) = > {
2023-04-30 10:53:15 +03:00
await removeDB ( )
location . reload ( ) ;
2023-05-04 06:18:17 +03:00
}
const onReloadClicked = ( ) = > {
location . reload ( ) ;
2023-02-22 01:35:26 +03:00
}
return (
< div className = "error-container" >
< div className = "top-error-message" >
2023-06-11 07:25:44 +03:00
{ messageBuilderState . getMessage ( __filename , "Problem" ) }
2023-02-22 01:35:26 +03:00
< / div >
< div className = "top-error-description" >
2023-06-11 07:25:44 +03:00
< p > { messageBuilderState . getMessage ( __filename , "Problem-sub1" ) } < / p >
< p > { messageBuilderState . getMessage ( __filename , "Problem-sub2" ) } < / p >
< p > < button onClick = { onClearCacheClicked } > { messageBuilderState . getMessage ( __filename , "Problem-action1" ) } < / button > < / p >
< p > < button onClick = { onReloadClicked } > { messageBuilderState . getMessage ( __filename , "Problem-action2" ) } < / button > < / p >
2023-02-22 01:35:26 +03:00
< / div >
< div className = "error-detail" >
< div className = "error-name" >
{ errorName }
< / div >
< div className = "error-message" >
{ errorMessage }
< / div >
< div className = "error-info-container" >
{ errorInfos . map ( x = > {
return < div className = "error-info-line" key = { x } > { x } < / div >
} ) }
< / div >
< / div >
< / div >
)
} , [ error ] )
const updateError = ( error : Error , errorInfo : React.ErrorInfo ) = > {
console . log ( "error compo" , error , errorInfo )
setError ( { error , errorInfo } )
}
2023-04-10 18:21:17 +03:00
2023-04-10 21:07:14 +03:00
useEffect ( ( ) = > {
const loadDefaultModelType = async ( ) = > {
2023-06-24 03:52:21 +03:00
getGUISetting ( )
2023-04-10 21:07:14 +03:00
}
loadDefaultModelType ( )
} , [ ] )
2023-04-10 18:21:17 +03:00
2023-06-24 02:52:02 +03:00
if ( ! appGuiSettingState . guiSettingLoaded ) {
2023-06-24 03:52:21 +03:00
return < > loading . . . < / >
2023-04-04 21:35:16 +03:00
} else {
return (
< ErrorBoundary fallback = { errorComponent } onError = { updateError } >
< AppStateProvider >
< App > < / App >
< / AppStateProvider >
< / ErrorBoundary >
)
}
2023-01-07 14:07:39 +03:00
}
root . render (
2023-02-16 18:09:56 +03:00
< AppRootProvider >
< AppStateWrapper > < / AppStateWrapper >
< / AppRootProvider >
2023-01-07 14:07:39 +03:00
) ;
2023-02-22 01:35:26 +03:00