Skip to content

Commit

Permalink
fix inbox data update in the loop
Browse files Browse the repository at this point in the history
  • Loading branch information
andreymikhadyuk committed Dec 18, 2023
1 parent de7f269 commit 984e7d0
Showing 1 changed file with 52 additions and 35 deletions.
87 changes: 52 additions & 35 deletions src/shared/hooks/useCases/useInboxItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
FeedItemFollow,
FeedItemFollowWithMetadata,
InboxItem,
Timestamp,
} from "@/shared/models";
import { inboxActions, InboxItems, selectInboxItems } from "@/store/states";

Expand Down Expand Up @@ -186,6 +187,7 @@ export const useInboxItems = (
const dispatch = useDispatch();
const isMounted = useIsMounted();
const [newItemsBatches, setNewItemsBatches] = useState<ItemsBatch[]>([]);
const [lastUpdatedAt, setLastUpdatedAt] = useState<Timestamp | null>(null);
const inboxItems = useSelector(selectInboxItems);
const user = useSelector(selectUser());
const inboxItemsRef = useRef(inboxItems);
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 984e7d0

Please sign in to comment.