mirror of
https://github.com/coder/coder.git
synced 2026-11-08 05:32:55 +08:00
42 lines
1.0 KiB
TypeScript
42 lines
1.0 KiB
TypeScript
import { useSearchParams } from "react-router";
|
|
|
|
type UseSearchParamsKeyConfig = Readonly<{
|
|
key: string;
|
|
searchParams?: URLSearchParams;
|
|
defaultValue?: string;
|
|
replace?: boolean;
|
|
}>;
|
|
|
|
type UseSearchParamKeyResult = Readonly<{
|
|
value: string;
|
|
setValue: (newValue: string) => void;
|
|
deleteValue: () => void;
|
|
}>;
|
|
|
|
export const useSearchParamsKey = (
|
|
config: UseSearchParamsKeyConfig,
|
|
): UseSearchParamKeyResult => {
|
|
// Cannot use function update form for setSearchParams, because by default, it
|
|
// will always be linked to innerSearchParams, ignoring the config's params
|
|
const [innerSearchParams, setSearchParams] = useSearchParams();
|
|
|
|
const {
|
|
key,
|
|
searchParams = innerSearchParams,
|
|
defaultValue = "",
|
|
replace = true,
|
|
} = config;
|
|
|
|
return {
|
|
value: searchParams.get(key) ?? defaultValue,
|
|
setValue: (newValue) => {
|
|
searchParams.set(key, newValue);
|
|
setSearchParams(searchParams, { replace });
|
|
},
|
|
deleteValue: () => {
|
|
searchParams.delete(key);
|
|
setSearchParams(searchParams, { replace });
|
|
},
|
|
};
|
|
};
|