History

Fix NaN from NEGATIVE_INFINITY subtraction in sortThreadsForSidebar

When two threads both yield Number.NEGATIVE_INFINITY timestamps, the subtraction (-Infinity) - (-Infinity) produces NaN, which bypasses the ID-based tiebreaker and causes unpredictable sort order. Use explicit comparison (=== / >) instead of subtraction, matching the pattern already used in sortProjectsForSidebar.

Cursor Agent committed 7 months ago

Commit e09809f1 parent 1f96d6d1 file · +3 −1Browse files

Unified · Split

apps/web/src/components/Sidebar.logic.ts+3−1
@@ -270,8 +270,10 @@ export function sortThreadsForSidebar< · 269 unmodified lines
270 T extends Pick<Thread, "id" | "createdAt" | "updatedAt" | "messages">,270 T extends Pick<Thread, "id" | "createdAt" | "updatedAt" | "messages">,
271>(threads: readonly T[], sortOrder: SidebarThreadSortOrder): T[] {271>(threads: readonly T[], sortOrder: SidebarThreadSortOrder): T[] {
272 return threads.toSorted((left, right) => {272 return threads.toSorted((left, right) => {
273 const rightTimestamp = getThreadSortTimestamp(right, sortOrder);
274 const leftTimestamp = getThreadSortTimestamp(left, sortOrder);
273 const byTimestamp =275 const byTimestamp =
274 getThreadSortTimestamp(right, sortOrder) - getThreadSortTimestamp(left, sortOrder);276 rightTimestamp === leftTimestamp ? 0 : rightTimestamp > leftTimestamp ? 1 : -1;
275 if (byTimestamp !== 0) return byTimestamp;277 if (byTimestamp !== 0) return byTimestamp;
276 return right.id.localeCompare(left.id);278 return right.id.localeCompare(left.id);
277 });279 });