63 lines
1.3 KiB
TypeScript
Raw Normal View History

import { useState } from 'react'
2022-08-09 22:14:59 -05:00
import { useParams } from 'react-router-dom'
2022-08-14 21:26:50 -05:00
import Button from '@mui/material/Button'
import Typography from '@mui/material/Typography'
2022-08-09 22:14:59 -05:00
2022-08-14 10:10:05 -05:00
import { usePeerRoom } from '../../hooks/usePeerRoom'
import { PeerRoom } from '../../services/PeerRoom'
2022-08-13 12:11:59 -05:00
2022-08-14 21:26:50 -05:00
enum PeerActions {
MESSAGE = 'MESSAGE',
}
interface RoomProps {
peerRoom: PeerRoom
roomId: string
}
2022-08-13 12:11:59 -05:00
function Room({ peerRoom, roomId }: RoomProps) {
const { makeAction } = peerRoom
2022-08-13 12:11:59 -05:00
const [[sendMessage, receiveMessage]] = useState(() =>
makeAction<string>(PeerActions.MESSAGE)
2022-08-14 21:26:50 -05:00
)
receiveMessage(message => {
console.log(message)
})
return (
<div>
<Typography>Room ID: {roomId}</Typography>
<Typography>
Open this page in another tab and open the console.
</Typography>
<Button
onClick={() => {
sendMessage('Hello!')
}}
variant="contained"
>
Say hi
</Button>
</div>
)
2022-08-09 22:14:59 -05:00
}
function RoomLoader() {
const { roomId = '' } = useParams()
const peerRoom = usePeerRoom({
appId: `${encodeURI(window.location.origin)}_${process.env.REACT_APP_NAME}`,
roomId,
})
if (peerRoom) {
return <Room peerRoom={peerRoom} roomId={roomId} />
} else {
return <>Loading...</>
}
}
export { RoomLoader as Room }