Skip to content

Commit d5b738a

Browse files
committed
feat(room list item): use vm of NotificationDecoration in RoomListItemViewModel
1 parent d3b3cee commit d5b738a

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/components/viewmodels/roomlist/RoomListItemViewModel.tsx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ import dispatcher from "../../../dispatcher/dispatcher";
1212
import type { ViewRoomPayload } from "../../../dispatcher/payloads/ViewRoomPayload";
1313
import { Action } from "../../../dispatcher/actions";
1414
import { hasAccessToOptionsMenu } from "./utils";
15+
import {
16+
type NotificationDecorationViewState,
17+
useNotificationDecorationViewModel,
18+
} from "../notification_decoration/NotificationDecorationViewModel";
19+
import { _t } from "../../../languageHandler";
1520

1621
export interface RoomListItemViewState {
1722
/**
@@ -22,6 +27,14 @@ export interface RoomListItemViewState {
2227
* Open the room having given roomId.
2328
*/
2429
openRoom: () => void;
30+
/**
31+
* The a11y label for the room list item.
32+
*/
33+
a11yLabel: string;
34+
/**
35+
* The notification state of the room.
36+
*/
37+
notificationDecorationViewState: NotificationDecorationViewState;
2538
}
2639

2740
/**
@@ -31,6 +44,11 @@ export interface RoomListItemViewState {
3144
export function useRoomListItemViewModel(room: Room): RoomListItemViewState {
3245
// incoming: Check notification menu rights
3346
const showHoverMenu = hasAccessToOptionsMenu(room);
47+
// Use the VM of the notification decoration because we need to put the a11y label on the room list item
48+
// The VM is passed to the NotificationDecoration component by the RoomListItemView component
49+
// This is a bit of a hack but it's the easiest way to avoid to put twice the same listeners to compute the same notification
50+
const notificationDecorationViewState = useNotificationDecorationViewModel(room);
51+
const a11yLabel = getA11yLabel(room, notificationDecorationViewState);
3452

3553
// Actions
3654

@@ -43,7 +61,38 @@ export function useRoomListItemViewModel(room: Room): RoomListItemViewState {
4361
}, [room]);
4462

4563
return {
64+
notificationDecorationViewState,
4665
showHoverMenu,
4766
openRoom,
67+
a11yLabel,
4868
};
4969
}
70+
71+
/**
72+
* Get the a11y label for the room list item
73+
* @param room
74+
* @param notificationDecorationViewState
75+
*/
76+
function getA11yLabel(room: Room, notificationDecorationViewState: NotificationDecorationViewState): string {
77+
if (notificationDecorationViewState.isMessageNotSent) {
78+
return _t("a11y|room_messsage_not_sent", {
79+
roomName: room.name,
80+
});
81+
} else if (notificationDecorationViewState.isInvite) {
82+
return _t("a11y|room_n_unread_invite", {
83+
roomName: room.name,
84+
});
85+
} else if (notificationDecorationViewState.isHighlighted) {
86+
return _t("a11y|room_n_unread_messages_mentions", {
87+
roomName: room.name,
88+
count: notificationDecorationViewState.rawUnreadCount,
89+
});
90+
} else if (notificationDecorationViewState.hasUnread) {
91+
return _t("a11y|room_n_unread_messages", {
92+
roomName: room.name,
93+
count: notificationDecorationViewState.rawUnreadCount,
94+
});
95+
} else {
96+
return _t("room_list|room|open_room", { roomName: room.name });
97+
}
98+
}

src/i18n/strings/en_EN.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
"other": "%(count)s unread messages including mentions."
1313
},
1414
"recent_rooms": "Recent rooms",
15+
"room_messsage_not_sent": "Open room %(roomName)s with a message not sent",
16+
"room_n_unread_invite": "Open room %(roomName)s invitation",
17+
"room_n_unread_messages": {
18+
"one": "Open room %(roomName)s with 1 unread message.",
19+
"other": "Open room %(roomName)s with %(count)s unread messages."
20+
},
21+
"room_n_unread_messages_mentions": {
22+
"one": "Open room %(roomName)s with 1 unread mention.",
23+
"other": "Open room %(roomName)s with %(count)s unread messages including mentions."
24+
},
1525
"room_name": "Room %(name)s",
1626
"room_status_bar": "Room status bar",
1727
"seek_bar_label": "Audio seek bar",

0 commit comments

Comments
 (0)