-
Notifications
You must be signed in to change notification settings - Fork 340
Description
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
- Start with an existing message list:
[10:00, 10:01, 10:03]
-
Insert a new message with timestamp
10:02. -
The logic evaluates:
10:00 < 10:02 -> true (index 0)
- 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.