2023-06-25 05:38:43 +00:00
|
|
|
import { useCurrentStateAndParams, useRouter } from '@uirouter/react';
|
|
|
|
|
|
|
|
export function useParamState<T>(
|
|
|
|
param: string,
|
2023-11-14 12:54:44 +00:00
|
|
|
parseParam: (param: string | undefined) => T | undefined = (param) =>
|
|
|
|
param as unknown as T
|
2023-06-25 05:38:43 +00:00
|
|
|
) {
|
|
|
|
const {
|
|
|
|
params: { [param]: paramValue },
|
|
|
|
} = useCurrentStateAndParams();
|
|
|
|
const router = useRouter();
|
|
|
|
const state = parseParam(paramValue);
|
|
|
|
|
|
|
|
return [
|
|
|
|
state,
|
2023-11-14 12:54:44 +00:00
|
|
|
(value?: T) => {
|
2023-06-25 05:38:43 +00:00
|
|
|
router.stateService.go('.', { [param]: value });
|
|
|
|
},
|
|
|
|
] as const;
|
|
|
|
}
|