You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
674 B
29 lines
674 B
import { useSearchParams } from "react-router-dom";
|
|
|
|
type Command = (param: string) => void;
|
|
interface Commands {
|
|
fill?: Command;
|
|
submit?: Command;
|
|
mask?: Command;
|
|
}
|
|
|
|
export function useCommand(commands: Commands = {}) {
|
|
const [searchParams, setSearchParams] = useSearchParams();
|
|
|
|
if (commands === undefined) return;
|
|
|
|
let shouldUpdate = false;
|
|
searchParams.forEach((param, name) => {
|
|
const commandName = name as keyof Commands;
|
|
if (typeof commands[commandName] === "function") {
|
|
commands[commandName]!(param);
|
|
searchParams.delete(name);
|
|
shouldUpdate = true;
|
|
}
|
|
});
|
|
|
|
if (shouldUpdate) {
|
|
setSearchParams(searchParams);
|
|
}
|
|
}
|