diff --git a/src/renderer/src/components/sidebar/workspace-kanban-worktree-groups.test.ts b/src/renderer/src/components/sidebar/workspace-kanban-worktree-groups.test.ts index b30c7815c..00fbbe840 100644 --- a/src/renderer/src/components/sidebar/workspace-kanban-worktree-groups.test.ts +++ b/src/renderer/src/components/sidebar/workspace-kanban-worktree-groups.test.ts @@ -68,6 +68,57 @@ describe('groupWorkspaceKanbanWorktrees', () => { expect(grouped.get('doing')?.map((item) => item.id)).toEqual(['b', 'c', 'a']) }) + it('does not crash when a worktree is missing its displayName under Manual sort', () => { + // Repro for crash 99657ab1: a worktree reached the sidebar with an + // undefined displayName, so `a.displayName.localeCompare(...)` threw + // `Cannot read properties of undefined (reading 'localeCompare')`. + expect(() => + groupWorkspaceKanbanWorktrees({ + worktrees: [ + worktree({ + id: 'a', + displayName: undefined as unknown as string, + workspaceStatus: 'doing', + manualOrder: 100 + }), + worktree({ + id: 'b', + displayName: undefined as unknown as string, + workspaceStatus: 'doing', + manualOrder: 100 + }) + ], + visibleWorktreeIds: new Set(['a', 'b']), + workspaceStatuses: statuses, + sortBy: 'manual' + }) + ).not.toThrow() + }) + + it('does not crash when a worktree is missing its displayName outside Manual sort', () => { + expect(() => + groupWorkspaceKanbanWorktrees({ + worktrees: [ + worktree({ + id: 'a', + displayName: undefined as unknown as string, + workspaceStatus: 'doing', + lastActivityAt: 10 + }), + worktree({ + id: 'b', + displayName: undefined as unknown as string, + workspaceStatus: 'doing', + lastActivityAt: 10 + }) + ], + visibleWorktreeIds: new Set(['a', 'b']), + workspaceStatuses: statuses, + sortBy: 'recent' + }) + ).not.toThrow() + }) + it('keeps pinned then recent ordering outside Manual sort', () => { const grouped = groupWorkspaceKanbanWorktrees({ worktrees: [ diff --git a/src/renderer/src/components/sidebar/workspace-kanban-worktree-groups.ts b/src/renderer/src/components/sidebar/workspace-kanban-worktree-groups.ts index ffa17cbde..3a23c176b 100644 --- a/src/renderer/src/components/sidebar/workspace-kanban-worktree-groups.ts +++ b/src/renderer/src/components/sidebar/workspace-kanban-worktree-groups.ts @@ -2,15 +2,20 @@ import type { WorkspaceStatus, WorkspaceStatusDefinition, Worktree } from '../.. import type { SortBy } from './smart-sort' import { getWorkspaceStatus } from './workspace-status' +// Why: displayName is typed non-optional, but persisted/discovered worktrees +// have reached the sidebar with an undefined name (crash 99657ab1), which made +// `displayName.localeCompare(...)` throw and took down the sidebar. Compare on a +// safe string so a missing name only affects tie-break order, never crashes. +function compareDisplayName(a: Worktree, b: Worktree): number { + return (a.displayName ?? '').localeCompare(b.displayName ?? '') +} + function sortBoardWorktrees(a: Worktree, b: Worktree): number { - return b.lastActivityAt - a.lastActivityAt || a.displayName.localeCompare(b.displayName) + return b.lastActivityAt - a.lastActivityAt || compareDisplayName(a, b) } function sortManualBoardWorktrees(a: Worktree, b: Worktree): number { - return ( - (b.manualOrder ?? b.sortOrder) - (a.manualOrder ?? a.sortOrder) || - a.displayName.localeCompare(b.displayName) - ) + return (b.manualOrder ?? b.sortOrder) - (a.manualOrder ?? a.sortOrder) || compareDisplayName(a, b) } export function groupWorkspaceKanbanWorktrees(params: {