Compare commits

..

1 Commits

Author SHA1 Message Date
dante01yoon
9a7075cceb fix(workspace): guard sortWorkspaces against missing workspace dates
sortWorkspaces called .localeCompare on created_at/joined_at. The type marks
them required, but a malformed workspaces response can omit one. With 2+
workspaces the comparator runs and throws, which aborts teamWorkspaceStore
init (initState='error') and leaves the account/profile menu blank. Fall back
to '' for an absent date so one bad workspace can't blank the whole menu.
2026-06-17 14:50:01 +09:00
2 changed files with 19 additions and 2 deletions

View File

@@ -2,6 +2,8 @@ import { createTestingPinia } from '@pinia/testing'
import { setActivePinia } from 'pinia'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import type { WorkspaceWithRole } from '@/platform/workspace/api/workspaceApi'
import { sortWorkspaces, useTeamWorkspaceStore } from './teamWorkspaceStore'
// Mock workspaceAuthStore
@@ -975,4 +977,17 @@ describe('sortWorkspaces', () => {
'w-team-new-owner'
])
})
it('does not crash when a workspace is missing its date fields', () => {
// A malformed API response (dates are contractually required) must not throw
// and blank the entire account menu via a sort crash.
const input = [
{ id: 'w-owner', name: 'Owner Team', role: 'owner', type: 'team' },
{ id: 'w-member', name: 'Member Team', role: 'member', type: 'team' },
{ id: 'w-personal', name: 'Personal', role: 'owner', type: 'personal' }
] as unknown as WorkspaceWithRole[]
expect(() => sortWorkspaces(input)).not.toThrow()
expect(sortWorkspaces(input)[0].id).toBe('w-personal')
})
})

View File

@@ -80,8 +80,10 @@ export function sortWorkspaces<T extends WorkspaceWithRole>(list: T[]): T[] {
return [...list].sort((a, b) => {
if (a.type === 'personal') return -1
if (b.type === 'personal') return 1
const dateA = a.role === 'owner' ? a.created_at : a.joined_at
const dateB = b.role === 'owner' ? b.created_at : b.joined_at
// Fall back to '' if a date is absent so one malformed workspace can't throw
// and blank the entire account menu (init aborts when sorting throws).
const dateA = (a.role === 'owner' ? a.created_at : a.joined_at) ?? ''
const dateB = (b.role === 'owner' ? b.created_at : b.joined_at) ?? ''
return dateA.localeCompare(dateB)
})
}