import { lazy, Suspense, useEffect, useState } from 'react' import Box from '@mui/material/Box' import Typography from '@mui/material/Typography' import { v4 as uuid } from 'uuid' import { encryption } from 'services/Encryption' import { EnvironmentUnsupportedDialog, isEnvironmentSupported, } from 'components/Shell/EnvironmentUnsupportedDialog' import { WholePageLoading } from 'components/Loading/Loading' import { ColorMode, UserSettings } from 'models/settings' import type { BootstrapProps } from './Bootstrap' const Bootstrap = lazy(() => import('./Bootstrap')) export interface InitProps extends Omit { getUuid?: typeof uuid } // NOTE: This is meant to be a thin layer around the Bootstrap component that // only handles asynchronous creation of the public/private keys that Bootstrap // requires. const Init = ({ getUuid = uuid, ...props }: InitProps) => { const [userSettings, setUserSettings] = useState(null) const [errorMessage, setErrorMessage] = useState(null) useEffect(() => { ;(async () => { if (userSettings !== null) return try { const { publicKey, privateKey } = await encryption.generateKeyPair() setUserSettings({ userId: getUuid(), customUsername: '', colorMode: ColorMode.DARK, playSoundOnNewMessage: true, showNotificationOnNewMessage: true, showActiveTypingStatus: true, publicKey, privateKey, }) } catch (e) { console.error(e) setErrorMessage( 'Chitchatter was unable to boot up. Please check the browser console.' ) } })() }, [getUuid, userSettings]) if (!isEnvironmentSupported) { return } if (errorMessage) { return ( {errorMessage} ) } if (userSettings === null) { return } return ( }> ) } export default Init