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

Commit 796e12e

Browse files
authored
Go to space landing page when clicking on a selected space (#6442)
* Go to space landing page when clicking on a selected space Signed-off-by: Robin Townsend <[email protected]> * Go to home landing page when clicking on selected home space Signed-off-by: Robin Townsend <[email protected]> * Remove metaspace behavior * Add tests * Use the dispatcher action enum * Break up the onClick assignment
1 parent 007b881 commit 796e12e

File tree

2 files changed

+92
-8
lines changed

2 files changed

+92
-8
lines changed

src/components/views/spaces/SpaceTreeLevel.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import { SpaceKey } from "../../../stores/spaces";
2525
import SpaceTreeLevelLayoutStore from "../../../stores/spaces/SpaceTreeLevelLayoutStore";
2626
import NotificationBadge from "../rooms/NotificationBadge";
2727
import { _t } from "../../../languageHandler";
28+
import defaultDispatcher from "../../../dispatcher/dispatcher";
29+
import { Action } from "../../../dispatcher/actions";
2830
import { ContextMenuTooltipButton } from "../../../accessibility/context_menu/ContextMenuTooltipButton";
2931
import { toRightOf, useContextMenu } from "../../structures/ContextMenu";
3032
import MatrixClientContext from "../../../contexts/MatrixClientContext";
@@ -103,6 +105,10 @@ export const SpaceButton: React.FC<IButtonProps> = ({
103105
/>;
104106
}
105107

108+
const viewSpaceHome = () => defaultDispatcher.dispatch({ action: Action.ViewRoom, room_id: space.roomId });
109+
const activateSpace = () => SpaceStore.instance.setActiveSpace(spaceKey ?? space.roomId);
110+
const onClick = props.onClick ?? (selected && space ? viewSpaceHome : activateSpace);
111+
106112
return (
107113
<AccessibleTooltipButton
108114
{...props}
@@ -112,7 +118,7 @@ export const SpaceButton: React.FC<IButtonProps> = ({
112118
mx_SpaceButton_narrow: isNarrow,
113119
})}
114120
title={label}
115-
onClick={spaceKey ? () => SpaceStore.instance.setActiveSpace(spaceKey) : props.onClick}
121+
onClick={onClick}
116122
onContextMenu={openMenu}
117123
forceHide={!isNarrow || menuDisplayed}
118124
inputRef={handle}
@@ -261,12 +267,6 @@ export class SpaceItem extends React.PureComponent<IItemProps, IItemState> {
261267
}
262268
};
263269

264-
private onClick = (ev: React.MouseEvent) => {
265-
ev.preventDefault();
266-
ev.stopPropagation();
267-
SpaceStore.instance.setActiveSpace(this.props.space.roomId);
268-
};
269-
270270
render() {
271271
// eslint-disable-next-line @typescript-eslint/no-unused-vars
272272
const { space, activeSpaces, isNested, isPanelCollapsed, onExpand, parents, innerRef, dragHandleProps,
@@ -328,7 +328,6 @@ export class SpaceItem extends React.PureComponent<IItemProps, IItemState> {
328328
notificationState={notificationState}
329329
isNarrow={isPanelCollapsed}
330330
avatarSize={isNested ? 24 : 32}
331-
onClick={this.onClick}
332331
onKeyDown={this.onKeyDown}
333332
ContextMenuComponent={this.props.space.getMyMembership() === "join" ? SpaceContextMenu : undefined}
334333
>
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import React from "react";
18+
import { mount } from "enzyme";
19+
20+
import { stubClient, mkRoom } from "../../../test-utils";
21+
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
22+
import DMRoomMap from "../../../../src/utils/DMRoomMap";
23+
import defaultDispatcher from "../../../../src/dispatcher/dispatcher";
24+
import { Action } from "../../../../src/dispatcher/actions";
25+
import { SpaceButton } from "../../../../src/components/views/spaces/SpaceTreeLevel";
26+
import { MetaSpace, SpaceKey } from "../../../../src/stores/spaces";
27+
import SpaceStore from "../../../../src/stores/spaces/SpaceStore";
28+
29+
jest.mock("../../../../src/stores/spaces/SpaceStore", () => {
30+
// eslint-disable-next-line @typescript-eslint/no-var-requires
31+
const EventEmitter = require("events");
32+
class MockSpaceStore extends EventEmitter {
33+
activeSpace: SpaceKey = "!space1";
34+
setActiveSpace = jest.fn();
35+
}
36+
37+
return { instance: new MockSpaceStore() };
38+
});
39+
40+
describe("SpaceButton", () => {
41+
stubClient();
42+
const space = mkRoom(MatrixClientPeg.get(), "!1:example.org");
43+
DMRoomMap.makeShared();
44+
45+
const dispatchSpy = jest.spyOn(defaultDispatcher, "dispatch");
46+
47+
afterEach(jest.clearAllMocks);
48+
49+
describe("real space", () => {
50+
it("activates the space on click", () => {
51+
const button = mount(<SpaceButton space={space} selected={false} label="My space" />);
52+
53+
expect(SpaceStore.instance.setActiveSpace).not.toHaveBeenCalled();
54+
button.simulate("click");
55+
expect(SpaceStore.instance.setActiveSpace).toHaveBeenCalledWith("!1:example.org");
56+
});
57+
58+
it("navigates to the space home on click if already active", () => {
59+
const button = mount(<SpaceButton space={space} selected={true} label="My space" />);
60+
61+
expect(dispatchSpy).not.toHaveBeenCalled();
62+
button.simulate("click");
63+
expect(dispatchSpy).toHaveBeenCalledWith({ action: Action.ViewRoom, room_id: "!1:example.org" });
64+
});
65+
});
66+
67+
describe("metaspace", () => {
68+
it("activates the metaspace on click", () => {
69+
const button = mount(<SpaceButton spaceKey={MetaSpace.People} selected={false} label="People" />);
70+
71+
expect(SpaceStore.instance.setActiveSpace).not.toHaveBeenCalled();
72+
button.simulate("click");
73+
expect(SpaceStore.instance.setActiveSpace).toHaveBeenCalledWith(MetaSpace.People);
74+
});
75+
76+
it("does nothing on click if already active", () => {
77+
const button = mount(<SpaceButton spaceKey={MetaSpace.People} selected={true} label="People" />);
78+
79+
button.simulate("click");
80+
expect(dispatchSpy).not.toHaveBeenCalled();
81+
// Re-activating the metaspace is a no-op
82+
expect(SpaceStore.instance.setActiveSpace).toHaveBeenCalledWith(MetaSpace.People);
83+
});
84+
});
85+
});

0 commit comments

Comments
 (0)