Files
documenso/packages/lib/client-only/hooks/use-update-search-params.ts
T
ephraimduncan 335fee09a9 feat: add faceted table toolbars and multi-value filters
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.
2026-02-16 18:40:23 +00:00

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,
});
};
};