Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions apps/app/src/components/ConversationsSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Conversations sidebar component β€” left sidebar with conversation list.
*/

import { useEffect, useRef, useState } from "react";
import { useEffect, useMemo, useRef, useState } from "react";
import { useApp } from "../AppContext";

interface ConversationsSidebarProps {
Expand Down Expand Up @@ -54,11 +54,13 @@ export function ConversationsSidebar({
}
}, [editingId]);

const sortedConversations = [...conversations].sort((a, b) => {
const aTime = new Date(a.updatedAt).getTime();
const bTime = new Date(b.updatedAt).getTime();
return bTime - aTime;
});
// ⚑ Bolt: Memoize the sorting of conversations and use lexicographical string comparison
// instead of new Date() allocations to improve render performance on every state update.
const sortedConversations = useMemo(() => {
return [...conversations].sort((a, b) =>
b.updatedAt.localeCompare(a.updatedAt),
);
}, [conversations]);
Comment on lines +59 to +63

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Potential sorting inconsistency:
The sorting logic in useMemo uses localeCompare on the updatedAt string fields:

b.updatedAt.localeCompare(a.updatedAt)

This assumes that all updatedAt values are ISO 8601 date strings, where lexicographical order matches chronological order. If the backend or other parts of the codebase ever provide non-ISO date formats, the sorting will be incorrect, leading to inconsistent conversation ordering.

Recommended solution:
Ensure that all updatedAt values are always ISO 8601 strings. Alternatively, use explicit date parsing for robustness:

return [...conversations].sort((a, b) => new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime());

This is slightly less performant but guarantees correct ordering regardless of date format.

Comment on lines +57 to +63

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

While localeCompare is an improvement over new Date() allocations, it is significantly slower than direct string comparison operators (> and <) because it involves locale-aware processing. For ISO 8601 date strings, which are designed to be lexicographically sortable, a simple comparison is much more efficient. Additionally, the added comment is redundant as the code's intent is clear from the implementation.

  const sortedConversations = useMemo(() => {
    return [...conversations].sort((a, b) =>
      b.updatedAt > a.updatedAt ? 1 : b.updatedAt < a.updatedAt ? -1 : 0,
    );
  }, [conversations]);


const handleDoubleClick = (conv: { id: string; title: string }) => {
setEditingId(conv.id);
Expand Down
Loading