Files

208 lines
8.3 KiB
TypeScript
Raw Permalink Normal View History

2026-10-05 18:10:18 -04:00
import { useState } from 'react';
import { useStore } from '@tanstack/react-form';
import { toast } from 'sonner';
import { useAppForm, Form } from '@/components/form';
import Select, { type SelectGroup } from '@/components/ui/Select';
2026-10-12 03:14:51 -04:00
import { type LocationWithNodes, useAdminNodesGroupedByLocation, useAllAdminNodeTags } from '@/api/admin/nodes/queries';
2026-10-05 18:10:18 -04:00
import {
type AdminServer,
transferAdminServerInput,
2026-10-12 03:14:51 -04:00
useAdminEggForServer,
2026-10-05 18:10:18 -04:00
useAdminServerUnassignedAllocations,
useTransferAdminServer,
} from '@/api/admin/servers/queries';
2026-10-12 03:14:51 -04:00
import { relationshipData } from '@/api/relationships';
2026-10-05 18:10:18 -04:00
import { allocationLabel, transferServerBodyFromFormValues } from '@/components/admin/servers/helpers';
2026-10-12 03:14:51 -04:00
import TagCheckAlert from '@/components/admin/tags/TagCheckAlert';
import { nodeAcceptsEgg, tagCheckError } from '@/components/admin/tags/helpers';
2026-10-05 18:10:18 -04:00
import { Dialog } from '@/components/elements/dialog';
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
import Label from '@/components/elements/Label';
import Button from '@/components/elements/Button';
interface Props {
server: AdminServer;
open: boolean;
onClose: () => void;
}
interface Values {
allocationId: number;
allocationAdditional: number[];
}
2026-10-12 03:14:51 -04:00
/** Whether a node accepts the egg; every node does until the tags have loaded. */
const useNodeAcceptsEgg = (eggId: number, enabled: boolean) => {
const { data: nodes } = useAllAdminNodeTags({ enabled });
const { data: egg } = useAdminEggForServer(eggId, { enabled });
const eggTags = relationshipData(egg?.attributes.relationships?.tags);
const nodeTags = new Map(
(nodes?.data ?? []).map((node) => [node.attributes.id, relationshipData(node.attributes.relationships?.tags)])
);
return (nodeId: number) => !egg || nodeAcceptsEgg(nodeTags.get(nodeId) ?? [], eggTags);
};
const transferNodeGroups = (
groups: LocationWithNodes[],
currentNodeId: number,
acceptsEgg: (nodeId: number) => boolean
): SelectGroup[] =>
groups.flatMap(({ location, nodes }) => {
const options = nodes
.filter((node) => node.id !== currentNodeId)
.map((node) => ({
value: node.id,
label: acceptsEgg(node.id) ? node.name : `${node.name} (does not accept this egg)`,
}));
const { long, short } = location.attributes;
return options.length > 0 ? [{ label: long ? `${long} (${short})` : short, options }] : [];
});
2026-10-05 18:10:18 -04:00
export default function TransferServerModal({ server, open, onClose }: Props) {
const { attributes } = server;
const transferServer = useTransferAdminServer();
const [nodeId, setNodeId] = useState(0);
2026-10-12 03:14:51 -04:00
const [rejection, setRejection] = useState<string | null>(null);
2026-10-05 18:10:18 -04:00
const { data: groups = [] } = useAdminNodesGroupedByLocation({
enabled: open,
});
2026-10-12 03:14:51 -04:00
const acceptsEgg = useNodeAcceptsEgg(attributes.egg, open);
2026-10-05 18:10:18 -04:00
const { data: allocationsResponse } = useAdminServerUnassignedAllocations(nodeId, {
enabled: open && nodeId > 0,
});
const allocations = allocationsResponse?.data ?? [];
const form = useAppForm({
defaultValues: { allocationId: 0, allocationAdditional: [] } as Values,
2026-10-12 03:14:51 -04:00
onSubmitMeta: { ignoreTags: false },
onSubmit: async ({ value, meta }) => {
2026-10-05 18:10:18 -04:00
if (!nodeId) {
toast.error('A target node must be selected.');
2026-10-08 16:53:20 -04:00
2026-10-05 18:10:18 -04:00
return;
}
2026-10-08 16:53:20 -04:00
2026-10-05 18:10:18 -04:00
if (!value.allocationId) {
toast.error('A default allocation must be selected.');
2026-10-08 16:53:20 -04:00
2026-10-05 18:10:18 -04:00
return;
}
try {
await transferServer.mutateAsync(
2026-10-12 03:14:51 -04:00
transferAdminServerInput(attributes.id, {
...transferServerBodyFromFormValues({
2026-10-05 18:10:18 -04:00
nodeId: Number(nodeId),
allocationId: Number(value.allocationId),
allocationAdditional: value.allocationAdditional.map(Number),
2026-10-12 03:14:51 -04:00
}),
ignore_tags: meta.ignoreTags,
})
2026-10-05 18:10:18 -04:00
);
2026-10-12 03:14:51 -04:00
setRejection(null);
2026-10-05 18:10:18 -04:00
onClose();
2026-10-12 03:14:51 -04:00
} catch (error) {
setRejection(tagCheckError(error, 'node_id'));
2026-10-05 18:10:18 -04:00
}
},
});
const isSubmitting = useStore(form.store, (state) => state.isSubmitting);
const defaultAllocationId = useStore(form.store, (state) => state.values.allocationId);
const allocationOptions = allocations.map((allocation) => ({
value: allocation.attributes.id,
label: allocationLabel(allocation),
}));
const additionalOptions = allocationOptions.filter((option) => option.value !== defaultAllocationId);
return (
<Dialog
open={open}
2026-10-08 16:53:20 -04:00
title='Transfer server'
2026-10-05 18:10:18 -04:00
preventExternalClose={isSubmitting}
hideCloseIcon={isSubmitting}
onClose={onClose}
>
<SpinnerOverlay visible={isSubmitting} />
2026-10-08 16:53:20 -04:00
<Form form={form} className='m-0'>
<Label htmlFor='nodeId'>Node</Label>
2026-10-05 18:10:18 -04:00
<Select
2026-10-08 16:53:20 -04:00
id='nodeId'
2026-10-05 18:10:18 -04:00
value={nodeId}
2026-10-08 16:53:20 -04:00
placeholder='Select a target node...'
2026-10-12 03:14:51 -04:00
groups={transferNodeGroups(groups, attributes.node, acceptsEgg)}
2026-10-05 18:10:18 -04:00
onChange={(value) => {
setNodeId(Number(value));
2026-10-12 03:14:51 -04:00
setRejection(null);
2026-10-05 18:10:18 -04:00
form.setFieldValue('allocationId', 0);
form.setFieldValue('allocationAdditional', []);
}}
/>
2026-10-08 16:53:20 -04:00
<p className='mt-1 text-xs text-muted-foreground'>The node this server will be transferred to.</p>
<div className='mt-6'>
<form.AppField name='allocationId'>
2026-10-05 18:10:18 -04:00
{(field) => (
<field.SelectField
2026-10-08 16:53:20 -04:00
id='allocationId'
label='Default Allocation'
description='The main allocation that will be assigned to this server.'
2026-10-05 18:10:18 -04:00
options={[
{ value: 0, label: 'Select an allocation...', disabled: true },
...allocationOptions,
]}
onChange={(value) =>
form.setFieldValue('allocationAdditional', (current) =>
current.filter((id) => id !== Number(value))
)
}
/>
)}
</form.AppField>
</div>
2026-10-08 16:53:20 -04:00
<div className='mt-6'>
<form.AppField name='allocationAdditional'>
2026-10-05 18:10:18 -04:00
{(field) => (
<field.MultiSelectField
2026-10-08 16:53:20 -04:00
id='allocationAdditional'
label='Additional Allocation(s)'
description='Additional allocations to assign to this server.'
2026-10-05 18:10:18 -04:00
options={additionalOptions}
/>
)}
</form.AppField>
</div>
2026-10-12 03:14:51 -04:00
{rejection && (
<div className='mt-6'>
<TagCheckAlert
message={rejection}
action='Transfer anyway'
pending={isSubmitting}
onAction={() => void form.handleSubmit({ ignoreTags: true })}
/>
</div>
)}
2026-10-08 16:53:20 -04:00
<div className='flex flex-wrap justify-end mt-6'>
<Button type='button' isSecondary className='w-full sm:w-auto sm:mr-2' onClick={onClose}>
2026-10-05 18:10:18 -04:00
Cancel
</Button>
<Button
2026-10-08 16:53:20 -04:00
className='w-full mt-4 sm:w-auto sm:mt-0'
type='button'
2026-10-05 18:10:18 -04:00
disabled={isSubmitting}
onClick={() => form.handleSubmit()}
>
Confirm Transfer
</Button>
</div>
</Form>
</Dialog>
);
}