2022-08-17 09:28:22 -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'
|
2022-08-17 09:28:22 -05:00
|
|
|
import FormControl from '@mui/material/FormControl'
|
2022-08-14 21:26:50 -05:00
|
|
|
import Typography from '@mui/material/Typography'
|
2022-08-17 09:28:22 -05:00
|
|
|
import TextField from '@mui/material/TextField'
|
2022-08-09 22:14:59 -05:00
|
|
|
|
2022-08-16 09:40:01 -05:00
|
|
|
import { usePeerRoom, usePeerRoomAction, PeerActions } from 'hooks/usePeerRoom'
|
2022-08-13 12:11:59 -05:00
|
|
|
|
2022-08-17 09:10:36 -05:00
|
|
|
export function Room() {
|
|
|
|
const { roomId = '' } = useParams()
|
|
|
|
|
2022-08-17 09:28:22 -05:00
|
|
|
const [message, setMessage] = useState('')
|
|
|
|
|
2022-08-17 09:10:36 -05:00
|
|
|
const peerRoom = usePeerRoom({
|
|
|
|
appId: `${encodeURI(window.location.origin)}_${process.env.REACT_APP_NAME}`,
|
|
|
|
roomId,
|
|
|
|
})
|
2022-08-13 12:11:59 -05:00
|
|
|
|
2022-08-16 09:40:01 -05:00
|
|
|
const [sendMessage, receiveMessage] = usePeerRoomAction<string>(
|
|
|
|
peerRoom,
|
|
|
|
PeerActions.MESSAGE
|
2022-08-14 21:26:50 -05:00
|
|
|
)
|
|
|
|
|
2022-08-17 09:28:22 -05:00
|
|
|
const handleMessageChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
const { value } = event.target
|
|
|
|
setMessage(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleMessageSubmit = (
|
|
|
|
event: React.SyntheticEvent<HTMLFormElement>
|
|
|
|
) => {
|
|
|
|
event.preventDefault()
|
|
|
|
sendMessage(message)
|
|
|
|
setMessage('')
|
|
|
|
}
|
|
|
|
|
2022-08-14 21:26:50 -05:00
|
|
|
receiveMessage(message => {
|
|
|
|
console.log(message)
|
|
|
|
})
|
|
|
|
|
|
|
|
return (
|
2022-08-17 09:28:22 -05:00
|
|
|
<div className="p-4">
|
2022-08-14 21:26:50 -05:00
|
|
|
<Typography>Room ID: {roomId}</Typography>
|
|
|
|
<Typography>
|
|
|
|
Open this page in another tab and open the console.
|
|
|
|
</Typography>
|
2022-08-17 09:28:22 -05:00
|
|
|
<form onSubmit={handleMessageSubmit} className="max-w-xl mt-8">
|
|
|
|
<FormControl fullWidth>
|
|
|
|
<TextField
|
|
|
|
label="Your message"
|
|
|
|
variant="outlined"
|
|
|
|
value={message}
|
|
|
|
onChange={handleMessageChange}
|
|
|
|
size="medium"
|
|
|
|
/>
|
|
|
|
</FormControl>
|
|
|
|
<Button
|
|
|
|
variant="contained"
|
|
|
|
type="submit"
|
|
|
|
disabled={message.length === 0}
|
|
|
|
sx={{
|
|
|
|
marginTop: 2,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Send
|
|
|
|
</Button>
|
|
|
|
</form>
|
2022-08-14 21:26:50 -05:00
|
|
|
</div>
|
|
|
|
)
|
2022-08-09 22:14:59 -05:00
|
|
|
}
|