| 1 | "use client"; |
| 2 | |
| 3 | import { useCreateAtom, useSelector |
| 4 | import { |
| 5 | type ColumnFiltersState, |
| 6 | functionalUpdate, |
| 7 | type PaginationState, |
| 8 | type SortingState, |
| 9 | type Updater, |
| 10 | } from "@tanstack/react-table"; |
| 11 | import { useCallback, useEffect |
| 12 | |
| 13 | export function useServerTableState({ |
| 14 | initialSorting = [], |
| 15 | pageSize, |
| 16 | }: { |
| 17 | initialSorting?: SortingState |
| 18 | pageSize: number |
| 19 | }) { |
| 20 | const columnFiltersAtom = |
| 21 | const globalFilterAtom = |
| 22 | const paginationAtom = |
| 23 | pageIndex: 0, |
| 24 | pageSize, |
| 25 | }); |
| 26 | const sortingAtom = |
| 27 | const columnFilters = |
| 28 | const globalFilter = |
| 29 | const pagination = |
| 30 | const sorting = |
| 31 | const atoms = |
| 32 | () => ({ |
| 33 | columnFilters: columnFiltersAtom, |
| 34 | globalFilter: globalFilterAtom, |
| 35 | pagination: paginationAtom, |
| 36 | sorting: sortingAtom, |
| 37 | }), |
| 38 | [columnFiltersAtom, globalFilterAtom, |
| 39 | ); |
| 40 | |
| 41 | useEffect(() => |
| 42 | const resetPage = |
| 43 | const current = |
| 44 | if (current.pageIndex |
| 45 | paginationAtom.set({ |
| 46 | } |
| 47 | }; |
| 48 | const subscriptions = |
| 49 | columnFiltersAtom.subscribe(resetPage |
| 50 | globalFilterAtom.subscribe(resetPage |
| 51 | sortingAtom.subscribe(resetPage |
| 52 | ]; |
| 53 | |
| 54 | return () => |
| 55 | for (const subscription |
| 56 | }; |
| 57 | }, [columnFiltersAtom, |
| 58 | |
| 59 | const setPagination = |
| 60 | (updater: Updater |
| 61 | paginationAtom.set((current |
| 62 | }, |
| 63 | [paginationAtom], |
| 64 | ); |
| 65 | |
| 66 | return { |
| 67 | atoms, |
| 68 | columnFilters, |
| 69 | globalFilter, |
| 70 | pagination, |
| 71 | setPagination, |
| 72 | sorting, |
| 73 | }; |
| 74 | } |