|
| 1 | +import React, { useEffect, useReducer, useState } from 'react'; |
| 2 | +import { IntlProvider } from 'react-intl' |
| 3 | +import CreateInstance from './components/CreateInstance'; |
| 4 | +import EditInstance from './components/EditInstance'; |
| 5 | +import EditHtmlTemplate from './components/EditHtmlTemplate'; |
| 6 | +import DeployPanel from './components/DeployPanel'; |
| 7 | +import LoadingScreen from './components/LoadingScreen'; |
| 8 | +import { appInitialState, appReducer } from './reducers/state'; |
| 9 | +import { |
| 10 | + connectRemix, |
| 11 | + initDispatch, |
| 12 | + updateState, |
| 13 | + selectTheme, |
| 14 | +} from './actions'; |
| 15 | +import { AppContext } from './contexts'; |
| 16 | +import remixClient from './remix-client'; |
| 17 | +import './App.css'; |
| 18 | + |
| 19 | +function App(): JSX.Element { |
| 20 | + const [locale, setLocale] = useState<{code: string; messages: any}>({ |
| 21 | + code: 'en', |
| 22 | + messages: null, |
| 23 | + }) |
| 24 | + const [appState, dispatch] = useReducer(appReducer, appInitialState); |
| 25 | + const { isAiLoading } = appState; |
| 26 | + |
| 27 | + useEffect(() => { |
| 28 | + updateState(appState); |
| 29 | + }, [appState]); |
| 30 | + useEffect(() => { |
| 31 | + initDispatch(dispatch); |
| 32 | + updateState(appState); |
| 33 | + connectRemix().then(() => { |
| 34 | + remixClient.call('theme', 'currentTheme').then((theme: any) => { |
| 35 | + selectTheme(theme.name); |
| 36 | + }); |
| 37 | + remixClient.on('theme', 'themeChanged', (theme: any) => { |
| 38 | + selectTheme(theme.name); |
| 39 | + }); |
| 40 | + // @ts-ignore |
| 41 | + remixClient.call('locale', 'currentLocale').then((locale: any) => { |
| 42 | + setLocale(locale) |
| 43 | + }) |
| 44 | + // @ts-ignore |
| 45 | + remixClient.on('locale', 'localeChanged', (locale: any) => { |
| 46 | + setLocale(locale) |
| 47 | + }) |
| 48 | + // @ts-ignore |
| 49 | + remixClient.on('ai-dapp-generator', 'generationProgress', (progress: any) => { |
| 50 | + if (progress.status === 'started') { |
| 51 | + dispatch({ type: 'SET_AI_LOADING', payload: true }); |
| 52 | + } |
| 53 | + }); |
| 54 | + // @ts-ignore |
| 55 | + remixClient.on('ai-dapp-generator', 'dappGenerated', () => { |
| 56 | + dispatch({ type: 'SET_AI_LOADING', payload: false }); |
| 57 | + }); |
| 58 | + // @ts-ignore |
| 59 | + remixClient.on('ai-dapp-generator', 'dappUpdated', () => { |
| 60 | + dispatch({ type: 'SET_AI_LOADING', payload: false }); |
| 61 | + }); |
| 62 | + }); |
| 63 | + }, []); |
| 64 | + return ( |
| 65 | + <AppContext.Provider |
| 66 | + value={{ |
| 67 | + dispatch, |
| 68 | + appState, |
| 69 | + }} |
| 70 | + > |
| 71 | + <IntlProvider locale={locale.code} messages={locale.messages}> |
| 72 | + {appState.instance.htmlTemplate ? ( |
| 73 | + <div className="container-fluid pt-3"> |
| 74 | + <EditHtmlTemplate /> |
| 75 | + </div> |
| 76 | + ) : Object.keys(appState.instance.abi).length > 0 ? ( |
| 77 | + <div className="row m-0 pt-3"> |
| 78 | + <EditInstance /> |
| 79 | + <DeployPanel /> |
| 80 | + </div> |
| 81 | + ) : ( |
| 82 | + <div className="row m-0 pt-3"> |
| 83 | + <CreateInstance isAiLoading={isAiLoading} /> |
| 84 | + </div> |
| 85 | + )} |
| 86 | + <LoadingScreen /> |
| 87 | + </IntlProvider> |
| 88 | + </AppContext.Provider> |
| 89 | + ); |
| 90 | +} |
| 91 | + |
| 92 | +export default App; |
0 commit comments