From d341c56b1f90db2d633876b6245ad88b7fdc5f60 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 29 Nov 2022 14:55:48 +0000 Subject: [PATCH] Fix issues around up arrow event edit shortcut (#9645) --- src/components/structures/RoomView.tsx | 13 ++++++++----- src/utils/EventUtils.ts | 3 ++- test/utils/EventUtils-test.ts | 10 ++++++++++ 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/components/structures/RoomView.tsx b/src/components/structures/RoomView.tsx index b9150624fe..1346cb49b9 100644 --- a/src/components/structures/RoomView.tsx +++ b/src/components/structures/RoomView.tsx @@ -25,7 +25,6 @@ import { logger } from "matrix-js-sdk/src/logger"; import { EventTimeline } from 'matrix-js-sdk/src/models/event-timeline'; import { EventType } from 'matrix-js-sdk/src/@types/event'; import { RoomState, RoomStateEvent } from 'matrix-js-sdk/src/models/room-state'; -import { EventTimelineSet } from "matrix-js-sdk/src/models/event-timeline-set"; import { CallState, MatrixCall } from "matrix-js-sdk/src/webrtc/call"; import { throttle } from "lodash"; import { MatrixError } from 'matrix-js-sdk/src/http-api'; @@ -1211,10 +1210,14 @@ export class RoomView extends React.Component { }); }; - private onRoomTimelineReset = (room: Room, timelineSet: EventTimelineSet) => { - if (!room || room.roomId !== this.state.room?.roomId) return; - logger.log(`Live timeline of ${room.roomId} was reset`); - this.setState({ liveTimeline: timelineSet.getLiveTimeline() }); + private onRoomTimelineReset = (room?: Room): void => { + if (room && + room.roomId === this.state.room?.roomId && + room.getLiveTimeline() !== this.state.liveTimeline + ) { + logger.log(`Live timeline of ${room.roomId} was reset`); + this.setState({ liveTimeline: room.getLiveTimeline() }); + } }; private getRoomTombstone(room = this.state.room) { diff --git a/src/utils/EventUtils.ts b/src/utils/EventUtils.ts index 69e322ac6d..69c3351def 100644 --- a/src/utils/EventUtils.ts +++ b/src/utils/EventUtils.ts @@ -110,7 +110,8 @@ export function findEditableEvent({ events: MatrixEvent[]; isForward: boolean; fromEventId?: string; -}): MatrixEvent { +}): MatrixEvent | undefined { + if (!events.length) return; const maxIdx = events.length - 1; const inc = isForward ? 1 : -1; const beginIdx = isForward ? 0 : maxIdx; diff --git a/test/utils/EventUtils-test.ts b/test/utils/EventUtils-test.ts index 644f274c19..bf72dcd9fa 100644 --- a/test/utils/EventUtils-test.ts +++ b/test/utils/EventUtils-test.ts @@ -34,6 +34,7 @@ import { canEditContent, canEditOwnEvent, fetchInitialEvent, + findEditableEvent, isContentActionable, isLocationEvent, isVoiceMessage, @@ -430,4 +431,13 @@ describe('EventUtils', () => { expect(room.getThread(THREAD_ROOT)).toBeInstanceOf(Thread); }); }); + + describe("findEditableEvent", () => { + it("should not explode when given empty events array", () => { + expect(findEditableEvent({ + events: [], + isForward: true, + })).toBeUndefined(); + }); + }); });