-
-
Notifications
You must be signed in to change notification settings - Fork 720
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Refactor Table State Persistence in Feature Toggle List (#5527)
new custom hook, `usePersistentTableState` Co-authored-by: Mateusz Kwasniewski <[email protected]>
- Loading branch information
Showing
3 changed files
with
86 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { useEffect } from 'react'; | ||
import { useSearchParams } from 'react-router-dom'; | ||
import { createLocalStorage } from 'utils/createLocalStorage'; | ||
import { useQueryParams } from 'use-query-params'; | ||
|
||
const usePersistentSearchParams = (key: string) => { | ||
const [searchParams, setSearchParams] = useSearchParams(); | ||
const { value, setValue } = createLocalStorage(key, {}); | ||
useEffect(() => { | ||
const params = Object.fromEntries(searchParams.entries()); | ||
if (Object.keys(params).length > 0) { | ||
return; | ||
} | ||
if (Object.keys(value).length === 0) { | ||
return; | ||
} | ||
|
||
setSearchParams(value, { replace: true }); | ||
}, []); | ||
|
||
return setValue; | ||
}; | ||
|
||
export const usePersistentTableState = < | ||
T extends Parameters<typeof useQueryParams>[0], | ||
>( | ||
key: string, | ||
queryParamsDefinition: T, | ||
) => { | ||
const updateStoredParams = usePersistentSearchParams(key); | ||
|
||
const [tableState, setTableState] = useQueryParams(queryParamsDefinition); | ||
|
||
useEffect(() => { | ||
const { offset, ...rest } = tableState; | ||
updateStoredParams(rest); | ||
}, [JSON.stringify(tableState)]); | ||
|
||
return [tableState, setTableState] as const; | ||
}; |
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,30 @@ | ||
// Custom additional serializers for query params library | ||
// used in `useQueryParams` hook | ||
|
||
const encodeBoolean = ( | ||
bool: boolean | null | undefined, | ||
): string | null | undefined => { | ||
if (bool == null) { | ||
return bool; | ||
} | ||
|
||
return bool ? 'true' : 'false'; | ||
}; | ||
|
||
const decodeBoolean = ( | ||
input: string | (string | null)[] | null | undefined, | ||
): boolean | null | undefined => { | ||
if (input === 'true') { | ||
return true; | ||
} | ||
if (input === 'false') { | ||
return false; | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
export const BooleansStringParam = { | ||
encode: encodeBoolean, | ||
decode: decodeBoolean, | ||
}; |