From 0e919ce4854c5f66870f4465b8e1c4b793960025 Mon Sep 17 00:00:00 2001
From: su-ex <codeworks@supercable.onl>
Date: Thu, 30 May 2024 14:35:16 +0200
Subject: On space switch don't automatically show last opened room

---
 src/components/structures/MatrixChat.tsx | 18 +++++++++++++++++-
 src/settings/Settings.tsx                |  6 ++++++
 src/stores/spaces/SpaceStore.ts          |  1 +
 3 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/src/components/structures/MatrixChat.tsx b/src/components/structures/MatrixChat.tsx
index 0f722ae53d..47f09400d7 100644
--- a/src/components/structures/MatrixChat.tsx
+++ b/src/components/structures/MatrixChat.tsx
@@ -1456,7 +1456,15 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
         if (this.screenAfterLogin && this.screenAfterLogin.screen) {
             this.showScreen(this.screenAfterLogin.screen, this.screenAfterLogin.params);
             this.screenAfterLogin = undefined;
-        } else if (localStorage && localStorage.getItem("mx_last_room_id")) {
+        } else if (
+            !SettingsStore.getValue("Spaces.returnToPreviouslyOpenedRoom") &&
+            localStorage &&
+            localStorage.getItem("mx_active_space") &&
+            localStorage.getItem("mx_active_space")[0] === "!"
+        ) {
+            // SC: Show the last viewed space
+            this.viewLastSpace();
+        } else if (SettingsStore.getValue("Spaces.returnToPreviouslyOpenedRoom") && localStorage && localStorage.getItem("mx_last_room_id")) {
             // Before defaulting to directory, show the last viewed room
             this.viewLastRoom();
         } else {
@@ -1468,6 +1476,14 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
         }
     }
 
+    private viewLastSpace(): void { // SC
+        // SC-ToDo: Make this work for meta spaces
+        dis.dispatch({
+            action: Action.ViewRoom,
+            room_id: localStorage.getItem("mx_active_space"),
+        });
+    }
+
     private viewLastRoom(): void {
         dis.dispatch<ViewRoomPayload>({
             action: Action.ViewRoom,
diff --git a/src/settings/Settings.tsx b/src/settings/Settings.tsx
index b6cde99d81..61a17d09ae 100644
--- a/src/settings/Settings.tsx
+++ b/src/settings/Settings.tsx
@@ -370,6 +370,12 @@ export const SETTINGS: Settings = {
         supportedLevels: LEVELS_ACCOUNT_SETTINGS,
         default: true,
     },
+    "Spaces.returnToPreviouslyOpenedRoom": {
+        displayName: _td("Return to the room previously opened in a space"),
+        description: _td("If disabled, the space overview will be shown when switching to another space."),
+        supportedLevels: LEVELS_ACCOUNT_SETTINGS,
+        default: false,
+    },
     // SC settings end
     "feature_video_rooms": {
         isFeature: true,
diff --git a/src/stores/spaces/SpaceStore.ts b/src/stores/spaces/SpaceStore.ts
index fcd7ff659b..33407f74e8 100644
--- a/src/stores/spaces/SpaceStore.ts
+++ b/src/stores/spaces/SpaceStore.ts
@@ -288,6 +288,7 @@ export class SpaceStoreClass extends AsyncStoreWithClient<EmptyObject> {
             // else if the last viewed room in this space is joined then view that
             // else view space home or home depending on what is being clicked on
             if (
+                SettingsStore.getValue("Spaces.returnToPreviouslyOpenedRoom") &&
                 roomId &&
                 cliSpace?.getMyMembership() !== KnownMembership.Invite &&
                 this.matrixClient.getRoom(roomId)?.getMyMembership() === KnownMembership.Join &&
-- 
2.49.0

