import { useState } from 'react'; import { type Schedule, type Task } from '@/api/server/schedules/queries'; import { useStore } from '@tanstack/react-form'; import { useAppForm, Form } from '@/components/form'; import { createScheduleTaskInput, updateScheduleTaskInput, useCreateServerScheduleTask, useUpdateServerScheduleTask, } from '@/api/server/schedules/queries'; import { Dialog, type DialogProps } from '@/components/elements/dialog'; import { useCurrentServer } from '@/api/server/queries'; interface Props { schedule: Schedule; task?: Task; } type Action = 'command' | 'power' | 'backup'; interface TaskFormValues { action: Action; payload: string; timeOffset: number | null; continueOnFailure: boolean; } const TaskDetailsForm = ({ schedule, task, onClose }: Props & { onClose: () => void }) => { const [formError, setFormError] = useState(null); const server = useCurrentServer()!; const uuid = server.attributes.uuid; const createTask = useCreateServerScheduleTask(schedule); const updateTask = useUpdateServerScheduleTask(schedule); const backupLimit = server.attributes.feature_limits.backups; const initialAction = (task?.attributes.action || 'command') as Action; const initialPayload = task?.attributes.payload || ''; const defaultValues: TaskFormValues = { action: initialAction, payload: initialPayload, timeOffset: task?.attributes.time_offset ?? 0, continueOnFailure: task?.attributes.continue_on_failure || false, }; const form = useAppForm({ defaultValues, onSubmit: async ({ value }) => { setFormError(null); if (backupLimit === 0 && value.action === 'backup') { setFormError("A backup task cannot be created when the server's backup limit is set to 0."); return; } const { timeOffset } = value; if (timeOffset === null) { return; } const values = { ...value, timeOffset }; try { if (task) { await updateTask.mutateAsync(updateScheduleTaskInput(uuid, schedule, task.attributes.id, values)); } else { await createTask.mutateAsync(createScheduleTaskInput(uuid, schedule, values)); } onClose(); } catch { // Error toast is handled by the mutation. } }, }); const action = useStore(form.store, (state) => state.values.action); const payloadValidators = { onChangeListenTo: ['action' as const], onChange: ({ value }: { value: string }) => form.getFieldValue('action') !== 'command' || value.length > 0 ? undefined : 'A task payload must be provided.', }; const setPayloadForAction = (nextAction: Action) => { form.setFieldValue( 'payload', nextAction === initialAction ? initialPayload || '' : nextAction === 'power' ? 'start' : '' ); }; return (
{formError && (
{formError}
)}
{(field) => ( setPayloadForAction(value as Action)} /> )}
value === null ? 'A time offset must be provided.' : value < 0 ? 'The time offset must be at least 0 seconds.' : value > 900 ? 'The time offset must be less than 900 seconds.' : undefined, }} > {(field) => ( )}
{action === 'command' ? ( {(field) => } ) : action === 'power' ? ( {(field) => ( )} ) : ( {(field) => ( )} )}
{(field) => ( )}
{task ? 'Save Changes' : 'Create Task'}
); }; export default function TaskDetailsModal({ open, onClose, ...props }: Props & DialogProps) { const formKey = `${props.schedule.attributes.id}:${props.task?.attributes.id ?? 'new'}`; return ( ); }