2026-10-05 18:10:18 -04:00
import { useState , type ReactNode } from 'react' ;
import { useStore } from '@tanstack/react-form' ;
import { useNavigate } from '@tanstack/react-router' ;
import { useAppForm , Form } from '@/components/form' ;
import {
type AdminServer ,
deleteAdminServerInput ,
forceDeleteAdminServerInput ,
useDeleteAdminServer ,
useForceDeleteAdminServer ,
} from '@/api/admin/servers/queries' ;
import { useServerDetail } from '@/components/admin/servers/useServerDetail' ;
import TitledGreyBox from '@/components/elements/TitledGreyBox' ;
import { Dialog } from '@/components/elements/dialog' ;
import SpinnerOverlay from '@/components/elements/SpinnerOverlay' ;
import Button from '@/components/elements/Button' ;
import { Alert } from '@/components/elements/alert' ;
type DeleteMode = 'safe' | 'force' ;
interface Props {
server : AdminServer ;
}
interface DeleteServerDialogProps extends Props {
mode : DeleteMode ;
open : boolean ;
onClose : () => void ;
}
function DeleteServerDialog ({ server , mode , open , onClose } : DeleteServerDialogProps ) {
const { attributes } = server ;
const navigate = useNavigate ();
const deleteServer = useDeleteAdminServer ();
const forceDeleteServer = useForceDeleteAdminServer ();
const form = useAppForm ({
defaultValues : { confirm : '' },
onSubmit : async () => {
try {
if ( mode === 'force' ) {
await forceDeleteServer . mutateAsync ( forceDeleteAdminServerInput ( attributes . id , attributes . name ));
} else {
await deleteServer . mutateAsync ( deleteAdminServerInput ( attributes . id , attributes . name ));
}
2026-10-08 16:53:20 -04:00
2026-10-05 18:10:18 -04:00
await navigate ({ to : '/panel/servers' });
} catch {
// Error toast is handled by the mutation.
}
},
});
const isSubmitting = useStore ( form . store , ( state ) => state . isSubmitting );
const close = () => {
form . reset ();
onClose ();
};
return (
< Dialog
open = { open }
title = { mode === 'force' ? 'Forcibly delete server' : 'Confirm server deletion' }
preventExternalClose = { isSubmitting }
hideCloseIcon = { isSubmitting }
onClose = { close }
>
< SpinnerOverlay visible = { isSubmitting } />
2026-10-08 16:53:20 -04:00
< p className = 'text-sm' >
2026-10-05 18:10:18 -04:00
Deleting a server is a permanent action , it cannot be undone . This will permanently delete the { ' ' }
< strong >{ attributes . name }</ strong > server and remove all associated data .
{ mode === 'force' && (
2026-10-08 16:53:20 -04:00
< span className = 'block mt-2 text-destructive' >
2026-10-05 18:10:18 -04:00
Force deleting may leave dangling files on the daemon if it reports an error .
</ span >
)}
</ 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 }) =>
value === attributes . name ? undefined : 'The server name must be provided.' ,
}}
>
{( field ) => (
< field.TextField
2026-10-08 16:53:20 -04:00
type = 'text'
2026-10-05 18:10:18 -04:00
id = { `confirm_server_name_ ${ mode } ` }
2026-10-08 16:53:20 -04:00
label = 'Confirm Server Name'
description = 'Enter the name of this server 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' disabled = { isSubmitting } onClick = { close }>
2026-10-05 18:10:18 -04:00
Cancel
</ Button >
< form.AppForm >
2026-10-08 16:53:20 -04:00
< form.SubmitButton color = 'red' >
2026-10-05 18:10:18 -04:00
{ mode === 'force' ? 'Force Delete Server' : 'Delete Server' }
</ form.SubmitButton >
</ form.AppForm >
</ div >
</ Form >
</ Dialog >
);
}
const DeleteActionCard = ({
title ,
warning ,
description ,
action ,
} : {
title : string ;
warning : ReactNode ;
description : ReactNode ;
action : ReactNode ;
}) => (
2026-10-08 16:53:20 -04:00
< TitledGreyBox title = { title } className = 'flex h-full flex-col' contentClassName = 'flex flex-1 flex-col p-0' >
< div className = 'flex flex-1 flex-col gap-3 p-3' >
< Alert type = 'danger' >
< strong className = 'font-semibold' > Danger ! </ strong > { warning }
2026-10-05 18:10:18 -04:00
</ Alert >
2026-10-08 16:53:20 -04:00
< p className = 'text-sm text-muted-foreground' >{ description }</ p >
2026-10-05 18:10:18 -04:00
</ div >
2026-10-08 16:53:20 -04:00
< div className = 'flex items-center justify-end border-t border-border p-3' >{ action }</ div >
2026-10-05 18:10:18 -04:00
</ TitledGreyBox >
);
function ServerDeleteContent ({ server } : Props ) {
const [ mode , setMode ] = useState < DeleteMode | null >( null );
const close = () => setMode ( null );
return (
< div >
2026-10-08 16:53:20 -04:00
< DeleteServerDialog server = { server } mode = 'safe' open = { mode === 'safe' } onClose = { close } />
< DeleteServerDialog server = { server } mode = 'force' open = { mode === 'force' } onClose = { close } />
2026-10-05 18:10:18 -04:00
2026-10-08 16:53:20 -04:00
< div className = 'grid grid-cols-1 gap-6 lg:grid-cols-2' >
2026-10-05 18:10:18 -04:00
< DeleteActionCard
2026-10-08 16:53:20 -04:00
title = 'Safely Delete Server'
2026-10-05 18:10:18 -04:00
warning = {
<>
Deleting a server is an irreversible action . < strong > All server data </ strong > ( including
files and users ) will be removed from the system .
</>
}
2026-10-08 16:53:20 -04:00
description = 'This action will attempt to delete the server from both the panel and daemon. If either one reports an error the action will be cancelled.'
2026-10-05 18:10:18 -04:00
action = {
2026-10-08 16:53:20 -04:00
< Button color = 'red' className = 'whitespace-nowrap' onClick = {() => setMode ( 'safe' )}>
2026-10-05 18:10:18 -04:00
Safely Delete This Server
</ Button >
}
/>
< DeleteActionCard
2026-10-08 16:53:20 -04:00
title = 'Force Delete Server'
2026-10-05 18:10:18 -04:00
warning = {
<>
Deleting a server is an irreversible action . < strong > All server data </ strong > will be
removed . This method may leave dangling files on your daemon if it reports an error .
</>
}
2026-10-08 16:53:20 -04:00
description = 'This action will attempt to delete the server from both the panel and daemon. If the daemon does not respond, or reports an error, the deletion will continue.'
2026-10-05 18:10:18 -04:00
action = {
2026-10-08 16:53:20 -04:00
< Button color = 'red' className = 'whitespace-nowrap' onClick = {() => setMode ( 'force' )}>
2026-10-05 18:10:18 -04:00
Forcibly Delete This Server
</ Button >
}
/>
</ div >
</ div >
);
}
export default function ServerDeleteTab() {
const { server } = useServerDetail ();
return < ServerDeleteContent server = { server } />;
}