Skip to content

Commit

Permalink
don't restore MemberInfo from RightPanel history when viewing a room
Browse files Browse the repository at this point in the history
As bringing up a specific MemberInfo when you view a room is freaky,
even if it happened to be the last thing you were doing in that room.
Fixes element-hq/element-web#21487
  • Loading branch information
ara4n committed Mar 20, 2022
1 parent d7a3f39 commit f54f03b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/components/structures/RightPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,30 @@ export default class RightPanel extends React.Component<IProps, IState> {
public componentDidMount(): void {
this.context.on(RoomStateEvent.Members, this.onRoomStateMember);
RightPanelStore.instance.on(UPDATE_EVENT, this.onRightPanelStoreUpdate);

if (this.props.room?.roomId) {
// skip cards which are not to be restored from history
// (i.e. memberinfo cards which have already been viewed)
while (RightPanelStore.instance.currentCardForRoom(this.props.room.roomId)?.state.skipFromHistory) {
RightPanelStore.instance.popCard();
}
}
}

public componentWillUnmount(): void {
this.context?.removeListener(RoomStateEvent.Members, this.onRoomStateMember);
RightPanelStore.instance.off(UPDATE_EVENT, this.onRightPanelStoreUpdate);
}

private static skipHistory(card: IRightPanelCard): boolean {
// determines phases whose history should not be persisted per-room
// so we don't find ourselves being teleported into random old MemberInfo
// views when viewing a given room.
// See https://github.com/vector-im/element-web/issues/21487
return (card.phase === RightPanelPhases.RoomMemberInfo ||
card.phase === RightPanelPhases.Room3pidMemberInfo);
}

public static getDerivedStateFromProps(props: IProps): Partial<IState> {
let currentCard: IRightPanelCard;
if (props.room) {
Expand All @@ -100,6 +117,11 @@ export default class RightPanel extends React.Component<IProps, IState> {
currentCard = RightPanelStore.instance.currentGroup;
}

if (RightPanel.skipHistory(currentCard)) {
// having displayed this card, don't restore this card from history in future
RightPanelStore.instance.skipHistoryForCard(currentCard);
}

if (currentCard?.phase && !RightPanelStore.instance.isPhaseValid(currentCard.phase, !!props.room)) {
// XXX: We can probably get rid of this workaround once GroupView is dead, it's unmounting happens weirdly
// late causing the app to soft-crash due to lack of a room object being passed to a RightPanel
Expand Down
6 changes: 5 additions & 1 deletion src/stores/right-panel/RightPanelStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export default class RightPanelStore extends ReadyWatchingStore {
if (!this.isPhaseValid(targetPhase)) return;

if ((targetPhase === this.currentCardForRoom(rId)?.phase && !!cardState)) {
// Update state: set right panel with a new state but keep the phase (dont know it this is ever needed...)
// Update state: set right panel with a new state but keep the phase (don't know it this is ever needed...)
const hist = this.byRoom[rId]?.history ?? [];
hist[hist.length - 1].state = cardState;
this.emitAndUpdateSettings();
Expand Down Expand Up @@ -214,6 +214,10 @@ export default class RightPanelStore extends ReadyWatchingStore {
this.emitAndUpdateSettings();
}

public skipHistoryForCard(card: IRightPanelCard) {
card.state.skipFromHistory = true;
}

public popCard(roomId: string = null) {
const rId = roomId ?? this.viewedRoomId;
if (!this.byRoom[rId]) return;
Expand Down
6 changes: 6 additions & 0 deletions src/stores/right-panel/RightPanelStoreIPanelState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export interface IRightPanelCardState {
threadHeadEvent?: MatrixEvent;
initialEvent?: MatrixEvent;
isInitialEventHighlighted?: boolean;
// should this card be skipped when switching to a room?
skipFromHistory?: boolean;
}

export interface IRightPanelCardStateStored {
Expand All @@ -54,6 +56,8 @@ export interface IRightPanelCardStateStored {
threadHeadEventId?: string;
initialEventId?: string;
isInitialEventHighlighted?: boolean;
// should this card be skipped when switching to a room?
skipFromHistory?: boolean;
}

export interface IRightPanelCard {
Expand Down Expand Up @@ -104,6 +108,7 @@ export function convertCardToStore(panelState: IRightPanelCard): IRightPanelCard
panelState.state.initialEvent.getId() : undefined,
memberId: !!state?.member?.userId ?
panelState.state.member.userId : undefined,
skipFromHistory: state.skipFromHistory,
};

return { state: stateStored, phase: panelState.phase };
Expand All @@ -125,6 +130,7 @@ function convertStoreToCard(panelStateStore: IRightPanelCardStored, room: Room):
room.findEventById(stateStored.initialEventId) : undefined,
member: !!stateStored?.memberId ?
room.getMember(stateStored.memberId) : undefined,
skipFromHistory: stateStored.skipFromHistory,
};

return { state: state, phase: panelStateStore.phase };
Expand Down

0 comments on commit f54f03b

Please sign in to comment.