@@ -45,14 +45,22 @@ import { RoomViewStore } from "../RoomViewStore";
4545export default class RightPanelStore extends ReadyWatchingStore {
4646 private static internalInstance : RightPanelStore ;
4747
48- private global ?: IRightPanelForRoom = null ;
49- private byRoom : {
50- [ roomId : string ] : IRightPanelForRoom ;
51- } = { } ;
48+ private global ?: IRightPanelForRoom ;
49+ private byRoom : { [ roomId : string ] : IRightPanelForRoom } ;
5250 private viewedRoomId : Optional < string > ;
5351
5452 private constructor ( ) {
5553 super ( defaultDispatcher ) ;
54+ this . reset ( ) ;
55+ }
56+
57+ /**
58+ * Resets the store. Intended for test usage only.
59+ */
60+ public reset ( ) {
61+ this . global = null ;
62+ this . byRoom = { } ;
63+ this . viewedRoomId = null ;
5664 }
5765
5866 protected async onReady ( ) : Promise < any > {
@@ -134,19 +142,20 @@ export default class RightPanelStore extends ReadyWatchingStore {
134142 const cardState = redirect ?. state ?? ( Object . keys ( card . state ?? { } ) . length === 0 ? null : card . state ) ;
135143
136144 // Checks for wrong SetRightPanelPhase requests
137- if ( ! this . isPhaseValid ( targetPhase ) ) return ;
145+ if ( ! this . isPhaseValid ( targetPhase , Boolean ( rId ) ) ) return ;
138146
139147 if ( ( targetPhase === this . currentCardForRoom ( rId ) ?. phase && ! ! cardState ) ) {
140148 // Update state: set right panel with a new state but keep the phase (don't know it this is ever needed...)
141149 const hist = this . byRoom [ rId ] ?. history ?? [ ] ;
142150 hist [ hist . length - 1 ] . state = cardState ;
143151 this . emitAndUpdateSettings ( ) ;
144- } else if ( targetPhase !== this . currentCard ?. phase ) {
145- // Set right panel and erase history.
146- this . show ( ) ;
147- this . setRightPanelCache ( { phase : targetPhase , state : cardState ?? { } } , rId ) ;
152+ } else if ( targetPhase !== this . currentCardForRoom ( rId ) ?. phase || ! this . byRoom [ rId ] ) {
153+ // Set right panel and initialize/erase history
154+ const history = [ { phase : targetPhase , state : cardState ?? { } } ] ;
155+ this . byRoom [ rId ] = { history, isOpen : true } ;
156+ this . emitAndUpdateSettings ( ) ;
148157 } else {
149- this . show ( ) ;
158+ this . show ( rId ) ;
150159 this . emitAndUpdateSettings ( ) ;
151160 }
152161 }
@@ -156,23 +165,23 @@ export default class RightPanelStore extends ReadyWatchingStore {
156165 const rId = roomId ?? this . viewedRoomId ;
157166 const history = cards . map ( c => ( { phase : c . phase , state : c . state ?? { } } ) ) ;
158167 this . byRoom [ rId ] = { history, isOpen : true } ;
159- this . show ( ) ;
168+ this . show ( rId ) ;
160169 this . emitAndUpdateSettings ( ) ;
161170 }
162171
172+ // Appends a card to the history and shows the right panel if not already visible
163173 public pushCard (
164174 card : IRightPanelCard ,
165175 allowClose = true ,
166176 roomId : string = null ,
167177 ) {
168- // This function appends a card to the history and shows the right panel if now already visible.
169178 const rId = roomId ?? this . viewedRoomId ;
170179 const redirect = this . getVerificationRedirect ( card ) ;
171180 const targetPhase = redirect ?. phase ?? card . phase ;
172- const pState = redirect ?. state ?? ( Object . keys ( card . state ?? { } ) . length === 0 ? null : card . state ) ;
181+ const pState = redirect ?. state ?? card . state ?? { } ;
173182
174183 // Checks for wrong SetRightPanelPhase requests
175- if ( ! this . isPhaseValid ( targetPhase ) ) return ;
184+ if ( ! this . isPhaseValid ( targetPhase , Boolean ( rId ) ) ) return ;
176185
177186 const roomCache = this . byRoom [ rId ] ;
178187 if ( ! ! roomCache ) {
@@ -182,12 +191,12 @@ export default class RightPanelStore extends ReadyWatchingStore {
182191 } else {
183192 // setup room panel cache with the new card
184193 this . byRoom [ rId ] = {
185- history : [ { phase : targetPhase , state : pState ?? { } } ] ,
194+ history : [ { phase : targetPhase , state : pState } ] ,
186195 // if there was no right panel store object the the panel was closed -> keep it closed, except if allowClose==false
187196 isOpen : ! allowClose ,
188197 } ;
189198 }
190- this . show ( ) ;
199+ this . show ( rId ) ;
191200 this . emitAndUpdateSettings ( ) ;
192201 }
193202
@@ -200,35 +209,39 @@ export default class RightPanelStore extends ReadyWatchingStore {
200209 return removedCard ;
201210 }
202211
203- public togglePanel ( roomId : string = null ) {
212+ public togglePanel ( roomId : string | null ) {
204213 const rId = roomId ?? this . viewedRoomId ;
205214 if ( ! this . byRoom [ rId ] ) return ;
206215
207216 this . byRoom [ rId ] . isOpen = ! this . byRoom [ rId ] . isOpen ;
208217 this . emitAndUpdateSettings ( ) ;
209218 }
210219
211- public show ( ) {
212- if ( ! this . isOpen ) {
213- this . togglePanel ( ) ;
220+ public show ( roomId : string | null ) {
221+ if ( ! this . isOpenForRoom ( roomId ?? this . viewedRoomId ) ) {
222+ this . togglePanel ( roomId ) ;
214223 }
215224 }
216225
217- public hide ( ) {
218- if ( this . isOpen ) {
219- this . togglePanel ( ) ;
226+ public hide ( roomId : string | null ) {
227+ if ( this . isOpenForRoom ( roomId ?? this . viewedRoomId ) ) {
228+ this . togglePanel ( roomId ) ;
220229 }
221230 }
222231
223232 private loadCacheFromSettings ( ) {
224- const room = this . viewedRoomId && this . mxClient ?. getRoom ( this . viewedRoomId ) ;
225- if ( ! ! room ) {
226- this . global = this . global ??
227- convertToStatePanel ( SettingsStore . getValue ( "RightPanel.phasesGlobal" ) , room ) ;
228- this . byRoom [ this . viewedRoomId ] = this . byRoom [ this . viewedRoomId ] ??
229- convertToStatePanel ( SettingsStore . getValue ( "RightPanel.phases" , this . viewedRoomId ) , room ) ;
230- } else {
231- console . warn ( "Could not restore the right panel after load because there was no associated room object." ) ;
233+ if ( this . viewedRoomId ) {
234+ const room = this . mxClient ?. getRoom ( this . viewedRoomId ) ;
235+ if ( ! ! room ) {
236+ this . global = this . global ??
237+ convertToStatePanel ( SettingsStore . getValue ( "RightPanel.phasesGlobal" ) , room ) ;
238+ this . byRoom [ this . viewedRoomId ] = this . byRoom [ this . viewedRoomId ] ??
239+ convertToStatePanel ( SettingsStore . getValue ( "RightPanel.phases" , this . viewedRoomId ) , room ) ;
240+ } else {
241+ logger . warn (
242+ "Could not restore the right panel after load because there was no associated room object." ,
243+ ) ;
244+ }
232245 }
233246 }
234247
@@ -273,37 +286,31 @@ export default class RightPanelStore extends ReadyWatchingStore {
273286 case RightPanelPhases . ThreadView :
274287 if ( ! SettingsStore . getValue ( "feature_thread" ) ) return false ;
275288 if ( ! card . state . threadHeadEvent ) {
276- console . warn ( "removed card from right panel because of missing threadHeadEvent in card state" ) ;
289+ logger . warn ( "removed card from right panel because of missing threadHeadEvent in card state" ) ;
277290 }
278291 return ! ! card . state . threadHeadEvent ;
279292 case RightPanelPhases . RoomMemberInfo :
280293 case RightPanelPhases . SpaceMemberInfo :
281294 case RightPanelPhases . EncryptionPanel :
282295 if ( ! card . state . member ) {
283- console . warn ( "removed card from right panel because of missing member in card state" ) ;
296+ logger . warn ( "removed card from right panel because of missing member in card state" ) ;
284297 }
285298 return ! ! card . state . member ;
286299 case RightPanelPhases . Room3pidMemberInfo :
287300 case RightPanelPhases . Space3pidMemberInfo :
288301 if ( ! card . state . memberInfoEvent ) {
289- console . warn ( "removed card from right panel because of missing memberInfoEvent in card state" ) ;
302+ logger . warn ( "removed card from right panel because of missing memberInfoEvent in card state" ) ;
290303 }
291304 return ! ! card . state . memberInfoEvent ;
292305 case RightPanelPhases . Widget :
293306 if ( ! card . state . widgetId ) {
294- console . warn ( "removed card from right panel because of missing widgetId in card state" ) ;
307+ logger . warn ( "removed card from right panel because of missing widgetId in card state" ) ;
295308 }
296309 return ! ! card . state . widgetId ;
297310 }
298311 return true ;
299312 }
300313
301- private setRightPanelCache ( card : IRightPanelCard , roomId ?: string ) {
302- const history = [ { phase : card . phase , state : card . state ?? { } } ] ;
303- this . byRoom [ roomId ?? this . viewedRoomId ] = { history, isOpen : true } ;
304- this . emitAndUpdateSettings ( ) ;
305- }
306-
307314 private getVerificationRedirect ( card : IRightPanelCard ) : IRightPanelCard {
308315 if ( card . phase === RightPanelPhases . RoomMemberInfo && card . state ) {
309316 // RightPanelPhases.RoomMemberInfo -> needs to be changed to RightPanelPhases.EncryptionPanel if there is a pending verification request
@@ -322,7 +329,7 @@ export default class RightPanelStore extends ReadyWatchingStore {
322329 return null ;
323330 }
324331
325- public isPhaseValid ( targetPhase : RightPanelPhases , isViewingRoom = this . isViewingRoom ) : boolean {
332+ private isPhaseValid ( targetPhase : RightPanelPhases , isViewingRoom : boolean ) : boolean {
326333 if ( ! RightPanelPhases [ targetPhase ] ) {
327334 logger . warn ( `Tried to switch right panel to unknown phase: ${ targetPhase } ` ) ;
328335 return false ;
@@ -386,10 +393,6 @@ export default class RightPanelStore extends ReadyWatchingStore {
386393 this . emitAndUpdateSettings ( ) ;
387394 }
388395
389- private get isViewingRoom ( ) : boolean {
390- return ! ! this . viewedRoomId ;
391- }
392-
393396 public static get instance ( ) : RightPanelStore {
394397 if ( ! RightPanelStore . internalInstance ) {
395398 RightPanelStore . internalInstance = new RightPanelStore ( ) ;
0 commit comments