useTableSort.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // @ts-nocheck
  2. import { useCallback, useMemo } from 'react'
  3. import { formatSortURLParams, sortsToUrlParams } from '@/components/grid/BrivenGrid.utils'
  4. import type { Sort } from '@/components/grid/types'
  5. import { useTableEditorFiltersSort } from '@/hooks/misc/useTableEditorFiltersSort'
  6. import { useTableEditorTableStateSnapshot } from '@/state/table-editor-table'
  7. /**
  8. * Hook for managing table sort URL parameters and saving.
  9. * Uses snapshot ONLY to get table name for formatting/mapping.
  10. * Does NOT format initial sorts (needs table name externally).
  11. * Does NOT interact with snapshot directly.
  12. */
  13. export function useTableSort() {
  14. const { sorts: urlSorts, setParams } = useTableEditorFiltersSort()
  15. const snap = useTableEditorTableStateSnapshot()
  16. const tableName = useMemo(() => snap.table?.name || '', [snap])
  17. const sorts = useMemo(() => {
  18. return formatSortURLParams(tableName, urlSorts)
  19. }, [tableName, urlSorts])
  20. const onApplySorts = useCallback(
  21. (appliedSorts: Sort[]) => {
  22. if (!tableName) {
  23. return console.warn(
  24. '[useTableSort] Table name missing in callback, cannot apply sort correctly.'
  25. )
  26. }
  27. const sortsWithTable = appliedSorts.map((sort) => ({ ...sort, table: tableName }))
  28. const newUrlSorts = sortsToUrlParams(sortsWithTable)
  29. setParams((prevParams) => ({ ...prevParams, sort: newUrlSorts }))
  30. },
  31. [snap, setParams]
  32. )
  33. /**
  34. * Adds a new sort for a column or updates the direction of an existing one.
  35. * New sorts are added to the beginning of the array (highest precedence).
  36. * If the column already exists, its `ascending` direction is updated.
  37. * Calls `onApplySorts` to update URL parameters and trigger side effects.
  38. *
  39. * @param columnKey The key/name of the column to sort.
  40. * @param ascending The sort direction (true for ascending, false for descending).
  41. */
  42. const addOrUpdateSort = useCallback(
  43. (columnKey: string, ascending: boolean) => {
  44. if (!tableName || !columnKey) return
  45. // Use the derived 'sorts' state from the hook
  46. const existingSortIndex = sorts.findIndex((s) => s.column === columnKey)
  47. let newSorts = [...sorts] // Create a mutable copy
  48. if (existingSortIndex !== -1) {
  49. // Column already exists in sorts: Update the existing sort (toggle handled by removeSort)
  50. newSorts[existingSortIndex] = { ...newSorts[existingSortIndex], ascending: ascending }
  51. } else {
  52. // Column doesn't exist in sorts: Add it to the beginning
  53. newSorts.unshift({ table: tableName, column: columnKey, ascending: ascending })
  54. }
  55. onApplySorts(newSorts)
  56. },
  57. [tableName, sorts, onApplySorts] // Depend on derived sorts and callback
  58. )
  59. /**
  60. * Removes a sort criterion for a specific column.
  61. * Calls `onApplySorts` with the filtered array to update URL parameters and trigger side effects.
  62. *
  63. * @param columnKey The key/name of the column to remove from sorting.
  64. */
  65. const removeSort = useCallback(
  66. (columnKey: string) => {
  67. if (!tableName || !columnKey) return
  68. // Use the derived 'sorts' state from the hook
  69. const newSorts = sorts.filter((s) => s.column !== columnKey)
  70. onApplySorts(newSorts)
  71. },
  72. [tableName, sorts, onApplySorts] // Depend on derived sorts and callback
  73. )
  74. return {
  75. sorts,
  76. urlSorts,
  77. onApplySorts,
  78. addOrUpdateSort,
  79. removeSort,
  80. }
  81. }