import axios from 'axios'; import type { Automation, Trigger } from 'ontime-types'; import { useState } from 'react'; import { deleteAutomation } from '../../../../common/api/automation'; import { maybeAxiosError } from '../../../../common/api/utils'; import Button from '../../../../common/components/buttons/Button'; import Dialog from '../../../../common/components/dialog/Dialog'; import Info from '../../../../common/components/info/Info'; import { getLifecycleLabel } from '../../../../common/constants/timerLifecycle'; import * as Panel from '../../panel-utils/PanelUtils'; interface DeleteAutomationDialogProps { automation: Automation; /** global triggers pointing at this automation, they are deleted along with it */ attachedTriggers: Trigger[]; onCancel: () => void; onDeleted: () => void; } /** * Deleting takes the automation's global triggers with it, so say so before it happens rather * than leaving the user to discover it in the triggers list. * * An automation attached to an event is still refused by the server: that reference lives in * the rundown and removing it is an edit to the show, not to this panel. */ export default function DeleteAutomationDialog({ automation, attachedTriggers, onCancel, onDeleted, }: DeleteAutomationDialogProps) { const [error, setError] = useState(null); const [isDeleting, setIsDeleting] = useState(false); const [isAttachedToEvent, setIsAttachedToEvent] = useState(false); const handleDelete = async () => { setError(null); setIsAttachedToEvent(false); setIsDeleting(true); try { await deleteAutomation(automation.id); onDeleted(); } catch (error) { setError(maybeAxiosError(error)); setIsAttachedToEvent(axios.isAxiosError(error) && error.response?.status === 409); } finally { setIsDeleting(false); } }; return ( Delete {automation.title}? This cannot be undone. {attachedTriggers.length > 0 && ( {attachedTriggers.length === 1 ? 'Its trigger is deleted with it' : `Its ${attachedTriggers.length} triggers are deleted with it`} {attachedTriggers.map((trigger) => getLifecycleLabel(trigger.trigger)).join(', ')} )} {error && ( Could not delete this automation {error} {isAttachedToEvent ? 'An automation attached to a single event has to be removed from that event first, in the event editor.' : 'Try again. If the problem persists, check the network log for details.'} )} } footerElements={ <> } /> ); }