Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#601 add unread num chatting #692

Open
wants to merge 9 commits into
base: dev
Choose a base branch
from
29 changes: 24 additions & 5 deletions src/components/Chat/MessagesBody/MessageSet/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { memo, useCallback } from "react";
import { memo, useCallback, useMemo } from "react";

import type { BotChat, LayoutType, UserChat } from "@/types/chat";

Expand Down Expand Up @@ -59,6 +59,10 @@ type MessageSetProps = {

const MessageSet = ({ chats, layoutType, roomInfo }: MessageSetProps) => {
const { oid: userOid } = useValueRecoilState("loginInfo") || {};
const readAts = useMemo(() => {
const readAts = roomInfo?.part?.map((user) => user?.readAt);
return readAts;
}, [chats, roomInfo]);
Comment on lines +62 to +65
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

연산에서 chats가 사용되지 않아 dependency array 에서 제외되어도 될 것 같아요.

Suggested change
const readAts = useMemo(() => {
const readAts = roomInfo?.part?.map((user) => user?.readAt);
return readAts;
}, [chats, roomInfo]);
const readAts = useMemo(
() => roomInfo?.part?.map((user) => user?.readAt) || [],
[roomInfo]
);

const authorId = chats?.[0]?.authorId;
const authorProfileUrl =
"authorProfileUrl" in chats?.[0] ? chats?.[0].authorProfileUrl : "";
Expand Down Expand Up @@ -127,12 +131,20 @@ const MessageSet = ({ chats, layoutType, roomInfo }: MessageSetProps) => {
}),
[userOid, authorId, layoutType]
);
const styleSubinfoWrap = {
display: "flex",
flexDirection: "column" as any,
alignItems: authorId === userOid ? "flex-end" : "flex-start",
};
const styleTime = {
...theme.font8,
color: theme.gray_text,
marginBottom: "1px",
minWidth: "fit-content",
};
const styleUnreadNum = {
...theme.font8_medium,
};

return (
<div css={style}>
Expand Down Expand Up @@ -168,11 +180,18 @@ const MessageSet = ({ chats, layoutType, roomInfo }: MessageSetProps) => {
color={authorId === userOid ? theme.white : theme.black}
/>
</div>
{index === chats.length - 1 && (
<div css={styleTime} className="selectable">
{dayjs(chat.time).format("H시 mm분")}
<div css={styleSubinfoWrap}>
<div css={styleUnreadNum} className="selectable">
{readAts?.filter((readAt) => readAt < chat.time).length == 0
? null
: readAts?.filter((readAt) => readAt < chat.time).length}
Comment on lines +185 to +187
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

같은 연산이 두 번 반복되는데, 한 번의 계산으로 줄일 수 있을 것 같습니다 !

</div>
)}
{index === chats.length - 1 && (
<div css={styleTime} className="selectable">
{dayjs(chat.time).format("H시 mm분")}
</div>
)}
</div>
</div>
))}
</div>
Expand Down
9 changes: 9 additions & 0 deletions src/components/Skeleton/SocketToastProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { ioServer } from "@/tools/loadenv";

export type SocketChatEventListner = (chats: Array<Chat>) => void;
export type SocketUpdateEventListner = (roomId: string) => void;
export type SocketVoidEventListner = () => void;

let isSocketReady: boolean = false;
Expand All @@ -20,6 +21,7 @@ let initEventListener: Nullable<SocketChatEventListner> = null;
let reconnectEventListener: Nullable<SocketVoidEventListner> = null;
let pushBackEventListener: Nullable<SocketChatEventListner> = null;
let pushFrontEventListener: Nullable<SocketChatEventListner> = null;
let updateEventListener: Nullable<SocketUpdateEventListner> = null;

const SocketToastProvider = () => {
const { id: userId } = useValueRecoilState("loginInfo") || {};
Expand Down Expand Up @@ -57,6 +59,9 @@ const SocketToastProvider = () => {
socket.on("chat_push_front", ({ chats }) => {
if (pushFrontEventListener) pushFrontEventListener(chats);
});
socket.on("chat_update", ({ roomId }) => {
if (updateEventListener) updateEventListener(roomId);
});

return () => {
socket.disconnect();
Expand All @@ -72,16 +77,19 @@ const registerSocketEventListener = ({
reconnectListener,
pushBackListener,
pushFrontListener,
updateListener,
}: {
initListener?: SocketChatEventListner;
reconnectListener?: SocketVoidEventListner;
pushBackListener?: SocketChatEventListner;
pushFrontListener?: SocketChatEventListner;
updateListener?: SocketUpdateEventListner;
}) => {
initEventListener = initListener;
reconnectEventListener = reconnectListener;
pushBackEventListener = pushBackListener;
pushFrontEventListener = pushFrontListener;
updateEventListener = updateListener;
};

// socket event listener 해제
Expand All @@ -90,6 +98,7 @@ const resetSocketEventListener = () => {
reconnectEventListener = null;
pushBackEventListener = null;
pushFrontEventListener = null;
updateEventListener = null;
};

// socket이 연결된 이후 event 함수를 실행합니다.
Expand Down
11 changes: 11 additions & 0 deletions src/hooks/chat/useBodyScrollControllerEffect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ export default (
chatBodyRef.current.scrollTop = scrollTop;
}, [chats]);

const lastChat = chats.length > 0 ? chats[chats.length - 1] : {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const lastChat = chats.length > 0 ? chats[chats.length - 1] : {};
const lastChat = chats.at(-1);

로 더 간단히 할 수 있을 것 같아요.

useEffect(() => {
socketReady(() =>
axios({
url: "/chats/read",
method: "post",
data: { roomId },
})
);
}, ["time" in lastChat ? lastChat?.time : ""]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}, ["time" in lastChat ? lastChat?.time : ""]);
}, [lastChat?.time, roodId]);

과 차이가 없을 것 같습니다 ! 그리고 roodId 도 추가되어야 할 것 같아요.


useEffect(() => {
const chatBody = chatBodyRef?.current;
let isBottomOnScrollCache: boolean = true;
Expand Down
10 changes: 6 additions & 4 deletions src/hooks/chat/useSocketChatEffect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export default (
initListener: (chats) => {
if (isExpired) return;
isSendingMessage.current = false;

setChats(
getCleanupChats([createInfScrollCheckoutChat(), ...chats]),
() => {
Expand All @@ -55,7 +54,6 @@ export default (
},
reconnectListener: () => {
if (isExpired) return;

setChats(
(prevChats: Chats): Chats => {
const lastChat = prevChats[prevChats.length - 1];
Expand All @@ -73,6 +71,7 @@ export default (
},
pushBackListener: (chats: Array<Chat>) => {
chats = chats.filter((chat) => chat.roomId === roomId);

if (isExpired || chats.length <= 0) return;

const isMyMessage = chats.some((chat) => chat.authorId === userOid);
Expand Down Expand Up @@ -106,7 +105,6 @@ export default (
},
pushFrontListener: (chats: Array<Chat>) => {
if (isExpired) return;

if (chats.length === 0) {
setChats(
(prevChats: Chats) => {
Expand Down Expand Up @@ -135,6 +133,10 @@ export default (
);
}
},
updateListener: (_roomId: string) => {
if (isExpired || roomId !== _roomId) return;
fetchRoomInfo();
},
});

// 채팅 로드 API 호출
Expand All @@ -148,5 +150,5 @@ export default (
isExpired = true;
resetSocketEventListener();
};
}, [roomInfo, setChats, setDisplayNewMessage]);
}, [roomInfo?._id, setChats, setDisplayNewMessage]);
};
1 change: 1 addition & 0 deletions src/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ declare global {
| `${PixelValue} ${PixelValue} ${PixelValue} ${PixelValue}`;
type Padding = Margin;
type User = {
readAt: any;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type을 any 보다는 구체적으로 명시하는 것이 좋을 것 같습니다 !

Suggested change
readAt: any;
readAt?: Date;

_id: string;
name: string;
nickname: string;
Expand Down