Skip to content

Commit

Permalink
CW-2366 Optimized useDiscussionChatAdapter hook
Browse files Browse the repository at this point in the history
  • Loading branch information
pvm-code committed Dec 14, 2023
1 parent c111c2b commit 7a972d5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ export const useDiscussionChatAdapter = (options: Options): Return => {
const user = useSelector(selectUser());
const userId = user?.uid;
const { data: commonMembers, fetchCommonMembers } = useCommonMembers();
const users = useMemo(
() =>
commonMembers
.filter((member) => member.userId !== userId)
.map(({ user }) => user),
[userId, commonMembers],

const allUsers = useMemo(() => commonMembers.map(({user}) => user),[commonMembers]);

const discussionUsers = useMemo(
() => allUsers.filter((user) => user.uid !== userId),
[userId, allUsers],
);
const discussionMessagesData = useDiscussionMessagesById({
discussionId,
hasPermissionToHide,
users,
users: allUsers,
textStyles,
onFeedItemClick,
onUserClick,
Expand All @@ -65,7 +65,7 @@ export const useDiscussionChatAdapter = (options: Options): Return => {
return {
discussionMessagesData,
markDiscussionMessageItemAsSeen: markFeedItemAsSeen,
discussionUsers: users,
discussionUsers,
fetchDiscussionUsers,
};
};
31 changes: 4 additions & 27 deletions src/shared/hooks/useCases/useDiscussionMessagesById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import { useEffect, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { useUpdateEffect } from "react-use";
import classNames from 'classnames';
import { isEqual, xor } from "lodash";
import firebase from "@/shared/utils/firebase";
import { fetchOwners } from "@/pages/OldCommon/store/api";
import { DiscussionMessageService } from "@/services";
import { LoadingState } from "@/shared/interfaces";
import { ModerationFlags } from "@/shared/interfaces/Moderation";
Expand Down Expand Up @@ -70,8 +68,6 @@ export const useDiscussionMessagesById = ({
const dispatch = useDispatch();
const { getCommonPagePath, getCommonPageAboutTabPath } = useRoutesContext();
const [defaultState, setDefaultState] = useState({ ...DEFAULT_STATE });
const [messageOwners, setMessageOwners] = useState<User[]>([]);
const [messageOwnersIds, setMessageOwnersIds] = useState<string[]>([]);
const [lastVisible, setLastVisible] = useState<Record<string, firebase.firestore.QueryDocumentSnapshot<DiscussionMessage>>>({});
const [isEndOfList, setIsEndOfList] = useState<Record<string, boolean>>({});
const [isLoading, setIsLoading] = useState(true);
Expand Down Expand Up @@ -255,16 +251,6 @@ export const useDiscussionMessagesById = ({
);
};

const fetchMessageOwners = async (ids: string[]): Promise<User[]> => {
if (isEqual(messageOwnersIds, ids)) {
return [...messageOwners];
}
const newOwnerIds = xor(messageOwnersIds, ids);
const owners = (await fetchOwners(newOwnerIds)) as User[];
setMessageOwnersIds(ids);
setMessageOwners([...messageOwners, ...owners]);
return owners;
};

useEffect(() => {
(async () => {
Expand All @@ -276,16 +262,7 @@ export const useDiscussionMessagesById = ({
({ moderation }) =>
moderation?.flag !== ModerationFlags.Hidden || hasPermissionToHide,
);
const ownerIds = Array.from(
new Set(
filteredMessages
?.filter(checkIsUserDiscussionMessage)
.map((d) => d.ownerId),
),
) as string[];
const owners = await fetchMessageOwners(ownerIds);

const loadedDiscussionMessages = await Promise.all(filteredMessages.map(async (d) => {
const loadedDiscussionMessages = filteredMessages.map((d) => {
const newDiscussionMessage = { ...d };
const parentMessage = filteredMessages.find(
({ id }) => id === d.parentId,
Expand All @@ -294,7 +271,7 @@ export const useDiscussionMessagesById = ({
checkIsUserDiscussionMessage(d) &&
checkIsUserDiscussionMessage(newDiscussionMessage)
) {
newDiscussionMessage.owner = owners.find((o) => o.uid === d.ownerId);
newDiscussionMessage.owner = users.find((o) => o.uid === d.ownerId);
}
newDiscussionMessage.parentMessage = parentMessage
? {
Expand All @@ -311,12 +288,12 @@ export const useDiscussionMessagesById = ({
}
: null;
return newDiscussionMessage;
}));
});

setDiscussionMessagesWithOwners(loadedDiscussionMessages);
setIsLoading(false);
})();
}, [state.data, messageOwnersIds, messageOwners, hasPermissionToHide]);
}, [state.data, hasPermissionToHide, users]);

return {
...state,
Expand Down

0 comments on commit 7a972d5

Please sign in to comment.