Skip to content

Commit 7a65dac

Browse files
authored
feat: create a hook for closing chat (#702)
1 parent e4c7452 commit 7a65dac

8 files changed

Lines changed: 262 additions & 213 deletions

File tree

client/src/App.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Navigate, Route, Routes } from 'react-router-dom';
22
import { useEffect, useState } from 'react';
33

4+
import { ChatProvider } from './context/ChatContext';
45
import ComingSoon from 'pages/ComingSoon';
56
import Home from 'pages/Home';
67
import NavBar from 'components/NavBar';
@@ -48,7 +49,7 @@ function App() {
4849
{isLoggedIn && <NavBar />}
4950
<Routes>
5051
<Route exact path="/" element={<ProtectedRoutes isLoggedIn={isLoggedIn} />}>
51-
<Route exact path="/" element={<Start />} />
52+
<Route exact path="/" element={<ChatProvider><Start /></ChatProvider>} />
5253
{/* TODO: Sepreate searching and foundUser into different routes */}
5354
<Route exact path="/founduser" element={<Searching />} />
5455
<Route exact path="/friends" element={<ComingSoon />} />

client/src/components/Anonymous.jsx

Lines changed: 26 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,51 @@
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';
42
import {
5-
NEW_EVENT_CLOSE,
63
NEW_EVENT_DELETE_MESSAGE,
74
NEW_EVENT_DISPLAY,
85
NEW_EVENT_EDIT_MESSAGE,
9-
NEW_EVENT_RECEIVE_MESSAGE,
10-
NEW_EVENT_ONLINE_STATUS,
6+
NEW_EVENT_ONLINE_STATUS,
7+
NEW_EVENT_RECEIVE_MESSAGE,
118
} 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';
1812

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';
2018
import { socket } from 'src/lib/socketConnection';
21-
import { useChat } from 'src/context/ChatContext';
2219
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';
2323
import { useDialog } from 'src/context/DialogContext';
24+
import { useNavigate } from 'react-router-dom';
2425

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';
3426
const centerItems = `flex items-center justify-center`;
3527

36-
const Anonymous = ({ onChatClosed }) => {
37-
const { app, endSearch } = useApp();
28+
const Anonymous = () => {
29+
const { app } = useApp();
3830
const { authState } = useAuth();
39-
const { currentChatId, onlineStatus } = app;
40-
const { clearTimer } = useCheckTimePassed();
41-
42-
const currentChatIdRef = useRef(currentChatId);
31+
const { currentChatId, onlineStatus } = app;
4332

44-
const [isTyping, setIsTyping] = useState(false);
45-
const [autoSearchAfterClose, setAutoSearchAfterClose] = useState(false);
33+
const [isTyping, setIsTyping] = useState(false);
4634
const [disconnected, setDisconnected] = useState(false);
4735
const [buddyOnlineStatus, setBuddyOnlineStatus] = useState(null);
4836

49-
const autoSearchRef = useRef();
50-
autoSearchRef.current = autoSearchAfterClose;
37+
const { setDialog } = useDialog();
38+
const { handleClose, closeChatHandler } = useCloseChat()
39+
40+
5141
/**
5242
* @type {React.MutableRefObject<null | ReturnType<setTimeout>>}
5343
*/
5444
const typingStatusTimeoutRef = useRef(null);
5545

5646
const navigate = useNavigate();
57-
const { messages: state, closeChat } = useChat();
58-
const { setDialog } = useDialog();
47+
const { messages: state } = useChat();
48+
5949

6050
const onDisplay = useCallback(({ isTyping, chatId }) => {
6151
// eslint-disable-next-line curly
@@ -110,52 +100,6 @@ const Anonymous = ({ onChatClosed }) => {
110100
setIsTyping(false);
111101
}, []);
112102

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-
};
159103

160104
const blockUser = async () => {
161105
// Get the other user id
@@ -201,7 +145,7 @@ const Anonymous = ({ onChatClosed }) => {
201145
const result = await blockUser();
202146
if (result.success) {
203147
alert('User blocked successfully');
204-
closeChatHandler(false);
148+
await closeChatHandler(false);
205149
} else {
206150
alert(result.message || 'Error blocking user. Please try again later.');
207151
}
@@ -347,7 +291,3 @@ const Anonymous = ({ onChatClosed }) => {
347291
};
348292

349293
export default Anonymous;
350-
351-
Anonymous.propTypes = {
352-
onChatClosed: PropTypes.func,
353-
};

0 commit comments

Comments
 (0)