remnantchat/src/Bootstrap.tsx

68 lines
1.8 KiB
TypeScript
Raw Normal View History

2022-08-20 16:52:31 -05:00
import { useEffect, useState } from 'react'
2022-08-09 21:28:46 -05:00
import { Routes, Route } from 'react-router-dom'
2022-08-20 14:20:51 -05:00
import { v4 as uuid } from 'uuid'
2022-08-20 16:52:31 -05:00
import localforage from 'localforage'
2022-08-27 21:48:22 -05:00
import Box from '@mui/material/Box'
2022-08-09 21:28:46 -05:00
2022-08-20 16:52:31 -05:00
import { Home } from 'pages/Home/'
import { PublicRoom } from 'pages/PublicRoom/'
import { UserSettings } from 'models/settings'
import { PersistedStorageKeys } from 'models/storage'
2022-08-09 09:35:49 -05:00
2022-08-20 16:52:31 -05:00
export interface BootstrapProps {
persistedStorage?: typeof localforage
getUuid?: typeof uuid
}
function Bootstrap({
persistedStorage = localforage.createInstance({
name: 'chitchatter',
description: 'Persisted settings data for chitchatter',
}),
getUuid = uuid,
}: BootstrapProps) {
const [hasLoadedSettings, setHasLoadedSettings] = useState(false)
const [settings, setSettings] = useState({ userId: getUuid() })
const { userId } = settings
useEffect(() => {
;(async () => {
if (hasLoadedSettings) return
const persistedUserSettings =
await persistedStorage.getItem<UserSettings>(
PersistedStorageKeys.USER_SETTINGS
)
if (persistedUserSettings) {
setSettings(persistedUserSettings)
} else {
await persistedStorage.setItem(
PersistedStorageKeys.USER_SETTINGS,
settings
)
}
setHasLoadedSettings(true)
})()
}, [hasLoadedSettings, persistedStorage, settings, userId])
2022-08-20 14:20:51 -05:00
2022-08-09 21:28:46 -05:00
return (
2022-08-28 18:32:34 -05:00
<Box className="Chitchatter" sx={{ height: '100vh' }}>
2022-08-20 16:52:31 -05:00
{hasLoadedSettings ? (
<Routes>
{['/', '/index.html'].map(path => (
<Route key={path} path={path} element={<Home />} />
))}
<Route
path="/public/:roomId"
element={<PublicRoom userId={userId} />}
/>
</Routes>
) : null}
2022-08-27 21:48:22 -05:00
</Box>
2022-08-09 21:28:46 -05:00
)
2022-08-09 09:35:49 -05:00
}
export default Bootstrap