mirror of
https://github.com/documenso/documenso.git
synced 2026-07-25 01:15:49 +10:00
335fee09a9
Consolidating document and template filtering into shared faceted toolbars makes filtering easier to discover and use. Supporting comma-separated query params enables multi-select filters across UI, server queries, and E2E coverage.
27 lines
930 B
TypeScript
27 lines
930 B
TypeScript
import type { NavigateOptions } from 'react-router';
|
|
import { useSearchParams } from 'react-router';
|
|
|
|
type SearchParamValues = Record<string, string | number | boolean | null | undefined>;
|
|
type UpdateSearchParamsOptions = Pick<NavigateOptions, 'preventScrollReset' | 'replace' | 'state'>;
|
|
|
|
export const useUpdateSearchParams = (defaultOptions: UpdateSearchParamsOptions = {}) => {
|
|
const [searchParams, setSearchParams] = useSearchParams();
|
|
|
|
return (params: SearchParamValues, options?: UpdateSearchParamsOptions) => {
|
|
const nextSearchParams = new URLSearchParams(searchParams?.toString() ?? '');
|
|
|
|
Object.entries(params).forEach(([key, value]) => {
|
|
if (value === undefined || value === null) {
|
|
nextSearchParams.delete(key);
|
|
} else {
|
|
nextSearchParams.set(key, String(value));
|
|
}
|
|
});
|
|
|
|
setSearchParams(nextSearchParams, {
|
|
...defaultOptions,
|
|
...options,
|
|
});
|
|
};
|
|
};
|