Skip to content

Commit

Permalink
[Chat] fix: infinite scroll trigger inspection time > prev message 유무…
Browse files Browse the repository at this point in the history
…로 변경
  • Loading branch information
dia-triple committed Aug 29, 2023
1 parent b41e078 commit 6871a8f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 21 deletions.
27 changes: 7 additions & 20 deletions packages/chat/src/chat/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { useChat } from './chat-context'
import { useChatMessage } from './use-chat-message'
import { getChatListHeight, useScrollContext } from './scroll-context'

const MINIMUM_INITIAL_INTERSECTING_TIME = 3000
export const CHAT_CONTAINER_ID = 'chat-inner-container'

export interface ChatProps {
Expand Down Expand Up @@ -227,31 +226,19 @@ export const Chat = ({
}, [postMessage, setPostMessage]) // eslint-disable-line react-hooks/exhaustive-deps

const onChangeScroll = async ({
target,
isIntersecting,
time,
}: IntersectionObserverEntry) => {
if (isIntersecting) {
const prevScrollY = getChatListHeight()

if (!(target as HTMLElement).dataset.viewStartedAt) {
;(target as HTMLElement).dataset.viewStartedAt = time.toString()
} else {
const viewStartedAt = Number(
(target as HTMLElement).dataset.viewStartedAt,
)
if (
time - viewStartedAt >= MINIMUM_INITIAL_INTERSECTING_TIME &&
hasPrevMessage
) {
const pastMessages = await fetchPastMessages()
if (hasPrevMessage) {
const pastMessages = await fetchPastMessages()

await dispatch({
action: ChatActions.PAST,
messages: pastMessages,
})
setScrollY(prevScrollY)
}
await dispatch({
action: ChatActions.PAST,
messages: pastMessages,
})
setScrollY(prevScrollY)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/chat/src/chat/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export const ChatReducer = (

export const initialChatState: ChatState = {
messages: [],
hasPrevMessage: true,
hasPrevMessage: false,
otherUnreadInfo: [],
firstMessageId: null,
lastMessageId: null,
Expand Down

0 comments on commit 6871a8f

Please sign in to comment.