Skip to content

Bug: Broken Message Insertion Logic in messageListHelpers #1082

@deepak0x

Description

@deepak0x

A bug exists in the message insertion logic that causes messages to be inserted at incorrect positions in the message list. The insertMessage function assumes a descending (newest-first) sort order, while the rest of the application renders messages in ascending (oldest-first) order.

This mismatch leads to messages appearing out of chronological order in the UI.


Observed Behavior

The insertMessage function attempts to keep messages sorted by timestamp (ts) using the following logic:

messages.findIndex((m) => new Date(m.ts) < new Date(message.ts))

If the message list is stored in ascending order (oldest to newest), this condition matches the first message in the list, causing the new message to be inserted at the beginning.

This results in an incorrect and scrambled message order.


Steps to Reproduce

  1. Start with an existing message list:
[10:00, 10:01, 10:03]
  1. Insert a new message with timestamp 10:02.

  2. The logic evaluates:

10:00 < 10:02  -> true (index 0)
  1. The new message is unshifted to the beginning:
[10:02, 10:00, 10:01, 10:03]

The messages are now out of chronological order.


Expected Behavior

The insertion logic should match the render order.

For an ascending message list, the function should:

  • Find the first message with a timestamp greater than the new message
  • Insert the new message before that item
  • Append to the end if no such message exists

This ensures messages remain correctly ordered in the UI.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions