Files

213 lines
8.6 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';
2026-11-23 14:16:00 +00:00
import { useTranslation, type Translation } from '@/sdk/localization';
2026-10-05 18:10:18 -04:00
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,
2026-11-23 14:16:00 +00:00
acceptsEgg: (nodeId: number) => boolean,
t: Translation['t']
2026-10-12 03:14:51 -04:00
): SelectGroup[] =>
groups.flatMap(({ location, nodes }) => {
const options = nodes
.filter((node) => node.id !== currentNodeId)
.map((node) => ({
value: node.id,
2026-11-23 14:16:00 +00:00
label: acceptsEgg(node.id) ? node.name : t('{{name}} (does not accept this egg)', { name: node.name }),
2026-10-12 03:14:51 -04:00
}));
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) {
2026-11-23 14:16:00 +00:00
const { t } = useTranslation();
2026-10-05 18:10:18 -04:00
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) {
2026-11-23 14:16:00 +00:00
toast.error(t('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) {
2026-11-23 14:16:00 +00:00
toast.error(t('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-11-23 14:16:00 +00:00
title={t('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'>
2026-11-23 14:16:00 +00:00
<Label htmlFor='nodeId'>{t('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-11-23 14:16:00 +00:00
placeholder={t('Select a target node...')}
groups={transferNodeGroups(groups, attributes.node, acceptsEgg, t)}
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-11-23 14:16:00 +00:00
<p className='mt-1 text-xs text-muted-foreground'>
{t('The node this server will be transferred to.')}
</p>
2026-10-08 16:53:20 -04:00
<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'
2026-11-23 14:16:00 +00:00
label={t('Default Allocation')}
description={t('The main allocation that will be assigned to this server.')}
2026-10-05 18:10:18 -04:00
options={[
2026-11-23 14:16:00 +00:00
{ value: 0, label: t('Select an allocation...'), disabled: true },
2026-10-05 18:10:18 -04:00
...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'
2026-11-23 14:16:00 +00:00
label={t('Additional Allocation(s)')}
description={t('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}
2026-11-23 14:16:00 +00:00
action={t('Transfer anyway')}
2026-10-12 03:14:51 -04:00
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-11-23 14:16:00 +00:00
{t('Cancel')}
2026-10-05 18:10:18 -04:00
</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()}
>
2026-11-23 14:16:00 +00:00
{t('Confirm Transfer')}
2026-10-05 18:10:18 -04:00
</Button>
</div>
</Form>
</Dialog>
);
}