-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathRoomListHeaderViewModel.ts
More file actions
315 lines (276 loc) · 11.1 KB
/
RoomListHeaderViewModel.ts
File metadata and controls
315 lines (276 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
* Copyright 2025 New Vector Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
* Please see LICENSE files in the repository root for full details.
*/
import { JoinRule, type MatrixClient, type Room, RoomEvent, RoomType } from "matrix-js-sdk/src/matrix";
import {
BaseViewModel,
type RoomListHeaderViewSnapshot,
type RoomListHeaderViewModel as RoomListHeaderViewModelInterface,
type SortOption,
} from "@element-hq/web-shared-components";
import defaultDispatcher from "../../dispatcher/dispatcher";
import PosthogTrackers from "../../PosthogTrackers";
import { Action } from "../../dispatcher/actions";
import { getMetaSpaceName, type MetaSpace, UPDATE_HOME_BEHAVIOUR, UPDATE_SELECTED_SPACE } from "../../stores/spaces";
import { type SpaceStoreClass } from "../../stores/spaces/SpaceStore";
import {
shouldShowSpaceSettings,
showCreateNewRoom,
showSpaceInvite,
showSpacePreferences,
showSpaceSettings,
} from "../../utils/space";
import type { ViewRoomPayload } from "../../dispatcher/payloads/ViewRoomPayload";
import type { RoomListSectionsCollapseStateChangedPayload } from "../../dispatcher/payloads/RoomListSectionsCollapseStateChangedPayload";
import SettingsStore from "../../settings/SettingsStore";
import RoomListStoreV3 from "../../stores/room-list-v3/RoomListStoreV3";
import { SortingAlgorithm } from "../../stores/room-list-v3/skip-list/sorters";
import { SettingLevel } from "../../settings/SettingLevel";
import { createRoom, hasCreateRoomRights } from "./utils";
export interface Props {
/**
* The Matrix client instance.
*/
matrixClient: MatrixClient;
/**
* The space store instance.
*/
spaceStore: SpaceStoreClass;
}
/**
* ViewModel for the RoomListHeader.
* Manages the state and actions for the room list header.
*/
export class RoomListHeaderViewModel
extends BaseViewModel<RoomListHeaderViewSnapshot, Props>
implements RoomListHeaderViewModelInterface
{
/**
* Reference to the currently active space.
* Used to manage event listeners.
*/
private activeSpace: Room | null;
public constructor(props: Props) {
super(props, getInitialSnapshot(props.spaceStore, props.matrixClient));
// Listen for video rooms feature flag changes
const settingsFeatureVideoRef = SettingsStore.watchSetting(
"feature_video_rooms",
null,
this.onVideoRoomsFeatureFlagChange,
);
this.disposables.track(() => SettingsStore.unwatchSetting(settingsFeatureVideoRef));
// Listen for space changes
this.disposables.trackListener(props.spaceStore, UPDATE_SELECTED_SPACE, this.onSpaceChange);
this.disposables.trackListener(props.spaceStore, UPDATE_HOME_BEHAVIOUR, this.onHomeBehaviourChange);
// Listen for space name changes
this.activeSpace = props.spaceStore.activeSpaceRoom;
if (this.activeSpace) {
this.disposables.trackListener(this.activeSpace, RoomEvent.Name, this.onSpaceNameChange);
}
// Listen for section collapse state changes from RoomListViewModel
const dispatcherRef = defaultDispatcher.register(this.onDispatch);
this.disposables.track(() => defaultDispatcher.unregister(dispatcherRef));
}
/**
* Handles space change events.
*/
private readonly onSpaceChange = (): void => {
const activeSpace = this.props.spaceStore.activeSpaceRoom;
this.activeSpace?.off(RoomEvent.Name, this.onSpaceNameChange);
this.activeSpace = activeSpace;
// Add new room listener if needed
if (this.activeSpace) {
this.disposables.trackListener(this.activeSpace, RoomEvent.Name, this.onSpaceNameChange);
}
this.snapshot.merge({
...computeHeaderSpaceState(this.props.spaceStore, this.props.matrixClient),
});
};
/**
* Handles home behaviour change events.
*/
private readonly onHomeBehaviourChange = (): void => {
this.snapshot.merge({ title: getHeaderTitle(this.props.spaceStore) });
};
/**
* Handles space name change events.
*/
private onSpaceNameChange = (): void => {
this.snapshot.merge({ title: getHeaderTitle(this.props.spaceStore) });
};
/**
* Handles video rooms feature flag change events.
*/
private readonly onVideoRoomsFeatureFlagChange = (): void => {
this.snapshot.merge({
canCreateVideoRoom: getCanCreateVideoRoom(this.snapshot.current.canCreateRoom),
});
};
public createChatRoom = (e: Event): void => {
defaultDispatcher.fire(Action.CreateChat);
PosthogTrackers.trackInteraction("WebRoomListHeaderPlusMenuCreateChatItem", e);
};
public createRoom = (e: Event): void => {
createRoom(this.activeSpace);
PosthogTrackers.trackInteraction("WebRoomListHeaderPlusMenuCreateRoomItem", e);
};
public createVideoRoom = (): void => {
const type = SettingsStore.getValue("feature_element_call_video_rooms")
? RoomType.UnstableCall
: RoomType.ElementVideo;
if (this.activeSpace) {
showCreateNewRoom(this.activeSpace, type);
} else {
defaultDispatcher.dispatch({
action: Action.CreateRoom,
type,
});
}
};
public openSpaceHome = (): void => {
if (!this.activeSpace) return;
defaultDispatcher.dispatch<ViewRoomPayload>({
action: Action.ViewRoom,
room_id: this.activeSpace.roomId,
metricsTrigger: undefined,
});
};
public inviteInSpace = (): void => {
if (!this.activeSpace) return;
showSpaceInvite(this.activeSpace);
};
public openSpacePreferences = (): void => {
if (!this.activeSpace) return;
showSpacePreferences(this.activeSpace);
};
public openSpaceSettings = (): void => {
if (!this.activeSpace) return;
showSpaceSettings(this.activeSpace);
};
public sort = (option: SortOption): void => {
const oldSortingAlgorithm = RoomListStoreV3.instance.activeSortAlgorithm;
let newSortingAlgorithm: SortingAlgorithm;
switch (option) {
case "alphabetical":
newSortingAlgorithm = SortingAlgorithm.Alphabetic;
break;
case "recent":
newSortingAlgorithm = SortingAlgorithm.Recency;
break;
case "unread-first":
newSortingAlgorithm = SortingAlgorithm.Unread;
break;
}
RoomListStoreV3.instance.resort(newSortingAlgorithm);
this.snapshot.merge({ activeSortOption: option });
// Record analytics for this action
if (oldSortingAlgorithm) {
PosthogTrackers.trackRoomListSortingAlgorithmChange(oldSortingAlgorithm, newSortingAlgorithm);
}
};
public toggleMessagePreview = (): void => {
PosthogTrackers.trackInteraction("WebRoomListMessagePreviewToggle");
const isMessagePreviewEnabled = !SettingsStore.getValue("RoomList.showMessagePreview");
SettingsStore.setValue("RoomList.showMessagePreview", null, SettingLevel.DEVICE, isMessagePreviewEnabled);
this.snapshot.merge({ isMessagePreviewEnabled });
};
public createSection = (): void => {
RoomListStoreV3.instance.createSection();
};
public collapseOrExpandSections = (): void => {
const action =
this.snapshot.current.collapseSections === "expand"
? Action.RoomListExpandAllSections
: Action.RoomListCollapseAllSections;
defaultDispatcher.fire(action);
};
private readonly onDispatch = (payload: { action: string }): void => {
if (payload.action === Action.RoomListSectionsCollapseStateChanged) {
const { collapseSections } = payload as RoomListSectionsCollapseStateChangedPayload;
this.snapshot.merge({
collapseSections: collapseSections && (collapseSections === "collapse" ? "expand" : "collapse"),
});
}
};
}
/**
* Get the initial snapshot for the RoomListHeaderViewModel.
* @param spaceStore - The space store instance.
* @param matrixClient - The Matrix client instance.
* @returns
*/
function getInitialSnapshot(spaceStore: SpaceStoreClass, matrixClient: MatrixClient): RoomListHeaderViewSnapshot {
const sortingAlgorithm = SettingsStore.getValue("RoomList.preferredSorting");
let activeSortOption: SortOption;
switch (sortingAlgorithm) {
case SortingAlgorithm.Alphabetic:
activeSortOption = "alphabetical";
break;
case SortingAlgorithm.Recency:
activeSortOption = "recent";
break;
case SortingAlgorithm.Unread:
activeSortOption = "unread-first";
break;
}
const isMessagePreviewEnabled = SettingsStore.getValue("RoomList.showMessagePreview");
return {
activeSortOption,
isMessagePreviewEnabled,
...computeHeaderSpaceState(spaceStore, matrixClient),
};
}
/**
* Get the header title based on the active space.
* @param spaceStore - The space store instance.
*/
function getHeaderTitle(spaceStore: SpaceStoreClass): string {
const activeSpace = spaceStore.activeSpaceRoom;
const spaceName = activeSpace?.name;
return spaceName ?? getMetaSpaceName(spaceStore.activeSpace as MetaSpace, spaceStore.allRoomsInHome);
}
/**
* Determine if the user can create a video room.
* @param canCreateRoom - Whether the user can create a room.
*/
function getCanCreateVideoRoom(canCreateRoom: boolean): boolean {
return SettingsStore.getValue("feature_video_rooms") && canCreateRoom;
}
/**
* Computes the header space state based on the active space and user permissions.
* @param spaceStore - The space store instance.
* @param matrixClient - The Matrix client instance.
* @returns The header space state containing title, permissions, and display flags.
*/
function computeHeaderSpaceState(
spaceStore: SpaceStoreClass,
matrixClient: MatrixClient,
): Omit<RoomListHeaderViewSnapshot, "activeSortOption" | "isMessagePreviewEnabled"> {
const activeSpace = spaceStore.activeSpaceRoom;
const title = getHeaderTitle(spaceStore);
const canCreateRoom = hasCreateRoomRights(matrixClient, activeSpace);
const canCreateVideoRoom = getCanCreateVideoRoom(canCreateRoom);
const displayComposeMenu = canCreateRoom;
const displaySpaceMenu = Boolean(activeSpace);
const canInviteInSpace = Boolean(
activeSpace?.getJoinRule() === JoinRule.Public || activeSpace?.canInvite(matrixClient.getSafeUserId()),
);
const canAccessSpaceSettings = Boolean(activeSpace && shouldShowSpaceSettings(activeSpace));
const isSectionFeatureEnabled = SettingsStore.getValue("feature_room_list_sections");
const useComposeIcon = !isSectionFeatureEnabled;
const canCreateSection = isSectionFeatureEnabled;
return {
title,
canCreateRoom,
canCreateVideoRoom,
displayComposeMenu,
displaySpaceMenu,
canInviteInSpace,
canAccessSpaceSettings,
canCreateSection,
useComposeIcon,
};
}