2026-10-05 18:10:18 -04:00
import React from 'react' ;
import {
type AdminServer ,
reinstallAdminServerInput ,
toggleAdminServerInstallStateInput ,
updateAdminServerSuspensionInput ,
useAdminServerTransferProgress ,
useReinstallAdminServer ,
useToggleAdminServerInstallState ,
useUpdateAdminServerSuspension ,
} from '@/api/admin/servers/queries' ;
import { useAdminNodes } from '@/api/admin/nodes/queries' ;
import { useServerDetail } from '@/components/admin/servers/useServerDetail' ;
import TransferServerModal from '@/components/admin/servers/TransferServerModal' ;
import TitledGreyBox from '@/components/elements/TitledGreyBox' ;
import Button from '@/components/elements/Button' ;
import { Dialog } from '@/components/elements/dialog' ;
import Tooltip from '@/components/elements/tooltip/Tooltip' ;
import { Alert , type AlertType } from '@/components/elements/alert' ;
import { isObject } from '@/lib/objects' ;
import { canReinstallServer , SKIPPED_INSTALL_SCRIPT_MESSAGE } from '@/lib/serverStatus' ;
interface Props {
server : AdminServer ;
}
const descriptionClass = 'text-sm leading-relaxed text-card-foreground' ;
const actionButtonClass =
'whitespace-nowrap disabled:border-border disabled:bg-popover disabled:text-muted-foreground disabled:opacity-100' ;
type CardAlert = { type : AlertType ; title : string ; message : string };
const ManageActionCard = ({
title ,
alert ,
description ,
action ,
} : {
title : string ;
alert? : CardAlert ;
description : React.ReactNode ;
action : React.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' >
2026-10-05 18:10:18 -04:00
{ alert && (
< Alert type = { alert . type }>
2026-10-08 16:53:20 -04:00
< strong className = 'font-semibold' >{ alert . title }</ strong > { alert . message }
2026-10-05 18:10:18 -04:00
</ Alert >
)}
< p className = { descriptionClass }>{ description }</ p >
</ 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 >
);
// The span is what receives the hover: a disabled button does not emit pointer events.
const ActionTrigger = ({ reason , children } : { reason? : string ; children : React.ReactElement }) => (
< Tooltip content = { reason } disabled = { ! reason }>
2026-10-08 16:53:20 -04:00
< span className = 'inline-flex' >{ children }</ span >
2026-10-05 18:10:18 -04:00
</ Tooltip >
);
2026-10-08 16:53:20 -04:00
const reinstallBlockedReasonFor = ( attributes : AdminServer [ 'attributes' ]) : string | undefined => {
if ( attributes . container . installed !== 1 ) {
return 'This action is disabled until the server is installed.' ;
}
if ( canReinstallServer ( attributes . status , attributes . container . skip_scripts )) {
return undefined ;
}
return SKIPPED_INSTALL_SCRIPT_MESSAGE ;
};
2026-10-05 18:10:18 -04:00
function ServerManageContent ({ server } : Props ) {
const { attributes } = server ;
const reinstallServer = useReinstallAdminServer ();
const toggleInstallState = useToggleAdminServerInstallState ();
const updateSuspension = useUpdateAdminServerSuspension ();
const { data : transfer } = useAdminServerTransferProgress ( attributes . id );
const { data : nodes } = useAdminNodes ({ page : 1 });
const mutationOptions = ( close : () => void ) => ({ onSettled : close });
const loading = reinstallServer . isPending || toggleInstallState . isPending || updateSuspension . isPending ;
const activeTransfer = isObject ( transfer );
const canTransfer = ( nodes ? . meta . pagination . total ?? 0 ) > 1 ;
2026-10-08 16:53:20 -04:00
const reinstallBlockedReason = reinstallBlockedReasonFor ( attributes );
2026-10-05 18:10:18 -04:00
if ( attributes . status === 'install_failed' ) {
return (
2026-10-08 16:53:20 -04:00
< TitledGreyBox title = 'Server Cannot Be Managed' >
2026-10-05 18:10:18 -04:00
< p className = { descriptionClass }>
This server is in a failed install state and cannot be recovered . Delete and re - create the server .
</ p >
</ TitledGreyBox >
);
}
return (
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
< ManageActionCard
2026-10-08 16:53:20 -04:00
title = 'Reinstall Server'
2026-10-05 18:10:18 -04:00
alert = {{ type : 'danger' , title : 'Danger!' , message : 'This could overwrite server data.' }}
2026-10-08 16:53:20 -04:00
description = 'This will reinstall the server with the assigned service scripts.'
2026-10-05 18:10:18 -04:00
action = {
< Dialog.ConfirmTrigger
2026-10-08 16:53:20 -04:00
title = 'Reinstall Server'
confirm = 'Reinstall'
2026-10-05 18:10:18 -04:00
pending = { reinstallServer . isPending }
trigger = {({ onClick }) => (
< ActionTrigger reason = { reinstallBlockedReason }>
< Button
2026-10-08 16:53:20 -04:00
color = 'red'
2026-10-05 18:10:18 -04:00
className = { actionButtonClass }
disabled = { loading || reinstallBlockedReason !== undefined }
onClick = { onClick }
>
Reinstall Server
</ Button >
</ ActionTrigger >
)}
onConfirmed = {( _event , close ) =>
reinstallServer . mutate ( reinstallAdminServerInput ( attributes . id ), mutationOptions ( close ))
}
>
This will reinstall the server with its assigned service scripts .{ ' ' }
< strong > This could overwrite server data .</ strong >
</ Dialog.ConfirmTrigger >
}
/>
< ManageActionCard
2026-10-08 16:53:20 -04:00
title = 'Install Status'
description = 'If you need to change the install status from uninstalled to installed, or vice versa, you may do so with the button below.'
2026-10-05 18:10:18 -04:00
action = {
< Dialog.ConfirmTrigger
2026-10-08 16:53:20 -04:00
title = 'Toggle Install Status'
confirm = 'Toggle'
2026-10-05 18:10:18 -04:00
pending = { toggleInstallState . isPending }
trigger = {({ onClick }) => (
< Button className = { actionButtonClass } disabled = { loading } onClick = { onClick }>
Toggle Install Status
</ Button >
)}
onConfirmed = {( _event , close ) =>
toggleInstallState . mutate (
toggleAdminServerInstallStateInput ( attributes . id ),
mutationOptions ( close )
)
}
>
This will flip the install status between installed and installing for this server .
</ Dialog.ConfirmTrigger >
}
/>
{ attributes . suspended ? (
< ManageActionCard
2026-10-08 16:53:20 -04:00
title = 'Unsuspend Server'
description = 'This will unsuspend the server and restore normal user access.'
2026-10-05 18:10:18 -04:00
action = {
< Dialog.ConfirmTrigger
2026-10-08 16:53:20 -04:00
title = 'Unsuspend Server'
confirm = 'Unsuspend'
2026-10-05 18:10:18 -04:00
pending = { updateSuspension . isPending }
trigger = {({ onClick }) => (
< Button
2026-10-08 16:53:20 -04:00
color = 'green'
2026-10-05 18:10:18 -04:00
className = { actionButtonClass }
disabled = { loading }
onClick = { onClick }
>
Unsuspend Server
</ Button >
)}
onConfirmed = {( _event , close ) =>
updateSuspension . mutate (
updateAdminServerSuspensionInput ( attributes . id , false ),
mutationOptions ( close )
)
}
>
This will unsuspend the server and restore normal user access .
</ Dialog.ConfirmTrigger >
}
/>
) : (
< ManageActionCard
2026-10-08 16:53:20 -04:00
title = 'Suspend Server'
description = 'This will suspend the server, stop any running processes, and immediately block the user from accessing their files or managing the server.'
2026-10-05 18:10:18 -04:00
action = {
< Dialog.ConfirmTrigger
2026-10-08 16:53:20 -04:00
title = 'Suspend Server'
confirm = 'Suspend'
2026-10-05 18:10:18 -04:00
pending = { updateSuspension . isPending }
trigger = {({ onClick }) => (
< ActionTrigger
reason = {
activeTransfer
? 'This server is being transferred and cannot be suspended.'
: undefined
}
>
< Button
2026-10-08 16:53:20 -04:00
color = 'orange'
2026-10-05 18:10:18 -04:00
className = { actionButtonClass }
disabled = { loading || activeTransfer }
onClick = { onClick }
>
Suspend Server
</ Button >
</ ActionTrigger >
)}
onConfirmed = {( _event , close ) =>
updateSuspension . mutate (
updateAdminServerSuspensionInput ( attributes . id , true ),
mutationOptions ( close )
)
}
>
This will suspend the server , stop any running processes , and block the user from accessing
their files or managing the server .
</ Dialog.ConfirmTrigger >
}
/>
)}
< ManageActionCard
2026-10-08 16:53:20 -04:00
title = 'Transfer Server'
2026-10-05 18:10:18 -04:00
alert = {
activeTransfer
? undefined
: { type : 'warning' , title : 'Warning!' , message : 'This feature may have bugs.' }
}
description = {
activeTransfer
? 'This server is currently being transferred to another node.'
: 'Transfer this server to another node connected to this panel.'
}
action = {
< Dialog.Trigger
trigger = {({ onClick }) => (
< ActionTrigger
reason = {
! activeTransfer && ! canTransfer
? 'Transferring a server requires more than one node to be configured on this panel.'
: undefined
}
>
< Button
2026-10-08 16:53:20 -04:00
color = 'green'
2026-10-05 18:10:18 -04:00
className = { actionButtonClass }
disabled = { loading || activeTransfer || ! canTransfer }
onClick = { onClick }
>
Transfer Server
</ Button >
</ ActionTrigger >
)}
>
{({ open , onClose }) => < TransferServerModal server = { server } open = { open } onClose = { onClose } />}
</ Dialog.Trigger >
}
/>
</ div >
);
}
export default function ServerManageTab() {
const { server } = useServerDetail ();
return < ServerManageContent server = { server } />;
}