-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: create a hook to manage sorting state
fix: fix failing builds
- Loading branch information
1 parent
704dbb2
commit c5572b4
Showing
8 changed files
with
68 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { SortingState, Updater } from "@tanstack/react-table"; | ||
import { useCallback, useMemo } from "react"; | ||
import { useSearchParams } from "react-router-dom"; | ||
|
||
/** | ||
* | ||
* useReactTableSortState is a custom hook that manages the sorting state of a | ||
* react-table instance. It uses the URL search params to store the sorting | ||
* state. It returns the sorting state and a function to update the sorting state. | ||
* | ||
*/ | ||
export default function useReactTableSortState( | ||
sortByKey = "sortBy", | ||
sortOrderKey = "sortOrder" | ||
): [SortingState, (newSortBy: Updater<SortingState>) => void] { | ||
const [searchParams, setSearchParams] = useSearchParams(); | ||
|
||
const sortBy = searchParams.get(sortByKey) || undefined; | ||
const sortOrder = searchParams.get(sortOrderKey) || undefined; | ||
|
||
const tableSortByState = useMemo(() => { | ||
if (!sortBy || !sortOrder) { | ||
return []; | ||
} | ||
return [ | ||
{ | ||
id: sortBy, | ||
desc: sortOrder === "desc" | ||
} | ||
] satisfies SortingState; | ||
}, [sortBy, sortOrder]); | ||
|
||
console.log(tableSortByState); | ||
|
||
const updateSortByFn = useCallback( | ||
(newSortBy: Updater<SortingState>) => { | ||
const sort = typeof newSortBy === "function" ? newSortBy([]) : newSortBy; | ||
if (sort.length === 0) { | ||
searchParams.delete(sortByKey); | ||
searchParams.delete(sortOrderKey); | ||
} else { | ||
searchParams.set(sortByKey, sort[0]?.id); | ||
searchParams.set(sortOrderKey, sort[0].desc ? "desc" : "asc"); | ||
} | ||
setSearchParams(searchParams); | ||
}, | ||
[searchParams, setSearchParams, sortByKey, sortOrderKey] | ||
); | ||
|
||
return [tableSortByState, updateSortByFn]; | ||
} |