1 import { useCallback, useEffect, useMemo } from 'react';
2 import { useHistory, useLocation } from 'react-router-dom';
4 import type { ParsedSearchParams } from '@proton/shared/lib/helpers/url';
5 import { changeSearchParams, getSearchParams } from '@proton/shared/lib/helpers/url';
7 type SetSearchParams = (nextInit: ParsedSearchParams | ((prev: ParsedSearchParams) => ParsedSearchParams)) => void;
10 * A convenient wrapper for reading and writing search parameters via the
11 * URLSearchParams interface.
13 export default function useSearchParams(init?: ParsedSearchParams): [ParsedSearchParams, SetSearchParams] {
14 const location = useLocation();
15 const history = useHistory();
17 const searchParams = useMemo(() => getSearchParams(location.hash), [location.hash]);
19 const setSearchParams = useCallback<SetSearchParams>(
21 const newParams = typeof update === 'function' ? update(searchParams) : update;
22 const newRoute = changeSearchParams(location.pathname, location.hash, newParams);
23 history.push(newRoute);
25 [history, searchParams]
30 setSearchParams(init);
34 return [searchParams, setSearchParams];