| 12 | } |
| 13 | |
| 14 | export function useCommand(commands: Commands = {}) { |
| 15 | const [searchParams, setSearchParams] = useSearchParams(); |
| 16 | |
| 17 | useEffect(() => { |
| 18 | let shouldUpdate = false; |
| 19 | searchParams.forEach((param, name) => { |
| 20 | const commandName = name as keyof Commands; |
| 21 | if (typeof commands[commandName] === "function") { |
| 22 | commands[commandName]!(param); |
| 23 | searchParams.delete(name); |
| 24 | shouldUpdate = true; |
| 25 | } |
| 26 | }); |
| 27 | |
| 28 | if (shouldUpdate) { |
| 29 | setSearchParams(searchParams); |
| 30 | } |
| 31 | // eslint-disable-next-line react-hooks/exhaustive-deps |
| 32 | }, [searchParams, commands]); |
| 33 | } |
| 34 | |
| 35 | interface ChatCommands { |
| 36 | new?: Command; |