Fix wrong space gets selected with some rooms

This commit is contained in:
Ajay Bura 2022-08-20 11:01:42 +05:30
parent db7f1746f6
commit 0fc128150b
2 changed files with 48 additions and 28 deletions

View file

@ -2,14 +2,6 @@ import appDispatcher from '../dispatcher';
import cons from '../state/cons'; import cons from '../state/cons';
export function selectTab(tabId) { export function selectTab(tabId) {
const roomId = (tabId !== cons.tabs.HOME && tabId !== cons.tabs.DIRECTS) ? tabId : null;
appDispatcher.dispatch({
type: cons.actions.navigation.SELECT_SPACE,
roomId,
asBase: true,
});
appDispatcher.dispatch({ appDispatcher.dispatch({
type: cons.actions.navigation.SELECT_TAB, type: cons.actions.navigation.SELECT_TAB,
tabId, tabId,

View file

@ -16,17 +16,17 @@ class Navigation extends EventEmitter {
this.isRoomSettings = false; this.isRoomSettings = false;
this.recentRooms = []; this.recentRooms = [];
this.selectedSpaceToRoom = new Map(); this.spaceToRoom = new Map();
this.rawModelStack = []; this.rawModelStack = [];
} }
_setSpacePath(roomId, asBase) { _addToSpacePath(roomId, asRoot) {
if (typeof roomId !== 'string') { if (typeof roomId !== 'string') {
this.selectedSpacePath = [cons.tabs.HOME]; this.selectedSpacePath = [cons.tabs.HOME];
return; return;
} }
if (asBase) { if (asRoot) {
this.selectedSpacePath = [roomId]; this.selectedSpacePath = [roomId];
return; return;
} }
@ -38,21 +38,21 @@ class Navigation extends EventEmitter {
this.selectedSpacePath.push(roomId); this.selectedSpacePath.push(roomId);
} }
_addSelectedSpaceToRoom(roomId) { _mapRoomToSpace(roomId) {
const { roomList } = this.initMatrix; const { roomList } = this.initMatrix;
if ( if (
this.selectedTab === cons.tabs.HOME this.selectedTab === cons.tabs.HOME
&& roomList.rooms.has(roomId) && roomList.rooms.has(roomId)
&& !roomList.roomIdToParents.has(roomId) && !roomList.roomIdToParents.has(roomId)
) { ) {
this.selectedSpaceToRoom.set(cons.tabs.HOME, { this.spaceToRoom.set(cons.tabs.HOME, {
roomId, roomId,
timestamp: Date.now(), timestamp: Date.now(),
}); });
return; return;
} }
if (this.selectedTab === cons.tabs.DIRECTS && roomList.directs.has(roomId)) { if (this.selectedTab === cons.tabs.DIRECTS && roomList.directs.has(roomId)) {
this.selectedSpaceToRoom.set(cons.tabs.DIRECTS, { this.spaceToRoom.set(cons.tabs.DIRECTS, {
roomId, roomId,
timestamp: Date.now(), timestamp: Date.now(),
}); });
@ -63,7 +63,7 @@ class Navigation extends EventEmitter {
if (!parents) return; if (!parents) return;
if (parents.has(this.selectedSpaceId)) { if (parents.has(this.selectedSpaceId)) {
this.selectedSpaceToRoom.set(this.selectedSpaceId, { this.spaceToRoom.set(this.selectedSpaceId, {
roomId, roomId,
timestamp: Date.now(), timestamp: Date.now(),
}); });
@ -73,7 +73,7 @@ class Navigation extends EventEmitter {
_selectRoom(roomId, eventId) { _selectRoom(roomId, eventId) {
const prevSelectedRoomId = this.selectedRoomId; const prevSelectedRoomId = this.selectedRoomId;
this.selectedRoomId = roomId; this.selectedRoomId = roomId;
if (prevSelectedRoomId !== roomId) this._addSelectedSpaceToRoom(roomId); if (prevSelectedRoomId !== roomId) this._mapRoomToSpace(roomId);
this.removeRecentRoom(prevSelectedRoomId); this.removeRecentRoom(prevSelectedRoomId);
this.addRecentRoom(prevSelectedRoomId); this.addRecentRoom(prevSelectedRoomId);
this.removeRecentRoom(this.selectedRoomId); this.removeRecentRoom(this.selectedRoomId);
@ -90,7 +90,7 @@ class Navigation extends EventEmitter {
} }
_selectTabWithRoom(roomId) { _selectTabWithRoom(roomId) {
const { roomList } = this.initMatrix; const { roomList, accountData } = this.initMatrix;
if (roomList.directs.has(roomId)) { if (roomList.directs.has(roomId)) {
this._selectSpace(null, true, false); this._selectSpace(null, true, false);
@ -103,11 +103,28 @@ class Navigation extends EventEmitter {
return; return;
} }
const parents = [...roomList.roomIdToParents.get(roomId)]; const parents = roomList.roomIdToParents.get(roomId);
if (parents) {
const sortedParents = parents.sort((p1, p2) => { const { categorizedSpaces } = accountData;
const t1 = this.selectedSpaceToRoom.get(p1)?.timestamp ?? 0; if (categorizedSpaces.has(this.selectedSpaceId)) {
const t2 = this.selectedSpaceToRoom.get(p2)?.timestamp ?? 0; const allChildSpaces = roomList.getCategorizedSpaces([this.selectedSpaceId]);
if ([...parents].find((pId) => allChildSpaces.has(pId))) {
// No need to select tab
// As one of parent is child of selected categorized space.
return;
}
}
const spaceInPath = [...this.selectedSpacePath].reverse().find((sId) => parents.has(sId));
if (spaceInPath) {
this._selectSpace(spaceInPath, false, false);
return;
}
if (parents.size > 0) {
const sortedParents = [...parents].sort((p1, p2) => {
const t1 = this.spaceToRoom.get(p1)?.timestamp ?? 0;
const t2 = this.spaceToRoom.get(p2)?.timestamp ?? 0;
return t2 - t1; return t2 - t1;
}); });
this._selectSpace(sortedParents[0], true, false); this._selectSpace(sortedParents[0], true, false);
@ -138,22 +155,28 @@ class Navigation extends EventEmitter {
this.emit(cons.events.navigation.TAB_SELECTED, this.selectedTab); this.emit(cons.events.navigation.TAB_SELECTED, this.selectedTab);
} }
_selectSpace(roomId, asBase, selectRoom = true) { _selectSpace(roomId, asRoot, selectRoom = true) {
this._setSpacePath(roomId, asBase); this._addToSpacePath(roomId, asRoot);
this.selectedSpaceId = roomId; this.selectedSpaceId = roomId;
if (!asBase && selectRoom) this._selectRoomWithSpace(this.selectedSpaceId); if (!asRoot && selectRoom) this._selectRoomWithSpace(this.selectedSpaceId);
this.emit(cons.events.navigation.SPACE_SELECTED, this.selectedSpaceId); this.emit(cons.events.navigation.SPACE_SELECTED, this.selectedSpaceId);
} }
_selectRoomWithSpace(spaceId) { _selectRoomWithSpace(spaceId) {
if (!spaceId) return; if (!spaceId) return;
const data = this.selectedSpaceToRoom.get(spaceId); const data = this.spaceToRoom.get(spaceId);
if (data) { if (data) {
this._selectRoom(data.roomId); this._selectRoom(data.roomId);
return; return;
} }
const { roomList } = this.initMatrix; const { roomList } = this.initMatrix;
// TODO:
/**
* if space is categorized get -r all child rooms
* filter the one with with latest ts
* select
*/
const children = roomList.getSpaceChildren(spaceId); const children = roomList.getSpaceChildren(spaceId);
if (!children) { if (!children) {
this._selectRoom(null); this._selectRoom(null);
@ -165,7 +188,7 @@ class Navigation extends EventEmitter {
_selectRoomWithTab(tabId) { _selectRoomWithTab(tabId) {
const { roomList } = this.initMatrix; const { roomList } = this.initMatrix;
if (tabId === cons.tabs.HOME || tabId === cons.tabs.DIRECTS) { if (tabId === cons.tabs.HOME || tabId === cons.tabs.DIRECTS) {
const data = this.selectedSpaceToRoom.get(tabId); const data = this.spaceToRoom.get(tabId);
if (data) { if (data) {
this._selectRoom(data.roomId); this._selectRoom(data.roomId);
return; return;
@ -206,10 +229,15 @@ class Navigation extends EventEmitter {
navigate(action) { navigate(action) {
const actions = { const actions = {
[cons.actions.navigation.SELECT_TAB]: () => { [cons.actions.navigation.SELECT_TAB]: () => {
const roomId = (
action.tabId !== cons.tabs.HOME && action.tabId !== cons.tabs.DIRECTS
) ? action.tabId : null;
this._selectSpace(roomId, true);
this._selectTab(action.tabId); this._selectTab(action.tabId);
}, },
[cons.actions.navigation.SELECT_SPACE]: () => { [cons.actions.navigation.SELECT_SPACE]: () => {
this._selectSpace(action.roomId, action.asBase); this._selectSpace(action.roomId, false);
}, },
[cons.actions.navigation.SELECT_ROOM]: () => { [cons.actions.navigation.SELECT_ROOM]: () => {
this._selectTabWithRoom(action.roomId); this._selectTabWithRoom(action.roomId);