Refactor EventTile using the MVVM pattern - #7a (#33640)

* Make EventTileViewModel an owned root VM

* Move timestamp sub-VMs under EventTileViewModel

* Move thread-list action bar VM under EventTileViewModel

* Clean up for improved readability

* Clean up to avoid duplicate EventTile render derivations

* Avoid mutating EventTileViewModel during render

* Move EventTile child VM syncing into adapters

* Replace timestamp VM field setters with batched setProps

* Component wrappers at the end of the file

* Lazy-create EventTile child view models
This commit is contained in:
rbondesson
2026-05-29 05:56:31 +00:00
committed by GitHub
parent 0ed98941bd
commit 3aa3678c2d
6 changed files with 392 additions and 273 deletions
@@ -5,7 +5,7 @@
* Please see LICENSE files in the repository root for full details.
*/
import { EventStatus, EventType, type MatrixEvent, MsgType } from "matrix-js-sdk/src/matrix";
import { EventStatus, EventType, MatrixEvent, MsgType } from "matrix-js-sdk/src/matrix";
import { mkEvent } from "../../test-utils";
import { TimelineRenderingType } from "../../../src/contexts/RoomContext";
@@ -450,4 +450,74 @@ describe("EventTileViewModel", () => {
showInDefaultLayout: false,
});
});
it("updates an instance snapshot when inputs change", () => {
const vm = new EventTileViewModel(makeProps());
const listener = jest.fn();
const unsubscribe = vm.subscribe(listener);
expect(vm.getSnapshot().snapshot.timestamp.show).toBe(false);
vm.setProps(makeProps({ interaction: { hover: true } }));
expect(vm.getSnapshot().snapshot.timestamp.show).toBe(true);
expect(listener).toHaveBeenCalled();
unsubscribe();
vm.dispose();
});
it("lazily owns timestamp child view models", () => {
const vm = new EventTileViewModel(makeProps());
const messageTimestampViewModel = vm.getMessageTimestampViewModel({ ts: 123 });
const linkedMessageTimestampViewModel = vm.getLinkedMessageTimestampViewModel({ ts: 456 });
expect(messageTimestampViewModel.getSnapshot().href).toBeUndefined();
expect(linkedMessageTimestampViewModel.getSnapshot().href).toBeUndefined();
vm.dispose();
});
it("does not initialize timestamp child view models for events without an origin timestamp", () => {
const mxEvent = new MatrixEvent({
type: EventType.RoomMessage,
room_id: roomId,
sender: userId,
content: { msgtype: MsgType.Text, body: "Hello" },
event_id: "$event",
});
const vm = new EventTileViewModel(
makeProps({
event: {
mxEvent,
},
timestamp: {
hideTimestamp: true,
},
}),
);
expect(vm.getSnapshot().timestamp.displayState.showRealTimestamp).toBe(false);
vm.dispose();
});
it("owns and updates the thread-list action bar child view model", () => {
const vm = new EventTileViewModel(makeProps());
const onViewInRoomClick = jest.fn();
const onCopyLinkClick = jest.fn();
const threadListActionBarViewModel = vm.getThreadListActionBarViewModel({
onViewInRoomClick,
onCopyLinkClick,
});
threadListActionBarViewModel.onViewInRoomClick(null);
threadListActionBarViewModel.onCopyLinkClick(null);
expect(onViewInRoomClick).toHaveBeenCalledWith(null);
expect(onCopyLinkClick).toHaveBeenCalledWith(null);
vm.dispose();
});
});