Files
documenso/packages/lib/client-only/hooks/use-update-search-params.ts
T
ephraimduncan 921e0a0de6 perf: reduce rerenders and async waterfalls in document tables
Parallelize user + team lookups in findDocuments via Promise.all,
dedupe redundant getTeamById call in the internal TRPC route,
stabilize useUpdateSearchParams callback with useCallback + refs,
memoize parsed search params in toolbar components, fix stale
columns memo deps in template documents table, and use replace:true
for search/filter URL updates to avoid history spam.
2026-02-16 20:25:03 +00:00

38 lines
1.2 KiB
TypeScript

import { useCallback, useRef } from 'react';
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();
const searchParamsRef = useRef(searchParams);
searchParamsRef.current = searchParams;
const defaultOptionsRef = useRef(defaultOptions);
defaultOptionsRef.current = defaultOptions;
return useCallback(
(params: SearchParamValues, options?: UpdateSearchParamsOptions) => {
const nextSearchParams = new URLSearchParams(searchParamsRef.current?.toString() ?? '');
Object.entries(params).forEach(([key, value]) => {
if (value === undefined || value === null) {
nextSearchParams.delete(key);
} else {
nextSearchParams.set(key, String(value));
}
});
setSearchParams(nextSearchParams, {
...defaultOptionsRef.current,
...options,
});
},
[setSearchParams],
);
};