diff --git a/frontend/src/hooks/useConversationMessages.ts b/frontend/src/hooks/useConversationMessages.ts index f3e3f0f..aac3fbe 100644 --- a/frontend/src/hooks/useConversationMessages.ts +++ b/frontend/src/hooks/useConversationMessages.ts @@ -127,8 +127,13 @@ export function useConversationMessages( ) return; - // Get the oldest message as cursor for the next page - const oldestMessage = messages[messages.length - 1]; + // Get the true oldest message as cursor for the next page + const oldestMessage = messages.reduce((oldest, msg) => { + if (!oldest) return msg; + if (msg.received_at < oldest.received_at) return msg; + if (msg.received_at === oldest.received_at && msg.id < oldest.id) return msg; + return oldest; + }, null as Message | null); if (!oldestMessage) return; setLoadingOlder(true);