feat: display received messages

This commit is contained in:
Jeremy Kahn 2022-08-18 09:14:13 -05:00
parent be160cf29b
commit 85c4f24bd6
4 changed files with 34 additions and 13 deletions

View File

@ -5,38 +5,46 @@ import FormControl from '@mui/material/FormControl'
import Typography from '@mui/material/Typography' import Typography from '@mui/material/Typography'
import TextField from '@mui/material/TextField' import TextField from '@mui/material/TextField'
import { usePeerRoom, usePeerRoomAction, PeerActions } from 'hooks/usePeerRoom' import { usePeerRoom, usePeerRoomAction } from 'hooks/usePeerRoom'
import { PeerActions } from 'models/network'
import { UnsentMessage, ReceivedMessage } from 'models/chat'
export function Room() { export function Room() {
const { roomId = '' } = useParams() const { roomId = '' } = useParams()
const [message, setMessage] = useState('') const [textMessage, setTextMessage] = useState('')
const [receivedMessages, setReceivedMessages] = useState<ReceivedMessage[]>(
[]
)
const peerRoom = usePeerRoom({ const peerRoom = usePeerRoom({
appId: `${encodeURI(window.location.origin)}_${process.env.REACT_APP_NAME}`, appId: `${encodeURI(window.location.origin)}_${process.env.REACT_APP_NAME}`,
roomId, roomId,
}) })
const [sendMessage, receiveMessage] = usePeerRoomAction<string>( const [sendMessage, receiveMessage] = usePeerRoomAction<UnsentMessage>(
peerRoom, peerRoom,
PeerActions.MESSAGE PeerActions.MESSAGE
) )
const handleMessageChange = (event: React.ChangeEvent<HTMLInputElement>) => { const handleMessageChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const { value } = event.target const { value } = event.target
setMessage(value) setTextMessage(value)
} }
const handleMessageSubmit = ( const handleMessageSubmit = (
event: React.SyntheticEvent<HTMLFormElement> event: React.SyntheticEvent<HTMLFormElement>
) => { ) => {
event.preventDefault() event.preventDefault()
sendMessage(message) sendMessage({ text: textMessage, timeSent: Date.now() })
setMessage('') setTextMessage('')
} }
receiveMessage(message => { receiveMessage(message => {
console.log(message) setReceivedMessages([
...receivedMessages,
{ ...message, timeReceived: Date.now() },
])
}) })
return ( return (
@ -50,7 +58,7 @@ export function Room() {
<TextField <TextField
label="Your message" label="Your message"
variant="outlined" variant="outlined"
value={message} value={textMessage}
onChange={handleMessageChange} onChange={handleMessageChange}
size="medium" size="medium"
/> />
@ -58,7 +66,7 @@ export function Room() {
<Button <Button
variant="contained" variant="contained"
type="submit" type="submit"
disabled={message.length === 0} disabled={textMessage.length === 0}
sx={{ sx={{
marginTop: 2, marginTop: 2,
}} }}
@ -66,6 +74,11 @@ export function Room() {
Send Send
</Button> </Button>
</form> </form>
<div>
{receivedMessages.map((message, idx) => (
<Typography key={`${idx}_${message}`}>{message.text}</Typography>
))}
</div>
</div> </div>
) )
} }

View File

@ -1,10 +1,7 @@
import { useState } from 'react' import { useState } from 'react'
import { PeerRoom } from 'services/PeerRoom' import { PeerRoom } from 'services/PeerRoom'
import { PeerActions } from 'models/network'
export enum PeerActions {
MESSAGE = 'MESSAGE',
}
export function usePeerRoomAction<T>(peerRoom: PeerRoom, action: PeerActions) { export function usePeerRoomAction<T>(peerRoom: PeerRoom, action: PeerActions) {
const [peerRoomAction] = useState(() => peerRoom.makeAction<T>(action)) const [peerRoomAction] = useState(() => peerRoom.makeAction<T>(action))

8
src/models/chat.ts Normal file
View File

@ -0,0 +1,8 @@
export interface UnsentMessage {
text: string
timeSent: number
}
export interface ReceivedMessage extends UnsentMessage {
timeReceived: number
}

3
src/models/network.ts Normal file
View File

@ -0,0 +1,3 @@
export enum PeerActions {
MESSAGE = 'MESSAGE',
}