remnantchat/src/Bootstrap.tsx

163 lines
4.4 KiB
TypeScript
Raw Normal View History

import {
forwardRef,
useCallback,
useEffect,
useMemo,
useState,
SyntheticEvent,
} 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-28 21:25:49 -05:00
import AppBar from '@mui/material/AppBar'
import Toolbar from '@mui/material/Toolbar'
2022-08-27 21:48:22 -05:00
import Box from '@mui/material/Box'
2022-08-28 21:25:49 -05:00
import Typography from '@mui/material/Typography'
2022-08-29 22:05:56 -05:00
import StepIcon from '@mui/material/StepIcon'
import Tooltip from '@mui/material/Tooltip'
import Snackbar from '@mui/material/Snackbar'
import MuiAlert, { AlertProps, AlertColor } from '@mui/material/Alert'
2022-08-09 21:28:46 -05:00
2022-08-28 21:25:49 -05:00
import { ShellContext } from 'ShellContext'
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'
import { AlertOptions } from 'models/shell'
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
}
const Alert = forwardRef<HTMLDivElement, AlertProps>(function Alert(
props,
ref
) {
return <MuiAlert elevation={6} ref={ref} variant="filled" {...props} />
})
2022-08-20 16:52:31 -05:00
function Bootstrap({
persistedStorage = localforage.createInstance({
name: 'chitchatter',
description: 'Persisted settings data for chitchatter',
}),
getUuid = uuid,
}: BootstrapProps) {
const [hasLoadedSettings, setHasLoadedSettings] = useState(false)
const [isAlertShowing, setIsAlertShowing] = useState(false)
const [alertSeverity, setAlertSeverity] = useState<AlertColor>('info')
2022-08-20 16:52:31 -05:00
const [settings, setSettings] = useState({ userId: getUuid() })
const { userId } = settings
2022-08-28 21:25:49 -05:00
const [title, setTitle] = useState('')
const [alertText, setAlertText] = useState('')
2022-08-29 22:05:56 -05:00
const [numberOfPeers, setNumberOfPeers] = useState(1)
2022-08-28 21:25:49 -05:00
const showAlert = useCallback<
(message: string, options?: AlertOptions) => void
>((message, options) => {
setAlertText(message)
setAlertSeverity(options?.severity ?? 'info')
setIsAlertShowing(true)
}, [])
2022-08-29 22:05:56 -05:00
const shellContextValue = useMemo(
() => ({ numberOfPeers, setNumberOfPeers, setTitle, showAlert }),
[numberOfPeers, setNumberOfPeers, setTitle, showAlert]
2022-08-29 22:05:56 -05:00
)
2022-08-20 16:52:31 -05:00
const handleAlertClose = (
_event?: SyntheticEvent | Event,
reason?: string
) => {
if (reason === 'clickaway') {
return
}
setIsAlertShowing(false)
}
2022-08-20 16:52:31 -05:00
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-31 19:18:14 -05:00
useEffect(() => {
document.title = title
}, [title])
2022-08-09 21:28:46 -05:00
return (
2022-08-28 21:25:49 -05:00
<ShellContext.Provider value={shellContextValue}>
<Box
className="Chitchatter"
2022-08-29 22:20:04 -05:00
sx={{
height: '100vh',
display: 'flex',
flexDirection: 'column',
2022-08-31 19:26:38 -05:00
paddingTop: 8,
2022-08-29 22:20:04 -05:00
}}
2022-08-28 21:25:49 -05:00
>
<Snackbar
open={isAlertShowing}
autoHideDuration={6000}
onClose={handleAlertClose}
anchorOrigin={{ vertical: 'top', horizontal: 'center' }}
>
<Alert
onClose={handleAlertClose}
severity={alertSeverity}
variant="standard"
>
{alertText}
</Alert>
</Snackbar>
2022-08-29 22:20:04 -05:00
<AppBar position="fixed">
2022-08-29 22:05:56 -05:00
<Toolbar
variant="regular"
sx={{
display: 'flex',
flexDirection: 'row',
justifyContent: 'space-between',
}}
>
2022-08-31 19:26:38 -05:00
<Typography variant="h6">{title}</Typography>
2022-08-29 22:05:56 -05:00
<Tooltip title="Number of peers in the room">
<StepIcon icon={numberOfPeers} sx={{ marginLeft: 'auto' }} />
</Tooltip>
2022-08-28 21:25:49 -05:00
</Toolbar>
</AppBar>
{hasLoadedSettings ? (
<Routes>
{['/', '/index.html'].map(path => (
<Route key={path} path={path} element={<Home />} />
))}
<Route
path="/public/:roomId"
element={<PublicRoom userId={userId} />}
/>
</Routes>
) : null}
</Box>
</ShellContext.Provider>
2022-08-09 21:28:46 -05:00
)
2022-08-09 09:35:49 -05:00
}
export default Bootstrap