diff --git a/src/components/message-item/message-item.scss b/src/components/message-item/message-item.scss index 1439f865e..2732aee6f 100644 --- a/src/components/message-item/message-item.scss +++ b/src/components/message-item/message-item.scss @@ -193,8 +193,8 @@ } &__contents-wrapper { - animation: message 0.15s ease-in 0s forwards; - opacity: 0; + //animation: message 0.15s ease-in 0s forwards; + //opacity: 0; display: block; margin-right: 16px; max-width: 70%; diff --git a/src/components/message-list/message-list.tsx b/src/components/message-list/message-list.tsx index 6f9a59632..ba307f377 100644 --- a/src/components/message-list/message-list.tsx +++ b/src/components/message-list/message-list.tsx @@ -115,7 +115,9 @@ const MessageList = () => { [messages, messagesIds, formatDateForSeparator], ); - useEffect(loadMore, [loadMore, selectedChatId]); + console.log(separatedMessagesPacks); + + // useEffect(loadMore, [loadMore, selectedChatId]); if (!selectedChatId) { return ; @@ -134,47 +136,42 @@ const MessageList = () => { )} {selectedMessageIds.length > 0 && } - {!areMessagesLoading || messagesIds?.length - ? Boolean(messagesIds?.length) && ( - - {separatedMessagesPacks.map((pack) => ( -
- {pack.messages.map((messageId, index) => ( - - ))} - {pack.messages.length > 0 && ( -
- {pack.date} -
- )} -
+ {areMessagesLoading && !messagesIds?.length ? ( + + ) : ( + + {separatedMessagesPacks?.map((pack) => ( +
+ {pack.messages.map((messageId, index) => ( + ))} - - ) - : areMessagesLoading && - hasMoreMessages && ( - <> - - - )} + {pack.messages.length > 0 && ( +
+ {pack.date} +
+ )} +
+ ))} +
+ )} ); diff --git a/src/hooks/use-intersection-observer.ts b/src/hooks/use-intersection-observer.ts index 58a36b630..e4c308ae3 100644 --- a/src/hooks/use-intersection-observer.ts +++ b/src/hooks/use-intersection-observer.ts @@ -1,13 +1,16 @@ import { useEffect, RefObject, useState, useRef, useCallback } from 'react'; -import throttle from 'lodash/fp/throttle'; +import debounce from 'lodash/debounce'; import noop from 'lodash/noop'; +import throttle from 'lodash/throttle'; type IntersectionObserverHook = { rootRef: RefObject; threshold?: number | number[]; margin?: number; throttleMs?: number; + skipFirst?: boolean; + debounceMs?: number; }; type TargetCallback = (entry: IntersectionObserverEntry) => void; @@ -20,11 +23,12 @@ type IntersectionController = { }; export function useIntersectionObserver( - { rootRef, threshold, margin, throttleMs }: IntersectionObserverHook, + { rootRef, threshold, margin, throttleMs, skipFirst, debounceMs }: IntersectionObserverHook, rootCallback?: RootCallback, ) { const intersectionControllerRef = useRef(); const rootCallbackRef = useRef(); + rootCallbackRef.current = rootCallback; useEffect( @@ -60,10 +64,10 @@ export function useIntersectionObserver( entriesAccumulator.clear(); }; - const scheduler = throttleMs ? throttle : undefined; + const scheduler = (throttleMs && throttle) || (debounceMs && debounce) || undefined; const observerCallback = scheduler - ? scheduler(throttleMs as number, observerCallbackSync) + ? scheduler(observerCallbackSync, throttleMs || debounceMs, { leading: !skipFirst }) : observerCallbackSync; const observer = new IntersectionObserver( @@ -71,6 +75,7 @@ export function useIntersectionObserver( entries.forEach((entry) => { entriesAccumulator.set(entry.target, entry); }); + observerCallback(); }, { diff --git a/src/utils/debounce.ts b/src/utils/debounce.ts new file mode 100644 index 000000000..e5f09ab3b --- /dev/null +++ b/src/utils/debounce.ts @@ -0,0 +1,30 @@ +export function debounce void>( + fn: F, + ms: number, + shouldRunFirst = true, + shouldRunLast = true, +) { + let waitingTimeout: number | undefined; + + return (...args: Parameters) => { + if (waitingTimeout) { + clearTimeout(waitingTimeout); + waitingTimeout = undefined; + } else if (shouldRunFirst) { + // eslint-disable-next-line + // @ts-ignore + fn(...args); + } + + // eslint-disable-next-line no-restricted-globals + waitingTimeout = self.setTimeout(() => { + if (shouldRunLast) { + // eslint-disable-next-line + // @ts-ignore + fn(...args); + } + + waitingTimeout = undefined; + }, ms); + }; +}