-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: save table page state in local storage
Closes #597
- Loading branch information
Showing
14 changed files
with
112 additions
and
99 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 was deleted.
Oops, something went wrong.
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,52 @@ | ||
import { useState } from 'react'; | ||
|
||
import type { Updater, PaginationState } from '@tanstack/react-table'; | ||
|
||
import type { TableKeys } from '@/utils/constants/tables'; | ||
|
||
import { useFeatureFlag } from './useFeatureFlag'; | ||
|
||
const DEFAULT_PAGINATION_STATE = { pageIndex: 0, pageSize: 10 } as const; | ||
|
||
export const usePaginationState = ( | ||
selectedTable: TableKeys, | ||
): { | ||
pagination: PaginationState; | ||
paginationUpdater: (updater: Updater<PaginationState>) => void; | ||
} => { | ||
const { showDev } = useFeatureFlag(); | ||
const storageData = window.localStorage.getItem(selectedTable); | ||
let deserializedState = DEFAULT_PAGINATION_STATE; | ||
|
||
if (storageData && showDev) { | ||
try { | ||
deserializedState = JSON.parse(storageData); | ||
} catch { | ||
// if stringify throws an error, we use the default state already set | ||
} | ||
} | ||
|
||
const [pagination, setPagination] = | ||
useState<PaginationState>(deserializedState); | ||
|
||
const paginationUpdater = (updater: Updater<PaginationState>): void => { | ||
let newState; | ||
if (typeof updater === 'function') { | ||
newState = updater(pagination); | ||
} else { | ||
newState = updater; | ||
} | ||
try { | ||
const serializedState = JSON.stringify(newState); | ||
window.localStorage.setItem(selectedTable, serializedState); | ||
setPagination(newState); | ||
} catch { | ||
// if the state can't be serialized, the last state will continue | ||
} | ||
}; | ||
|
||
const return_value = showDev | ||
? { pagination, paginationUpdater } | ||
: { pagination, paginationUpdater: setPagination }; | ||
return return_value; | ||
}; |
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
Oops, something went wrong.