1export const CHAT_LIST_ANCHOR_OFFSET = 16;
2
3export interface ChatListAnchoredEndSpace {
4 readonly anchorIndex: number;
5 readonly anchorOffset: number;
6}
7
8export interface ChatListAnchorOptions {
9 readonly anchorOffset?: number;
10}
11
12export function resolveChatListAnchoredEndSpace<Item, AnchorId>(
13 items: ReadonlyArray<Item>,
14 anchorId: AnchorId | null,
15 getAnchorId: (item: Item) => AnchorId | null,
16 options: ChatListAnchorOptions = {},
17): ChatListAnchoredEndSpace | undefined {
18 if (anchorId === null) {
19 return undefined;
20 }
21
22 for (let index = 0; index < items.length; index += 1) {
23 const item = items[index];
24 if (item === undefined) {
25 continue;
26 }
27
28 const itemAnchorId = getAnchorId(item);
29 if (itemAnchorId === null) {
30 continue;
31 }
32
33 return itemAnchorId === anchorId
34 ? {
35 anchorIndex: index,
36 anchorOffset: options.anchorOffset ?? CHAT_LIST_ANCHOR_OFFSET,
37 }
38 : undefined;
39 }
40
41 return undefined;
42}