import { useMemo } from 'react'; import { useStore } from '@tanstack/react-form'; import { useAppForm, Form } from '@/components/form'; import { type AdminServer, updateAdminServerBuildInput, useAdminServerBuildAllocations, useAdminServerUnassignedAllocations, useUpdateAdminServerBuild, } from '@/api/admin/servers/queries'; import { allocationLabel, serverBuildBodyFromFormValues, type ServerBuildValues, } from '@/components/admin/servers/helpers'; import { type NumberInputValue, requiredNumber, submittedNumber } from '@/components/admin/numberInput'; import { useServerDetail } from '@/components/admin/servers/useServerDetail'; import TitledGreyBox from '@/components/elements/TitledGreyBox'; import { ServerError } from '@/components/elements/ScreenBlock'; import { useTranslation, type Translation } from '@/sdk/localization'; interface Props { server: AdminServer; } interface Values { cpu: NumberInputValue; threads: string; memory: NumberInputValue; swap: NumberInputValue; disk: NumberInputValue; io: NumberInputValue; oomDisabled: string; databaseLimit: NumberInputValue; allocationLimit: NumberInputValue; backupLimit: NumberInputValue; allocationId: number; addAllocations: number[]; removeAllocations: number[]; } const serverToValues = (server: AdminServer): Values => ({ cpu: server.attributes.limits.cpu, threads: server.attributes.limits.threads ?? '', memory: server.attributes.limits.memory, swap: server.attributes.limits.swap, disk: server.attributes.limits.disk, io: server.attributes.limits.io, oomDisabled: server.attributes.limits.oom_disabled ? 'true' : 'false', databaseLimit: server.attributes.feature_limits.databases ?? 0, allocationLimit: server.attributes.feature_limits.allocations ?? 0, backupLimit: server.attributes.feature_limits.backups, allocationId: server.attributes.allocation, addAllocations: [], removeAllocations: [], }); const buildValidators = (t: Translation['t']) => ({ cpu: requiredNumber(t('A CPU limit must be provided.'), (value) => value >= 0 ? undefined : t('Invalid CPU limit.') ), memory: requiredNumber(t('A memory limit must be provided.'), (value) => value >= 0 ? undefined : t('Invalid memory limit.') ), swap: requiredNumber(t('A swap limit must be provided.'), (value) => value >= -1 ? undefined : t('Invalid swap limit.') ), disk: requiredNumber(t('A disk limit must be provided.'), (value) => value >= 0 ? undefined : t('Invalid disk limit.') ), io: requiredNumber(t('An IO proportion must be provided.'), (value): string | undefined => { if (value < 10) { return t('The IO proportion must be at least 10.'); } if (value > 1000) { return t('The IO proportion must not exceed 1000.'); } return undefined; }), databaseLimit: requiredNumber(t('A database limit must be provided.'), (value) => value >= 0 ? undefined : t('Invalid database limit.') ), allocationLimit: requiredNumber(t('An allocation limit must be provided.'), (value) => value >= 0 ? undefined : t('Invalid allocation limit.') ), backupLimit: requiredNumber(t('A backup limit must be provided.'), (value) => value >= 0 ? undefined : t('Invalid backup limit.') ), }); function ServerBuildForm({ server }: Props) { const { t } = useTranslation(); const validators = useMemo(() => buildValidators(t), [t]); const { attributes } = server; const updateServerBuild = useUpdateAdminServerBuild(); const { data: assignedResponse } = useAdminServerBuildAllocations(attributes.id, attributes.node); const assigned = assignedResponse?.data ?? []; const { data: unassignedResponse } = useAdminServerUnassignedAllocations(attributes.node); const unassigned = unassignedResponse?.data ?? []; const form = useAppForm({ defaultValues: serverToValues(server), onSubmit: async ({ value }) => { const payload: ServerBuildValues = { allocationId: Number(value.allocationId), addAllocations: value.addAllocations.map(Number), removeAllocations: value.removeAllocations.map(Number), oomDisabled: value.oomDisabled === 'true', limits: { cpu: submittedNumber(value.cpu), threads: value.threads, memory: submittedNumber(value.memory), swap: submittedNumber(value.swap), disk: submittedNumber(value.disk), io: submittedNumber(value.io), }, featureLimits: { databases: submittedNumber(value.databaseLimit), allocations: submittedNumber(value.allocationLimit), backups: submittedNumber(value.backupLimit), }, }; try { await updateServerBuild.mutateAsync( updateAdminServerBuildInput(attributes.id, serverBuildBodyFromFormValues(payload)) ); form.reset({ ...value, addAllocations: [], removeAllocations: [] }); } catch { // Error toast is handled by the mutation. } }, }); const defaultAllocationId = useStore(form.store, (state) => state.values.allocationId); const assignedOptions = assigned.map((allocation) => ({ value: allocation.attributes.id, label: allocationLabel(allocation), })); const removableOptions = assignedOptions.filter((option) => option.value !== defaultAllocationId); return (
); } export default function ServerBuildTab() { const { t } = useTranslation(); const { server } = useServerDetail(); if (server.attributes.container.installed !== 1) { return (