diff --git a/src/components/structures/MatrixChat.tsx b/src/components/structures/MatrixChat.tsx index 689561fd60..9a4e83828c 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 from "../../stores/SpaceStore"; +import SpaceStore, {LAST_VIEWED_ROOMS, LAST_VIEWED_ROOMS_HOME} from "../../stores/SpaceStore"; import SpaceRoomDirectory from "./SpaceRoomDirectory"; import {replaceableComponent} from "../../utils/replaceableComponent"; import RoomListStore from "../../stores/room-list/RoomListStore"; @@ -875,6 +875,14 @@ export default class MatrixChat extends React.PureComponent { 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 bcf95a82be..20223cb8a6 100644 --- a/src/stores/SpaceStore.tsx +++ b/src/stores/SpaceStore.tsx @@ -41,6 +41,10 @@ type SpaceKey = string | symbol; interface IState {} const ACTIVE_SPACE_LS_KEY = "mx_active_space"; +export const LAST_VIEWED_ROOMS = "mx_last_viewed_rooms"; + +// We can't use HOME_SPACE here because JSON.stringify() will ignore any Symbols +export const LAST_VIEWED_ROOMS_HOME = "home_space"; export const HOME_SPACE = Symbol("home-space"); export const SUGGESTED_ROOMS = Symbol("suggested-rooms"); @@ -111,6 +115,18 @@ 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 spaceId = space?.roomId || LAST_VIEWED_ROOMS_HOME; + const lastViewedRooms = JSON.parse(window.localStorage.getItem(LAST_VIEWED_ROOMS)); + const roomId = lastViewedRooms[spaceId]; + + if (roomId) { + defaultDispatcher.dispatch({ + action: "view_room", + room_id: roomId, + }); + } // TODO: Handle else + // persist space selected if (space) { window.localStorage.setItem(ACTIVE_SPACE_LS_KEY, space.roomId);