Skip to content

Conversation

@smian1
Copy link
Collaborator

@smian1 smian1 commented Jan 8, 2026

Summary

  • Fixes incorrect conversation date group sorting in the web app
  • The previous sorting logic parsed formatted date strings (e.g., "Mon, Jan 6") which lack year info, causing incorrect chronological ordering
  • Now uses actual conversation timestamps (started_at/created_at) for accurate sorting

Test plan

  • Open the web app conversations page
  • Verify conversations are sorted correctly with newest dates at top
  • Check that "Today" and "Yesterday" groups appear before older dates

Date group sorting was incorrect because parsing formatted date strings
(e.g., "Mon, Jan 6") lacks year info. Now uses conversation timestamps
(started_at/created_at) for accurate chronological ordering.
Copilot AI review requested due to automatic review settings January 8, 2026 09:17
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly addresses the conversation sorting issue by using full timestamps instead of formatted date strings that lack year information. The changes in both ConversationList.tsx and ConversationSplitView.tsx are appropriate.

My review includes suggestions to make the sorting logic more robust by handling potential invalid date values, which would prevent unpredictable sorting behavior. This ensures the fix is not only correct for the expected case but also resilient to unexpected data.

Comment on lines +66 to +68
const dateA = new Date(convA?.started_at || convA?.created_at);
const dateB = new Date(convB?.started_at || convB?.created_at);
return dateB.getTime() - dateA.getTime();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This correctly fixes the sorting issue across years. However, it could be made more robust against potentially invalid date strings.

If started_at or created_at were ever to be invalid date strings, new Date(...).getTime() would return NaN. The behavior of Array.prototype.sort() with NaN is inconsistent across JavaScript engines and can lead to unpredictable sorting.

I recommend adding a check to handle NaN values gracefully, ensuring invalid dates are pushed to the end of the list. This makes the sorting more robust.

Also, the optional chaining (?.) on convA and convB is not necessary, as the logic that creates groupedConversations ensures that the conversation arrays for each date key are never empty.

    const dateA = new Date(convA.started_at || convA.created_at);
    const dateB = new Date(convB.started_at || convB.created_at);
    const timeA = dateA.getTime();
    const timeB = dateB.getTime();

    if (isNaN(timeA) || isNaN(timeB)) {
      if (isNaN(timeA) && isNaN(timeB)) return 0;
      return isNaN(timeA) ? 1 : -1;
    }
    return timeB - timeA;

Comment on lines +206 to +208
const dateA = new Date(convA?.started_at || convA?.created_at);
const dateB = new Date(convB?.started_at || convB?.created_at);
return dateB.getTime() - dateA.getTime();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This correctly fixes the sorting issue across years. However, it could be made more robust against potentially invalid date strings.

If started_at or created_at were ever to be invalid date strings, new Date(...).getTime() would return NaN. The behavior of Array.prototype.sort() with NaN is inconsistent across JavaScript engines and can lead to unpredictable sorting.

I recommend adding a check to handle NaN values gracefully, ensuring invalid dates are pushed to the end of the list. This makes the sorting more robust.

Also, the optional chaining (?.) on convA and convB is not necessary, as the logic that creates displayedConversations ensures that the conversation arrays for each date key are never empty.

Suggested change
const dateA = new Date(convA?.started_at || convA?.created_at);
const dateB = new Date(convB?.started_at || convB?.created_at);
return dateB.getTime() - dateA.getTime();
const dateA = new Date(convA.started_at || convA.created_at);
const dateB = new Date(convB.started_at || convB.created_at);
const timeA = dateA.getTime();
const timeB = dateB.getTime();
if (isNaN(timeA) || isNaN(timeB)) {
if (isNaN(timeA) && isNaN(timeB)) return 0;
return isNaN(timeA) ? 1 : -1;
}
return timeB - timeA;

@smian1 smian1 merged commit 4fb3924 into BasedHardware:main Jan 8, 2026
6 checks passed
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug in conversation date group sorting within the web app. Previously, the sorting logic parsed formatted date strings (e.g., "Mon, Jan 6") which lacked year information, causing incorrect chronological ordering. The fix now uses actual conversation timestamps (started_at/created_at) for accurate date-based sorting.

  • Replaces date string parsing with timestamp-based comparison
  • Ensures "Today" and "Yesterday" groups appear first, followed by older dates in descending order
  • Applies the fix consistently across two components

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
web/app/src/components/conversations/ConversationSplitView.tsx Updates the date sorting logic to use conversation timestamps instead of parsing formatted date strings
web/app/src/components/conversations/ConversationList.tsx Applies the same timestamp-based sorting fix for consistent behavior

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +202 to +208
// Use actual conversation timestamps instead of parsing the date string
// (which lacks year info and causes incorrect sorting)
const convA = displayedConversations[a][0];
const convB = displayedConversations[b][0];
const dateA = new Date(convA?.started_at || convA?.created_at);
const dateB = new Date(convB?.started_at || convB?.created_at);
return dateB.getTime() - dateA.getTime();
Copy link

Copilot AI Jan 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code doesn't handle the case where the first conversation in a group might be undefined (empty array) or when both started_at and created_at are null/undefined. This could result in invalid Date objects where getTime() returns NaN, leading to unpredictable sorting behavior. Consider adding validation to ensure the dates are valid before comparing them.

Suggested change
// Use actual conversation timestamps instead of parsing the date string
// (which lacks year info and causes incorrect sorting)
const convA = displayedConversations[a][0];
const convB = displayedConversations[b][0];
const dateA = new Date(convA?.started_at || convA?.created_at);
const dateB = new Date(convB?.started_at || convB?.created_at);
return dateB.getTime() - dateA.getTime();
const getConversationTime = (conv: Conversation | undefined): number => {
if (!conv) return 0;
const rawDate = conv.started_at ?? conv.created_at;
if (!rawDate) return 0;
const time = new Date(rawDate).getTime();
return Number.isNaN(time) ? 0 : time;
};
// Use actual conversation timestamps instead of parsing the date string
// (which lacks year info and causes incorrect sorting)
const convA = displayedConversations[a]?.[0];
const convB = displayedConversations[b]?.[0];
const timeA = getConversationTime(convA);
const timeB = getConversationTime(convB);
return timeB - timeA;

Copilot uses AI. Check for mistakes.
Comment on lines +64 to +68
const convA = groupedConversations[a][0];
const convB = groupedConversations[b][0];
const dateA = new Date(convA?.started_at || convA?.created_at);
const dateB = new Date(convB?.started_at || convB?.created_at);
return dateB.getTime() - dateA.getTime();
Copy link

Copilot AI Jan 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code doesn't handle the case where the first conversation in a group might be undefined (empty array) or when both started_at and created_at are null/undefined. This could result in invalid Date objects where getTime() returns NaN, leading to unpredictable sorting behavior. Consider adding validation to ensure the dates are valid before comparing them.

Copilot uses AI. Check for mistakes.
@smian1 smian1 deleted the fix/web-conversation-sorting branch January 8, 2026 09:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant