2026-10-05 18:10:18 -04:00
import { useStore } from '@tanstack/react-form' ;
import { useAppForm , Form } from '@/components/form' ;
import type { AdminEgg } from '@/api/admin/eggs/queries' ;
import { deleteAdminEggInput , useDeleteAdminEgg } from '@/api/admin/eggs/queries' ;
import { Dialog } from '@/components/elements/dialog' ;
import SpinnerOverlay from '@/components/elements/SpinnerOverlay' ;
import Button from '@/components/elements/Button' ;
2026-11-23 14:16:00 +00:00
import Translate from '@/components/elements/Translate' ;
import { useTranslation } from '@/sdk/localization' ;
2026-10-05 18:10:18 -04:00
interface Props {
egg : AdminEgg ;
onDeleted : () => Promise < void >;
}
type DeleteEggDialogProps = Props & {
open : boolean ;
onClose : () => void ;
};
// The API rejects deleting an egg that servers or child eggs still use.
function DeleteEggDialog ({ egg , onDeleted , open , onClose } : DeleteEggDialogProps ) {
2026-11-23 14:16:00 +00:00
const { t } = useTranslation ();
2026-10-05 18:10:18 -04:00
const deleteEgg = useDeleteAdminEgg ();
const form = useAppForm ({
defaultValues : { confirm : '' },
onSubmit : async () => {
try {
await deleteEgg . mutateAsync ( deleteAdminEggInput ( egg . attributes . id , egg . attributes . name ));
await onDeleted ();
onClose ();
} catch {
// Error toast is handled by the mutation.
}
},
});
const isSubmitting = useStore ( form . store , ( state ) => state . isSubmitting );
return (
< Dialog
open = { open }
2026-11-23 14:16:00 +00:00
title = { t ( 'Confirm egg deletion' )}
2026-10-05 18:10:18 -04:00
preventExternalClose = { isSubmitting }
hideCloseIcon = { isSubmitting }
onClose = {() => {
onClose ();
form . reset ();
}}
>
< SpinnerOverlay visible = { isSubmitting } />
2026-10-08 16:53:20 -04:00
< p className = 'text-sm' >
2026-11-23 14:16:00 +00:00
< Translate
i18nKey = 'Deleting an egg is a permanent action, it cannot be undone. This will permanently delete the <bold>{{name}}</bold> egg. An egg with attached servers cannot be deleted.'
values = {{ name : egg.attributes.name }}
components = {{ bold : < strong /> }}
/>
2026-10-05 18:10:18 -04:00
</ p >
2026-10-08 16:53:20 -04:00
< Form form = { form } className = 'm-0 mt-6' >
2026-10-05 18:10:18 -04:00
< form.AppField
2026-10-08 16:53:20 -04:00
name = 'confirm'
2026-10-05 18:10:18 -04:00
validators = {{
onChange : ({ value }) =>
2026-11-23 14:16:00 +00:00
value === egg . attributes . name ? undefined : t ( 'The egg name must be provided.' ),
2026-10-05 18:10:18 -04:00
}}
>
{( field ) => (
< field.TextField
2026-10-08 16:53:20 -04:00
type = 'text'
id = 'confirm_egg_name'
2026-11-23 14:16:00 +00:00
label = { t ( 'Confirm Name' )}
description = { t ( 'Enter the name of this egg to confirm deletion.' )}
2026-10-05 18:10:18 -04:00
/>
)}
</ form.AppField >
2026-10-08 16:53:20 -04:00
< div className = 'mt-6 text-right' >
< Button type = 'button' isSecondary className = 'mr-2' onClick = { onClose }>
2026-11-23 14:16:00 +00:00
{ t ( 'Cancel' )}
2026-10-05 18:10:18 -04:00
</ Button >
< form.AppForm >
2026-11-23 14:16:00 +00:00
< form.SubmitButton color = 'red' >{ t ( 'Delete Egg' )}</ form.SubmitButton >
2026-10-05 18:10:18 -04:00
</ form.AppForm >
</ div >
</ Form >
</ Dialog >
);
}
export default function DeleteEggButton ({ egg , onDeleted } : Props ) {
2026-11-23 14:16:00 +00:00
const { t } = useTranslation ();
2026-10-05 18:10:18 -04:00
return (
< Dialog.Trigger
trigger = {({ onClick }) => (
2026-10-08 16:53:20 -04:00
< Button color = 'red' isSecondary onClick = { onClick }>
2026-11-23 14:16:00 +00:00
{ t ( 'Delete Egg' )}
2026-10-05 18:10:18 -04:00
</ Button >
)}
>
{({ open , onClose }) => < DeleteEggDialog egg = { egg } onDeleted = { onDeleted } open = { open } onClose = { onClose } />}
</ Dialog.Trigger >
);
}