mirror of
https://github.com/supabase/supabase.git
synced 2026-10-10 11:55:05 +03:00
* init * update Popovers to use new hooks * Update Header.tsx * made primitive components for filter and sorts * Delete FilterPopoverWrapper.tsx * Delete SortPopoverWrapper.tsx * remove * Create README.md * Update README.md * fix sort popover issues * Update SupabaseGrid.tsx * move DeleteConfirmationDialogs into context * fix issue with * more stuff for alaister * fix ts and tables pages * First round of clean up * Update README.md * Smol fix * Fix issues identified * Smol fix * Fix updating table name in database/tables not invalidating * Improve SQL editor invalidation logic * Add fix to reopen last opened table when landing on table editor * Smol fix --------- Co-authored-by: Alaister Young <a@alaisteryoung.com> Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
51 lines
1.7 KiB
TypeScript
51 lines
1.7 KiB
TypeScript
import { useCallback, useMemo } from 'react'
|
|
|
|
import { formatSortURLParams, sortsToUrlParams } from 'components/grid/SupabaseGrid.utils'
|
|
import type { Sort } from 'components/grid/types'
|
|
import { useTableEditorFiltersSort } from 'hooks/misc/useTableEditorFiltersSort'
|
|
import { useTableEditorTableStateSnapshot } from 'state/table-editor-table'
|
|
import { useSaveTableEditorState } from './useSaveTableEditorState'
|
|
|
|
/**
|
|
* Hook for managing table sort URL parameters and saving.
|
|
* Uses snapshot ONLY to get table name for formatting/mapping.
|
|
* Uses useSaveTableEditorState for saving and side effects.
|
|
* Does NOT format initial sorts (needs table name externally).
|
|
* Does NOT interact with snapshot directly.
|
|
*/
|
|
export function useTableSort() {
|
|
const { sorts: urlSorts, setParams } = useTableEditorFiltersSort()
|
|
const snap = useTableEditorTableStateSnapshot()
|
|
const { saveSortsAndTriggerSideEffects } = useSaveTableEditorState()
|
|
|
|
const tableName = useMemo(() => snap.table?.name || '', [snap])
|
|
|
|
const sorts = useMemo(() => {
|
|
return formatSortURLParams(tableName, urlSorts)
|
|
}, [tableName, urlSorts])
|
|
|
|
const onApplySorts = useCallback(
|
|
(appliedSorts: Sort[]) => {
|
|
if (!tableName) {
|
|
return console.warn(
|
|
'[useTableSort] Table name missing in callback, cannot apply sort correctly.'
|
|
)
|
|
}
|
|
|
|
const sortsWithTable = appliedSorts.map((sort) => ({ ...sort, table: tableName }))
|
|
const newUrlSorts = sortsToUrlParams(sortsWithTable)
|
|
|
|
setParams((prevParams) => ({ ...prevParams, sort: newUrlSorts }))
|
|
|
|
saveSortsAndTriggerSideEffects(newUrlSorts)
|
|
},
|
|
[snap, setParams, saveSortsAndTriggerSideEffects]
|
|
)
|
|
|
|
return {
|
|
sorts,
|
|
urlSorts,
|
|
onApplySorts,
|
|
}
|
|
}
|