0
0
Fork 0

Update directory page options to use URL params (#31977)

This commit is contained in:
Renaud Chaput 2024-09-19 17:34:08 +02:00 committed by GitHub
parent 57a38f071b
commit ae03e4ffc6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 51 additions and 15 deletions

View file

@ -0,0 +1,31 @@
import { useMemo, useCallback } from 'react';
import { useLocation, useHistory } from 'react-router';
export function useSearchParams() {
const { search } = useLocation();
return useMemo(() => new URLSearchParams(search), [search]);
}
export function useSearchParam(name: string, defaultValue?: string) {
const searchParams = useSearchParams();
const history = useHistory();
const value = searchParams.get(name) ?? defaultValue;
const setValue = useCallback(
(value: string | null) => {
if (value === null) {
searchParams.delete(name);
} else {
searchParams.set(name, value);
}
history.push({ search: searchParams.toString() });
},
[history, name, searchParams],
);
return [value, setValue] as const;
}