2022-08-15 21:38:56 -05:00
|
|
|
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'
|
2022-08-15 21:38:56 -05:00
|
|
|
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',
|
|
|
|
}
|
|
|
|
|
2022-08-15 21:38:56 -05:00
|
|
|
interface RoomProps {
|
|
|
|
peerRoom: PeerRoom
|
|
|
|
roomId: string
|
|
|
|
}
|
2022-08-13 12:11:59 -05:00
|
|
|
|
2022-08-15 21:38:56 -05:00
|
|
|
function Room({ peerRoom, roomId }: RoomProps) {
|
|
|
|
const { makeAction } = peerRoom
|
2022-08-13 12:11:59 -05:00
|
|
|
|
2022-08-15 21:38:56 -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
|
|
|
}
|
2022-08-15 21:38:56 -05:00
|
|
|
|
|
|
|
function RoomLoader() {
|
|
|
|
const { roomId = '' } = useParams()
|
|
|
|
|
|
|
|
const peerRoom = usePeerRoom({
|
2022-08-15 21:47:45 -05:00
|
|
|
appId: `${encodeURI(window.location.origin)}_${process.env.REACT_APP_NAME}`,
|
2022-08-15 21:38:56 -05:00
|
|
|
roomId,
|
|
|
|
})
|
|
|
|
|
|
|
|
if (peerRoom) {
|
|
|
|
return <Room peerRoom={peerRoom} roomId={roomId} />
|
|
|
|
} else {
|
|
|
|
return <>Loading...</>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export { RoomLoader as Room }
|