|
1 | | -import { useCallback, useEffect, useRef, useState } from 'react'; |
2 | | -import { useNavigate } from 'react-router-dom'; |
3 | | -import PropTypes from 'prop-types'; |
| 1 | +import { Dropdown, IconButton, Tooltip, Whisper } from 'rsuite'; |
4 | 2 | import { |
5 | | - NEW_EVENT_CLOSE, |
6 | 3 | NEW_EVENT_DELETE_MESSAGE, |
7 | 4 | NEW_EVENT_DISPLAY, |
8 | 5 | NEW_EVENT_EDIT_MESSAGE, |
9 | | - NEW_EVENT_RECEIVE_MESSAGE, |
10 | | - NEW_EVENT_ONLINE_STATUS, |
| 6 | + NEW_EVENT_ONLINE_STATUS, |
| 7 | + NEW_EVENT_RECEIVE_MESSAGE, |
11 | 8 | } from '../../../constants.json'; |
12 | | -// Rsuite |
13 | | -import { Dropdown, IconButton, Tooltip, Whisper } from 'rsuite'; |
14 | | -import { Icon } from '@rsuite/icons'; |
15 | | - |
16 | | -// Icons |
17 | | -import { BiArrowBack} from 'react-icons/bi'; |
| 9 | +import { createClassesFromArray, isExplicitDisconnection } from 'src/lib/utils'; |
| 10 | +import { useCallback, useEffect, useRef, useState } from 'react'; |
| 11 | +import useKeyPress, { ShortcutFlags } from 'src/hooks/useKeyPress'; |
18 | 12 |
|
19 | | -// Store |
| 13 | +import { BiArrowBack } from 'react-icons/bi'; |
| 14 | +import Chat from 'components/Chat'; |
| 15 | +import { Icon } from '@rsuite/icons'; |
| 16 | +import MenuToggle from './MenuToggle'; |
| 17 | +import { api } from 'src/lib/axios'; |
20 | 18 | import { socket } from 'src/lib/socketConnection'; |
21 | | -import { useChat } from 'src/context/ChatContext'; |
22 | 19 | import { useApp } from 'src/context/AppContext'; |
| 20 | +import { useAuth } from 'src/context/AuthContext'; |
| 21 | +import { useChat } from 'src/context/ChatContext'; |
| 22 | +import useCloseChat from 'src/hooks/useCloseChat'; |
23 | 23 | import { useDialog } from 'src/context/DialogContext'; |
| 24 | +import { useNavigate } from 'react-router-dom'; |
24 | 25 |
|
25 | | -// Components |
26 | | -import Chat from 'components/Chat'; |
27 | | -import { createClassesFromArray, isExplicitDisconnection } from 'src/lib/utils'; |
28 | | - |
29 | | -import useKeyPress, { ShortcutFlags } from 'src/hooks/useKeyPress'; |
30 | | -import useCheckTimePassed from 'src/hooks/useCheckTimePassed'; |
31 | | -import { useAuth } from 'src/context/AuthContext'; |
32 | | -import { api } from 'src/lib/axios'; |
33 | | -import MenuToggle from './MenuToggle'; |
34 | 26 | const centerItems = `flex items-center justify-center`; |
35 | 27 |
|
36 | | -const Anonymous = ({ onChatClosed }) => { |
37 | | - const { app, endSearch } = useApp(); |
| 28 | +const Anonymous = () => { |
| 29 | + const { app } = useApp(); |
38 | 30 | const { authState } = useAuth(); |
39 | | - const { currentChatId, onlineStatus } = app; |
40 | | - const { clearTimer } = useCheckTimePassed(); |
41 | | - |
42 | | - const currentChatIdRef = useRef(currentChatId); |
| 31 | + const { currentChatId, onlineStatus } = app; |
43 | 32 |
|
44 | | - const [isTyping, setIsTyping] = useState(false); |
45 | | - const [autoSearchAfterClose, setAutoSearchAfterClose] = useState(false); |
| 33 | + const [isTyping, setIsTyping] = useState(false); |
46 | 34 | const [disconnected, setDisconnected] = useState(false); |
47 | 35 | const [buddyOnlineStatus, setBuddyOnlineStatus] = useState(null); |
48 | 36 |
|
49 | | - const autoSearchRef = useRef(); |
50 | | - autoSearchRef.current = autoSearchAfterClose; |
| 37 | + const { setDialog } = useDialog(); |
| 38 | + const { handleClose, closeChatHandler } = useCloseChat() |
| 39 | + |
| 40 | + |
51 | 41 | /** |
52 | 42 | * @type {React.MutableRefObject<null | ReturnType<setTimeout>>} |
53 | 43 | */ |
54 | 44 | const typingStatusTimeoutRef = useRef(null); |
55 | 45 |
|
56 | 46 | const navigate = useNavigate(); |
57 | | - const { messages: state, closeChat } = useChat(); |
58 | | - const { setDialog } = useDialog(); |
| 47 | + const { messages: state } = useChat(); |
| 48 | + |
59 | 49 |
|
60 | 50 | const onDisplay = useCallback(({ isTyping, chatId }) => { |
61 | 51 | // eslint-disable-next-line curly |
@@ -110,52 +100,6 @@ const Anonymous = ({ onChatClosed }) => { |
110 | 100 | setIsTyping(false); |
111 | 101 | }, []); |
112 | 102 |
|
113 | | - const emitClose = useCallback((err, chatClosed) => { |
114 | | - if (err) { |
115 | | - alert('An error occured whiles closing chat.'); |
116 | | - setAutoSearchAfterClose(false); |
117 | | - return err; |
118 | | - } |
119 | | - |
120 | | - if (chatClosed) { |
121 | | - closeChat(currentChatId); |
122 | | - } |
123 | | - |
124 | | - endSearch(); |
125 | | - |
126 | | - if (chatClosed && autoSearchRef.current) { |
127 | | - if (onChatClosed) { |
128 | | - onChatClosed(); |
129 | | - } |
130 | | - |
131 | | - setAutoSearchAfterClose(false); |
132 | | - } else { |
133 | | - navigate('/'); |
134 | | - } |
135 | | - |
136 | | - clearTimer(); |
137 | | - }, []); |
138 | | - |
139 | | - const closeChatHandler = (autoSearch = false) => { |
140 | | - const currentChatId = currentChatIdRef.current; |
141 | | - |
142 | | - if (!currentChatId) { |
143 | | - navigate('/'); |
144 | | - return; |
145 | | - } |
146 | | - |
147 | | - setAutoSearchAfterClose(autoSearch); |
148 | | - |
149 | | - socket.timeout(30000).emit(NEW_EVENT_CLOSE, currentChatId, emitClose); |
150 | | - }; |
151 | | - |
152 | | - const handleClose = (autoSearch = false) => { |
153 | | - setDialog({ |
154 | | - isOpen: true, |
155 | | - text: 'Are you sure you want to close this chat?', |
156 | | - handler: () => closeChatHandler(autoSearch), |
157 | | - }); |
158 | | - }; |
159 | 103 |
|
160 | 104 | const blockUser = async () => { |
161 | 105 | // Get the other user id |
@@ -201,7 +145,7 @@ const Anonymous = ({ onChatClosed }) => { |
201 | 145 | const result = await blockUser(); |
202 | 146 | if (result.success) { |
203 | 147 | alert('User blocked successfully'); |
204 | | - closeChatHandler(false); |
| 148 | + await closeChatHandler(false); |
205 | 149 | } else { |
206 | 150 | alert(result.message || 'Error blocking user. Please try again later.'); |
207 | 151 | } |
@@ -347,7 +291,3 @@ const Anonymous = ({ onChatClosed }) => { |
347 | 291 | }; |
348 | 292 |
|
349 | 293 | export default Anonymous; |
350 | | - |
351 | | -Anonymous.propTypes = { |
352 | | - onChatClosed: PropTypes.func, |
353 | | -}; |
0 commit comments