99 lines
2.5 KiB
TypeScript
Raw Normal View History

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-19 09:42:14 -05:00
import { v4 as uuid } from 'uuid'
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-18 09:14:13 -05:00
import { usePeerRoom, usePeerRoomAction } from 'hooks/usePeerRoom'
import { PeerActions } from 'models/network'
import { UnsentMessage, ReceivedMessage } from 'models/chat'
2022-08-13 12:11:59 -05:00
2022-08-18 21:10:16 -05:00
export interface RoomProps {
appId?: string
2022-08-19 09:42:14 -05:00
getUuid?: typeof uuid
2022-08-20 14:20:51 -05:00
userId: string
2022-08-18 21:10:16 -05:00
}
export function Room({
2022-08-20 14:20:51 -05:00
userId,
2022-08-18 21:10:16 -05:00
appId = `${encodeURI(window.location.origin)}_${process.env.REACT_APP_NAME}`,
2022-08-19 09:42:14 -05:00
getUuid = uuid,
2022-08-18 21:10:16 -05:00
}: RoomProps) {
2022-08-17 09:10:36 -05:00
const { roomId = '' } = useParams()
2022-08-18 09:14:13 -05:00
const [textMessage, setTextMessage] = useState('')
const [receivedMessages, setReceivedMessages] = useState<ReceivedMessage[]>(
[]
)
2022-08-17 09:28:22 -05:00
2022-08-17 09:10:36 -05:00
const peerRoom = usePeerRoom({
2022-08-18 21:10:16 -05:00
appId,
2022-08-17 09:10:36 -05:00
roomId,
})
2022-08-13 12:11:59 -05:00
2022-08-18 09:14:13 -05:00
const [sendMessage, receiveMessage] = usePeerRoomAction<UnsentMessage>(
2022-08-16 09:40:01 -05:00
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
2022-08-18 09:14:13 -05:00
setTextMessage(value)
2022-08-17 09:28:22 -05:00
}
const handleMessageSubmit = (
event: React.SyntheticEvent<HTMLFormElement>
) => {
event.preventDefault()
2022-08-20 14:20:51 -05:00
sendMessage({
authorId: userId,
text: textMessage,
timeSent: Date.now(),
id: getUuid(),
})
2022-08-18 09:14:13 -05:00
setTextMessage('')
2022-08-17 09:28:22 -05:00
}
2022-08-14 21:26:50 -05:00
receiveMessage(message => {
2022-08-18 09:14:13 -05:00
setReceivedMessages([
...receivedMessages,
{ ...message, timeReceived: Date.now() },
])
2022-08-14 21:26:50 -05:00
})
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>
2022-08-20 14:20:51 -05:00
<Typography>Open this page in another tab.</Typography>
2022-08-17 09:28:22 -05:00
<form onSubmit={handleMessageSubmit} className="max-w-xl mt-8">
<FormControl fullWidth>
<TextField
variant="outlined"
2022-08-18 09:14:13 -05:00
value={textMessage}
2022-08-17 09:28:22 -05:00
onChange={handleMessageChange}
size="medium"
2022-08-18 21:36:13 -05:00
placeholder="Your message"
2022-08-17 09:28:22 -05:00
/>
</FormControl>
<Button
variant="contained"
type="submit"
2022-08-18 09:14:13 -05:00
disabled={textMessage.length === 0}
2022-08-17 09:28:22 -05:00
sx={{
marginTop: 2,
}}
>
Send
</Button>
</form>
2022-08-18 09:14:13 -05:00
<div>
{receivedMessages.map((message, idx) => (
<Typography key={`${idx}_${message}`}>{message.text}</Typography>
))}
</div>
2022-08-14 21:26:50 -05:00
</div>
)
2022-08-09 22:14:59 -05:00
}