Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { filter, get, indexOf, last, map, max, min, values } from 'lodash-es';
import { get, indexOf, last, max, min, values } from 'lodash-es';
import { useCallback } from 'react';
import {
History as ChatHistory,
Expand Down Expand Up @@ -40,12 +40,7 @@ export function useManageMessageSiblings(chatId: string, history?: ChatHistory):
if (!history?.messages) return;

const isPrev = direction === 'prev';
const siblings: Array<string> = message.parentId
? get(history.messages[message.parentId], 'childrenIds', [])
: map(
filter(values(history.messages), (msg) => msg.parentId === null),
'id',
);
const siblings = getOrderedSiblingIds(message.parentId ?? null);

const currentIndex = indexOf(siblings, message.id);
const targetIndex = isPrev ? max([currentIndex - 1, 0]) : min([currentIndex + 1, siblings.length - 1]);
Expand All @@ -64,12 +59,7 @@ export function useManageMessageSiblings(chatId: string, history?: ChatHistory):
(message: Message) => {
if (!history?.messages) return { siblings: [], currentIndex: -1, hasSiblings: false };

const siblings = message.parentId
? get(history.messages[message.parentId], 'childrenIds', [])
: map(
filter(values(history.messages), (msg) => msg.parentId === null),
'id',
);
const siblings = getOrderedSiblingIds(message.parentId ?? null);

const currentIndex = indexOf(siblings, message.id);

Expand All @@ -82,6 +72,20 @@ export function useManageMessageSiblings(chatId: string, history?: ChatHistory):
[history],
);

const getOrderedSiblingIds = useCallback(
(parentId: string | null): Array<string> => {
if (!history?.messages) return [];

return values(history.messages)
.filter((msg) => msg.parentId === parentId)
.sort(
(a, b) => (a.timestamp !== b.timestamp ? a.timestamp - b.timestamp : a.id.localeCompare(b.id)), // safety fallback
)
.map((msg) => msg.id);
},
[history],
);

return {
showPreviousSibling: (msg) => navigateSibling(msg, 'prev'),
showNextSibling: (msg) => navigateSibling(msg, 'next'),
Expand Down
Loading