Skip to content

Commit

Permalink
πŸ› 이전 λ©”μ‹œμ§€ 페칭 μ‹œ μ±„νŒ… 슀크둀 μœ„μΉ˜κ°€ 잘λͺ» μ§€μ •λ˜λŠ” 문제 μˆ˜μ •
Browse files Browse the repository at this point in the history
  • Loading branch information
js43o committed Dec 10, 2023
1 parent df2f6d9 commit 0b26c87
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 32 deletions.
16 changes: 11 additions & 5 deletions app/frontend/src/components/Sidebar/Contents/Chatting/ChatList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type ChatListProps = {
chatItems: ChatMessage[];
userId: string;
participants: ResponseParticipant[];
fetchPrevMessages: () => void;
fetchPrevMessages: (callback: () => void) => void;
};

export function ChatList({
Expand All @@ -26,6 +26,7 @@ export function ChatList({
const listElemRef = useRef<HTMLUListElement>(null);
const observableRef = useRef<HTMLDivElement | null>(null);
const exposed = useObserver(observableRef);
const loading = useRef(false);

useEffect(() => {
if (!listElemRef.current) {
Expand All @@ -42,6 +43,9 @@ export function ChatList({
listElemRef.current.scrollTo({
top: scrollHeight - prevScrollHeightRef.current,
});
prevScrollHeightRef.current = scrollHeight;

return;
}

if (
Expand All @@ -51,14 +55,16 @@ export function ChatList({
listElemRef.current.scrollTo({
top: scrollHeight - clientHeight,
});
prevScrollHeightRef.current = scrollHeight;
}

prevScrollHeightRef.current = scrollHeight;
}, [chatItems, exposed]);

useEffect(() => {
if (exposed) {
fetchPrevMessages();
if (exposed && !loading.current) {
loading.current = true;
fetchPrevMessages(() => {
loading.current = false;
});
}
}, [exposed, fetchPrevMessages]);

Expand Down
65 changes: 38 additions & 27 deletions app/frontend/src/hooks/useChatting.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useRef, useState } from 'react';
import { useCallback, useEffect, useRef, useState } from 'react';

import SocketClient from '@morak/chat/src/client/index';
import {
Expand Down Expand Up @@ -50,34 +50,37 @@ export function useChatting(postId: string) {
[postId],
);

const fetchPrevMessages = useCallback(() => {
if (lastDateRef.current === null) {
return;
}

if (lastDateRef.current === undefined) {
lastDateRef.current =
chatItems.length > 0 ? chatItems[0].date : new Date();
}

socketClient.requestPrevMessage(
postId,
lastDateRef.current,
(status, messages) => {
if (status !== 200) {
return;
}
const fetchPrevMessages = useCallback(
(callback: () => void) => {
if (lastDateRef.current === null) {
return;
}

if (lastDateRef.current === undefined) {
lastDateRef.current = new Date();
}

socketClient.requestPrevMessage(
postId,
lastDateRef.current,
(status, messages) => {
if (status !== 200) {
return;
}

if (messages.length === 0) {
lastDateRef.current = null;
return;
}
if (messages.length === 0) {
lastDateRef.current = null;
return;
}

lastDateRef.current = messages[messages.length - 1].date;
setChatItems((items) => [...messages.reverse(), ...items]);
},
);
}, [postId, chatItems]);
lastDateRef.current = messages[messages.length - 1].date;
setChatItems((items) => [...messages.reverse(), ...items]);
callback();
},
);
},
[postId],
);

const subscribeToChat = useCallback(
() =>
Expand Down Expand Up @@ -112,6 +115,14 @@ export function useChatting(postId: string) {
lastDateRef.current = undefined;
}, []);

useEffect(() => {
if (lastDateRef.current === null) {
return;
}

lastDateRef.current = chatItems.length > 0 ? chatItems[0].date : undefined;
}, [chatItems]);

return {
chatItems,
resetChatItems,
Expand Down

0 comments on commit 0b26c87

Please sign in to comment.