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

Commit d3da171

Browse files
authored
Show room create icon if "UIComponent.roomCreation" is enabled (#10364)
* Show room create button in RoomSublist if "UIComponent.roomCreation" is enabled Signed-off-by: Mikhail Aheichyk <[email protected]> * Revert "Show room create button in RoomSublist if "UIComponent.roomCreation" is enabled" This reverts commit a332a0b. * Use UIComponent.ExploreRooms to display menu item "Explore public rooms" ("Explore rooms" in case of space room) in context menu of "Rooms" section of RoomList component. The context menu will be available if one of UIComponent.ExploreRooms or UIComponent.CreateRooms is enabled. Signed-off-by: Mikhail Aheichyk <[email protected]> * "Explore rooms" of space room should not be controlled by UIComponent.ExploreRooms. Signed-off-by: Mikhail Aheichyk <[email protected]> --------- Signed-off-by: Mikhail Aheichyk <[email protected]> Co-authored-by: Mikhail Aheichyk <[email protected]>
1 parent 24eed96 commit d3da171

File tree

2 files changed

+244
-26
lines changed

2 files changed

+244
-26
lines changed

src/components/views/rooms/RoomList.tsx

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ const UntaggedAuxButton: React.FC<IAuxButtonProps> = ({ tabIndex }) => {
213213
});
214214

215215
const showCreateRoom = shouldShowComponent(UIComponent.CreateRooms);
216+
const showExploreRooms = shouldShowComponent(UIComponent.ExploreRooms);
217+
216218
const videoRoomsEnabled = useFeatureEnabled("feature_video_rooms");
217219
const elementCallVideoRoomsEnabled = useFeatureEnabled("feature_element_call_video_rooms");
218220

@@ -337,17 +339,19 @@ const UntaggedAuxButton: React.FC<IAuxButtonProps> = ({ tabIndex }) => {
337339
)}
338340
</>
339341
)}
340-
<IconizedContextMenuOption
341-
label={_t("Explore public rooms")}
342-
iconClassName="mx_RoomList_iconExplore"
343-
onClick={(e) => {
344-
e.preventDefault();
345-
e.stopPropagation();
346-
closeMenu();
347-
PosthogTrackers.trackInteraction("WebRoomListRoomsSublistPlusMenuExploreRoomsItem", e);
348-
defaultDispatcher.fire(Action.ViewRoomDirectory);
349-
}}
350-
/>
342+
{showExploreRooms ? (
343+
<IconizedContextMenuOption
344+
label={_t("Explore public rooms")}
345+
iconClassName="mx_RoomList_iconExplore"
346+
onClick={(e) => {
347+
e.preventDefault();
348+
e.stopPropagation();
349+
closeMenu();
350+
PosthogTrackers.trackInteraction("WebRoomListRoomsSublistPlusMenuExploreRoomsItem", e);
351+
defaultDispatcher.fire(Action.ViewRoomDirectory);
352+
}}
353+
/>
354+
) : null}
351355
</IconizedContextMenuOptionList>
352356
);
353357
}
@@ -361,22 +365,26 @@ const UntaggedAuxButton: React.FC<IAuxButtonProps> = ({ tabIndex }) => {
361365
);
362366
}
363367

364-
return (
365-
<>
366-
<ContextMenuTooltipButton
367-
tabIndex={tabIndex}
368-
onClick={openMenu}
369-
className="mx_RoomSublist_auxButton"
370-
tooltipClassName="mx_RoomSublist_addRoomTooltip"
371-
aria-label={_t("Add room")}
372-
title={_t("Add room")}
373-
isExpanded={menuDisplayed}
374-
inputRef={handle}
375-
/>
368+
if (showCreateRoom || showExploreRooms) {
369+
return (
370+
<>
371+
<ContextMenuTooltipButton
372+
tabIndex={tabIndex}
373+
onClick={openMenu}
374+
className="mx_RoomSublist_auxButton"
375+
tooltipClassName="mx_RoomSublist_addRoomTooltip"
376+
aria-label={_t("Add room")}
377+
title={_t("Add room")}
378+
isExpanded={menuDisplayed}
379+
inputRef={handle}
380+
/>
376381

377-
{contextMenu}
378-
</>
379-
);
382+
{contextMenu}
383+
</>
384+
);
385+
}
386+
387+
return null;
380388
};
381389

382390
const TAG_AESTHETICS: TagAestheticsMap = {
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
/*
2+
Copyright 2023 Mikhail Aheichyk
3+
Copyright 2023 Nordeck IT + Consulting GmbH.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
import React, { ComponentProps } from "react";
19+
import { render, screen, within } from "@testing-library/react";
20+
import userEvent from "@testing-library/user-event";
21+
import { mocked } from "jest-mock";
22+
import { Room } from "matrix-js-sdk/src/models/room";
23+
24+
import RoomList from "../../../../src/components/views/rooms/RoomList";
25+
import ResizeNotifier from "../../../../src/utils/ResizeNotifier";
26+
import { MetaSpace } from "../../../../src/stores/spaces";
27+
import { shouldShowComponent } from "../../../../src/customisations/helpers/UIComponents";
28+
import { UIComponent } from "../../../../src/settings/UIFeature";
29+
import dis from "../../../../src/dispatcher/dispatcher";
30+
import { Action } from "../../../../src/dispatcher/actions";
31+
import * as testUtils from "../../../test-utils";
32+
import { mkSpace, stubClient } from "../../../test-utils";
33+
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
34+
import SpaceStore from "../../../../src/stores/spaces/SpaceStore";
35+
import DMRoomMap from "../../../../src/utils/DMRoomMap";
36+
37+
jest.mock("../../../../src/customisations/helpers/UIComponents", () => ({
38+
shouldShowComponent: jest.fn(),
39+
}));
40+
41+
jest.mock("../../../../src/dispatcher/dispatcher");
42+
43+
const getUserIdForRoomId = jest.fn();
44+
const getDMRoomsForUserId = jest.fn();
45+
// @ts-ignore
46+
DMRoomMap.sharedInstance = { getUserIdForRoomId, getDMRoomsForUserId };
47+
48+
describe("RoomList", () => {
49+
stubClient();
50+
const client = MatrixClientPeg.get();
51+
const store = SpaceStore.instance;
52+
53+
function getComponent(props: Partial<ComponentProps<typeof RoomList>> = {}): JSX.Element {
54+
return (
55+
<RoomList
56+
onKeyDown={jest.fn()}
57+
onFocus={jest.fn()}
58+
onBlur={jest.fn()}
59+
onResize={jest.fn()}
60+
resizeNotifier={new ResizeNotifier()}
61+
isMinimized={false}
62+
activeSpace={MetaSpace.Home}
63+
{...props}
64+
/>
65+
);
66+
}
67+
68+
describe("Rooms", () => {
69+
describe("when meta space is active", () => {
70+
beforeEach(() => {
71+
store.setActiveSpace(MetaSpace.Home);
72+
});
73+
74+
it("does not render add room button when UIComponent customisation disables CreateRooms and ExploreRooms", () => {
75+
const disabled: UIComponent[] = [UIComponent.CreateRooms, UIComponent.ExploreRooms];
76+
mocked(shouldShowComponent).mockImplementation((feature) => !disabled.includes(feature));
77+
render(getComponent());
78+
79+
const roomsList = screen.getByRole("group", { name: "Rooms" });
80+
expect(within(roomsList).queryByRole("button", { name: "Add room" })).not.toBeInTheDocument();
81+
});
82+
83+
it("renders add room button with menu when UIComponent customisation allows CreateRooms or ExploreRooms", async () => {
84+
let disabled: UIComponent[] = [];
85+
mocked(shouldShowComponent).mockImplementation((feature) => !disabled.includes(feature));
86+
const { rerender } = render(getComponent());
87+
88+
const roomsList = screen.getByRole("group", { name: "Rooms" });
89+
const addRoomButton = within(roomsList).getByRole("button", { name: "Add room" });
90+
expect(screen.queryByRole("menu")).not.toBeInTheDocument();
91+
92+
await userEvent.click(addRoomButton);
93+
94+
const menu = screen.getByRole("menu");
95+
96+
expect(within(menu).getByRole("menuitem", { name: "New room" })).toBeInTheDocument();
97+
expect(within(menu).getByRole("menuitem", { name: "Explore public rooms" })).toBeInTheDocument();
98+
99+
disabled = [UIComponent.CreateRooms];
100+
rerender(getComponent());
101+
102+
expect(addRoomButton).toBeInTheDocument();
103+
expect(menu).toBeInTheDocument();
104+
expect(within(menu).queryByRole("menuitem", { name: "New room" })).not.toBeInTheDocument();
105+
expect(within(menu).getByRole("menuitem", { name: "Explore public rooms" })).toBeInTheDocument();
106+
107+
disabled = [UIComponent.ExploreRooms];
108+
rerender(getComponent());
109+
110+
expect(addRoomButton).toBeInTheDocument();
111+
expect(menu).toBeInTheDocument();
112+
expect(within(menu).getByRole("menuitem", { name: "New room" })).toBeInTheDocument();
113+
expect(within(menu).queryByRole("menuitem", { name: "Explore public rooms" })).not.toBeInTheDocument();
114+
});
115+
116+
it("renders add room button and clicks explore public rooms", async () => {
117+
mocked(shouldShowComponent).mockReturnValue(true);
118+
render(getComponent());
119+
120+
const roomsList = screen.getByRole("group", { name: "Rooms" });
121+
await userEvent.click(within(roomsList).getByRole("button", { name: "Add room" }));
122+
123+
const menu = screen.getByRole("menu");
124+
await userEvent.click(within(menu).getByRole("menuitem", { name: "Explore public rooms" }));
125+
126+
expect(dis.fire).toHaveBeenCalledWith(Action.ViewRoomDirectory);
127+
});
128+
});
129+
130+
describe("when room space is active", () => {
131+
let rooms: Room[];
132+
const mkSpaceForRooms = (spaceId: string, children: string[] = []) =>
133+
mkSpace(client, spaceId, rooms, children);
134+
135+
const space1 = "!space1:server";
136+
137+
beforeEach(async () => {
138+
rooms = [];
139+
mkSpaceForRooms(space1);
140+
mocked(client).getRoom.mockImplementation(
141+
(roomId) => rooms.find((room) => room.roomId === roomId) || null,
142+
);
143+
await testUtils.setupAsyncStoreWithClient(store, client);
144+
145+
store.setActiveSpace(space1);
146+
});
147+
148+
it("does not render add room button when UIComponent customisation disables CreateRooms and ExploreRooms", () => {
149+
const disabled: UIComponent[] = [UIComponent.CreateRooms, UIComponent.ExploreRooms];
150+
mocked(shouldShowComponent).mockImplementation((feature) => !disabled.includes(feature));
151+
render(getComponent());
152+
153+
const roomsList = screen.getByRole("group", { name: "Rooms" });
154+
expect(within(roomsList).queryByRole("button", { name: "Add room" })).not.toBeInTheDocument();
155+
});
156+
157+
it("renders add room button with menu when UIComponent customisation allows CreateRooms or ExploreRooms", async () => {
158+
let disabled: UIComponent[] = [];
159+
mocked(shouldShowComponent).mockImplementation((feature) => !disabled.includes(feature));
160+
const { rerender } = render(getComponent());
161+
162+
const roomsList = screen.getByRole("group", { name: "Rooms" });
163+
const addRoomButton = within(roomsList).getByRole("button", { name: "Add room" });
164+
expect(screen.queryByRole("menu")).not.toBeInTheDocument();
165+
166+
await userEvent.click(addRoomButton);
167+
168+
const menu = screen.getByRole("menu");
169+
170+
expect(within(menu).getByRole("menuitem", { name: "Explore rooms" })).toBeInTheDocument();
171+
expect(within(menu).getByRole("menuitem", { name: "New room" })).toBeInTheDocument();
172+
expect(within(menu).getByRole("menuitem", { name: "Add existing room" })).toBeInTheDocument();
173+
174+
disabled = [UIComponent.CreateRooms];
175+
rerender(getComponent());
176+
177+
expect(addRoomButton).toBeInTheDocument();
178+
expect(menu).toBeInTheDocument();
179+
expect(within(menu).getByRole("menuitem", { name: "Explore rooms" })).toBeInTheDocument();
180+
expect(within(menu).queryByRole("menuitem", { name: "New room" })).not.toBeInTheDocument();
181+
expect(within(menu).queryByRole("menuitem", { name: "Add existing room" })).not.toBeInTheDocument();
182+
183+
disabled = [UIComponent.ExploreRooms];
184+
rerender(getComponent());
185+
186+
expect(addRoomButton).toBeInTheDocument();
187+
expect(menu).toBeInTheDocument();
188+
expect(within(menu).queryByRole("menuitem", { name: "Explore rooms" })).toBeInTheDocument();
189+
expect(within(menu).getByRole("menuitem", { name: "New room" })).toBeInTheDocument();
190+
expect(within(menu).getByRole("menuitem", { name: "Add existing room" })).toBeInTheDocument();
191+
});
192+
193+
it("renders add room button and clicks explore rooms", async () => {
194+
mocked(shouldShowComponent).mockReturnValue(true);
195+
render(getComponent());
196+
197+
const roomsList = screen.getByRole("group", { name: "Rooms" });
198+
await userEvent.click(within(roomsList).getByRole("button", { name: "Add room" }));
199+
200+
const menu = screen.getByRole("menu");
201+
await userEvent.click(within(menu).getByRole("menuitem", { name: "Explore rooms" }));
202+
203+
expect(dis.dispatch).toHaveBeenCalledWith({
204+
action: Action.ViewRoom,
205+
room_id: space1,
206+
});
207+
});
208+
});
209+
});
210+
});

0 commit comments

Comments
 (0)