Skip to content

Commit d88776e

Browse files
RoomListViewModel: Add functionality to toggle message preview setting (#29511)
* Add setting for showing message previews * Add hook to track and toggle message preview * Use hook in view model * Add tests * Fix tests * Fix lint * Fix typo
1 parent ff1da50 commit d88776e

File tree

6 files changed

+81
-0
lines changed

6 files changed

+81
-0
lines changed

src/components/viewmodels/roomlist/RoomListViewModel.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Please see LICENSE files in the repository root for full details.
88
import type { Room } from "matrix-js-sdk/src/matrix";
99
import { type PrimaryFilter, type SecondaryFilters, useFilteredRooms } from "./useFilteredRooms";
1010
import { type SortOption, useSorter } from "./useSorter";
11+
import { useMessagePreviewToggle } from "./useMessagePreviewToggle";
1112

1213
export interface RoomListViewState {
1314
/**
@@ -39,6 +40,16 @@ export interface RoomListViewState {
3940
* The currently active sort option.
4041
*/
4142
activeSortOption: SortOption;
43+
44+
/**
45+
* Whether message previews must be shown or not.
46+
*/
47+
shouldShowMessagePreview: boolean;
48+
49+
/**
50+
* A function to turn on/off message previews.
51+
*/
52+
toggleMessagePreview: () => void;
4253
}
4354

4455
/**
@@ -48,6 +59,7 @@ export interface RoomListViewState {
4859
export function useRoomListViewModel(): RoomListViewState {
4960
const { primaryFilters, rooms, activateSecondaryFilter, activeSecondaryFilter } = useFilteredRooms();
5061
const { activeSortOption, sort } = useSorter();
62+
const { shouldShowMessagePreview, toggleMessagePreview } = useMessagePreviewToggle();
5163

5264
return {
5365
rooms,
@@ -56,5 +68,7 @@ export function useRoomListViewModel(): RoomListViewState {
5668
activeSecondaryFilter,
5769
activeSortOption,
5870
sort,
71+
shouldShowMessagePreview,
72+
toggleMessagePreview,
5973
};
6074
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
import { useCallback, useState } from "react";
8+
9+
import SettingsStore from "../../../settings/SettingsStore";
10+
import { SettingLevel } from "../../../settings/SettingLevel";
11+
12+
interface MessagePreviewToggleState {
13+
shouldShowMessagePreview: boolean;
14+
toggleMessagePreview: () => void;
15+
}
16+
17+
/**
18+
* This hook:
19+
* - Provides a state that tracks whether message previews are turned on or off.
20+
* - Provides a function to toggle message previews.
21+
*/
22+
export function useMessagePreviewToggle(): MessagePreviewToggleState {
23+
const [shouldShowMessagePreview, setShouldShowMessagePreview] = useState(() =>
24+
SettingsStore.getValue("RoomList.showMessagePreview"),
25+
);
26+
27+
const toggleMessagePreview = useCallback((): void => {
28+
setShouldShowMessagePreview((current) => {
29+
const toggled = !current;
30+
SettingsStore.setValue("RoomList.showMessagePreview", null, SettingLevel.DEVICE, toggled);
31+
return toggled;
32+
});
33+
}, []);
34+
35+
return { toggleMessagePreview, shouldShowMessagePreview };
36+
}

src/settings/Settings.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ export interface Settings {
314314
"showImages": IBaseSetting<boolean>;
315315
"showAvatarsOnInvites": IBaseSetting<boolean>;
316316
"RoomList.preferredSorting": IBaseSetting<SortingAlgorithm>;
317+
"RoomList.showMessagePreview": IBaseSetting<boolean>;
317318
"RightPanel.phasesGlobal": IBaseSetting<IRightPanelForRoomStored | null>;
318319
"RightPanel.phases": IBaseSetting<IRightPanelForRoomStored | null>;
319320
"enableEventIndexing": IBaseSetting<boolean>;
@@ -1126,6 +1127,10 @@ export const SETTINGS: Settings = {
11261127
supportedLevels: [SettingLevel.DEVICE],
11271128
default: SortingAlgorithm.Recency,
11281129
},
1130+
"RoomList.showMessagePreview": {
1131+
supportedLevels: [SettingLevel.DEVICE],
1132+
default: false,
1133+
},
11291134
"RightPanel.phasesGlobal": {
11301135
supportedLevels: [SettingLevel.DEVICE],
11311136
default: null,

test/unit-tests/components/viewmodels/roomlist/RoomListViewModel-test.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,26 @@ describe("RoomListViewModel", () => {
218218
expect(vm.current.activeSortOption).toEqual(SortOption.AToZ);
219219
});
220220
});
221+
222+
describe("message preview toggle", () => {
223+
it("should return shouldShowMessagePreview based on setting", () => {
224+
jest.spyOn(SettingsStore, "getValue").mockImplementation(() => true);
225+
mockAndCreateRooms();
226+
const { result: vm } = renderHook(() => useRoomListViewModel());
227+
expect(vm.current.shouldShowMessagePreview).toEqual(true);
228+
});
229+
230+
it("should change setting on toggle", () => {
231+
jest.spyOn(SettingsStore, "getValue").mockImplementation(() => true);
232+
const fn = jest.spyOn(SettingsStore, "setValue").mockImplementation(async () => {});
233+
mockAndCreateRooms();
234+
const { result: vm } = renderHook(() => useRoomListViewModel());
235+
expect(vm.current.shouldShowMessagePreview).toEqual(true);
236+
act(() => {
237+
vm.current.toggleMessagePreview();
238+
});
239+
expect(vm.current.shouldShowMessagePreview).toEqual(false);
240+
expect(fn).toHaveBeenCalled();
241+
});
242+
});
221243
});

test/unit-tests/components/views/rooms/RoomListPanel/RoomList-test.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ describe("<RoomList />", () => {
3535
activeSecondaryFilter: SecondaryFilters.AllActivity,
3636
sort: jest.fn(),
3737
activeSortOption: SortOption.Activity,
38+
shouldShowMessagePreview: false,
39+
toggleMessagePreview: jest.fn(),
3840
};
3941

4042
// Needed to render a room list cell

test/unit-tests/components/views/rooms/RoomListPanel/RoomListPrimaryFilters-test.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ describe("<RoomListPrimaryFilters />", () => {
2828
activeSecondaryFilter: SecondaryFilters.AllActivity,
2929
sort: jest.fn(),
3030
activeSortOption: SortOption.Activity,
31+
shouldShowMessagePreview: false,
32+
toggleMessagePreview: jest.fn(),
3133
};
3234
});
3335

0 commit comments

Comments
 (0)