Skip to content

Commit 550f529

Browse files
Implement MessagePreviewViewModel (#29514)
* Implement message preview vm * Write tests
1 parent a6ad6e9 commit 550f529

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2025 New Vector Ltd.
3+
*
4+
* SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
5+
* Please see LICENSE files in the repository root for full details.
6+
*/
7+
8+
import { useCallback, useEffect, useState } from "react";
9+
10+
import type { Room } from "matrix-js-sdk/src/matrix";
11+
import { type MessagePreview, MessagePreviewStore } from "../../../stores/room-list/MessagePreviewStore";
12+
import { useEventEmitter } from "../../../hooks/useEventEmitter";
13+
14+
interface MessagePreviewViewState {
15+
/**
16+
* A string representation of the message preview if available.
17+
*/
18+
message?: string;
19+
}
20+
21+
/**
22+
* View model for rendering a message preview for a given room list item.
23+
* @param room The room for which we're rendering the message preview.
24+
* @see {@link MessagePreviewViewState} for what this view model returns.
25+
*/
26+
export function useMessagePreviewViewModel(room: Room): MessagePreviewViewState {
27+
const [messagePreview, setMessagePreview] = useState<MessagePreview | null>(null);
28+
29+
const updatePreview = useCallback(async (): Promise<void> => {
30+
/**
31+
* The second argument to getPreviewForRoom is a tag id which doesn't really make
32+
* much sense within the context of the new room list. We can pass an empty string
33+
* to match all tags for now but we should remember to actually change the implementation
34+
* in the store once we remove the legacy room list.
35+
*/
36+
const newPreview = await MessagePreviewStore.instance.getPreviewForRoom(room, "");
37+
setMessagePreview(newPreview);
38+
}, [room]);
39+
40+
/**
41+
* Update when the message preview has changed for this room.
42+
*/
43+
useEventEmitter(MessagePreviewStore.instance, MessagePreviewStore.getPreviewChangedEventName(room), () => {
44+
updatePreview();
45+
});
46+
47+
/**
48+
* Do an initial fetch of the message preview.
49+
*/
50+
useEffect(() => {
51+
updatePreview();
52+
}, [updatePreview]);
53+
54+
return {
55+
message: messagePreview?.text,
56+
};
57+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2025 New Vector Ltd.
3+
*
4+
* SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
5+
* Please see LICENSE files in the repository root for full details.
6+
*/
7+
8+
import { renderHook, waitFor } from "jest-matrix-react";
9+
import { type Room } from "matrix-js-sdk/src/matrix";
10+
11+
import { createTestClient, mkStubRoom } from "../../../../test-utils";
12+
import { type MessagePreview, MessagePreviewStore } from "../../../../../src/stores/room-list/MessagePreviewStore";
13+
import { useMessagePreviewViewModel } from "../../../../../src/components/viewmodels/roomlist/MessagePreviewViewModel";
14+
15+
describe("MessagePreviewViewModel", () => {
16+
let room: Room;
17+
18+
beforeEach(() => {
19+
const matrixClient = createTestClient();
20+
room = mkStubRoom("roomId", "roomName", matrixClient);
21+
});
22+
23+
it("should do an initial fetch of the message preview", async () => {
24+
// Mock the store to return some text.
25+
jest.spyOn(MessagePreviewStore.instance, "getPreviewForRoom").mockImplementation(async (room) => {
26+
return { text: "Hello world!" } as MessagePreview;
27+
});
28+
29+
const { result: vm } = renderHook(() => useMessagePreviewViewModel(room));
30+
31+
// Eventually, vm.message should have the text from the store.
32+
await waitFor(() => {
33+
expect(vm.current.message).toEqual("Hello world!");
34+
});
35+
});
36+
37+
it("should fetch message preview again on update from store", async () => {
38+
// Mock the store to return the text in variable message.
39+
let message = "Hello World!";
40+
jest.spyOn(MessagePreviewStore.instance, "getPreviewForRoom").mockImplementation(async (room) => {
41+
return { text: message } as MessagePreview;
42+
});
43+
jest.spyOn(MessagePreviewStore, "getPreviewChangedEventName").mockImplementation((room) => {
44+
return "UPDATE";
45+
});
46+
47+
const { result: vm } = renderHook(() => useMessagePreviewViewModel(room));
48+
49+
// Let's assume the message changed.
50+
message = "New message!";
51+
MessagePreviewStore.instance.emit("UPDATE");
52+
53+
/// vm.message should be the updated message.
54+
await waitFor(() => {
55+
expect(vm.current.message).toEqual(message);
56+
});
57+
});
58+
});

0 commit comments

Comments
 (0)