From a829ac5f2b4e18d14dd9d7c7f4f807b1eec17894 Mon Sep 17 00:00:00 2001
From: Harsh Narayan Jha <50262541+HarshNarayanJha@users.noreply.github.com>
Date: Tue, 24 Sep 2024 18:51:07 +0530
Subject: [PATCH] feature: add partnerGone state to persist chats
---
src/app/page.tsx | 14 +++++++++++---
src/hooks/useChatSocket.tsx | 5 +++++
2 files changed, 16 insertions(+), 3 deletions(-)
diff --git a/src/app/page.tsx b/src/app/page.tsx
index 3fb9134..b05aa74 100644
--- a/src/app/page.tsx
+++ b/src/app/page.tsx
@@ -7,10 +7,11 @@ import WaitingForChat from "@/components/sections/WaitingForChat";
import { useChatSocket } from "@/hooks/useChatSocket";
export default function Home() {
-
+
const {
user,
partner,
+ partnerGone,
messages,
isConnected,
isWaiting,
@@ -33,11 +34,18 @@ export default function Home() {
if (!isWaiting) {
if (!partner) { // if user hasn't allowed to find matches
content = ;
+
+ if (partnerGone) { // if the partner left, show the chat history
+ content = ;
+ }
} else { // if matches found
- content = ;
+ readIndex={readIndex} readMessage={readMessage} />;
}
} else { // if user is waiting for a partner
content = ;
diff --git a/src/hooks/useChatSocket.tsx b/src/hooks/useChatSocket.tsx
index 39f672e..8f8790c 100644
--- a/src/hooks/useChatSocket.tsx
+++ b/src/hooks/useChatSocket.tsx
@@ -11,6 +11,7 @@ export const useChatSocket = () => {
const [socket, setSocket] = useState | null>(null);
const [user, setUser] = useState(null);
const [partner, setPartner] = useState(null);
+ const [partnerGone, setPartnerGone] = useState(false);
const [isConnected, setIsConnected] = useState(false);
const [isWaiting, setIsWaiting] = useState(false);
const [messages, setMessages] = useState([]);
@@ -29,6 +30,7 @@ export const useChatSocket = () => {
setIsConnected(false);
setUser(null);
setPartner(null);
+ setPartnerGone(false);
setIsWaiting(false);
console.log('Disconnected from server');
});
@@ -40,6 +42,7 @@ export const useChatSocket = () => {
newSocket.on(ServerEvents.MATCHED, ({ partnerName }) => {
setPartner(partnerName);
+ setPartnerGone(false);
setIsWaiting(false);
console.log("Matched with a Partner");
setMessages([]);
@@ -55,6 +58,7 @@ export const useChatSocket = () => {
newSocket.on(ServerEvents.PARTNER_DISCONNECTED, () => {
setPartner(null);
+ setPartnerGone(true);
})
return () => {
@@ -95,6 +99,7 @@ export const useChatSocket = () => {
return {
user,
partner,
+ partnerGone,
messages,
isConnected,
isWaiting,