feat: hide and show peer counter

This commit is contained in:
Jeremy Kahn 2022-09-01 21:28:45 -05:00
parent bfa7158fb1
commit 899286369e
3 changed files with 28 additions and 6 deletions

View File

@ -4,6 +4,7 @@ import { AlertOptions } from 'models/shell'
interface ShellContextProps { interface ShellContextProps {
numberOfPeers: number numberOfPeers: number
setDoShowPeers: Dispatch<SetStateAction<boolean>>
setNumberOfPeers: Dispatch<SetStateAction<number>> setNumberOfPeers: Dispatch<SetStateAction<number>>
setTitle: Dispatch<SetStateAction<string>> setTitle: Dispatch<SetStateAction<string>>
showAlert: (message: string, options?: AlertOptions) => void showAlert: (message: string, options?: AlertOptions) => void
@ -11,6 +12,7 @@ interface ShellContextProps {
export const ShellContext = createContext<ShellContextProps>({ export const ShellContext = createContext<ShellContextProps>({
numberOfPeers: 1, numberOfPeers: 1,
setDoShowPeers: () => {},
setNumberOfPeers: () => {}, setNumberOfPeers: () => {},
setTitle: () => {}, setTitle: () => {},
showAlert: () => {}, showAlert: () => {},

View File

@ -67,6 +67,8 @@ export function Room({
) )
useEffect(() => { useEffect(() => {
shellContext.setDoShowPeers(true)
peerRoom.onPeerJoin(() => { peerRoom.onPeerJoin(() => {
shellContext.showAlert(`Someone has joined the room`, { shellContext.showAlert(`Someone has joined the room`, {
severity: 'success', severity: 'success',
@ -86,6 +88,10 @@ export function Room({
setNumberOfPeers(newNumberOfPeers) setNumberOfPeers(newNumberOfPeers)
shellContext.setNumberOfPeers(newNumberOfPeers) shellContext.setNumberOfPeers(newNumberOfPeers)
}) })
return () => {
shellContext.setDoShowPeers(false)
}
}, [numberOfPeers, peerRoom, shellContext]) }, [numberOfPeers, peerRoom, shellContext])
const [sendMessage, receiveMessage] = usePeerRoomAction<UnsentMessage>( const [sendMessage, receiveMessage] = usePeerRoomAction<UnsentMessage>(

View File

@ -105,6 +105,7 @@ export const Shell = ({ children }: ShellProps) => {
const theme = useTheme() const theme = useTheme()
const [isAlertShowing, setIsAlertShowing] = useState(false) const [isAlertShowing, setIsAlertShowing] = useState(false)
const [isDrawerOpen, setIsDrawerOpen] = useState(false) const [isDrawerOpen, setIsDrawerOpen] = useState(false)
const [doShowPeers, setDoShowPeers] = useState(false)
const [alertSeverity, setAlertSeverity] = useState<AlertColor>('info') const [alertSeverity, setAlertSeverity] = useState<AlertColor>('info')
const [title, setTitle] = useState('') const [title, setTitle] = useState('')
const [alertText, setAlertText] = useState('') const [alertText, setAlertText] = useState('')
@ -119,8 +120,14 @@ export const Shell = ({ children }: ShellProps) => {
}, []) }, [])
const shellContextValue = useMemo( const shellContextValue = useMemo(
() => ({ numberOfPeers, setNumberOfPeers, setTitle, showAlert }), () => ({
[numberOfPeers, setNumberOfPeers, setTitle, showAlert] numberOfPeers,
setDoShowPeers,
setNumberOfPeers,
setTitle,
showAlert,
}),
[numberOfPeers, setDoShowPeers, setNumberOfPeers, setTitle, showAlert]
) )
const handleAlertClose = ( const handleAlertClose = (
@ -191,12 +198,19 @@ export const Shell = ({ children }: ShellProps) => {
> >
<MenuIcon /> <MenuIcon />
</IconButton> </IconButton>
<Typography variant="h6" noWrap component="div"> <Typography
variant="h6"
noWrap
component="div"
sx={{ marginRight: 'auto' }}
>
{title} {title}
</Typography> </Typography>
<Tooltip title="Number of peers in the room"> {doShowPeers ? (
<StepIcon icon={numberOfPeers} sx={{ marginLeft: 'auto' }} /> <Tooltip title="Number of peers in the room">
</Tooltip> <StepIcon icon={numberOfPeers} sx={{ marginLeft: 'auto' }} />
</Tooltip>
) : null}
</Toolbar> </Toolbar>
</AppBar> </AppBar>
<Drawer <Drawer