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

[Chat] iOS 모바일에서 init 시점에 infinite scroll 로직 실행되는 오류 수정 #2868

Merged
merged 8 commits into from
Aug 30, 2023
39 changes: 27 additions & 12 deletions packages/chat/src/chat/chat.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { IncomingMessage } from 'http'

import React, { useCallback, useEffect, useMemo, useReducer } from 'react'
import React, {
useCallback,
useEffect,
useMemo,
useReducer,
useRef,
} from 'react'
import { StaticIntersectionObserver as IntersectionObserver } from '@titicaca/intersection-observer'
import { useUserAgentContext } from '@titicaca/react-contexts'
import { closeKeyboard } from '@titicaca/triple-web-to-native-interfaces'
Expand All @@ -25,7 +31,6 @@ import { useChat } from './chat-context'
import { useChatMessage } from './use-chat-message'
import { getChatListHeight, useScrollContext } from './scroll-context'

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

export interface ChatProps {
Expand Down Expand Up @@ -90,6 +95,8 @@ export const Chat = ({
dispatch,
] = useReducer(ChatReducer, initialChatState)

const isScrollReady = useRef(false)

const { os } = useUserAgentContext()
const isIos = useMemo(() => os.name === 'iOS', [os.name])
const updateUnread = useCallback(async () => {
Expand Down Expand Up @@ -216,10 +223,16 @@ export const Chat = ({
const handleChangeLastMessageId = async () => {
await updateUnread()
scrollToBottom()

if (!isScrollReady.current) {
isScrollReady.current = true
}
}

useEffect(() => {
void handleChangeLastMessageId()
if (lastMessageId) {
void handleChangeLastMessageId()
}
}, [lastMessageId])

useEffect(() => {
Expand All @@ -228,17 +241,19 @@ export const Chat = ({

const onChangeScroll = async ({
isIntersecting,
time,
}: IntersectionObserverEntry) => {
const prevScrollY = getChatListHeight()
if (isIntersecting && time >= MINIMUM_INTERSECTING_TIME && hasPrevMessage) {
const pastMessages = await fetchPastMessages()
if (isIntersecting) {
const prevScrollY = getChatListHeight()

await dispatch({
action: ChatActions.PAST,
messages: pastMessages,
})
setScrollY(prevScrollY)
if (isScrollReady.current && hasPrevMessage) {
const pastMessages = await fetchPastMessages()

await dispatch({
action: ChatActions.PAST,
messages: pastMessages,
})
setScrollY(prevScrollY)
}
}
}

Expand Down