Line data Source code
1 : /**
2 : * Shared asset sorting utilities
3 : * Used by both AssetBrowser and FormDropdown
4 : */
5 :
6 : import type { AssetSortOption } from '@/platform/assets/types/filterTypes'
7 :
8 : /**
9 : * Minimal interface for sortable items
10 : * Works with both AssetItem and FormDropdownItem
11 : */
12 : export interface SortableItem {
13 : name: string
14 : label?: string
15 : created_at?: string | null
16 : }
17 :
18 0 : function getDisplayName(item: SortableItem): string {
19 0 : return item.label ?? item.name
20 : }
21 :
22 : /**
23 : * Sort items by the specified sort option
24 : * @param items - Array of sortable items
25 : * @param sortBy - Sort option from AssetSortOption
26 : * @returns New sorted array (does not mutate input)
27 : */
28 20 : export function sortAssets<T extends SortableItem>(
29 : items: readonly T[],
30 : sortBy: AssetSortOption
31 : ): T[] {
32 20 : if (sortBy === 'default') {
33 18 : return items.slice()
34 : }
35 :
36 2 : const sorted = items.slice()
37 :
38 20 : switch (sortBy) {
39 : case 'name-desc':
40 0 : return sorted.sort((a, b) =>
41 : getDisplayName(b).localeCompare(getDisplayName(a), undefined, {
42 : numeric: true,
43 : sensitivity: 'base'
44 : })
45 : )
46 : case 'recent':
47 2 : return sorted.sort(
48 : (a, b) =>
49 : new Date(b.created_at ?? 0).getTime() -
50 : new Date(a.created_at ?? 0).getTime()
51 : )
52 : case 'name-asc':
53 : default:
54 0 : return sorted.sort((a, b) =>
55 : getDisplayName(a).localeCompare(getDisplayName(b), undefined, {
56 : numeric: true,
57 : sensitivity: 'base'
58 : })
59 : )
60 : }
61 : }
|