2023-12-11 23:19:31 +00:00
|
|
|
import { lazy, Suspense, useEffect, useState } from 'react'
|
2023-12-09 23:47:05 +00:00
|
|
|
import Box from '@mui/material/Box'
|
|
|
|
import Typography from '@mui/material/Typography'
|
|
|
|
import { v4 as uuid } from 'uuid'
|
|
|
|
|
2024-01-29 02:46:59 +00:00
|
|
|
import { encryption } from 'services/Encryption'
|
2023-12-09 23:47:05 +00:00
|
|
|
import {
|
|
|
|
EnvironmentUnsupportedDialog,
|
|
|
|
isEnvironmentSupported,
|
|
|
|
} from 'components/Shell/EnvironmentUnsupportedDialog'
|
|
|
|
import { WholePageLoading } from 'components/Loading/Loading'
|
|
|
|
import { ColorMode, UserSettings } from 'models/settings'
|
|
|
|
|
2023-12-11 23:19:31 +00:00
|
|
|
import type { BootstrapProps } from './Bootstrap'
|
|
|
|
|
2024-03-13 02:44:43 +00:00
|
|
|
const Bootstrap = lazy(() => import('./Bootstrap'))
|
2023-12-09 23:47:05 +00:00
|
|
|
|
|
|
|
export interface InitProps extends Omit<BootstrapProps, 'initialUserSettings'> {
|
|
|
|
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<UserSettings | null>(null)
|
|
|
|
const [errorMessage, setErrorMessage] = useState<string | null>(null)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
;(async () => {
|
|
|
|
if (userSettings !== null) return
|
|
|
|
|
|
|
|
try {
|
2024-01-29 02:46:59 +00:00
|
|
|
const { publicKey, privateKey } = await encryption.generateKeyPair()
|
2023-12-09 23:47:05 +00:00
|
|
|
|
|
|
|
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 <EnvironmentUnsupportedDialog />
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errorMessage) {
|
|
|
|
return (
|
|
|
|
<Box
|
|
|
|
sx={{
|
|
|
|
display: 'flex',
|
|
|
|
height: '100vh',
|
|
|
|
justifyContent: 'center',
|
|
|
|
alignItems: 'center',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Typography>{errorMessage}</Typography>
|
|
|
|
</Box>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (userSettings === null) {
|
|
|
|
return <WholePageLoading />
|
|
|
|
}
|
|
|
|
|
2023-12-11 23:19:31 +00:00
|
|
|
return (
|
|
|
|
<Suspense fallback={<WholePageLoading />}>
|
|
|
|
<Bootstrap {...props} initialUserSettings={userSettings} />
|
|
|
|
</Suspense>
|
|
|
|
)
|
2023-12-09 23:47:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Init
|