Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor sidebar unread handler #33792

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useOmnichannelPriorities } from '../../omnichannel/hooks/useOmnichannel
import RoomMenu from '../RoomMenu';
import { OmnichannelBadges } from '../badges/OmnichannelBadges';
import type { useAvatarTemplate } from '../hooks/useAvatarTemplate';
import { useUnreadDisplay } from '../hooks/useUnreadDisplay';
import { normalizeSidebarMessage } from './normalizeSidebarMessage';

export const getMessage = (room: IRoom, lastMessage: IMessage | undefined, t: TFunction): string | undefined => {
Expand All @@ -34,24 +35,6 @@ export const getMessage = (room: IRoom, lastMessage: IMessage | undefined, t: TF
return `${lastMessage.u.name || lastMessage.u.username}: ${normalizeSidebarMessage(lastMessage, t)}`;
};

export const getBadgeTitle = (userMentions: number, threadUnread: number, groupMentions: number, unread: number, t: TFunction) => {
const title = [] as string[];
if (userMentions) {
title.push(t('mentions_counter', { count: userMentions }));
}
if (threadUnread) {
title.push(t('threads_counter', { count: threadUnread }));
}
if (groupMentions) {
title.push(t('group_mentions_counter', { count: groupMentions }));
}
const count = unread - userMentions - groupMentions;
if (count > 0) {
title.push(t('unread_messages_counter', { count }));
}
return title.join(', ');
};

type RoomListRowProps = {
extended: boolean;
t: TFunction;
Expand Down Expand Up @@ -109,22 +92,10 @@ const SidebarItemTemplateWithData = ({
const href = roomCoordinator.getRouteLink(room.t, room) || '';
const title = roomCoordinator.getRoomName(room.t, room) || '';

const {
lastMessage,
hideUnreadStatus,
hideMentionStatus,
unread = 0,
alert,
userMentions,
groupMentions,
tunread = [],
tunreadUser = [],
rid,
t: type,
cl,
} = room;
const { unreadTitle, unreadVariant, showUnread, unreadCount, highlightUnread: highlighted } = useUnreadDisplay(room);

const { lastMessage, unread = 0, alert, rid, t: type, cl } = room;

const highlighted = Boolean(!hideUnreadStatus && (alert || unread));
const icon = (
<SidebarV2ItemIcon
highlighted={highlighted}
Expand All @@ -149,20 +120,11 @@ const SidebarItemTemplateWithData = ({
const message = extended && getMessage(room, lastMessage, t);
const subtitle = message ? <span className='message-body--unstyled' dangerouslySetInnerHTML={{ __html: message }} /> : null;

const threadUnread = tunread.length > 0;
const variant =
((userMentions || tunreadUser.length) && 'danger') || (threadUnread && 'primary') || (groupMentions && 'warning') || 'secondary';

const isUnread = unread > 0 || threadUnread;
const showBadge = !hideUnreadStatus || (!hideMentionStatus && (Boolean(userMentions) || tunreadUser.length > 0));

const badgeTitle = getBadgeTitle(userMentions, tunread.length, groupMentions, unread, t);

const badges = (
<>
{showBadge && isUnread && (
<SidebarV2ItemBadge variant={variant} title={badgeTitle}>
{unread + tunread?.length}
{showUnread && (
<SidebarV2ItemBadge variant={unreadVariant} title={unreadTitle}>
{unreadCount.total}
</SidebarV2ItemBadge>
)}
{isOmnichannelRoom(room) && <OmnichannelBadges room={room} />}
Expand Down Expand Up @@ -197,7 +159,7 @@ const SidebarItemTemplateWithData = ({
((): ReactElement => (
<RoomMenu
alert={alert}
threadUnread={threadUnread}
threadUnread={unreadCount.threads > 0}
rid={rid}
unread={!!unread}
roomOpen={selected}
Expand Down
56 changes: 56 additions & 0 deletions apps/meteor/client/sidebarv2/hooks/useUnreadDisplay.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import type { IRoom, ISubscription } from '@rocket.chat/core-typings';
import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';

export const useUnreadDisplay = ({
alert,
userMentions,
unread,
tunread,
tunreadUser,
groupMentions: groupMentionsProp,
hideMentionStatus,
hideUnreadStatus,
}: ISubscription & IRoom) => {
const { t } = useTranslation();

const unreadCount = useMemo(() => {
return {
mentions: userMentions + (tunreadUser?.length || 0),
threads: tunread?.length || 0,
groupMentions: groupMentionsProp,
total: unread + (tunread?.length || 0),
};
}, [groupMentionsProp, tunread?.length, tunreadUser?.length, unread, userMentions]);

const { groupMentions, mentions, threads, total } = unreadCount;

const unreadTitle = useMemo(() => {
const title = [] as string[];
if (mentions) {
title.push(t('mentions_counter', { count: mentions }));
}
if (threads) {
title.push(t('threads_counter', { count: threads }));
}
if (groupMentions) {
title.push(t('group_mentions_counter', { count: groupMentions }));
}
const count = total - mentions - groupMentions - threads;
if (count > 0) {
title.push(t('unread_messages_counter', { count }));
}
return title.join(', ');
}, [groupMentions, mentions, t, threads, total]);

const unreadVariant = useMemo(
() => (mentions && 'danger') || (threads && 'primary') || (groupMentions && 'warning') || 'secondary',
[groupMentions, mentions, threads],
) as 'danger' | 'primary' | 'warning' | 'secondary';

const showUnread = (!hideUnreadStatus || (!hideMentionStatus && (Boolean(mentions) || Boolean(groupMentions)))) && Boolean(total);

const highlightUnread = Boolean(!hideUnreadStatus && (alert || unread));

return { unreadTitle, unreadVariant, showUnread, unreadCount, highlightUnread };
};
24 changes: 7 additions & 17 deletions apps/meteor/client/views/room/Sidepanel/hooks/useItemData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { useTranslation } from 'react-i18next';

import { RoomIcon } from '../../../../components/RoomIcon';
import { roomCoordinator } from '../../../../lib/rooms/roomCoordinator';
import { getBadgeTitle, getMessage } from '../../../../sidebarv2/RoomList/SidebarItemTemplateWithData';
import { getMessage } from '../../../../sidebarv2/RoomList/SidebarItemTemplateWithData';
import { useAvatarTemplate } from '../../../../sidebarv2/hooks/useAvatarTemplate';
import { useUnreadDisplay } from '../../../../sidebarv2/hooks/useUnreadDisplay';

export const useItemData = (
room: ISubscription & IRoom,
Expand All @@ -15,7 +16,7 @@ export const useItemData = (
const { t } = useTranslation();
const AvatarTemplate = useAvatarTemplate();

const highlighted = Boolean(!room.hideUnreadStatus && (room.alert || room.unread));
const { unreadTitle, unreadVariant, showUnread, highlightUnread: highlighted, unreadCount } = useUnreadDisplay(room);

const icon = useMemo(
() => <SidebarItemIcon highlighted={highlighted} icon={<RoomIcon room={room} placement='sidebar' size='x20' />} />,
Expand All @@ -24,28 +25,17 @@ export const useItemData = (
const time = 'lastMessage' in room ? room.lastMessage?.ts : undefined;
const message = viewMode === 'extended' && getMessage(room, room.lastMessage, t);

const threadUnread = Number(room.tunread?.length) > 0;
const isUnread = room.unread > 0 || threadUnread;
const showBadge =
!room.hideUnreadStatus || (!room.hideMentionStatus && (Boolean(room.userMentions) || Number(room.tunreadUser?.length) > 0));
const badgeTitle = getBadgeTitle(room.userMentions, Number(room.tunread?.length), room.groupMentions, room.unread, t);
const variant =
((room.userMentions || room.tunreadUser?.length) && 'danger') ||
(threadUnread && 'primary') ||
(room.groupMentions && 'warning') ||
'secondary';

const badges = useMemo(
() => (
<>
{showBadge && isUnread && (
<SidebarItemBadge variant={variant} title={badgeTitle}>
{room.unread + (room.tunread?.length || 0)}
{showUnread && (
<SidebarItemBadge variant={unreadVariant} title={unreadTitle}>
{unreadCount.total}
</SidebarItemBadge>
)}
</>
),
[badgeTitle, isUnread, room.tunread?.length, room.unread, showBadge, variant],
[showUnread, unreadCount.total, unreadTitle, unreadVariant],
);

const itemData = useMemo(
Expand Down
Loading