Skip to content

Commit

Permalink
Merge pull request #2164 from daostack/CW-2048-add-actions-menu-to-dms
Browse files Browse the repository at this point in the history
Add action menu for dms with mark as read\unread functionality #2048
  • Loading branch information
andreymikhadyuk authored Oct 10, 2023
2 parents b920331 + 7a7ca8f commit ea43199
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/pages/inbox/components/ChatChannelItem/ChatChannelItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,20 @@ import { ChatChannel } from "@/shared/models";
import { getUserName } from "@/shared/utils";
import { inboxActions } from "@/store/states";
import { FeedItemBaseContent } from "../FeedItemBaseContent";
import { useChatChannelSubscription } from "./hooks";
import { useChatChannelSubscription, useMenuItems } from "./hooks";
import { getLastMessage } from "./utils";

export const ChatChannelItem: FC<ChatChannelFeedLayoutItemProps> = (props) => {
const { chatChannel, isActive, onActiveItemDataChange } = props;
const dispatch = useDispatch();
const isTabletView = useIsTabletView();
const {
fetchUser: fetchDMUser,
data: dmUser,
fetched: isDMUserFetched,
} = useUserById();
const { fetchUser: fetchDMUser, data: dmUser } = useUserById();
const {
data: chatChannelUserStatus,
fetched: isChatChannelUserStatusFetched,
fetchChatChannelUserStatus,
} = useChatChannelUserStatus();
const menuItems = useMenuItems({ chatChannelUserStatus });
const { setChatItem, feedItemIdForAutoChatOpen, shouldAllowChatAutoOpen } =
useChatContext();
const user = useSelector(selectUser());
Expand Down Expand Up @@ -126,7 +123,8 @@ export const ChatChannelItem: FC<ChatChannelFeedLayoutItemProps> = (props) => {
lastMessage={getLastMessage(chatChannel.lastMessage)}
canBeExpanded={false}
onClick={handleOpenChat}
menuItems={[]}
menuItems={menuItems}
seen={chatChannelUserStatus?.seen}
seenOnce={chatChannelUserStatus?.seenOnce}
ownerId={userId}
renderImage={renderImage}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum ChatChannelMenuItem {
MarkUnread = "markUnread",
MarkRead = "markRead",
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./chatChannelMenuItems";
1 change: 1 addition & 0 deletions src/pages/inbox/components/ChatChannelItem/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./useChatChannelSubscription";
export * from "./useMenuItems";
42 changes: 42 additions & 0 deletions src/pages/inbox/components/ChatChannelItem/hooks/useMenuItems.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React from "react";
import { ChatService } from "@/services";
import { Message3Icon } from "@/shared/icons";
import { ContextMenuItem as Item } from "@/shared/interfaces";
import { ChatChannelMenuItem } from "../constants";
import { getAllowedItems, Options as GetAllowedItemsOptions } from "../utils";

export const useMenuItems = (options: GetAllowedItemsOptions): Item[] => {
const { chatChannelUserStatus } = options;
const items: Item[] = [
{
id: ChatChannelMenuItem.MarkUnread,
text: "Mark as unread",
onClick: async () => {
if (!chatChannelUserStatus) {
return;
}

await ChatService.markChatChannelAsUnseen(
chatChannelUserStatus.chatChannelId,
);
},
icon: <Message3Icon />,
},
{
id: ChatChannelMenuItem.MarkRead,
text: "Mark as read",
onClick: async () => {
if (!chatChannelUserStatus) {
return;
}

await ChatService.markChatChannelAsSeen(
chatChannelUserStatus.chatChannelId,
);
},
icon: <Message3Icon />,
},
];

return getAllowedItems(items, options);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ContextMenuItem as Item } from "@/shared/interfaces";
import { ChatChannelUserStatus } from "@/shared/models";
import { ChatChannelMenuItem } from "../constants";

export interface Options {
chatChannelUserStatus: ChatChannelUserStatus | null;
}

const MENU_ITEM_TO_CHECK_FUNCTION_MAP: Record<
ChatChannelMenuItem,
(options: Options) => boolean
> = {
[ChatChannelMenuItem.MarkUnread]: ({ chatChannelUserStatus }) =>
Boolean(chatChannelUserStatus?.seen),
[ChatChannelMenuItem.MarkRead]: ({ chatChannelUserStatus }) =>
Boolean(chatChannelUserStatus && !chatChannelUserStatus.seen),
};

export const getAllowedItems = (items: Item[], options: Options): Item[] =>
items.filter(({ id }) => MENU_ITEM_TO_CHECK_FUNCTION_MAP[id](options));
1 change: 1 addition & 0 deletions src/pages/inbox/components/ChatChannelItem/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./getLastMessage";
export * from "./getAllowedItems";
12 changes: 12 additions & 0 deletions src/services/Chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,18 @@ class ChatService {
);
};

public markChatChannelAsUnseen = async (
chatChannelId: string,
options: { cancelToken?: CancelToken } = {},
): Promise<void> => {
const { cancelToken } = options;
await Api.post(
ApiEndpoint.MarkChatChannelAsUnseen(chatChannelId),
undefined,
{ cancelToken },
);
};

public markChatMessageAsSeen = async (
chatMessageId: string,
options: { cancelToken?: CancelToken } = {},
Expand Down
2 changes: 2 additions & 0 deletions src/shared/constants/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export const ApiEndpoint = {
`/chat/message/${chatMessageId}`,
MarkChatChannelAsSeen: (channelId: string) =>
`/chat/channel/${channelId}/seen`,
MarkChatChannelAsUnseen: (channelId: string) =>
`/chat/channel/${channelId}/unseen`,
MarkChatMessageAsSeen: (chatMessageId: string) =>
`/chat/message/${chatMessageId}/seen`,
};
1 change: 1 addition & 0 deletions src/shared/models/ChatChannelUserStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export interface ChatChannelUserStatus extends BaseEntity {
notSeenCount: number;
seenOnce?: boolean;
seenAt?: Timestamp;
seen: boolean;
}

0 comments on commit ea43199

Please sign in to comment.