Fix auto select room in categorized space

This commit is contained in:
Ajay Bura 2022-08-20 12:52:56 +05:30
parent 0fc128150b
commit 8b52c3cfff
2 changed files with 58 additions and 16 deletions

View file

@ -38,10 +38,10 @@ function SpaceOptions({ roomId, afterOptionSelect }) {
const handleMarkAsRead = () => { const handleMarkAsRead = () => {
const spaceChildren = roomList.getCategorizedSpaces([roomId]); const spaceChildren = roomList.getCategorizedSpaces([roomId]);
spaceChildren?.forEach((childIds, spaceId) => { spaceChildren?.forEach((childIds) => {
childIds?.forEach((childId) => { childIds?.forEach((childId) => {
markAsRead(childId); markAsRead(childId);
}) });
}); });
afterOptionSelect(); afterOptionSelect();
}; };

View file

@ -17,6 +17,7 @@ class Navigation extends EventEmitter {
this.recentRooms = []; this.recentRooms = [];
this.spaceToRoom = new Map(); this.spaceToRoom = new Map();
window.spaceToRoom = this.spaceToRoom;
this.rawModelStack = []; this.rawModelStack = [];
} }
@ -62,12 +63,12 @@ class Navigation extends EventEmitter {
const parents = roomList.roomIdToParents.get(roomId); const parents = roomList.roomIdToParents.get(roomId);
if (!parents) return; if (!parents) return;
if (parents.has(this.selectedSpaceId)) { [...parents].forEach((pId) => {
this.spaceToRoom.set(this.selectedSpaceId, { this.spaceToRoom.set(pId, {
roomId, roomId,
timestamp: Date.now(), timestamp: Date.now(),
}); });
} });
} }
_selectRoom(roomId, eventId) { _selectRoom(roomId, eventId) {
@ -105,10 +106,14 @@ class Navigation extends EventEmitter {
const parents = roomList.roomIdToParents.get(roomId); const parents = roomList.roomIdToParents.get(roomId);
if (parents.has(this.selectedSpaceId)) {
return;
}
const { categorizedSpaces } = accountData; const { categorizedSpaces } = accountData;
if (categorizedSpaces.has(this.selectedSpaceId)) { if (categorizedSpaces.has(this.selectedSpaceId)) {
const allChildSpaces = roomList.getCategorizedSpaces([this.selectedSpaceId]); const categories = roomList.getCategorizedSpaces([this.selectedSpaceId]);
if ([...parents].find((pId) => allChildSpaces.has(pId))) { if ([...parents].find((pId) => categories.has(pId))) {
// No need to select tab // No need to select tab
// As one of parent is child of selected categorized space. // As one of parent is child of selected categorized space.
return; return;
@ -149,6 +154,22 @@ class Navigation extends EventEmitter {
return roomId; return roomId;
} }
_getLatestSelectedRoomId(spaceIds) {
let ts = 0;
let roomId = null;
spaceIds.forEach((sId) => {
const data = this.spaceToRoom.get(sId);
if (!data) return;
const newTs = data.timestamp;
if (newTs > ts) {
ts = newTs;
roomId = data.roomId;
}
});
return roomId;
}
_selectTab(tabId, selectRoom = true) { _selectTab(tabId, selectRoom = true) {
this.selectedTab = tabId; this.selectedTab = tabId;
if (selectRoom) this._selectRoomWithTab(this.selectedTab); if (selectRoom) this._selectRoomWithTab(this.selectedTab);
@ -164,24 +185,45 @@ class Navigation extends EventEmitter {
_selectRoomWithSpace(spaceId) { _selectRoomWithSpace(spaceId) {
if (!spaceId) return; if (!spaceId) return;
const { roomList, accountData, matrixClient } = this.initMatrix;
const { categorizedSpaces } = accountData;
const data = this.spaceToRoom.get(spaceId); const data = this.spaceToRoom.get(spaceId);
if (data) { if (data && !categorizedSpaces.has(spaceId)) {
this._selectRoom(data.roomId); this._selectRoom(data.roomId);
return; return;
} }
const { roomList } = this.initMatrix; const children = [];
// TODO:
/** if (categorizedSpaces.has(spaceId)) {
* if space is categorized get -r all child rooms const categories = roomList.getCategorizedSpaces([spaceId]);
* filter the one with with latest ts
* select const latestSelectedRoom = this._getLatestSelectedRoomId([...categories.keys()]);
*/
const children = roomList.getSpaceChildren(spaceId); if (latestSelectedRoom) {
this._selectRoom(latestSelectedRoom);
return;
}
categories?.forEach((categoryId) => {
categoryId?.forEach((childId) => {
children.push(childId);
});
});
} else {
roomList.getSpaceChildren(spaceId).forEach((id) => {
if (matrixClient.getRoom(id)?.isSpaceRoom() === false) {
children.push(id);
}
});
}
if (!children) { if (!children) {
this._selectRoom(null); this._selectRoom(null);
return; return;
} }
this._selectRoom(this._getLatestActiveRoomId(children)); this._selectRoom(this._getLatestActiveRoomId(children));
} }