refactor: simplify index.tsx

This commit is contained in:
Jeremy Kahn 2022-08-31 22:05:24 -05:00
parent 0468977072
commit fb2a5144a2
4 changed files with 118 additions and 126 deletions

View File

@ -1,5 +1,4 @@
import { act, render } from '@testing-library/react'
import { MemoryRouter as Router } from 'react-router-dom'
import localforage from 'localforage'
import { PersistedStorageKeys } from 'models/storage'
@ -26,12 +25,10 @@ const renderBootstrap = async (overrides: BootstrapProps = {}) => {
})
render(
<Router>
<Bootstrap
persistedStorage={mockPersistedStorage as any as typeof localforage}
{...overrides}
/>
</Router>
<Bootstrap
persistedStorage={mockPersistedStorage as any as typeof localforage}
{...overrides}
/>
)
// https://kentcdodds.com/blog/fix-the-not-wrapped-in-act-warning#an-alternative-waiting-for-the-mocked-promise

View File

@ -1,5 +1,5 @@
import { useEffect, useState } from 'react'
import { Routes, Route } from 'react-router-dom'
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom'
import { v4 as uuid } from 'uuid'
import localforage from 'localforage'
@ -48,21 +48,23 @@ function Bootstrap({
}, [hasLoadedSettings, persistedStorage, settings, userId])
return (
<Shell>
{hasLoadedSettings ? (
<Routes>
{['/', '/index.html'].map(path => (
<Route key={path} path={path} element={<Home />} />
))}
<Route
path="/public/:roomId"
element={<PublicRoom userId={userId} />}
/>
</Routes>
) : (
<></>
)}
</Shell>
<Router>
<Shell>
{hasLoadedSettings ? (
<Routes>
{['/', '/index.html'].map(path => (
<Route key={path} path={path} element={<Home />} />
))}
<Route
path="/public/:roomId"
element={<PublicRoom userId={userId} />}
/>
</Routes>
) : (
<></>
)}
</Shell>
</Router>
)
}

View File

@ -11,6 +11,7 @@ import { Link } from 'react-router-dom'
import CssBaseline from '@mui/material/CssBaseline'
import { styled, useTheme } from '@mui/material/styles'
import MuiAppBar, { AppBarProps as MuiAppBarProps } from '@mui/material/AppBar'
import { ThemeProvider, createTheme } from '@mui/material/styles'
import Drawer from '@mui/material/Drawer'
import Toolbar from '@mui/material/Toolbar'
import Box from '@mui/material/Box'
@ -94,6 +95,12 @@ const DrawerHeader = styled('div')(({ theme }) => ({
justifyContent: 'flex-end',
}))
const darkTheme = createTheme({
palette: {
mode: 'dark',
},
})
export const Shell = ({ children }: ShellProps) => {
const theme = useTheme()
const [isAlertShowing, setIsAlertShowing] = useState(false)
@ -141,95 +148,97 @@ export const Shell = ({ children }: ShellProps) => {
return (
<ShellContext.Provider value={shellContextValue}>
<CssBaseline />
<Box
className="Chitchatter"
sx={{
height: '100vh',
display: 'flex',
paddingTop: 8,
}}
>
<Snackbar
open={isAlertShowing}
autoHideDuration={6000}
onClose={handleAlertClose}
anchorOrigin={{ vertical: 'top', horizontal: 'center' }}
>
<Alert
onClose={handleAlertClose}
severity={alertSeverity}
variant="standard"
>
{alertText}
</Alert>
</Snackbar>
<AppBar position="fixed" open={isDrawerOpen}>
<Toolbar
variant="regular"
sx={{
display: 'flex',
flexDirection: 'row',
justifyContent: 'space-between',
}}
>
<IconButton
size="large"
edge="start"
color="inherit"
aria-label="menu"
sx={{ mr: 2, ...(isDrawerOpen && { display: 'none' }) }}
onClick={handleDrawerOpen}
>
<MenuIcon />
</IconButton>
<Typography variant="h6" noWrap component="div">
{title}
</Typography>
<Tooltip title="Number of peers in the room">
<StepIcon icon={numberOfPeers} sx={{ marginLeft: 'auto' }} />
</Tooltip>
</Toolbar>
</AppBar>
<Drawer
<ThemeProvider theme={darkTheme}>
<CssBaseline />
<Box
className="Chitchatter"
sx={{
width: drawerWidth,
flexShrink: 0,
'& .MuiDrawer-paper': {
width: drawerWidth,
boxSizing: 'border-box',
},
height: '100vh',
display: 'flex',
paddingTop: 8,
}}
variant="persistent"
anchor="left"
open={isDrawerOpen}
>
<DrawerHeader>
<IconButton onClick={handleDrawerClose}>
{theme.direction === 'ltr' ? (
<ChevronLeftIcon />
) : (
<ChevronRightIcon />
)}
</IconButton>
</DrawerHeader>
<Divider />
<List>
<Link to="/">
<ListItem disablePadding>
<ListItemButton>
<ListItemIcon>
<Home />
</ListItemIcon>
<ListItemText primary="Home" />
</ListItemButton>
</ListItem>
</Link>
</List>
<Divider />
</Drawer>
<Main open={isDrawerOpen}>{children}</Main>
</Box>
<Snackbar
open={isAlertShowing}
autoHideDuration={6000}
onClose={handleAlertClose}
anchorOrigin={{ vertical: 'top', horizontal: 'center' }}
>
<Alert
onClose={handleAlertClose}
severity={alertSeverity}
variant="standard"
>
{alertText}
</Alert>
</Snackbar>
<AppBar position="fixed" open={isDrawerOpen}>
<Toolbar
variant="regular"
sx={{
display: 'flex',
flexDirection: 'row',
justifyContent: 'space-between',
}}
>
<IconButton
size="large"
edge="start"
color="inherit"
aria-label="menu"
sx={{ mr: 2, ...(isDrawerOpen && { display: 'none' }) }}
onClick={handleDrawerOpen}
>
<MenuIcon />
</IconButton>
<Typography variant="h6" noWrap component="div">
{title}
</Typography>
<Tooltip title="Number of peers in the room">
<StepIcon icon={numberOfPeers} sx={{ marginLeft: 'auto' }} />
</Tooltip>
</Toolbar>
</AppBar>
<Drawer
sx={{
width: drawerWidth,
flexShrink: 0,
'& .MuiDrawer-paper': {
width: drawerWidth,
boxSizing: 'border-box',
},
}}
variant="persistent"
anchor="left"
open={isDrawerOpen}
>
<DrawerHeader>
<IconButton onClick={handleDrawerClose}>
{theme.direction === 'ltr' ? (
<ChevronLeftIcon />
) : (
<ChevronRightIcon />
)}
</IconButton>
</DrawerHeader>
<Divider />
<List>
<Link to="/">
<ListItem disablePadding>
<ListItemButton>
<ListItemIcon>
<Home />
</ListItemIcon>
<ListItemText primary="Home" />
</ListItemButton>
</ListItem>
</Link>
</List>
<Divider />
</Drawer>
<Main open={isDrawerOpen}>{children}</Main>
</Box>
</ThemeProvider>
</ShellContext.Provider>
)
}

View File

@ -1,7 +1,4 @@
import ReactDOM from 'react-dom/client'
import { BrowserRouter as Router } from 'react-router-dom'
import { ThemeProvider, createTheme } from '@mui/material/styles'
import CssBaseline from '@mui/material/CssBaseline'
import 'typeface-roboto'
import * as serviceWorkerRegistration from 'serviceWorkerRegistration'
@ -9,21 +6,8 @@ import 'index.sass'
import Bootstrap from 'Bootstrap'
import reportWebVitals from 'reportWebVitals'
const darkTheme = createTheme({
palette: {
mode: 'dark',
},
})
const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement)
root.render(
<ThemeProvider theme={darkTheme}>
<CssBaseline />
<Router>
<Bootstrap />
</Router>
</ThemeProvider>
)
root.render(<Bootstrap />)
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))