Apps.utils.ts 728 B

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