mirror of
https://github.com/documenso/documenso.git
synced 2026-07-24 17:04:12 +10:00
921e0a0de6
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.
38 lines
1.2 KiB
TypeScript
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],
|
|
);
|
|
};
|