From 2dcb60b489e0c53101ab9586f9578277baf2e7ac Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C5=A0imon=20Brandner?= <simon.bra.ag@gmail.com>
Date: Fri, 26 Mar 2021 07:53:07 +0100
Subject: [PATCH] Move persisting of last viewed into SpaceStore
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com>
---
 src/components/structures/MatrixChat.tsx | 10 +---------
 src/stores/SpaceStore.tsx                |  8 ++++++++
 2 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/src/components/structures/MatrixChat.tsx b/src/components/structures/MatrixChat.tsx
index 9a4e83828c..689561fd60 100644
--- a/src/components/structures/MatrixChat.tsx
+++ b/src/components/structures/MatrixChat.tsx
@@ -79,7 +79,7 @@ import { CommunityPrototypeStore } from "../../stores/CommunityPrototypeStore";
 import DialPadModal from "../views/voip/DialPadModal";
 import { showToast as showMobileGuideToast } from '../../toasts/MobileGuideToast';
 import { shouldUseLoginForWelcome } from "../../utils/pages";
-import SpaceStore, {LAST_VIEWED_ROOMS, LAST_VIEWED_ROOMS_HOME} from "../../stores/SpaceStore";
+import SpaceStore from "../../stores/SpaceStore";
 import SpaceRoomDirectory from "./SpaceRoomDirectory";
 import {replaceableComponent} from "../../utils/replaceableComponent";
 import RoomListStore from "../../stores/room-list/RoomListStore";
@@ -875,14 +875,6 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
     private viewRoom(roomInfo: IRoomInfo) {
         this.focusComposer = true;
 
-        // persist last viewed room from a space
-        const activeSpace = SpaceStore.instance.activeSpace;
-        const activeSpaceId = activeSpace?.roomId || LAST_VIEWED_ROOMS_HOME;
-        const lastViewedRooms = JSON.parse(window.localStorage.getItem(LAST_VIEWED_ROOMS)) || {};
-
-        lastViewedRooms[activeSpaceId] = roomInfo.room_id;
-        window.localStorage.setItem(LAST_VIEWED_ROOMS, JSON.stringify(lastViewedRooms));
-
         if (roomInfo.room_alias) {
             console.log(
                 `Switching to room alias ${roomInfo.room_alias} at event ` +
diff --git a/src/stores/SpaceStore.tsx b/src/stores/SpaceStore.tsx
index 063256f421..1f6cbb33b5 100644
--- a/src/stores/SpaceStore.tsx
+++ b/src/stores/SpaceStore.tsx
@@ -490,6 +490,14 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
         if (!SettingsStore.getValue("feature_spaces")) return;
         switch (payload.action) {
             case "view_room": {
+                // persist last viewed room from a space
+                const activeSpace = SpaceStore.instance.activeSpace;
+                const activeSpaceId = activeSpace?.roomId || LAST_VIEWED_ROOMS_HOME;
+                const lastViewedRooms = JSON.parse(window.localStorage.getItem(LAST_VIEWED_ROOMS)) || {};
+
+                lastViewedRooms[activeSpaceId] = payload.room_id;
+                window.localStorage.setItem(LAST_VIEWED_ROOMS, JSON.stringify(lastViewedRooms));
+
                 const room = this.matrixClient?.getRoom(payload.room_id);
 
                 if (room?.getMyMembership() === "join") {