From 2270ebece46e482d0357c7691070c1be7671ffe7 Mon Sep 17 00:00:00 2001 From: Robin Townsend Date: Thu, 22 Jul 2021 08:03:22 -0400 Subject: [PATCH 1/6] Go to space landing page when clicking on a selected space Signed-off-by: Robin Townsend --- src/components/views/spaces/SpaceTreeLevel.tsx | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/components/views/spaces/SpaceTreeLevel.tsx b/src/components/views/spaces/SpaceTreeLevel.tsx index c4a2a8f9d37..3c50a15d250 100644 --- a/src/components/views/spaces/SpaceTreeLevel.tsx +++ b/src/components/views/spaces/SpaceTreeLevel.tsx @@ -177,7 +177,15 @@ export class SpaceItem extends React.PureComponent { private onClick = (ev: React.MouseEvent) => { ev.preventDefault(); ev.stopPropagation(); - SpaceStore.instance.setActiveSpace(this.props.space); + + if (this.props.activeSpaces.includes(this.props.space)) { + defaultDispatcher.dispatch({ + action: "view_room", + room_id: this.props.space.roomId, + }); + } else { + SpaceStore.instance.setActiveSpace(this.props.space); + } }; private onMenuOpenClick = (ev: React.MouseEvent) => { From 088e6a8ebbea0e2fe46fb5045f2f77c7993c166f Mon Sep 17 00:00:00 2001 From: Robin Townsend Date: Thu, 22 Jul 2021 08:06:41 -0400 Subject: [PATCH 2/6] Go to home landing page when clicking on selected home space Signed-off-by: Robin Townsend --- src/components/views/spaces/SpacePanel.tsx | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/components/views/spaces/SpacePanel.tsx b/src/components/views/spaces/SpacePanel.tsx index 1c4043f1504..313a0c82669 100644 --- a/src/components/views/spaces/SpacePanel.tsx +++ b/src/components/views/spaces/SpacePanel.tsx @@ -20,6 +20,7 @@ import classNames from "classnames"; import { Room } from "matrix-js-sdk/src/models/room"; import { _t } from "../../../languageHandler"; +import dis from "../../../dispatcher/dispatcher"; import RoomAvatar from "../avatars/RoomAvatar"; import { useContextMenu } from "../../structures/ContextMenu"; import SpaceCreateMenu from "./SpaceCreateMenu"; @@ -140,10 +141,14 @@ const InnerSpacePanel = React.memo(({ children, isPanelCo const homeNotificationState = SpaceStore.spacesTweakAllRoomsEnabled ? RoomNotificationStateStore.instance.globalState : SpaceStore.instance.getNotificationState(HOME_SPACE); + const onHomeClick = activeSpace ? + () => SpaceStore.instance.setActiveSpace(null) : + () => dis.dispatch({ action: "view_home_page" }); + return
SpaceStore.instance.setActiveSpace(null)} + onClick={onHomeClick} selected={!activeSpace} tooltip={SpaceStore.spacesTweakAllRoomsEnabled ? _t("All rooms") : _t("Home")} notificationState={homeNotificationState} From a35e4c51a1cd983e1ff4ce2688d4c9e8f9c3e173 Mon Sep 17 00:00:00 2001 From: Robin Townsend Date: Wed, 11 May 2022 13:25:31 -0400 Subject: [PATCH 3/6] Remove metaspace behavior --- src/components/views/spaces/SpaceTreeLevel.tsx | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/components/views/spaces/SpaceTreeLevel.tsx b/src/components/views/spaces/SpaceTreeLevel.tsx index 707e05334f4..052303b5f9f 100644 --- a/src/components/views/spaces/SpaceTreeLevel.tsx +++ b/src/components/views/spaces/SpaceTreeLevel.tsx @@ -104,11 +104,9 @@ export const SpaceButton: React.FC = ({ />; } - const onClick = props.onClick ?? (selected ? - (space ? - () => dis.dispatch({ action: "view_room", room_id: space.roomId }) : - () => dis.dispatch({ action: "view_home_page" })) : - () => SpaceStore.instance.setActiveSpace(spaceKey ?? space.roomId)); + const onClick = props.onClick ?? (selected && space + ? () => dis.dispatch({ action: "view_room", room_id: space.roomId }) + : () => SpaceStore.instance.setActiveSpace(spaceKey ?? space.roomId)); return ( Date: Mon, 16 May 2022 15:06:49 -0400 Subject: [PATCH 4/6] Add tests --- .../views/spaces/SpaceTreeLevel-test.tsx | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 test/components/views/spaces/SpaceTreeLevel-test.tsx diff --git a/test/components/views/spaces/SpaceTreeLevel-test.tsx b/test/components/views/spaces/SpaceTreeLevel-test.tsx new file mode 100644 index 00000000000..09661c71f4f --- /dev/null +++ b/test/components/views/spaces/SpaceTreeLevel-test.tsx @@ -0,0 +1,85 @@ +/* +Copyright 2022 The Matrix.org Foundation C.I.C. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +import React from "react"; +import { mount } from "enzyme"; + +import { stubClient, mkRoom } from "../../../test-utils"; +import { MatrixClientPeg } from "../../../../src/MatrixClientPeg"; +import DMRoomMap from "../../../../src/utils/DMRoomMap"; +import defaultDispatcher from "../../../../src/dispatcher/dispatcher"; +import { Action } from "../../../../src/dispatcher/actions"; +import { SpaceButton } from "../../../../src/components/views/spaces/SpaceTreeLevel"; +import { MetaSpace, SpaceKey } from "../../../../src/stores/spaces"; +import SpaceStore from "../../../../src/stores/spaces/SpaceStore"; + +jest.mock("../../../../src/stores/spaces/SpaceStore", () => { + // eslint-disable-next-line @typescript-eslint/no-var-requires + const EventEmitter = require("events"); + class MockSpaceStore extends EventEmitter { + activeSpace: SpaceKey = "!space1"; + setActiveSpace = jest.fn(); + } + + return { instance: new MockSpaceStore() }; +}); + +describe("SpaceButton", () => { + stubClient(); + const space = mkRoom(MatrixClientPeg.get(), "!1:example.org"); + DMRoomMap.makeShared(); + + const dispatchSpy = jest.spyOn(defaultDispatcher, "dispatch"); + + afterEach(jest.clearAllMocks); + + describe("real space", () => { + it("activates the space on click", () => { + const button = mount(); + + expect(SpaceStore.instance.setActiveSpace).not.toHaveBeenCalled(); + button.simulate("click"); + expect(SpaceStore.instance.setActiveSpace).toHaveBeenCalledWith("!1:example.org"); + }); + + it("navigates to the space home on click if already active", () => { + const button = mount(); + + expect(dispatchSpy).not.toHaveBeenCalled(); + button.simulate("click"); + expect(dispatchSpy).toHaveBeenCalledWith({ action: Action.ViewRoom, room_id: "!1:example.org" }); + }); + }); + + describe("metaspace", () => { + it("activates the metaspace on click", () => { + const button = mount(); + + expect(SpaceStore.instance.setActiveSpace).not.toHaveBeenCalled(); + button.simulate("click"); + expect(SpaceStore.instance.setActiveSpace).toHaveBeenCalledWith(MetaSpace.People); + }); + + it("does nothing on click if already active", () => { + const button = mount(); + + button.simulate("click"); + expect(dispatchSpy).not.toHaveBeenCalled(); + // Re-activating the metaspace is a no-op + expect(SpaceStore.instance.setActiveSpace).toHaveBeenCalledWith(MetaSpace.People); + }); + }); +}); From c694ed3641c7305d1ce94246b56e599b1e1da33d Mon Sep 17 00:00:00 2001 From: Robin Townsend Date: Mon, 16 May 2022 15:12:11 -0400 Subject: [PATCH 5/6] Use the dispatcher action enum --- src/components/views/spaces/SpaceTreeLevel.tsx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/components/views/spaces/SpaceTreeLevel.tsx b/src/components/views/spaces/SpaceTreeLevel.tsx index 052303b5f9f..6b8d2e3ccea 100644 --- a/src/components/views/spaces/SpaceTreeLevel.tsx +++ b/src/components/views/spaces/SpaceTreeLevel.tsx @@ -25,7 +25,8 @@ import { SpaceKey } from "../../../stores/spaces"; import SpaceTreeLevelLayoutStore from "../../../stores/spaces/SpaceTreeLevelLayoutStore"; import NotificationBadge from "../rooms/NotificationBadge"; import { _t } from "../../../languageHandler"; -import dis from "../../../dispatcher/dispatcher"; +import defaultDispatcher from "../../../dispatcher/dispatcher"; +import { Action } from "../../../dispatcher/actions"; import { ContextMenuTooltipButton } from "../../../accessibility/context_menu/ContextMenuTooltipButton"; import { toRightOf, useContextMenu } from "../../structures/ContextMenu"; import MatrixClientContext from "../../../contexts/MatrixClientContext"; @@ -105,7 +106,7 @@ export const SpaceButton: React.FC = ({ } const onClick = props.onClick ?? (selected && space - ? () => dis.dispatch({ action: "view_room", room_id: space.roomId }) + ? () => defaultDispatcher.dispatch({ action: Action.ViewRoom, room_id: space.roomId }) : () => SpaceStore.instance.setActiveSpace(spaceKey ?? space.roomId)); return ( From 2d14572aa339c3d53c09ffb79a02d0cd2ff16be8 Mon Sep 17 00:00:00 2001 From: Robin Townsend Date: Tue, 17 May 2022 09:15:05 -0400 Subject: [PATCH 6/6] Break up the onClick assignment --- src/components/views/spaces/SpaceTreeLevel.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/components/views/spaces/SpaceTreeLevel.tsx b/src/components/views/spaces/SpaceTreeLevel.tsx index 6b8d2e3ccea..d9664150613 100644 --- a/src/components/views/spaces/SpaceTreeLevel.tsx +++ b/src/components/views/spaces/SpaceTreeLevel.tsx @@ -105,9 +105,9 @@ export const SpaceButton: React.FC = ({ />; } - const onClick = props.onClick ?? (selected && space - ? () => defaultDispatcher.dispatch({ action: Action.ViewRoom, room_id: space.roomId }) - : () => SpaceStore.instance.setActiveSpace(spaceKey ?? space.roomId)); + const viewSpaceHome = () => defaultDispatcher.dispatch({ action: Action.ViewRoom, room_id: space.roomId }); + const activateSpace = () => SpaceStore.instance.setActiveSpace(spaceKey ?? space.roomId); + const onClick = props.onClick ?? (selected && space ? viewSpaceHome : activateSpace); return (