Skip to content

Commit

Permalink
CW-performance
Browse files Browse the repository at this point in the history
Fixed subscription for DiscussionMessages
  • Loading branch information
MeyerPV committed Oct 7, 2024
1 parent 0608a19 commit a21a84e
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions src/shared/hooks/useCases/useDiscussionMessagesById.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useCallback } from "react";
import { useState, useCallback, useEffect, useRef } from "react";
import { useDispatch, useSelector } from "react-redux";
import { useDeepCompareEffect, useUpdateEffect } from "react-use";
import { trace } from "firebase/performance";
Expand Down Expand Up @@ -100,6 +100,8 @@ export const useDiscussionMessagesById = ({
const [discussionMessagesWithOwners, setDiscussionMessagesWithOwners] =
useState<any>();

const unsubscribeRef = useRef<firebase.Unsubscribe | null>(null);

useUpdateEffect(() => {
if (discussionId) {
setDiscussionMessagesWithOwners([]);
Expand Down Expand Up @@ -251,7 +253,7 @@ export const useDiscussionMessagesById = ({
const fetchDiscussionMessagesTrace = trace(perf, 'fetchDiscussionMessages');
fetchDiscussionMessagesTrace.start();

DiscussionMessageService.subscribeToDiscussionMessagesByDiscussionId(
unsubscribeRef.current = DiscussionMessageService.subscribeToDiscussionMessagesByDiscussionId(
discussionId,
lastVisible && lastVisible[discussionId],
async (
Expand All @@ -268,16 +270,16 @@ export const useDiscussionMessagesById = ({
...prevVisible,
[discussionId]: lastVisibleDocument,
}));

const hasLastVisibleDocument = !!lastVisibleDocument?.data();

const discussionsWithText = await Promise.all(
updatedDiscussionMessages.map(async (discussionMessage) => {
const isUserDiscussionMessage =
checkIsUserDiscussionMessage(discussionMessage);
const isSystemMessage =
checkIsSystemDiscussionMessage(discussionMessage);

const parsedText = await getTextFromTextEditorString({
userId,
ownerId: isUserDiscussionMessage
Expand All @@ -294,7 +296,7 @@ export const useDiscussionMessagesById = ({
onFeedItemClick,
onInternalLinkClick,
});

return {
...discussionMessage,
parsedText,
Expand All @@ -321,10 +323,35 @@ export const useDiscussionMessagesById = ({
},
);
fetchDiscussionMessagesTrace.stop();
} catch(err) {
} catch (err) {
setIsBatchLoading(false);
}
},[discussionId, isEndOfList, state.loading, state.data, isBatchLoading, lastVisible, userId, users, directParent, getCommonPagePath, getCommonPageAboutTabPath, onUserClick, onFeedItemClick, onInternalLinkClick, dispatch]);
}, [
discussionId,
isEndOfList,
state.loading,
state.data,
isBatchLoading,
lastVisible,
userId,
users,
directParent,
getCommonPagePath,
getCommonPageAboutTabPath,
onUserClick,
onFeedItemClick,
onInternalLinkClick,
dispatch,
]);

useEffect(() => {
// Cleanup subscription on unmount or when discussionId changes
return () => {
if (unsubscribeRef.current) {
unsubscribeRef.current();
}
};
}, [discussionId]);

useDeepCompareEffect(() => {
(async () => {
Expand Down

0 comments on commit a21a84e

Please sign in to comment.