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'; 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 validateCpu = requiredNumber('A CPU limit must be provided.', (value) => value >= 0 ? undefined : 'Invalid CPU limit.' ); const validateMemory = requiredNumber('A memory limit must be provided.', (value) => value >= 0 ? undefined : 'Invalid memory limit.' ); const validateSwap = requiredNumber('A swap limit must be provided.', (value) => value >= -1 ? undefined : 'Invalid swap limit.' ); const validateDisk = requiredNumber('A disk limit must be provided.', (value) => value >= 0 ? undefined : 'Invalid disk limit.' ); const validateIo = requiredNumber('An IO proportion must be provided.', (value): string | undefined => { if (value < 10) { return 'The IO proportion must be at least 10.'; } if (value > 1000) { return 'The IO proportion must not exceed 1000.'; } return undefined; }); const validateDatabaseLimit = requiredNumber('A database limit must be provided.', (value) => value >= 0 ? undefined : 'Invalid database limit.' ); const validateAllocationLimit = requiredNumber('An allocation limit must be provided.', (value) => value >= 0 ? undefined : 'Invalid allocation limit.' ); const validateBackupLimit = requiredNumber('A backup limit must be provided.', (value) => value >= 0 ? undefined : 'Invalid backup limit.' ); function ServerBuildForm({ server }: Props) { 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 (
{(field) => ( )}
{(field) => ( )}
{(field) => ( )}
{(field) => ( )}
{(field) => ( )}
{(field) => ( )}
{(field) => ( )}
{(field) => } {(field) => ( )} {(field) => }
{(field) => ( form.setFieldValue('removeAllocations', (current) => current.filter((id) => id !== Number(value)) ) } /> )}
{(field) => ( ({ value: allocation.attributes.id, label: allocationLabel(allocation), }))} /> )}
{(field) => ( )}
Update Build Configuration
); } export default function ServerBuildTab() { const { server } = useServerDetail(); if (server.attributes.container.installed !== 1) { return ; } return ; }