diff --git a/src/Bootstrap.tsx b/src/Bootstrap.tsx index 77f1737..df80149 100644 --- a/src/Bootstrap.tsx +++ b/src/Bootstrap.tsx @@ -1,42 +1,19 @@ -import { - forwardRef, - useCallback, - useEffect, - useMemo, - useState, - SyntheticEvent, -} from 'react' +import { useEffect, useState } from 'react' import { Routes, Route } from 'react-router-dom' import { v4 as uuid } from 'uuid' import localforage from 'localforage' -import AppBar from '@mui/material/AppBar' -import Toolbar from '@mui/material/Toolbar' -import Box from '@mui/material/Box' -import Typography from '@mui/material/Typography' -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' -import { ShellContext } from 'ShellContext' 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' +import { Shell } from 'components/Shell' export interface BootstrapProps { persistedStorage?: typeof localforage getUuid?: typeof uuid } -const Alert = forwardRef(function Alert( - props, - ref -) { - return -}) - function Bootstrap({ persistedStorage = localforage.createInstance({ name: 'chitchatter', @@ -45,37 +22,8 @@ function Bootstrap({ getUuid = uuid, }: BootstrapProps) { const [hasLoadedSettings, setHasLoadedSettings] = useState(false) - const [isAlertShowing, setIsAlertShowing] = useState(false) - const [alertSeverity, setAlertSeverity] = useState('info') const [settings, setSettings] = useState({ userId: getUuid() }) const { userId } = settings - const [title, setTitle] = useState('') - const [alertText, setAlertText] = useState('') - const [numberOfPeers, setNumberOfPeers] = useState(1) - - const showAlert = useCallback< - (message: string, options?: AlertOptions) => void - >((message, options) => { - setAlertText(message) - setAlertSeverity(options?.severity ?? 'info') - setIsAlertShowing(true) - }, []) - - const shellContextValue = useMemo( - () => ({ numberOfPeers, setNumberOfPeers, setTitle, showAlert }), - [numberOfPeers, setNumberOfPeers, setTitle, showAlert] - ) - - const handleAlertClose = ( - _event?: SyntheticEvent | Event, - reason?: string - ) => { - if (reason === 'clickaway') { - return - } - - setIsAlertShowing(false) - } useEffect(() => { ;(async () => { @@ -99,63 +47,22 @@ function Bootstrap({ })() }, [hasLoadedSettings, persistedStorage, settings, userId]) - useEffect(() => { - document.title = title - }, [title]) - return ( - - - - - {alertText} - - - - - {title} - - - - - - {hasLoadedSettings ? ( - - {['/', '/index.html'].map(path => ( - } /> - ))} - } - /> - - ) : null} - - + + {hasLoadedSettings ? ( + + {['/', '/index.html'].map(path => ( + } /> + ))} + } + /> + + ) : ( + <> + )} + ) } diff --git a/src/components/Shell/Shell.tsx b/src/components/Shell/Shell.tsx new file mode 100644 index 0000000..4be8c3a --- /dev/null +++ b/src/components/Shell/Shell.tsx @@ -0,0 +1,110 @@ +import { + forwardRef, + PropsWithChildren, + useCallback, + useEffect, + useMemo, + useState, + SyntheticEvent, +} from 'react' +import AppBar from '@mui/material/AppBar' +import Toolbar from '@mui/material/Toolbar' +import Box from '@mui/material/Box' +import Typography from '@mui/material/Typography' +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' + +import { ShellContext } from 'ShellContext' +import { AlertOptions } from 'models/shell' + +interface ShellProps extends PropsWithChildren {} + +const Alert = forwardRef(function Alert( + props, + ref +) { + return +}) + +export const Shell = ({ children }: ShellProps) => { + const [isAlertShowing, setIsAlertShowing] = useState(false) + const [alertSeverity, setAlertSeverity] = useState('info') + const [title, setTitle] = useState('') + const [alertText, setAlertText] = useState('') + const [numberOfPeers, setNumberOfPeers] = useState(1) + + const showAlert = useCallback< + (message: string, options?: AlertOptions) => void + >((message, options) => { + setAlertText(message) + setAlertSeverity(options?.severity ?? 'info') + setIsAlertShowing(true) + }, []) + + const shellContextValue = useMemo( + () => ({ numberOfPeers, setNumberOfPeers, setTitle, showAlert }), + [numberOfPeers, setNumberOfPeers, setTitle, showAlert] + ) + + const handleAlertClose = ( + _event?: SyntheticEvent | Event, + reason?: string + ) => { + if (reason === 'clickaway') { + return + } + + setIsAlertShowing(false) + } + + useEffect(() => { + document.title = title + }, [title]) + + return ( + + + + + {alertText} + + + + + {title} + + + + + + {children} + + + ) +} diff --git a/src/components/Shell/index.ts b/src/components/Shell/index.ts new file mode 100644 index 0000000..fd52c40 --- /dev/null +++ b/src/components/Shell/index.ts @@ -0,0 +1 @@ +export * from './Shell'