diff --git a/src/pages/common/components/FeedItem/components/ProjectFeedItem/ProjectFeedItem.tsx b/src/pages/common/components/FeedItem/components/ProjectFeedItem/ProjectFeedItem.tsx index e1b1ac96f0..7c948ed356 100644 --- a/src/pages/common/components/FeedItem/components/ProjectFeedItem/ProjectFeedItem.tsx +++ b/src/pages/common/components/FeedItem/components/ProjectFeedItem/ProjectFeedItem.tsx @@ -1,7 +1,6 @@ import React, { FC, ReactNode, useEffect } from "react"; import { useHistory } from "react-router-dom"; import classNames from "classnames"; -import { useCommonMember } from "@/pages/OldCommon/hooks"; import { useFeedItemContext } from "@/pages/common"; import { useRoutesContext } from "@/shared/contexts"; import { useCommon } from "@/shared/hooks/useCases"; @@ -9,6 +8,7 @@ import { OpenIcon } from "@/shared/icons"; import { CommonFeed } from "@/shared/models"; import { CommonAvatar, parseStringToTextEditorValue } from "@/shared/ui-kit"; import { checkIsProject } from "@/shared/utils"; +import { useFeedItemCounters } from "../../hooks"; import styles from "./ProjectFeedItem.module.scss"; interface ProjectFeedItemProps { @@ -22,16 +22,13 @@ export const ProjectFeedItem: FC = (props) => { const { getCommonPagePath } = useRoutesContext(); const { renderFeedItemBaseContent } = useFeedItemContext(); const { data: common, fetched: isCommonFetched, fetchCommon } = useCommon(); - const { - fetched: isCommonMemberFetched, - data: commonMember, - fetchCommonMember, - } = useCommonMember(); + const { unreadStreamsCount, unreadMessages } = useFeedItemCounters( + item.id, + common?.directParent?.commonId, + ); const commonId = item.data.id; - const unreadStreamsCount = - commonMember?.streamsUnreadCountByProjectStream[item.id] ?? null; const lastMessage = parseStringToTextEditorValue( - unreadStreamsCount !== null + Number.isInteger(unreadStreamsCount) ? `${unreadStreamsCount} updated stream${ unreadStreamsCount === 1 ? "" : "s" }` @@ -65,10 +62,6 @@ export const ProjectFeedItem: FC = (props) => { fetchCommon(commonId); }, [commonId]); - useEffect(() => { - fetchCommonMember(item.commonId); - }, [fetchCommonMember, item.commonId]); - return ( ( <> @@ -76,13 +69,12 @@ export const ProjectFeedItem: FC = (props) => { className: styles.container, titleWrapperClassName: styles.titleWrapper, lastActivity: item.updatedAt.seconds * 1000, - unreadMessages: - commonMember?.unreadCountByProjectStream[item.id] ?? 0, isMobileView: isMobileVersion, title: titleEl, onClick: handleClick, seenOnce: true, - isLoading: !isCommonFetched || !isCommonMemberFetched, + isLoading: !isCommonFetched, + unreadMessages, lastMessage, renderLeftContent, shouldHideBottomContent: !lastMessage, diff --git a/src/pages/common/components/FeedItem/hooks/index.ts b/src/pages/common/components/FeedItem/hooks/index.ts new file mode 100644 index 0000000000..269de5d6a0 --- /dev/null +++ b/src/pages/common/components/FeedItem/hooks/index.ts @@ -0,0 +1 @@ +export * from "./useFeedItemCounters"; diff --git a/src/pages/common/components/FeedItem/hooks/useFeedItemCounters.ts b/src/pages/common/components/FeedItem/hooks/useFeedItemCounters.ts new file mode 100644 index 0000000000..3b362207db --- /dev/null +++ b/src/pages/common/components/FeedItem/hooks/useFeedItemCounters.ts @@ -0,0 +1,34 @@ +import { useEffect } from "react"; +import { useCommonMember } from "@/pages/OldCommon/hooks"; +import { useGovernanceByCommonId } from "@/shared/hooks/useCases"; + +interface Return { + unreadStreamsCount?: number; + unreadMessages?: number; +} + +export const useFeedItemCounters = ( + feedItemId: string, + commonId?: string, +): Return => { + const { data: governance, fetchGovernance } = useGovernanceByCommonId(); + const { data: commonMember } = useCommonMember({ + shouldAutoReset: false, + withSubscription: true, + governanceCircles: governance?.circles, + commonId, + }); + const { streamsUnreadCountByProjectStream, unreadCountByProjectStream } = + commonMember || {}; + + useEffect(() => { + if (commonId) { + fetchGovernance(commonId); + } + }, [fetchGovernance, commonId]); + + return { + unreadStreamsCount: streamsUnreadCountByProjectStream?.[feedItemId], + unreadMessages: unreadCountByProjectStream?.[feedItemId], + }; +}; diff --git a/src/shared/models/Common.tsx b/src/shared/models/Common.tsx index 80a69ec68c..14a04fea5a 100644 --- a/src/shared/models/Common.tsx +++ b/src/shared/models/Common.tsx @@ -144,8 +144,8 @@ export interface CommonMember { rulesAccepted?: boolean; joinedAt: firebase.firestore.Timestamp; circleIds: string[]; - streamsUnreadCountByProjectStream: Record; - unreadCountByProjectStream: Record; + streamsUnreadCountByProjectStream?: Record; + unreadCountByProjectStream?: Record; } export interface CirclesPermissions { diff --git a/src/shared/models/CommonFeed.tsx b/src/shared/models/CommonFeed.tsx index 3d264dc525..219f661c18 100644 --- a/src/shared/models/CommonFeed.tsx +++ b/src/shared/models/CommonFeed.tsx @@ -14,7 +14,6 @@ export enum CommonFeedType { export interface CommonFeed extends BaseEntity, SoftDeleteEntity { userId: string; - commonId: string; data: Record & { type: CommonFeedType; id: string;