Merge pull request #5795 from SimonBrandner/spaces/context-switching

[Spaces] Context switching
pull/21833/head
Michael Telatynski 2021-04-20 12:38:54 +01:00 committed by GitHub
commit b2e9f6731b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 30 additions and 0 deletions

View File

@ -51,6 +51,12 @@ export const UPDATE_SELECTED_SPACE = Symbol("selected-space");
const MAX_SUGGESTED_ROOMS = 20; 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] const partitionSpacesAndRooms = (arr: Room[]): [Room[], Room[]] => { // [spaces, rooms]
return arr.reduce((result, room: Room) => { return arr.reduce((result, room: Room) => {
result[room.isSpaceRoom() ? 0 : 1].push(room); result[room.isSpaceRoom() ? 0 : 1].push(room);
@ -111,6 +117,25 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
this.emit(UPDATE_SELECTED_SPACE, this.activeSpace); this.emit(UPDATE_SELECTED_SPACE, this.activeSpace);
this.emit(SUGGESTED_ROOMS, this._suggestedRooms = []); 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 // persist space selected
if (space) { if (space) {
window.localStorage.setItem(ACTIVE_SPACE_LS_KEY, space.roomId); window.localStorage.setItem(ACTIVE_SPACE_LS_KEY, space.roomId);
@ -488,6 +513,11 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
case "view_room": { case "view_room": {
const room = this.matrixClient?.getRoom(payload.room_id); 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?.getMyMembership() === "join") {
if (room.isSpaceRoom()) { if (room.isSpaceRoom()) {
this.setActiveSpace(room); this.setActiveSpace(room);