From 9b5551a4bd44ee4903021402961e5100b3ec5892 Mon Sep 17 00:00:00 2001 From: Jeremy Kahn Date: Thu, 15 Sep 2022 21:16:41 -0500 Subject: [PATCH] refactor: move drawer to its own component file --- src/components/Shell/Drawer.tsx | 116 +++++++++++++++++++++++++ src/components/Shell/DrawerHeader.tsx | 10 +++ src/components/Shell/Shell.tsx | 117 ++++---------------------- 3 files changed, 142 insertions(+), 101 deletions(-) create mode 100644 src/components/Shell/Drawer.tsx create mode 100644 src/components/Shell/DrawerHeader.tsx diff --git a/src/components/Shell/Drawer.tsx b/src/components/Shell/Drawer.tsx new file mode 100644 index 0000000..d8ebccc --- /dev/null +++ b/src/components/Shell/Drawer.tsx @@ -0,0 +1,116 @@ +import { PropsWithChildren, useContext } from 'react' +import { Link } from 'react-router-dom' +import { Theme } from '@mui/material/styles' +import MuiDrawer from '@mui/material/Drawer' +import List from '@mui/material/List' +import Typography from '@mui/material/Typography' +import Divider from '@mui/material/Divider' +import IconButton from '@mui/material/IconButton' +import ChevronLeftIcon from '@mui/icons-material/ChevronLeft' +import ChevronRightIcon from '@mui/icons-material/ChevronRight' +import ListItem from '@mui/material/ListItem' +import ListItemButton from '@mui/material/ListItemButton' +import ListItemIcon from '@mui/material/ListItemIcon' +import ListItemText from '@mui/material/ListItemText' +import Home from '@mui/icons-material/Home' +import Brightness4Icon from '@mui/icons-material/Brightness4' +import Brightness7Icon from '@mui/icons-material/Brightness7' + +import { SettingsContext } from 'contexts/SettingsContext' +import { PeerNameDisplay } from 'components/PeerNameDisplay' + +import { DrawerHeader } from './DrawerHeader' + +export const drawerWidth = 240 + +export interface DrawerProps extends PropsWithChildren { + isDrawerOpen: boolean + onDrawerClose: () => void + onHomeLinkClick: () => void + theme: Theme + userPeerId: string +} + +export const Drawer = ({ + isDrawerOpen, + onDrawerClose, + onHomeLinkClick, + theme, + userPeerId, +}: DrawerProps) => { + const settingsContext = useContext(SettingsContext) + const colorMode = settingsContext.getUserSettings().colorMode + + const handleColorModeToggleClick = () => { + const newMode = colorMode === 'light' ? 'dark' : 'light' + settingsContext.updateUserSettings({ colorMode: newMode }) + } + + return ( + + + + {theme.direction === 'ltr' ? ( + + ) : ( + + )} + + + + + + Your user name:{' '} + + {userPeerId} + + + } + /> + + + + + + + + + + + + + + + + + {theme.palette.mode === 'dark' ? ( + + ) : ( + + )} + + + + + + + + ) +} diff --git a/src/components/Shell/DrawerHeader.tsx b/src/components/Shell/DrawerHeader.tsx new file mode 100644 index 0000000..15ab767 --- /dev/null +++ b/src/components/Shell/DrawerHeader.tsx @@ -0,0 +1,10 @@ +import { styled } from '@mui/material/styles' + +export const DrawerHeader = styled('div')(({ theme }) => ({ + display: 'flex', + alignItems: 'center', + padding: theme.spacing(0, 1), + // necessary for content to be below app bar + ...theme.mixins.toolbar, + justifyContent: 'flex-end', +})) diff --git a/src/components/Shell/Shell.tsx b/src/components/Shell/Shell.tsx index c61d6ea..6fa8178 100644 --- a/src/components/Shell/Shell.tsx +++ b/src/components/Shell/Shell.tsx @@ -8,16 +8,12 @@ import { useMemo, useState, } from 'react' -import { Link } from 'react-router-dom' import CssBaseline from '@mui/material/CssBaseline' import MuiAppBar, { AppBarProps as MuiAppBarProps } from '@mui/material/AppBar' import { ThemeProvider, createTheme, styled } from '@mui/material/styles' -import Drawer from '@mui/material/Drawer' import Toolbar from '@mui/material/Toolbar' import Box from '@mui/material/Box' -import List from '@mui/material/List' import Typography from '@mui/material/Typography' -import Divider from '@mui/material/Divider' import StepIcon from '@mui/material/StepIcon' import Tooltip from '@mui/material/Tooltip' import Snackbar from '@mui/material/Snackbar' @@ -31,23 +27,14 @@ import DialogContentText from '@mui/material/DialogContentText' import DialogTitle from '@mui/material/DialogTitle' import MenuIcon from '@mui/icons-material/Menu' import WarningIcon from '@mui/icons-material/Warning' -import ChevronLeftIcon from '@mui/icons-material/ChevronLeft' -import ChevronRightIcon from '@mui/icons-material/ChevronRight' -import ListItem from '@mui/material/ListItem' -import ListItemButton from '@mui/material/ListItemButton' -import ListItemIcon from '@mui/material/ListItemIcon' -import ListItemText from '@mui/material/ListItemText' -import Home from '@mui/icons-material/Home' -import Brightness4Icon from '@mui/icons-material/Brightness4' -import Brightness7Icon from '@mui/icons-material/Brightness7' import LinkIcon from '@mui/icons-material/Link' import { ShellContext } from 'contexts/ShellContext' import { SettingsContext } from 'contexts/SettingsContext' import { AlertOptions } from 'models/shell' -import { PeerNameDisplay } from 'components/PeerNameDisplay' -const drawerWidth = 240 +import { Drawer, drawerWidth } from './Drawer' +import { DrawerHeader } from './DrawerHeader' const Alert = forwardRef(function Alert( props, @@ -94,15 +81,6 @@ const AppBar = styled(MuiAppBar, { }), })) -const DrawerHeader = styled('div')(({ theme }) => ({ - display: 'flex', - alignItems: 'center', - padding: theme.spacing(0, 1), - // necessary for content to be below app bar - ...theme.mixins.toolbar, - justifyContent: 'flex-end', -})) - export interface ShellProps extends PropsWithChildren { userPeerId: string appNeedsUpdate: boolean @@ -139,11 +117,6 @@ export const Shell = ({ appNeedsUpdate, children, userPeerId }: ShellProps) => { const colorMode = settingsContext.getUserSettings().colorMode - const handleColorModeToggleClick = () => { - const newMode = colorMode === 'light' ? 'dark' : 'light' - settingsContext.updateUserSettings({ colorMode: newMode }) - } - const theme = useMemo( () => createTheme({ @@ -173,14 +146,6 @@ export const Shell = ({ appNeedsUpdate, children, userPeerId }: ShellProps) => { setIsDrawerOpen(true) } - const handleDrawerClose = () => { - setIsDrawerOpen(false) - } - - const handleHomeLinkClick = () => { - setIsDrawerOpen(false) - } - const handleLinkButtonClick = async () => { await navigator.clipboard.writeText(window.location.href) @@ -193,6 +158,14 @@ export const Shell = ({ appNeedsUpdate, children, userPeerId }: ShellProps) => { window.location.reload() } + const handleDrawerClose = () => { + setIsDrawerOpen(false) + } + + const handleHomeLinkClick = () => { + setIsDrawerOpen(false) + } + return ( @@ -295,70 +268,12 @@ export const Shell = ({ appNeedsUpdate, children, userPeerId }: ShellProps) => { - - - {theme.direction === 'ltr' ? ( - - ) : ( - - )} - - - - - - Your user name:{' '} - - {userPeerId} - - - } - /> - - - - - - - - - - - - - - - - - {theme.palette.mode === 'dark' ? ( - - ) : ( - - )} - - - - - - - + isDrawerOpen={isDrawerOpen} + onDrawerClose={handleDrawerClose} + onHomeLinkClick={handleHomeLinkClick} + theme={theme} + userPeerId={userPeerId} + />