forked from element-hq/element-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtualizedRoomListView.tsx
More file actions
377 lines (345 loc) · 13.4 KB
/
VirtualizedRoomListView.tsx
File metadata and controls
377 lines (345 loc) · 13.4 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
* Copyright 2026 Element Creations 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 React, { useCallback, useMemo, useRef, type JSX, type ReactNode } from "react";
import { type ScrollIntoViewLocation } from "react-virtuoso";
import { isEqual } from "lodash";
import { type Room } from "./RoomListItemAccessibilityWrapper/RoomListItemView";
import { useViewModel } from "../../core/viewmodel";
import { _t } from "../../core/i18n/i18n";
import {
FlatVirtualizedList,
getContainerAccessibleProps,
type VirtualizedListContext,
} from "../../core/VirtualizedList";
import type { RoomListViewSnapshot, RoomListViewModel } from "../RoomListView";
import { GroupedVirtualizedList } from "../../core/VirtualizedList";
import { RoomListSectionHeaderView } from "./RoomListSectionHeaderView";
import { RoomListItemAccessibilityWrapper } from "./RoomListItemAccessibilityWrapper";
/**
* Filter key type - opaque string type for filter identifiers
*/
export type FilterKey = string;
/**
* State for the room list data (nested within RoomListViewSnapshot)
*/
export interface RoomListViewState {
/** Optional active room index for keyboard navigation */
activeRoomIndex?: number;
/** Space ID for context tracking */
spaceId?: string;
/** Active filter keys for context tracking */
filterKeys?: FilterKey[];
}
/**
* Props for the VirtualizedRoomListView component
*/
export interface VirtualizedRoomListViewProps {
/**
* The view model containing all room list data and callbacks
*/
vm: RoomListViewModel;
/**
* Render function for room avatar
* @param room - The opaque Room object from the client
*/
renderAvatar: (room: Room) => ReactNode;
/**
* Optional callback for keyboard key down events
*/
onKeyDown?: (e: React.KeyboardEvent<HTMLDivElement>) => void;
}
/** Height of a single room list item in pixels (44px item + 8px padding bottom) */
const ROOM_LIST_ITEM_HEIGHT = 52;
/**
* Type for context used in ListView
*/
type Context = {
/** Space ID for context tracking */
spaceId: string;
/** Active filter keys for context tracking */
filterKeys: FilterKey[] | undefined;
/** Active room index for keyboard navigation */
activeRoomIndex: number | undefined;
/** Sections of the room list */
sections: RoomListViewSnapshot["sections"];
/** Total number of rooms in the list */
roomCount: number;
/** Number of sections in the list */
sectionCount: number;
/** Room list view model */
vm: RoomListViewModel;
/** List is in flat or section mode */
isFlatList: boolean;
};
/**
* Amount to extend the top and bottom of the viewport by.
* From manual testing and user feedback 25 items is reported to be enough to avoid blank space
* when using the mouse wheel, and the trackpad scrolling at a slow to moderate speed where you
* can still see/read the content. Using the trackpad to sling through a large percentage of the
* list quickly will still show blank space. We would likely need to simplify the item content to
* improve this case.
*/
const EXTENDED_VIEWPORT_HEIGHT = 25 * ROOM_LIST_ITEM_HEIGHT;
/**
* A virtualized list of rooms.
* This component provides efficient rendering of large room lists using virtualization,
* and renders RoomListItemView components for each room.
*
* @example
* ```tsx
* <VirtualizedRoomListView vm={roomListViewModel} renderAvatar={(room) => <Avatar room={room} />} />
* ```
*/
export function VirtualizedRoomListView({ vm, renderAvatar, onKeyDown }: VirtualizedRoomListViewProps): JSX.Element {
const snapshot = useViewModel(vm);
const { roomListState, sections, isFlatList } = snapshot;
const activeRoomIndex = roomListState.activeRoomIndex;
const lastSpaceId = useRef<string | undefined>(undefined);
const lastFilterKeys = useRef<FilterKey[] | undefined>(undefined);
const roomIds = useMemo(() => sections.flatMap((section) => section.roomIds), [sections]);
const roomCount = roomIds.length;
const sectionCount = sections.length;
const totalCount = roomCount + sectionCount;
const groups = useMemo(
() =>
sections.map((section) => ({
header: section.id,
items: section.roomIds,
})),
[sections],
);
/**
* Callback when the visible range changes
* Notifies the view model which rooms are visible
*/
const rangeChanged = useCallback(
(range: { startIndex: number; endIndex: number }) => {
vm.updateVisibleRooms(range.startIndex, range.endIndex);
},
[vm],
);
/**
* Get the item component for a specific index
* Gets the room's view model and passes it to RoomListItemView
*
* @param index - The index of the item in the list
* @param roomId - The ID of the room for this item
* @param context - The virtualization context containing list state
* @param onFocus - Callback to call when the item is focused
* @param isInLastSection - Whether this item is in the last section
* @param roomIndexInSection - The index of this room within its section
*/
const getItemComponent = useCallback(
(
index: number,
roomId: string,
context: VirtualizedListContext<Context>,
onFocus: (item: string, e: React.FocusEvent) => void,
isInLastSection?: boolean,
roomIndexInSection?: number,
): JSX.Element => {
const { activeRoomIndex, roomCount, vm, isFlatList } = context.context;
const isSelected = activeRoomIndex === index;
const roomItemVM = vm.getRoomItemViewModel(roomId);
// If we don't have a view model for this room, it means the room has been removed since the list was rendered - return an empty placeholder
if (!roomItemVM) {
return <React.Fragment key={`stale-${index}`} />;
}
// Item is focused when the list has focus AND this item's key matches tabIndexKey
// This matches the old RoomList implementation's roving tabindex pattern
const isFocused = context.focused && context.tabIndexKey === roomId;
const isFirstItem = isFlatList && index === 0;
const isLastItem = Boolean((isFlatList || isInLastSection) && index === roomCount - 1);
return (
<RoomListItemAccessibilityWrapper
key={roomId}
vm={roomItemVM}
renderAvatar={renderAvatar}
isSelected={isSelected}
isFocused={isFocused}
onFocus={onFocus}
roomIndex={index}
// For a flat list, we don't have sections, so roomIndexInSection is unused and can be set to 0
roomIndexInSection={roomIndexInSection || 0}
roomCount={roomCount}
isFirstItem={isFirstItem}
isLastItem={isLastItem}
isInFlatList={isFlatList}
/>
);
},
[renderAvatar],
);
/**
* Get the item component for a specific index in a grouped list
* Gets the room's view model and passes it to RoomListItemView
*/
const getItemComponentForGroupedList = useCallback(
(
index: number,
roomId: string,
context: VirtualizedListContext<Context>,
onFocus: (item: string, e: React.FocusEvent) => void,
groupIndex: number,
): JSX.Element => {
const { sections } = context.context;
const roomIndexInSection = sections[groupIndex].roomIds.findIndex((id) => id === roomId);
const isInLastSection = groupIndex === sections.length - 1;
return getItemComponent(index, roomId, context, onFocus, isInLastSection, roomIndexInSection);
},
[getItemComponent],
);
/**
* Get the item component for a specific index in a flat list
* Gets the room's view model and passes it to RoomListItemView
*/
const getItemComponentForFlatList = useCallback(
(
index: number,
roomId: string,
context: VirtualizedListContext<Context>,
onFocus: (item: string, e: React.FocusEvent) => void,
): JSX.Element => {
return getItemComponent(index, roomId, context, onFocus);
},
[getItemComponent],
);
/**
* Get the group header component for a specific group
*/
const getGroupHeaderComponent = useCallback(
(
groupIndex: number,
headerId: string,
context: VirtualizedListContext<Context>,
onFocus: (header: string, e: React.FocusEvent) => void,
): JSX.Element => {
const { vm, sectionCount, sections } = context.context;
const sectionHeaderVM = vm.getSectionHeaderViewModel(headerId);
const indexInList = sections
.slice(0, groupIndex)
// +1 for each section header
.reduce((acc, section) => acc + section.roomIds.length + 1, 0);
const roomCountInSection = sections[groupIndex].roomIds.length;
// Item is focused when the list has focus AND this item's key matches tabIndexKey
// This matches the old RoomList implementation's roving tabindex pattern
const isFocused = context.focused && context.tabIndexKey === headerId;
return (
<RoomListSectionHeaderView
vm={sectionHeaderVM}
isFocused={isFocused}
onFocus={onFocus}
indexInList={indexInList}
sectionIndex={groupIndex}
sectionCount={sectionCount}
roomCountInSection={roomCountInSection}
/>
);
},
[],
);
/**
* Get the key for a room item
* Since we're using virtualization, items are always room ID strings
*/
const getItemKey = useCallback((item: string): string => item, []);
/**
* Get the key for a group header
* We are passing the section ID as the header key, which is a string, so we can return it directly
*/
const getHeaderKey = useCallback((header: string): string => header, []);
const context = useMemo(
() => ({
spaceId: roomListState.spaceId || "",
filterKeys: roomListState.filterKeys,
sections,
activeRoomIndex,
roomCount,
sectionCount,
vm,
isFlatList,
}),
[
roomListState.spaceId,
roomListState.filterKeys,
sections,
activeRoomIndex,
roomCount,
sectionCount,
vm,
isFlatList,
],
);
/**
* Determine if we should scroll the active index into view
* This happens when the space or filters change
*/
const scrollIntoViewOnChange = useCallback(
(params: {
context: VirtualizedListContext<{ spaceId: string; filterKeys: FilterKey[] | undefined }>;
}): ScrollIntoViewLocation | null | undefined | false => {
const { spaceId, filterKeys } = params.context.context;
const shouldScrollIndexIntoView =
lastSpaceId.current !== spaceId || !isEqual(lastFilterKeys.current, filterKeys);
lastFilterKeys.current = filterKeys;
lastSpaceId.current = spaceId;
if (shouldScrollIndexIntoView) {
return {
align: "start",
index: activeRoomIndex || 0,
behavior: "auto",
};
}
return false;
},
[activeRoomIndex],
);
const isItemFocusable = useCallback(() => true, []);
const isGroupHeaderFocusable = useCallback(() => true, []);
const increaseViewportBy = useMemo(
() => ({
top: EXTENDED_VIEWPORT_HEIGHT,
bottom: EXTENDED_VIEWPORT_HEIGHT,
}),
[],
);
const commonProps = {
context,
scrollIntoViewOnChange,
// If fixedItemHeight is not set and initialTopMostItemIndex=undefined, virtuoso crashes
// If we don't set it, it works
...(activeRoomIndex !== undefined ? { initialTopMostItemIndex: activeRoomIndex } : {}),
["data-testid"]: "room-list",
["aria-label"]: _t("room_list|list_title"),
getItemKey,
isItemFocusable,
rangeChanged,
onKeyDown,
increaseViewportBy,
};
if (isFlatList) {
return (
<FlatVirtualizedList
{...commonProps}
{...getContainerAccessibleProps("listbox")}
items={roomIds}
getItemComponent={getItemComponentForFlatList}
/>
);
}
return (
<GroupedVirtualizedList<string, string, Context>
{...commonProps}
{...getContainerAccessibleProps("treegrid", totalCount)}
groups={groups}
getHeaderKey={getHeaderKey}
getGroupHeaderComponent={getGroupHeaderComponent}
getItemComponent={getItemComponentForGroupedList}
isGroupHeaderFocusable={isGroupHeaderFocusable}
/>
);
}