Files
t3code/packages/shared/src/chatList.ts
T
2026-06-24 15:39:17 -07:00

34 lines
855 B
TypeScript

export const CHAT_LIST_ANCHOR_OFFSET = 16;
export interface ChatListAnchoredEndSpace {
readonly anchorIndex: number;
readonly anchorOffset: number;
}
export interface ChatListAnchorOptions {
readonly anchorOffset?: number;
}
export function resolveChatListAnchoredEndSpace<Item, AnchorId>(
items: ReadonlyArray<Item>,
anchorId: AnchorId | null,
getAnchorId: (item: Item) => AnchorId | null,
options: ChatListAnchorOptions = {},
): ChatListAnchoredEndSpace | undefined {
if (anchorId === null) {
return undefined;
}
for (let index = items.length - 1; index >= 0; index -= 1) {
const item = items[index];
if (item !== undefined && getAnchorId(item) === anchorId) {
return {
anchorIndex: index,
anchorOffset: options.anchorOffset ?? CHAT_LIST_ANCHOR_OFFSET,
};
}
}
return undefined;
}