Skip to content

Commit

Permalink
Merge pull request #2034 from daostack/bugfix/CW-1994-new-counters
Browse files Browse the repository at this point in the history
FE activate new counters #1994
  • Loading branch information
andreymikhadyuk authored Sep 6, 2023
2 parents 53ea5d0 + 454b913 commit ac0d7b2
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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";
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 {
Expand All @@ -22,16 +22,13 @@ export const ProjectFeedItem: FC<ProjectFeedItemProps> = (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"
}`
Expand Down Expand Up @@ -65,24 +62,19 @@ export const ProjectFeedItem: FC<ProjectFeedItemProps> = (props) => {
fetchCommon(commonId);
}, [commonId]);

useEffect(() => {
fetchCommonMember(item.commonId);
}, [fetchCommonMember, item.commonId]);

return (
(
<>
{renderFeedItemBaseContent?.({
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,
Expand Down
1 change: 1 addition & 0 deletions src/pages/common/components/FeedItem/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./useFeedItemCounters";
34 changes: 34 additions & 0 deletions src/pages/common/components/FeedItem/hooks/useFeedItemCounters.ts
Original file line number Diff line number Diff line change
@@ -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],
};
};
4 changes: 2 additions & 2 deletions src/shared/models/Common.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ export interface CommonMember {
rulesAccepted?: boolean;
joinedAt: firebase.firestore.Timestamp;
circleIds: string[];
streamsUnreadCountByProjectStream: Record<string, number>;
unreadCountByProjectStream: Record<string, number>;
streamsUnreadCountByProjectStream?: Record<string, number>;
unreadCountByProjectStream?: Record<string, number>;
}

export interface CirclesPermissions {
Expand Down
1 change: 0 additions & 1 deletion src/shared/models/CommonFeed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export enum CommonFeedType {

export interface CommonFeed extends BaseEntity, SoftDeleteEntity {
userId: string;
commonId: string;
data: Record<string, unknown> & {
type: CommonFeedType;
id: string;
Expand Down

0 comments on commit ac0d7b2

Please sign in to comment.