feat: add message sending UI
This commit is contained in:
		
							parent
							
								
									96f2991209
								
							
						
					
					
						commit
						126456eced
					
				| @ -1,12 +1,17 @@ | |||||||
|  | import { useState } from 'react' | ||||||
| import { useParams } from 'react-router-dom' | import { useParams } from 'react-router-dom' | ||||||
| import Button from '@mui/material/Button' | import Button from '@mui/material/Button' | ||||||
|  | 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 { usePeerRoom, usePeerRoomAction, PeerActions } from 'hooks/usePeerRoom' | import { usePeerRoom, usePeerRoomAction, PeerActions } from 'hooks/usePeerRoom' | ||||||
| 
 | 
 | ||||||
| export function Room() { | export function Room() { | ||||||
|   const { roomId = '' } = useParams() |   const { roomId = '' } = useParams() | ||||||
| 
 | 
 | ||||||
|  |   const [message, setMessage] = useState('') | ||||||
|  | 
 | ||||||
|   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, | ||||||
| @ -17,24 +22,50 @@ export function Room() { | |||||||
|     PeerActions.MESSAGE |     PeerActions.MESSAGE | ||||||
|   ) |   ) | ||||||
| 
 | 
 | ||||||
|  |   const handleMessageChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||||||
|  |     const { value } = event.target | ||||||
|  |     setMessage(value) | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   const handleMessageSubmit = ( | ||||||
|  |     event: React.SyntheticEvent<HTMLFormElement> | ||||||
|  |   ) => { | ||||||
|  |     event.preventDefault() | ||||||
|  |     sendMessage(message) | ||||||
|  |     setMessage('') | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   receiveMessage(message => { |   receiveMessage(message => { | ||||||
|     console.log(message) |     console.log(message) | ||||||
|   }) |   }) | ||||||
| 
 | 
 | ||||||
|   return ( |   return ( | ||||||
|     <div> |     <div className="p-4"> | ||||||
|       <Typography>Room ID: {roomId}</Typography> |       <Typography>Room ID: {roomId}</Typography> | ||||||
|       <Typography> |       <Typography> | ||||||
|         Open this page in another tab and open the console. |         Open this page in another tab and open the console. | ||||||
|       </Typography> |       </Typography> | ||||||
|  |       <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 |         <Button | ||||||
|         onClick={() => { |  | ||||||
|           sendMessage('Hello!') |  | ||||||
|         }} |  | ||||||
|           variant="contained" |           variant="contained" | ||||||
|  |           type="submit" | ||||||
|  |           disabled={message.length === 0} | ||||||
|  |           sx={{ | ||||||
|  |             marginTop: 2, | ||||||
|  |           }} | ||||||
|         > |         > | ||||||
|         Say hi |           Send | ||||||
|         </Button> |         </Button> | ||||||
|  |       </form> | ||||||
|     </div> |     </div> | ||||||
|   ) |   ) | ||||||
| } | } | ||||||
|  | |||||||
| @ -25,7 +25,7 @@ export class PeerRoom { | |||||||
| // Memoization isn't just a performance optimization here. It is necessary to
 | // Memoization isn't just a performance optimization here. It is necessary to
 | ||||||
| // prevent subsequent calls to getPeerRoom from causing a room collision due to
 | // prevent subsequent calls to getPeerRoom from causing a room collision due to
 | ||||||
| // the amount of time it takes for Trystero rooms to be torn down (which is an
 | // the amount of time it takes for Trystero rooms to be torn down (which is an
 | ||||||
| // asynchronous operation).
 | // asynchronous operation that cannot be `await`-ed).
 | ||||||
| export const getPeerRoom = memoize((config: RoomConfig, roomId: string) => { | export const getPeerRoom = memoize((config: RoomConfig, roomId: string) => { | ||||||
|   return new PeerRoom(config, roomId) |   return new PeerRoom(config, roomId) | ||||||
| }) | }) | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user