2024-09-20 14:00:40 +00:00
|
|
|
import { useRouter } from '@uirouter/react';
|
|
|
|
|
2024-05-06 05:08:03 +00:00
|
|
|
import { useParamState } from '@/react/hooks/useParamState';
|
|
|
|
|
|
|
|
export function useTemplateParams() {
|
2024-09-20 14:00:40 +00:00
|
|
|
const router = useRouter();
|
|
|
|
const [id] = useParamState('templateId', (param) => {
|
2024-05-06 05:08:03 +00:00
|
|
|
if (!param) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const templateId = parseInt(param, 10);
|
|
|
|
if (Number.isNaN(templateId)) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return templateId;
|
|
|
|
});
|
|
|
|
|
2024-09-20 14:00:40 +00:00
|
|
|
const [type] = useParamState('templateType', (param) => {
|
2024-05-06 05:08:03 +00:00
|
|
|
if (param === 'app' || param === 'custom') {
|
|
|
|
return param;
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
});
|
|
|
|
|
|
|
|
return [{ id, type }, handleChange] as const;
|
|
|
|
|
|
|
|
function handleChange({
|
|
|
|
id,
|
|
|
|
type,
|
|
|
|
}: {
|
|
|
|
id: number | undefined;
|
|
|
|
type: 'app' | 'custom' | undefined;
|
|
|
|
}) {
|
2024-09-20 14:00:40 +00:00
|
|
|
router.stateService.go(
|
|
|
|
'.',
|
|
|
|
{ templateId: id, templateType: type },
|
|
|
|
{ reload: false }
|
|
|
|
);
|
2024-05-06 05:08:03 +00:00
|
|
|
}
|
|
|
|
}
|