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;
}) => (
{alert && (
{alert.title} {alert.message}
)}
{description}
{action}
);
// The span is what receives the hover: a disabled button does not emit pointer events.
const ActionTrigger = ({ reason, children }: { reason?: string; children: React.ReactElement }) => (
{children}
);
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;
};
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;
const reinstallBlockedReason = reinstallBlockedReasonFor(attributes);
if (attributes.status === 'install_failed') {
return (
This server is in a failed install state and cannot be recovered. Delete and re-create the server.
);
}
return (
(
)}
onConfirmed={(_event, close) =>
reinstallServer.mutate(reinstallAdminServerInput(attributes.id), mutationOptions(close))
}
>
This will reinstall the server with its assigned service scripts.{' '}
This could overwrite server data.
}
/>
(
)}
onConfirmed={(_event, close) =>
toggleInstallState.mutate(
toggleAdminServerInstallStateInput(attributes.id),
mutationOptions(close)
)
}
>
This will flip the install status between installed and installing for this server.
}
/>
{attributes.suspended ? (
(
)}
onConfirmed={(_event, close) =>
updateSuspension.mutate(
updateAdminServerSuspensionInput(attributes.id, false),
mutationOptions(close)
)
}
>
This will unsuspend the server and restore normal user access.
}
/>
) : (
(
)}
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.
}
/>
)}
(
)}
>
{({ open, onClose }) => }
}
/>
);
}
export default function ServerManageTab() {
const { server } = useServerDetail();
return ;
}