diff --git a/src/stores/SpaceStore.tsx b/src/stores/SpaceStore.tsx index dcdbc73cb7..290caba9d4 100644 --- a/src/stores/SpaceStore.tsx +++ b/src/stores/SpaceStore.tsx @@ -51,6 +51,12 @@ export const UPDATE_SELECTED_SPACE = Symbol("selected-space"); const MAX_SUGGESTED_ROOMS = 20; +const getLastViewedRoomsStorageKey = (space?: Room) => { + const lastViewRooms = "mx_last_viewed_rooms"; + const homeSpace = "home_space"; + return `${lastViewRooms}_${space?.roomId || homeSpace}`; +} + const partitionSpacesAndRooms = (arr: Room[]): [Room[], Room[]] => { // [spaces, rooms] return arr.reduce((result, room: Room) => { result[room.isSpaceRoom() ? 0 : 1].push(room); @@ -111,6 +117,25 @@ export class SpaceStoreClass extends AsyncStoreWithClient { this.emit(UPDATE_SELECTED_SPACE, this.activeSpace); this.emit(SUGGESTED_ROOMS, this._suggestedRooms = []); + // view last selected room from space + const roomId = window.localStorage.getItem(getLastViewedRoomsStorageKey(this.activeSpace)); + + if (roomId && this.matrixClient?.getRoom(roomId)?.getMyMembership() === "join") { + defaultDispatcher.dispatch({ + action: "view_room", + room_id: roomId, + }); + } else if (space) { + defaultDispatcher.dispatch({ + action: "view_room", + room_id: space.roomId, + }); + } else { + defaultDispatcher.dispatch({ + action: "view_home_page", + }); + } + // persist space selected if (space) { window.localStorage.setItem(ACTIVE_SPACE_LS_KEY, space.roomId); @@ -488,6 +513,11 @@ export class SpaceStoreClass extends AsyncStoreWithClient { case "view_room": { const room = this.matrixClient?.getRoom(payload.room_id); + // persist last viewed room from a space + if (room) { + window.localStorage.setItem(getLastViewedRoomsStorageKey(this.activeSpace), payload.room_id); + } + if (room?.getMyMembership() === "join") { if (room.isSpaceRoom()) { this.setActiveSpace(room);