From 984e7d0975fb10254de520818c9daf1dd57fedcb Mon Sep 17 00:00:00 2001 From: Andrey Mikhadyuk Date: Mon, 18 Dec 2023 12:35:46 +0400 Subject: [PATCH] fix inbox data update in the loop --- src/shared/hooks/useCases/useInboxItems.ts | 87 +++++++++++++--------- 1 file changed, 52 insertions(+), 35 deletions(-) diff --git a/src/shared/hooks/useCases/useInboxItems.ts b/src/shared/hooks/useCases/useInboxItems.ts index 496dad3443..9355d4258e 100644 --- a/src/shared/hooks/useCases/useInboxItems.ts +++ b/src/shared/hooks/useCases/useInboxItems.ts @@ -20,6 +20,7 @@ import { FeedItemFollow, FeedItemFollowWithMetadata, InboxItem, + Timestamp, } from "@/shared/models"; import { inboxActions, InboxItems, selectInboxItems } from "@/store/states"; @@ -186,6 +187,7 @@ export const useInboxItems = ( const dispatch = useDispatch(); const isMounted = useIsMounted(); const [newItemsBatches, setNewItemsBatches] = useState([]); + const [lastUpdatedAt, setLastUpdatedAt] = useState(null); const inboxItems = useSelector(selectInboxItems); const user = useSelector(selectUser()); const inboxItemsRef = useRef(inboxItems); @@ -279,47 +281,62 @@ export const useInboxItems = ( endBefore: inboxItems.firstDocTimestamp, unread, }, + addNewInboxItems, + ); + + return unsubscribe; + }, [ + inboxItems.firstDocTimestamp, + userId, + feedItemIdsForNotListening, + unread, + ]); + + useEffect(() => { + const endBefore = lastUpdatedAt || inboxItems.firstDocTimestamp; + + if (!endBefore || !userId) { + return; + } + + const unsubscribe = UserService.subscribeToNewInboxItems( + { + userId, + endBefore, + unread, + orderBy: "updatedAt", + }, (data) => { - addNewInboxItems(data); + const lastDocTimestampSeconds = + inboxItemsRef.current?.lastDocTimestamp?.seconds; + const currentInboxData = inboxItemsRef.current?.data; + const filteredData = + lastDocTimestampSeconds && currentInboxData + ? data.filter( + ({ item }) => + item.itemUpdatedAt.seconds >= lastDocTimestampSeconds && + !currentInboxData.some( + (currentItem) => currentItem.itemId === item.itemId, + ), + ) + : []; + + if (data[0]) { + setLastUpdatedAt(data[0].item.updatedAt); + } + + addNewInboxItems(filteredData); + + if (filteredData.length !== data.length) { + dispatch(inboxActions.setHasMoreInboxItems(true)); + } }, ); - const unsubscribeFromUpdateAtSubscription = - UserService.subscribeToNewInboxItems( - { - userId, - endBefore: inboxItems.firstDocTimestamp, - unread, - orderBy: "updatedAt", - }, - (data) => { - const lastDocTimestampSeconds = - inboxItemsRef.current?.lastDocTimestamp?.seconds; - const currentInboxData = inboxItemsRef.current?.data; - const filteredData = - lastDocTimestampSeconds && currentInboxData - ? data.filter( - ({ item }) => - item.itemUpdatedAt.seconds >= lastDocTimestampSeconds && - !currentInboxData.some( - (currentItem) => currentItem.itemId === item.itemId, - ), - ) - : []; - - addNewInboxItems(filteredData); - - if (filteredData.length !== data.length) { - dispatch(inboxActions.setHasMoreInboxItems(true)); - } - }, - ); - return () => { - unsubscribe(); - unsubscribeFromUpdateAtSubscription(); - }; + return unsubscribe; }, [ inboxItems.firstDocTimestamp, + lastUpdatedAt, userId, feedItemIdsForNotListening, unread,