Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit ef48443

Browse files
authored
Show chat panel when opening a video room with unread messages (#8812)
* Show chat panel when opening a video room with unread messages * Remove unnecessary calls to private methods in tests * Make room ID mandatory when toggling the right panel * Restore the isViewingRoom check * Test RightPanelStore * Make the constructor private again * Add even more tests * Fix onReady
1 parent 162be6c commit ef48443

File tree

11 files changed

+432
-124
lines changed

11 files changed

+432
-124
lines changed

src/components/structures/LoggedInView.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ class LoggedInView extends React.Component<IProps, IState> {
493493
break;
494494
case KeyBindingAction.ToggleRoomSidePanel:
495495
if (this.props.page_type === "room_view") {
496-
RightPanelStore.instance.togglePanel();
496+
RightPanelStore.instance.togglePanel(null);
497497
handled = true;
498498
}
499499
break;

src/components/structures/RightPanel.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,6 @@ export default class RightPanel extends React.Component<IProps, IState> {
9191
currentCard = RightPanelStore.instance.currentCardForRoom(props.room.roomId);
9292
}
9393

94-
if (currentCard?.phase && !RightPanelStore.instance.isPhaseValid(currentCard.phase, !!props.room)) {
95-
// XXX: We can probably get rid of this workaround once GroupView is dead, it's unmounting happens weirdly
96-
// late causing the app to soft-crash due to lack of a room object being passed to a RightPanel
97-
return null; // skip this update, we're about to be unmounted and don't have the appropriate props
98-
}
99-
10094
return {
10195
cardState: currentCard?.state,
10296
phase: currentCard?.phase,
@@ -142,7 +136,7 @@ export default class RightPanel extends React.Component<IProps, IState> {
142136
// When the user clicks close on the encryption panel cancel the pending request first if any
143137
this.state.cardState.verificationRequest.cancel();
144138
} else {
145-
RightPanelStore.instance.togglePanel();
139+
RightPanelStore.instance.togglePanel(this.props.room?.roomId);
146140
}
147141
};
148142

src/components/structures/RoomView.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
361361
) {
362362
// hide chat in right panel when the widget is minimized
363363
RightPanelStore.instance.setCard({ phase: RightPanelPhases.RoomSummary });
364-
RightPanelStore.instance.togglePanel();
364+
RightPanelStore.instance.togglePanel(this.state.roomId);
365365
}
366366
this.checkWidgets(this.state.room);
367367
};
@@ -1020,6 +1020,14 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
10201020
this.updatePermissions(room);
10211021
this.checkWidgets(room);
10221022

1023+
if (
1024+
this.getMainSplitContentType(room) !== MainSplitContentType.Timeline
1025+
&& RoomNotificationStateStore.instance.getRoomState(room).isUnread
1026+
) {
1027+
// Automatically open the chat panel to make unread messages easier to discover
1028+
RightPanelStore.instance.setCard({ phase: RightPanelPhases.Timeline }, true, room.roomId);
1029+
}
1030+
10231031
this.setState({
10241032
tombstone: this.getRoomTombstone(room),
10251033
liveTimeline: room.getLiveTimeline(),

src/components/views/right_panel/EncryptionPanel.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ const EncryptionPanel: React.FC<IProps> = (props: IProps) => {
122122
state: { member, verificationRequest: verificationRequest_ },
123123
});
124124
}
125-
if (!RightPanelStore.instance.isOpen) RightPanelStore.instance.togglePanel();
125+
if (!RightPanelStore.instance.isOpen) RightPanelStore.instance.togglePanel(null);
126126
}, [member]);
127127

128128
const requested =

src/components/views/right_panel/HeaderButtons.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ export default abstract class HeaderButtons<P = {}> extends React.Component<IPro
7070
public setPhase(phase: RightPanelPhases, cardState?: Partial<IRightPanelCardState>) {
7171
const rps = RightPanelStore.instance;
7272
if (rps.currentCard.phase == phase && !cardState && rps.isOpen) {
73-
rps.togglePanel();
73+
rps.togglePanel(null);
7474
} else {
7575
RightPanelStore.instance.setCard({ phase, state: cardState });
76-
if (!rps.isOpen) rps.togglePanel();
76+
if (!rps.isOpen) rps.togglePanel(null);
7777
}
7878
}
7979

src/components/views/right_panel/RoomHeaderButtons.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ export default class RoomHeaderButtons extends HeaderButtons<IProps> {
209209

210210
private onThreadsPanelClicked = (ev: ButtonEvent) => {
211211
if (RoomHeaderButtons.THREAD_PHASES.includes(this.state.phase)) {
212-
RightPanelStore.instance.togglePanel();
212+
RightPanelStore.instance.togglePanel(this.props.room?.roomId);
213213
} else {
214214
showThreadPanel();
215215
PosthogTrackers.trackInteraction("WebRoomHeaderButtonsThreadsButton", ev);

src/stores/ReadyWatchingStore.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ export abstract class ReadyWatchingStore extends EventEmitter implements IDestro
7373
// Everything after this is unnecessary (we only need to know once we have a client)
7474
// and we intentionally don't set the client before this point to avoid stores
7575
// updating for every event emitted during the cached sync.
76-
if (!(payload.prevState === SyncState.Prepared && payload.state !== SyncState.Prepared)) {
77-
return;
78-
}
79-
80-
if (this.matrixClient !== payload.matrixClient) {
76+
if (
77+
payload.prevState !== SyncState.Prepared
78+
&& payload.state === SyncState.Prepared
79+
&& this.matrixClient !== payload.matrixClient
80+
) {
8181
if (this.matrixClient) {
8282
await this.onNotReady();
8383
}

src/stores/right-panel/RightPanelStore.ts

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,22 @@ import { RoomViewStore } from "../RoomViewStore";
4545
export 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

Comments
 (0)