1
1
import { Box , styled , TextField , Typography } from "@mui/material" ;
2
2
import { useEffect , useRef , useState } from "react" ;
3
- import {
4
- CommunicationEvents ,
5
- communicationSocket ,
6
- } from "../../utils/communicationSocket" ;
7
- import { useMatch } from "../../contexts/MatchContext" ;
8
- import {
9
- USE_AUTH_ERROR_MESSAGE ,
10
- USE_MATCH_ERROR_MESSAGE ,
11
- } from "../../utils/constants" ;
12
- import { useAuth } from "../../contexts/AuthContext" ;
3
+ import { CommunicationEvents } from "../../utils/communicationSocket" ;
4
+ import { USE_COLLAB_ERROR_MESSAGE } from "../../utils/constants" ;
13
5
import { toast } from "react-toastify" ;
6
+ import { useCollab } from "../../contexts/CollabContext" ;
14
7
15
8
type Message = {
16
9
from : string ;
@@ -35,32 +28,26 @@ const StyledTypography = styled(Typography)(({ theme }) => ({
35
28
const Chat : React . FC < ChatProps > = ( { isActive, setHasNewMessage } ) => {
36
29
const [ messages , setMessages ] = useState < Message [ ] > ( [ ] ) ;
37
30
const [ inputValue , setInputValue ] = useState ( "" ) ;
38
- const match = useMatch ( ) ;
39
- const auth = useAuth ( ) ;
31
+
40
32
const messagesRef = useRef < HTMLDivElement > ( null ) ;
41
33
const errorHandledRef = useRef ( false ) ;
42
34
43
- if ( ! match ) {
44
- throw new Error ( USE_MATCH_ERROR_MESSAGE ) ;
45
- }
46
-
47
- if ( ! auth ) {
48
- throw new Error ( USE_AUTH_ERROR_MESSAGE ) ;
35
+ const collab = useCollab ( ) ;
36
+ if ( ! collab ) {
37
+ throw new Error ( USE_COLLAB_ERROR_MESSAGE ) ;
49
38
}
50
-
51
- const { getMatchId } = match ;
52
- const { user } = auth ;
39
+ const { communicationSocket, collabUser, roomId } = collab ;
53
40
54
41
useEffect ( ( ) => {
55
42
// join the room automatically when this loads
56
- communicationSocket . open ( ) ;
57
- communicationSocket . emit ( CommunicationEvents . JOIN , {
58
- roomId : getMatchId ( ) ,
59
- username : user ?. username ,
43
+ communicationSocket ? .open ( ) ;
44
+ communicationSocket ? .emit ( CommunicationEvents . JOIN , {
45
+ roomId : roomId ,
46
+ username : collabUser ?. username ,
60
47
} ) ;
61
48
62
49
return ( ) => {
63
- communicationSocket . emit ( CommunicationEvents . USER_DISCONNECT ) ;
50
+ communicationSocket ? .emit ( CommunicationEvents . USER_DISCONNECT ) ;
64
51
} ;
65
52
// eslint-disable-next-line react-hooks/exhaustive-deps
66
53
} , [ ] ) ;
@@ -78,19 +65,25 @@ const Chat: React.FC<ChatProps> = ({ isActive, setHasNewMessage }) => {
78
65
}
79
66
} ;
80
67
81
- communicationSocket . on ( CommunicationEvents . USER_JOINED , listener ) ;
82
- communicationSocket . on ( CommunicationEvents . TEXT_MESSAGE_RECEIVED , listener ) ;
83
- communicationSocket . on ( CommunicationEvents . DISCONNECTED , listener ) ;
84
- communicationSocket . on ( CommunicationEvents . CONNECT_ERROR , errorListener ) ;
68
+ communicationSocket ?. on ( CommunicationEvents . USER_JOINED , listener ) ;
69
+ communicationSocket ?. on (
70
+ CommunicationEvents . TEXT_MESSAGE_RECEIVED ,
71
+ listener
72
+ ) ;
73
+ communicationSocket ?. on ( CommunicationEvents . DISCONNECTED , listener ) ;
74
+ communicationSocket ?. on ( CommunicationEvents . CONNECT_ERROR , errorListener ) ;
85
75
86
76
return ( ) => {
87
- communicationSocket . off ( CommunicationEvents . USER_JOINED , listener ) ;
88
- communicationSocket . off (
77
+ communicationSocket ? .off ( CommunicationEvents . USER_JOINED , listener ) ;
78
+ communicationSocket ? .off (
89
79
CommunicationEvents . TEXT_MESSAGE_RECEIVED ,
90
80
listener
91
81
) ;
92
- communicationSocket . off ( CommunicationEvents . DISCONNECTED , listener ) ;
93
- communicationSocket . off ( CommunicationEvents . CONNECT_ERROR , errorListener ) ;
82
+ communicationSocket ?. off ( CommunicationEvents . DISCONNECTED , listener ) ;
83
+ communicationSocket ?. off (
84
+ CommunicationEvents . CONNECT_ERROR ,
85
+ errorListener
86
+ ) ;
94
87
} ;
95
88
// eslint-disable-next-line react-hooks/exhaustive-deps
96
89
} , [ ] ) ;
@@ -139,7 +132,7 @@ const Chat: React.FC<ChatProps> = ({ isActive, setHasNewMessage }) => {
139
132
{ msg . message }
140
133
</ Typography >
141
134
</ Box >
142
- ) : msg . from === user ?. username ? (
135
+ ) : msg . from === collabUser ?. username ? (
143
136
< Box
144
137
key = { id }
145
138
sx = { ( theme ) => ( {
@@ -187,10 +180,10 @@ const Chat: React.FC<ChatProps> = ({ isActive, setHasNewMessage }) => {
187
180
const trimmedValue = inputValue . trim ( ) ;
188
181
if ( e . key === "Enter" && ! e . shiftKey && trimmedValue !== "" ) {
189
182
e . preventDefault ( ) ;
190
- communicationSocket . emit ( CommunicationEvents . SEND_TEXT_MESSAGE , {
191
- roomId : getMatchId ( ) ,
183
+ communicationSocket ? .emit ( CommunicationEvents . SEND_TEXT_MESSAGE , {
184
+ roomId : roomId ,
192
185
message : trimmedValue ,
193
- username : user ?. username ,
186
+ username : collabUser ?. username ,
194
187
createdTime : Date . now ( ) ,
195
188
} ) ;
196
189
setInputValue ( "" ) ;
0 commit comments