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

Commit 25dfc88

Browse files
t3chguyAmy Walker
authored andcommitted
Fix regression with TimelinePanel props updates not taking effect (#9608)
* Fix regression with TimelinePanel props updates not taking effect * Add test
1 parent cf7d9e3 commit 25dfc88

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed

src/components/structures/TimelinePanel.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,8 @@ class TimelinePanel extends React.Component<IProps, IState> {
309309
this.initTimeline(this.props);
310310
}
311311

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

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

328-
const differentEventId = newProps.eventId != this.props.eventId;
329-
const differentHighlightedEventId = newProps.highlightedEventId != this.props.highlightedEventId;
330-
const differentAvoidJump = newProps.eventScrollIntoView && !this.props.eventScrollIntoView;
328+
const differentEventId = prevProps.eventId != this.props.eventId;
329+
const differentHighlightedEventId = prevProps.highlightedEventId != this.props.highlightedEventId;
330+
const differentAvoidJump = prevProps.eventScrollIntoView && !this.props.eventScrollIntoView;
331331
if (differentEventId || differentHighlightedEventId || differentAvoidJump) {
332-
logger.log(`TimelinePanel switching to eventId ${newProps.eventId} (was ${this.props.eventId}), ` +
333-
`scrollIntoView: ${newProps.eventScrollIntoView} (was ${this.props.eventScrollIntoView})`);
334-
this.initTimeline(newProps);
332+
logger.log(`TimelinePanel switching to eventId ${this.props.eventId} (was ${prevProps.eventId}), ` +
333+
`scrollIntoView: ${this.props.eventScrollIntoView} (was ${prevProps.eventScrollIntoView})`);
334+
this.initTimeline(this.props);
335335
}
336336
}
337337

test/components/structures/TimelinePanel-test.tsx

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const newReceipt = (eventId: string, userId: string, readTs: number, fullyReadTs
4545
return new MatrixEvent({ content: receiptContent, type: EventType.Receipt });
4646
};
4747

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

57-
return render(
58-
<TimelinePanel
59-
timelineSet={timelineSet}
60-
manageReadReceipts
61-
sendReadReceiptOnLoad
62-
/>,
63-
);
57+
return {
58+
timelineSet,
59+
manageReadReceipts: true,
60+
sendReadReceiptOnLoad: true,
61+
};
62+
};
63+
64+
const renderPanel = (room: Room, events: MatrixEvent[]): RenderResult => {
65+
const props = getProps(room, events);
66+
return render(<TimelinePanel {...props} />);
6467
};
6568

6669
const mockEvents = (room: Room, count = 2): MatrixEvent[] => {
@@ -172,4 +175,21 @@ describe('TimelinePanel', () => {
172175
expect(client.setRoomReadMarkers).toHaveBeenCalledWith(room.roomId, "", undefined, events[0]);
173176
});
174177
});
178+
179+
it("should scroll event into view when props.eventId changes", () => {
180+
const client = MatrixClientPeg.get();
181+
const room = mkRoom(client, "roomId");
182+
const events = mockEvents(room);
183+
184+
const props = {
185+
...getProps(room, events),
186+
onEventScrolledIntoView: jest.fn(),
187+
};
188+
189+
const { rerender } = render(<TimelinePanel {...props} />);
190+
expect(props.onEventScrolledIntoView).toHaveBeenCalledWith(undefined);
191+
props.eventId = events[1].getId();
192+
rerender(<TimelinePanel {...props} />);
193+
expect(props.onEventScrolledIntoView).toHaveBeenCalledWith(events[1].getId());
194+
});
175195
});

0 commit comments

Comments
 (0)