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

Fix regression with TimelinePanel props updates not taking effect #9608

Merged
merged 5 commits into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/components/structures/TimelinePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ class TimelinePanel extends React.Component<IProps, IState> {
this.initTimeline(this.props);
}

public componentDidUpdate(newProps) {
if (newProps.timelineSet !== this.props.timelineSet) {
public componentDidUpdate(prevProps) {
if (prevProps.timelineSet !== this.props.timelineSet) {
// throw new Error("changing timelineSet on a TimelinePanel is not supported");

// regrettably, this does happen; in particular, when joining a
Expand All @@ -325,13 +325,13 @@ class TimelinePanel extends React.Component<IProps, IState> {
logger.warn("Replacing timelineSet on a TimelinePanel - confusion may ensue");
}

const differentEventId = newProps.eventId != this.props.eventId;
const differentHighlightedEventId = newProps.highlightedEventId != this.props.highlightedEventId;
const differentAvoidJump = newProps.eventScrollIntoView && !this.props.eventScrollIntoView;
const differentEventId = prevProps.eventId != this.props.eventId;
const differentHighlightedEventId = prevProps.highlightedEventId != this.props.highlightedEventId;
const differentAvoidJump = prevProps.eventScrollIntoView && !this.props.eventScrollIntoView;
if (differentEventId || differentHighlightedEventId || differentAvoidJump) {
logger.log(`TimelinePanel switching to eventId ${newProps.eventId} (was ${this.props.eventId}), ` +
`scrollIntoView: ${newProps.eventScrollIntoView} (was ${this.props.eventScrollIntoView})`);
this.initTimeline(newProps);
logger.log(`TimelinePanel switching to eventId ${this.props.eventId} (was ${prevProps.eventId}), ` +
`scrollIntoView: ${this.props.eventScrollIntoView} (was ${prevProps.eventScrollIntoView})`);
this.initTimeline(this.props);
}
}

Expand Down
36 changes: 28 additions & 8 deletions test/components/structures/TimelinePanel-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const newReceipt = (eventId: string, userId: string, readTs: number, fullyReadTs
return new MatrixEvent({ content: receiptContent, type: EventType.Receipt });
};

const renderPanel = (room: Room, events: MatrixEvent[]): RenderResult => {
const getProps = (room: Room, events: MatrixEvent[]): TimelinePanel["props"] => {
const timelineSet = { room: room as Room } as EventTimelineSet;
const timeline = new EventTimeline(timelineSet);
events.forEach((event) => timeline.addEvent(event, true));
Expand All @@ -54,13 +54,16 @@ const renderPanel = (room: Room, events: MatrixEvent[]): RenderResult => {
timelineSet.getPendingEvents = () => events;
timelineSet.room.getEventReadUpTo = () => events[1].getId();

return render(
<TimelinePanel
timelineSet={timelineSet}
manageReadReceipts
sendReadReceiptOnLoad
/>,
);
return {
timelineSet,
manageReadReceipts: true,
sendReadReceiptOnLoad: true,
};
};

const renderPanel = (room: Room, events: MatrixEvent[]): RenderResult => {
const props = getProps(room, events);
return render(<TimelinePanel {...props} />);
};

const mockEvents = (room: Room, count = 2): MatrixEvent[] => {
Expand Down Expand Up @@ -172,4 +175,21 @@ describe('TimelinePanel', () => {
expect(client.setRoomReadMarkers).toHaveBeenCalledWith(room.roomId, "", undefined, events[0]);
});
});

it("should scroll event into view when props.eventId changes", () => {
const client = MatrixClientPeg.get();
const room = mkRoom(client, "roomId");
const events = mockEvents(room);

const props = {
...getProps(room, events),
onEventScrolledIntoView: jest.fn(),
};

const { rerender } = render(<TimelinePanel {...props} />);
expect(props.onEventScrolledIntoView).toHaveBeenCalledWith(undefined);
props.eventId = events[1].getId();
rerender(<TimelinePanel {...props} />);
expect(props.onEventScrolledIntoView).toHaveBeenCalledWith(events[1].getId());
});
});