Apps.utils.ts23 lines · main
| 1 | import type { PrivateApp } from '../PrivateApps.types' |
| 2 | import type { AppsSort } from './Apps.types' |
| 3 | |
| 4 | export function handleSortChange( |
| 5 | currentSort: AppsSort, |
| 6 | column: string, |
| 7 | setSort: (s: AppsSort) => void |
| 8 | ) { |
| 9 | const [currentCol, currentOrder] = currentSort.split(':') |
| 10 | if (currentCol === column) { |
| 11 | setSort(`${column}:${currentOrder === 'asc' ? 'desc' : 'asc'}` as AppsSort) |
| 12 | } else { |
| 13 | setSort(`${column}:asc` as AppsSort) |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | export function sortApps(apps: PrivateApp[], sort: AppsSort): PrivateApp[] { |
| 18 | const [, order] = sort.split(':') |
| 19 | return [...apps].sort((a, b) => { |
| 20 | const diff = new Date(a.created_at).getTime() - new Date(b.created_at).getTime() |
| 21 | return order === 'asc' ? diff : -diff |
| 22 | }) |
| 23 | } |