@@ -113,7 +113,7 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
113113 }
114114
115115 public async setActiveSpace ( space : Room | null , contextSwitch = true ) {
116- if ( space === this . activeSpace ) return ;
116+ if ( space === this . activeSpace || ( space && ! space ?. isSpaceRoom ( ) ) ) return ;
117117
118118 this . _activeSpace = space ;
119119 this . emit ( UPDATE_SELECTED_SPACE , this . activeSpace ) ;
@@ -195,15 +195,16 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
195195 const childEvents = room ?. currentState . getStateEvents ( EventType . SpaceChild ) . filter ( ev => ev . getContent ( ) ?. via ) ;
196196 return sortBy ( childEvents , getOrder )
197197 . map ( ev => this . matrixClient . getRoom ( ev . getStateKey ( ) ) )
198- . filter ( room => room ?. getMyMembership ( ) === "join" ) || [ ] ;
198+ . filter ( room => room ?. getMyMembership ( ) === "join" || room ?. getMyMembership ( ) === "invite" ) || [ ] ;
199199 }
200200
201201 public getChildRooms ( spaceId : string ) : Room [ ] {
202202 return this . getChildren ( spaceId ) . filter ( r => ! r . isSpaceRoom ( ) ) ;
203203 }
204204
205205 public getChildSpaces ( spaceId : string ) : Room [ ] {
206- return this . getChildren ( spaceId ) . filter ( r => r . isSpaceRoom ( ) ) ;
206+ // don't show invited subspaces as they surface at the top level for better visibility
207+ return this . getChildren ( spaceId ) . filter ( r => r . isSpaceRoom ( ) && r . getMyMembership ( ) === "join" ) ;
207208 }
208209
209210 public getParents ( roomId : string , canonicalOnly = false ) : Room [ ] {
@@ -409,32 +410,39 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
409410 } ) ;
410411 } , 100 , { trailing : true , leading : true } ) ;
411412
412- private onRoom = ( room : Room , membership ?: string , oldMembership ?: string ) => {
413- if ( ( membership || room . getMyMembership ( ) ) === "invite" ) {
414- this . _invitedSpaces . add ( room ) ;
415- this . emit ( UPDATE_INVITED_SPACES , this . invitedSpaces ) ;
416- } else if ( oldMembership === "invite" ) {
417- this . _invitedSpaces . delete ( room ) ;
418- this . emit ( UPDATE_INVITED_SPACES , this . invitedSpaces ) ;
419- } else if ( room ?. isSpaceRoom ( ) ) {
420- this . onSpaceUpdate ( ) ;
421- this . emit ( room . roomId ) ;
422- } else {
413+ private onRoom = ( room : Room , newMembership ?: string , oldMembership ?: string ) => {
414+ const membership = newMembership || room . getMyMembership ( ) ;
415+
416+ if ( ! room . isSpaceRoom ( ) ) {
423417 // this.onRoomUpdate(room);
424418 this . onRoomsUpdate ( ) ;
425- }
426419
427- if ( room . getMyMembership ( ) === "join" ) {
428- if ( ! room . isSpaceRoom ( ) ) {
420+ if ( membership === "join" ) {
421+ // the user just joined a room, remove it from the suggested list if it was there
429422 const numSuggestedRooms = this . _suggestedRooms . length ;
430423 this . _suggestedRooms = this . _suggestedRooms . filter ( r => r . room_id !== room . roomId ) ;
431424 if ( numSuggestedRooms !== this . _suggestedRooms . length ) {
432425 this . emit ( SUGGESTED_ROOMS , this . _suggestedRooms ) ;
433426 }
434- } else if ( room . roomId === RoomViewStore . getRoomId ( ) ) {
435- // if the user was looking at the space and then joined: select that space
436- this . setActiveSpace ( room ) ;
437427 }
428+ return ;
429+ }
430+
431+ // Space
432+ if ( membership === "invite" ) {
433+ this . _invitedSpaces . add ( room ) ;
434+ this . emit ( UPDATE_INVITED_SPACES , this . invitedSpaces ) ;
435+ } else if ( oldMembership === "invite" && membership !== "join" ) {
436+ this . _invitedSpaces . delete ( room ) ;
437+ this . emit ( UPDATE_INVITED_SPACES , this . invitedSpaces ) ;
438+ } else {
439+ this . onSpaceUpdate ( ) ;
440+ this . emit ( room . roomId ) ;
441+ }
442+
443+ if ( membership === "join" && room . roomId === RoomViewStore . getRoomId ( ) ) {
444+ // if the user was looking at the space and then joined: select that space
445+ this . setActiveSpace ( room ) ;
438446 }
439447 } ;
440448
@@ -498,6 +506,17 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
498506 }
499507 } ;
500508
509+ protected async reset ( ) {
510+ this . rootSpaces = [ ] ;
511+ this . orphanedRooms = new Set ( ) ;
512+ this . parentMap = new EnhancedMap ( ) ;
513+ this . notificationStateMap = new Map ( ) ;
514+ this . spaceFilteredRooms = new Map ( ) ;
515+ this . _activeSpace = null ;
516+ this . _suggestedRooms = [ ] ;
517+ this . _invitedSpaces = new Set ( ) ;
518+ }
519+
501520 protected async onNotReady ( ) {
502521 if ( ! SettingsStore . getValue ( "feature_spaces" ) ) return ;
503522 if ( this . matrixClient ) {
@@ -507,7 +526,7 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
507526 this . matrixClient . removeListener ( "Room.accountData" , this . onRoomAccountData ) ;
508527 this . matrixClient . removeListener ( "accountData" , this . onAccountData ) ;
509528 }
510- await this . reset ( { } ) ;
529+ await this . reset ( ) ;
511530 }
512531
513532 protected async onReady ( ) {
@@ -540,17 +559,14 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
540559 // as this is not helpful and can create loops of rooms/space switching
541560 if ( ! room || payload . context_switch ) break ;
542561
543- // persist last viewed room from a space
544-
545562 if ( room . isSpaceRoom ( ) ) {
546563 // Don't context switch when navigating to the space room
547564 // as it will cause you to end up in the wrong room
548565 this . setActiveSpace ( room , false ) ;
549566 } else if ( ! this . getSpaceFilteredRoomIds ( this . activeSpace ) . has ( room . roomId ) ) {
550- // TODO maybe reverse these first 2 clauses once space panel active is fixed
551- let parent = this . rootSpaces . find ( s => this . spaceFilteredRooms . get ( s . roomId ) ?. has ( room . roomId ) ) ;
567+ let parent = this . getCanonicalParent ( room . roomId ) ;
552568 if ( ! parent ) {
553- parent = this . getCanonicalParent ( room . roomId ) ;
569+ parent = this . rootSpaces . find ( s => this . spaceFilteredRooms . get ( s . roomId ) ?. has ( room . roomId ) ) ;
554570 }
555571 if ( ! parent ) {
556572 const parents = Array . from ( this . parentMap . get ( room . roomId ) || [ ] ) ;
@@ -584,7 +600,9 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
584600 return state ;
585601 }
586602
587- // traverse space tree with DFS calling fn on each space including the given root one
603+ // traverse space tree with DFS calling fn on each space including the given root one,
604+ // if includeRooms is true then fn will be called on each leaf room, if it is present in multiple sub-spaces
605+ // then fn will be called with it multiple times.
588606 public traverseSpace (
589607 spaceId : string ,
590608 fn : ( roomId : string ) => void ,
0 commit comments