-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { useNavigate, useSearch } from '@tanstack/react-router'; | ||
import { SimulatorInputSearch } from 'libs/routing/routes/sim'; | ||
Check failure on line 2 in src/hooks/useStrategyInput.ts
|
||
import { useState } from 'react'; | ||
import { debounce } from 'utils/helpers'; | ||
Check failure on line 4 in src/hooks/useStrategyInput.ts
|
||
|
||
export type StrategyInputDispatch = < | ||
T extends SimulatorInputSearch, | ||
K extends keyof T | ||
>( | ||
key: K, | ||
value: T[K] | ||
) => void; | ||
|
||
export const useStrategyInput = () => { | ||
const navigate = useNavigate(); | ||
const search: SimulatorInputSearch = useSearch({ strict: false }); | ||
const [state, setState] = useState(search); | ||
|
||
const navigateDebounced = debounce((key: string, value: string) => | ||
navigate({ | ||
search: (search) => ({ ...search, [key]: value }), | ||
params: {}, | ||
replace: true, | ||
resetScroll: false, | ||
}) | ||
); | ||
|
||
const dispatch: StrategyInputDispatch = (key, value) => { | ||
setState((state) => ({ ...state, [key]: value })); | ||
Check failure on line 29 in src/hooks/useStrategyInput.ts
|
||
navigateDebounced(key, value); | ||
}; | ||
|
||
return [state, dispatch] as const; | ||
}; |