|
| 1 | +import { getLocalStorageItem, setLocalStorageItem } from "@neolution-ch/javascript-utils"; |
| 2 | +import { OptionalNullable } from "../types/NullableTypes"; |
| 3 | +import { useReactDataTableStateProps } from "./useReactDataTableStateProps"; |
| 4 | +import { useReactDataTableStateResult } from "./useReactDataTableStateResult"; |
| 5 | +import { useReactDataTableState } from "./useReactDataTableState"; |
| 6 | +import { FilterModel } from "../types/TableState"; |
| 7 | +import { useCallback } from "react"; |
| 8 | + |
| 9 | +/** |
| 10 | + * A custom hook that will initialize all the state needed for the react data table and will persist it to local storage |
| 11 | + * @param props The properties to configure the initial state |
| 12 | + * @returns the state and the setters |
| 13 | + */ |
| 14 | +const usePersistentReactDataTableState = <TData, TFilter extends FilterModel = Record<string, never>>( |
| 15 | + props: OptionalNullable<useReactDataTableStateProps<TData, TFilter>> & { localStorageKey: string }, |
| 16 | +): useReactDataTableStateResult<TData, TFilter> => { |
| 17 | + const { |
| 18 | + initialColumnFilters, |
| 19 | + initialSorting, |
| 20 | + initialPagination, |
| 21 | + initialRowSelection, |
| 22 | + initialExpanded, |
| 23 | + initialColumnPinning, |
| 24 | + initialAfterSearchFilter, |
| 25 | + localStorageKey, |
| 26 | + } = props as useReactDataTableStateProps<TData, TFilter> & { localStorageKey: string }; |
| 27 | + |
| 28 | + const { |
| 29 | + pagination, |
| 30 | + setPagination, |
| 31 | + columnFilters, |
| 32 | + columnPinning, |
| 33 | + expanded, |
| 34 | + rowSelection, |
| 35 | + sorting, |
| 36 | + setColumnFilters, |
| 37 | + afterSearchFilter, |
| 38 | + setAfterSearchFilter, |
| 39 | + setColumnPinning, |
| 40 | + setExpanded, |
| 41 | + setRowSelection, |
| 42 | + setSorting, |
| 43 | + } = useReactDataTableState<TData, TFilter>({ |
| 44 | + initialColumnPinning: |
| 45 | + getLocalStorageItem<useReactDataTableStateProps<TData, TFilter>["initialColumnPinning"]>(`${localStorageKey}_columnPinning`) ?? |
| 46 | + initialColumnPinning, |
| 47 | + initialExpanded: |
| 48 | + getLocalStorageItem<useReactDataTableStateProps<TData, TFilter>["initialExpanded"]>(`${localStorageKey}_expanded`) ?? initialExpanded, |
| 49 | + initialPagination: |
| 50 | + getLocalStorageItem<useReactDataTableStateProps<TData, TFilter>["initialPagination"]>(`${localStorageKey}_pagination`) ?? |
| 51 | + initialPagination, |
| 52 | + initialRowSelection: |
| 53 | + getLocalStorageItem<useReactDataTableStateProps<TData, TFilter>["initialRowSelection"]>(`${localStorageKey}_rowSelection`) ?? |
| 54 | + initialRowSelection, |
| 55 | + initialSorting: |
| 56 | + getLocalStorageItem<useReactDataTableStateProps<TData, TFilter>["initialSorting"]>(`${localStorageKey}_sorting`) ?? initialSorting, |
| 57 | + initialColumnFilters: |
| 58 | + getLocalStorageItem<useReactDataTableStateProps<TData, TFilter>["initialColumnFilters"]>(`${localStorageKey}_columnFilters`) ?? |
| 59 | + initialColumnFilters, |
| 60 | + initialAfterSearchFilter: |
| 61 | + getLocalStorageItem<useReactDataTableStateProps<TData, TFilter>["initialAfterSearchFilter"]>( |
| 62 | + `${localStorageKey}_afterSearchFilter`, |
| 63 | + ) ?? initialAfterSearchFilter, |
| 64 | + } as useReactDataTableStateProps<TData, TFilter> as OptionalNullable<useReactDataTableStateProps<TData, TFilter>>); |
| 65 | + |
| 66 | + return { |
| 67 | + pagination, |
| 68 | + setPagination: useCallback( |
| 69 | + (newPagination) => { |
| 70 | + setPagination(newPagination); |
| 71 | + setLocalStorageItem(`${props.localStorageKey}_pagination`, newPagination); |
| 72 | + }, |
| 73 | + [props.localStorageKey, setPagination], |
| 74 | + ), |
| 75 | + columnFilters, |
| 76 | + setColumnFilters: useCallback( |
| 77 | + (newColumnFilters) => { |
| 78 | + setColumnFilters(newColumnFilters); |
| 79 | + setLocalStorageItem(`${props.localStorageKey}_columnFilters`, newColumnFilters); |
| 80 | + }, |
| 81 | + [props.localStorageKey, setColumnFilters], |
| 82 | + ), |
| 83 | + afterSearchFilter, |
| 84 | + setAfterSearchFilter: useCallback( |
| 85 | + (newAfterSearchFilter) => { |
| 86 | + setAfterSearchFilter(newAfterSearchFilter); |
| 87 | + setLocalStorageItem(`${props.localStorageKey}_afterSearchFilter`, newAfterSearchFilter); |
| 88 | + }, |
| 89 | + [props.localStorageKey, setAfterSearchFilter], |
| 90 | + ), |
| 91 | + columnPinning, |
| 92 | + setColumnPinning: useCallback( |
| 93 | + (newColumnPinning) => { |
| 94 | + setColumnPinning(newColumnPinning); |
| 95 | + setLocalStorageItem(`${props.localStorageKey}_columnPinning`, newColumnPinning); |
| 96 | + }, |
| 97 | + [props.localStorageKey, setColumnPinning], |
| 98 | + ), |
| 99 | + expanded, |
| 100 | + setExpanded: useCallback( |
| 101 | + (newExpanded) => { |
| 102 | + setExpanded(newExpanded); |
| 103 | + setLocalStorageItem(`${props.localStorageKey}_expanded`, newExpanded); |
| 104 | + }, |
| 105 | + [props.localStorageKey, setExpanded], |
| 106 | + ), |
| 107 | + rowSelection, |
| 108 | + setRowSelection: useCallback( |
| 109 | + (newRowSelection) => { |
| 110 | + setRowSelection(newRowSelection); |
| 111 | + setLocalStorageItem(`${props.localStorageKey}_rowSelection`, newRowSelection); |
| 112 | + }, |
| 113 | + [props.localStorageKey, setRowSelection], |
| 114 | + ), |
| 115 | + sorting, |
| 116 | + setSorting: useCallback( |
| 117 | + (newSorting) => { |
| 118 | + setSorting(newSorting); |
| 119 | + setLocalStorageItem(`${props.localStorageKey}_sorting`, newSorting); |
| 120 | + }, |
| 121 | + [props.localStorageKey, setSorting], |
| 122 | + ), |
| 123 | + }; |
| 124 | +}; |
| 125 | + |
| 126 | +export { usePersistentReactDataTableState }; |
0 commit comments