diff --git a/libs/mobile/chat/features/use-manage-messages-siblings/src/use-manage-messages-siblings.ts b/libs/mobile/chat/features/use-manage-messages-siblings/src/use-manage-messages-siblings.ts index 6513d7c..dfef78f 100644 --- a/libs/mobile/chat/features/use-manage-messages-siblings/src/use-manage-messages-siblings.ts +++ b/libs/mobile/chat/features/use-manage-messages-siblings/src/use-manage-messages-siblings.ts @@ -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, @@ -40,12 +40,7 @@ export function useManageMessageSiblings(chatId: string, history?: ChatHistory): if (!history?.messages) return; const isPrev = direction === 'prev'; - const siblings: Array = 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]); @@ -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); @@ -82,6 +72,20 @@ export function useManageMessageSiblings(chatId: string, history?: ChatHistory): [history], ); + const getOrderedSiblingIds = useCallback( + (parentId: string | null): Array => { + 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'),