forked from mastodon/mastodon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update directory page options to use URL params (mastodon#31977)
- Loading branch information
Showing
2 changed files
with
51 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters