Spaces fix edge cases around automatic space switching around room navigation

pull/21833/head
Michael Telatynski 2021-04-29 13:52:48 +01:00
parent 7552801103
commit 286463606c
1 changed files with 29 additions and 20 deletions

View File

@ -410,6 +410,22 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
}); });
}, 100, {trailing: true, leading: true}); }, 100, {trailing: true, leading: true});
private switchToRelatedSpace = (roomId: string) => {
if (this.suggestedRooms.find(r => r.room_id === roomId)) return;
let parent = this.getCanonicalParent(roomId);
if (!parent) {
parent = this.rootSpaces.find(s => this.spaceFilteredRooms.get(s.roomId)?.has(roomId));
}
if (!parent) {
const parents = Array.from(this.parentMap.get(roomId) || []);
parent = parents.find(p => this.matrixClient.getRoom(p));
}
// don't trigger a context switch when we are switching a space to match the chosen room
this.setActiveSpace(parent || null, false);
};
private onRoom = (room: Room, newMembership?: string, oldMembership?: string) => { private onRoom = (room: Room, newMembership?: string, oldMembership?: string) => {
const membership = newMembership || room.getMyMembership(); const membership = newMembership || room.getMyMembership();
@ -424,6 +440,11 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
if (numSuggestedRooms !== this._suggestedRooms.length) { if (numSuggestedRooms !== this._suggestedRooms.length) {
this.emit(SUGGESTED_ROOMS, this._suggestedRooms); this.emit(SUGGESTED_ROOMS, this._suggestedRooms);
} }
// if the room currently being viewed was just joined then switch to its related space
if (newMembership === "join" && room.roomId === RoomViewStore.getRoomId()) {
this.switchToRelatedSpace(room.roomId);
}
} }
return; return;
} }
@ -442,7 +463,7 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
if (membership === "join" && room.roomId === RoomViewStore.getRoomId()) { if (membership === "join" && room.roomId === RoomViewStore.getRoomId()) {
// if the user was looking at the space and then joined: select that space // if the user was looking at the space and then joined: select that space
this.setActiveSpace(room); this.setActiveSpace(room, false);
} }
}; };
@ -542,10 +563,7 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
// restore selected state from last session if any and still valid // restore selected state from last session if any and still valid
const lastSpaceId = window.localStorage.getItem(ACTIVE_SPACE_LS_KEY); const lastSpaceId = window.localStorage.getItem(ACTIVE_SPACE_LS_KEY);
if (lastSpaceId) { if (lastSpaceId) {
const space = this.rootSpaces.find(s => s.roomId === lastSpaceId); this.setActiveSpace(this.matrixClient.getRoom(lastSpaceId));
if (space) {
this.setActiveSpace(space);
}
} }
} }
@ -553,27 +571,18 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
if (!SettingsStore.getValue("feature_spaces")) return; if (!SettingsStore.getValue("feature_spaces")) return;
switch (payload.action) { switch (payload.action) {
case "view_room": { case "view_room": {
const room = this.matrixClient?.getRoom(payload.room_id);
// Don't auto-switch rooms when reacting to a context-switch // Don't auto-switch rooms when reacting to a context-switch
// as this is not helpful and can create loops of rooms/space switching // as this is not helpful and can create loops of rooms/space switching
if (!room || payload.context_switch) break; if (payload.context_switch) break;
if (room.isSpaceRoom()) { const roomId = payload.room_id;
const room = this.matrixClient?.getRoom(roomId);
if (room?.isSpaceRoom()) {
// Don't context switch when navigating to the space room // Don't context switch when navigating to the space room
// as it will cause you to end up in the wrong room // as it will cause you to end up in the wrong room
this.setActiveSpace(room, false); this.setActiveSpace(room, false);
} else if (!this.getSpaceFilteredRoomIds(this.activeSpace).has(room.roomId)) { } else if (!this.getSpaceFilteredRoomIds(this.activeSpace).has(roomId)) {
let parent = this.getCanonicalParent(room.roomId); this.switchToRelatedSpace(roomId);
if (!parent) {
parent = this.rootSpaces.find(s => this.spaceFilteredRooms.get(s.roomId)?.has(room.roomId));
}
if (!parent) {
const parents = Array.from(this.parentMap.get(room.roomId) || []);
parent = parents.find(p => this.matrixClient.getRoom(p));
}
// don't trigger a context switch when we are switching a space to match the chosen room
this.setActiveSpace(parent || null, false);
} }
// Persist last viewed room from a space // Persist last viewed room from a space