Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit cdffd1c

Browse files
authored
Fix missing metaspace notification badges (#11269)
* Fix missing metaspace notification badges * Simplify conditional types
1 parent eced103 commit cdffd1c

File tree

3 files changed

+78
-6
lines changed

3 files changed

+78
-6
lines changed

src/components/views/spaces/SpaceTreeLevel.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ interface IButtonProps extends Omit<ComponentProps<typeof AccessibleTooltipButto
6565

6666
export const SpaceButton: React.FC<IButtonProps> = ({
6767
space,
68-
spaceKey,
68+
spaceKey: _spaceKey,
6969
className,
7070
selected,
7171
label,
@@ -82,6 +82,8 @@ export const SpaceButton: React.FC<IButtonProps> = ({
8282
const [onFocus, isActive] = useRovingTabIndex(handle);
8383
const tabIndex = isActive ? 0 : -1;
8484

85+
const spaceKey = _spaceKey ?? space?.roomId;
86+
8587
let avatar = (
8688
<div className="mx_SpaceButton_avatarPlaceholder">
8789
<div className="mx_SpaceButton_icon" />
@@ -92,16 +94,16 @@ export const SpaceButton: React.FC<IButtonProps> = ({
9294
}
9395

9496
let notifBadge;
95-
if (space && notificationState) {
97+
if (spaceKey && notificationState) {
9698
let ariaLabel = _t("Jump to first unread room.");
97-
if (space.getMyMembership() === "invite") {
99+
if (space?.getMyMembership() === "invite") {
98100
ariaLabel = _t("Jump to first invite.");
99101
}
100102

101103
const jumpToNotification = (ev: MouseEvent): void => {
102104
ev.stopPropagation();
103105
ev.preventDefault();
104-
SpaceStore.instance.setActiveRoomInSpace(spaceKey ?? space.roomId);
106+
SpaceStore.instance.setActiveRoomInSpace(spaceKey);
105107
};
106108

107109
notifBadge = (
@@ -132,7 +134,9 @@ export const SpaceButton: React.FC<IButtonProps> = ({
132134
const viewSpaceHome = (): void =>
133135
// space is set here because of the assignment condition of onClick
134136
defaultDispatcher.dispatch({ action: Action.ViewRoom, room_id: space!.roomId });
135-
const activateSpace = (): void => SpaceStore.instance.setActiveSpace(spaceKey ?? space?.roomId ?? "");
137+
const activateSpace = (): void => {
138+
if (spaceKey) SpaceStore.instance.setActiveSpace(spaceKey);
139+
};
136140
const onClick = props.onClick ?? (selected && space ? viewSpaceHome : activateSpace);
137141

138142
return (

test/components/views/spaces/SpaceTreeLevel-test.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@ limitations under the License.
1717
import React from "react";
1818
import { fireEvent, getByTestId, render } from "@testing-library/react";
1919

20-
import { stubClient, mkRoom } from "../../../test-utils";
20+
import { mkRoom, stubClient } from "../../../test-utils";
2121
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
2222
import DMRoomMap from "../../../../src/utils/DMRoomMap";
2323
import defaultDispatcher from "../../../../src/dispatcher/dispatcher";
2424
import { Action } from "../../../../src/dispatcher/actions";
2525
import { SpaceButton } from "../../../../src/components/views/spaces/SpaceTreeLevel";
2626
import { MetaSpace, SpaceKey } from "../../../../src/stores/spaces";
2727
import SpaceStore from "../../../../src/stores/spaces/SpaceStore";
28+
import { StaticNotificationState } from "../../../../src/stores/notifications/StaticNotificationState";
29+
import { NotificationColor } from "../../../../src/stores/notifications/NotificationColor";
2830

2931
jest.mock("../../../../src/stores/spaces/SpaceStore", () => {
3032
// eslint-disable-next-line @typescript-eslint/no-var-requires
@@ -99,5 +101,22 @@ describe("SpaceButton", () => {
99101
// Re-activating the metaspace is a no-op
100102
expect(SpaceStore.instance.setActiveSpace).toHaveBeenCalledWith(MetaSpace.People);
101103
});
104+
105+
it("should render notificationState if one is provided", () => {
106+
const notificationState = new StaticNotificationState(null, 8, NotificationColor.Grey);
107+
108+
const { container, asFragment } = render(
109+
<SpaceButton
110+
spaceKey={MetaSpace.People}
111+
selected={true}
112+
label="People"
113+
data-testid="create-space-button"
114+
notificationState={notificationState}
115+
/>,
116+
);
117+
118+
expect(container.querySelector(".mx_NotificationBadge_count")).toHaveTextContent("8");
119+
expect(asFragment()).toMatchSnapshot();
120+
});
102121
});
103122
});
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`SpaceButton metaspace should render notificationState if one is provided 1`] = `
4+
<DocumentFragment>
5+
<div
6+
aria-label="People"
7+
class="mx_AccessibleButton mx_SpaceButton mx_SpaceButton_active"
8+
data-testid="create-space-button"
9+
role="button"
10+
tabindex="-1"
11+
>
12+
<div
13+
class="mx_SpaceButton_selectionWrapper"
14+
>
15+
<div
16+
class="mx_SpaceButton_avatarWrapper"
17+
>
18+
<div
19+
class="mx_SpaceButton_avatarPlaceholder"
20+
>
21+
<div
22+
class="mx_SpaceButton_icon"
23+
/>
24+
</div>
25+
<div
26+
class="mx_SpacePanel_badgeContainer"
27+
>
28+
<div
29+
class="mx_AccessibleButton mx_NotificationBadge mx_NotificationBadge_visible mx_NotificationBadge_2char"
30+
role="button"
31+
tabindex="-1"
32+
>
33+
<span
34+
class="mx_NotificationBadge_count"
35+
>
36+
8
37+
</span>
38+
</div>
39+
</div>
40+
</div>
41+
<span
42+
class="mx_SpaceButton_name"
43+
>
44+
People
45+
</span>
46+
</div>
47+
</div>
48+
</DocumentFragment>
49+
`;

0 commit comments

Comments
 (0)