2022-12-15 19:06:14 -07:00
|
|
|
import type { Actions } from 'easy-peasy';
|
|
|
|
import { useStoreActions } from 'easy-peasy';
|
|
|
|
import type { FormikHelpers } from 'formik';
|
|
|
|
import { Form, Formik, useField, useFormikContext } from 'formik';
|
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
import { object } from 'yup';
|
|
|
|
|
2023-10-02 16:56:48 -06:00
|
|
|
import type { Egg, EggVariable, LoadedEgg } from '@/api/admin/egg';
|
2022-12-15 19:06:14 -07:00
|
|
|
import { getEgg } from '@/api/admin/egg';
|
|
|
|
import type { Server } from '@/api/admin/server';
|
|
|
|
import { useServerFromRoute } from '@/api/admin/server';
|
|
|
|
import type { Values } from '@/api/admin/servers/updateServerStartup';
|
|
|
|
import updateServerStartup from '@/api/admin/servers/updateServerStartup';
|
|
|
|
import EggSelect from '@/components/admin/servers/EggSelect';
|
|
|
|
import NestSelector from '@/components/admin/servers/NestSelector';
|
|
|
|
import FormikSwitch from '@/components/elements/FormikSwitch';
|
|
|
|
import Button from '@/components/elements/Button';
|
|
|
|
import Input from '@/components/elements/Input';
|
|
|
|
import AdminBox from '@/components/admin/AdminBox';
|
|
|
|
import Field from '@/components/elements/Field';
|
|
|
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
|
|
|
import Label from '@/components/elements/Label';
|
|
|
|
import type { ApplicationStore } from '@/state';
|
2023-10-02 16:56:48 -06:00
|
|
|
import { WithRelationships } from '@/api/admin';
|
2022-12-15 19:06:14 -07:00
|
|
|
|
2023-09-29 16:33:15 -06:00
|
|
|
function ServerStartupLineContainer({ egg, server }: { egg?: Egg; server: Server }) {
|
2022-12-15 19:06:14 -07:00
|
|
|
const { isSubmitting, setFieldValue } = useFormikContext();
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-09-29 16:33:15 -06:00
|
|
|
if (egg === undefined) {
|
2022-12-15 19:06:14 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (server.eggId === egg.id) {
|
|
|
|
setFieldValue('image', server.container.image);
|
|
|
|
setFieldValue('startup', server.container.startup || '');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Whenever the egg is changed, set the server's startup command to the egg's default.
|
2023-01-12 12:25:58 -07:00
|
|
|
setFieldValue('image', Object.values(egg.dockerImages)[0] ?? '');
|
2022-12-15 19:06:14 -07:00
|
|
|
setFieldValue('startup', '');
|
|
|
|
}, [egg]);
|
|
|
|
|
|
|
|
return (
|
2023-09-29 16:33:15 -06:00
|
|
|
<AdminBox title={'Startup Command'} className="relative w-full">
|
2022-12-15 19:06:14 -07:00
|
|
|
<SpinnerOverlay visible={isSubmitting} />
|
|
|
|
|
2023-09-29 16:33:15 -06:00
|
|
|
<div className="mb-6">
|
2022-12-15 19:06:14 -07:00
|
|
|
<Field
|
|
|
|
id={'startup'}
|
|
|
|
name={'startup'}
|
|
|
|
label={'Startup Command'}
|
|
|
|
type={'text'}
|
|
|
|
description={
|
|
|
|
"Edit your server's startup command here. The following variables are available by default: {{SERVER_MEMORY}}, {{SERVER_IP}}, and {{SERVER_PORT}}."
|
|
|
|
}
|
|
|
|
placeholder={egg?.startup || ''}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div>
|
|
|
|
<Label>Default Startup Command</Label>
|
|
|
|
<Input value={egg?.startup || ''} readOnly />
|
|
|
|
</div>
|
|
|
|
</AdminBox>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function ServerServiceContainer({
|
2023-09-29 16:33:15 -06:00
|
|
|
selectedEggId,
|
2022-12-15 19:06:14 -07:00
|
|
|
setEgg,
|
|
|
|
nestId: _nestId,
|
|
|
|
}: {
|
2023-09-29 16:33:15 -06:00
|
|
|
selectedEggId?: number;
|
|
|
|
setEgg: (value: WithRelationships<Egg, 'variables'> | undefined) => void;
|
2022-12-15 19:06:14 -07:00
|
|
|
nestId: number;
|
|
|
|
}) {
|
|
|
|
const { isSubmitting } = useFormikContext();
|
|
|
|
|
|
|
|
const [nestId, setNestId] = useState<number>(_nestId);
|
|
|
|
|
|
|
|
return (
|
2023-09-28 13:48:21 -06:00
|
|
|
<AdminBox title={'Service Configuration'} isLoading={isSubmitting} className="w-full">
|
|
|
|
<div className="mb-6">
|
2022-12-15 19:06:14 -07:00
|
|
|
<NestSelector selectedNestId={nestId} onNestSelect={setNestId} />
|
|
|
|
</div>
|
2023-09-28 13:48:21 -06:00
|
|
|
<div className="mb-6">
|
2023-09-29 16:33:15 -06:00
|
|
|
<EggSelect nestId={nestId} selectedEggId={selectedEggId} onEggSelect={setEgg} />
|
2022-12-15 19:06:14 -07:00
|
|
|
</div>
|
2023-09-28 13:48:21 -06:00
|
|
|
<div className="bg-neutral-800 border border-neutral-900 shadow-inner p-4 rounded">
|
2022-12-15 19:06:14 -07:00
|
|
|
<FormikSwitch name={'skipScripts'} label={'Skip Egg Install Script'} description={'Soon™'} />
|
|
|
|
</div>
|
|
|
|
</AdminBox>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function ServerImageContainer() {
|
|
|
|
const { isSubmitting } = useFormikContext();
|
|
|
|
|
|
|
|
return (
|
2023-09-29 16:33:15 -06:00
|
|
|
<AdminBox title={'Image Configuration'} className="relative w-full">
|
2022-12-15 19:06:14 -07:00
|
|
|
<SpinnerOverlay visible={isSubmitting} />
|
|
|
|
|
2023-09-29 16:33:15 -06:00
|
|
|
<div className="md:w-full md:flex md:flex-col">
|
2022-12-15 19:06:14 -07:00
|
|
|
<div>
|
2023-01-12 12:25:58 -07:00
|
|
|
{/* TODO: make this a proper select but allow a custom image to be specified if needed. */}
|
2022-12-15 19:06:14 -07:00
|
|
|
<Field id={'image'} name={'image'} label={'Docker Image'} type={'text'} />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</AdminBox>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function ServerVariableContainer({ variable, value }: { variable: EggVariable; value?: string }) {
|
|
|
|
const key = 'environment.' + variable.environmentVariable;
|
|
|
|
|
|
|
|
const [, , { setValue, setTouched }] = useField<string | undefined>(key);
|
|
|
|
|
|
|
|
const { isSubmitting } = useFormikContext();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (value === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setValue(value);
|
|
|
|
setTouched(true);
|
|
|
|
}, [value]);
|
|
|
|
|
|
|
|
return (
|
2023-09-29 16:33:15 -06:00
|
|
|
<AdminBox className="relative w-full" title={<p className="text-sm uppercase">{variable.name}</p>}>
|
2022-12-15 19:06:14 -07:00
|
|
|
<SpinnerOverlay visible={isSubmitting} />
|
|
|
|
|
|
|
|
<Field
|
|
|
|
id={key}
|
|
|
|
name={key}
|
|
|
|
type={'text'}
|
|
|
|
placeholder={variable.defaultValue}
|
|
|
|
description={variable.description}
|
|
|
|
/>
|
|
|
|
</AdminBox>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function ServerStartupForm({
|
2023-09-29 16:33:15 -06:00
|
|
|
selectedEggId,
|
2022-12-15 19:06:14 -07:00
|
|
|
egg,
|
|
|
|
setEgg,
|
|
|
|
server,
|
|
|
|
}: {
|
2023-09-29 16:33:15 -06:00
|
|
|
selectedEggId?: number;
|
|
|
|
egg?: LoadedEgg;
|
|
|
|
setEgg: (value: LoadedEgg | undefined) => void;
|
2022-12-15 19:06:14 -07:00
|
|
|
server: Server;
|
|
|
|
}) {
|
|
|
|
const {
|
|
|
|
isSubmitting,
|
|
|
|
isValid,
|
|
|
|
values: { environment },
|
|
|
|
} = useFormikContext<Values>();
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Form>
|
2023-09-29 16:33:15 -06:00
|
|
|
<div className="flex flex-col mb-16">
|
|
|
|
<div className="flex flex-row mb-6">
|
2022-12-15 19:06:14 -07:00
|
|
|
<ServerStartupLineContainer egg={egg} server={server} />
|
|
|
|
</div>
|
|
|
|
|
2023-09-29 16:33:15 -06:00
|
|
|
<div className="grid grid-cols-1 md:grid-cols-2 gap-x-8 gap-y-6 mb-6">
|
|
|
|
<div className="flex">
|
|
|
|
<ServerServiceContainer selectedEggId={selectedEggId} setEgg={setEgg} nestId={server.nestId} />
|
2022-12-15 19:06:14 -07:00
|
|
|
</div>
|
|
|
|
|
2023-09-29 16:33:15 -06:00
|
|
|
<div className="flex">
|
2022-12-15 19:06:14 -07:00
|
|
|
<ServerImageContainer />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2023-09-29 16:33:15 -06:00
|
|
|
<div className="grid grid-cols-1 md:grid-cols-2 gap-y-6 gap-x-8">
|
2022-12-15 19:06:14 -07:00
|
|
|
{/* This ensures that no variables are rendered unless the environment has a value for the variable. */}
|
|
|
|
{egg?.relationships.variables
|
|
|
|
?.filter(v => Object.keys(environment).find(e => e === v.environmentVariable) !== undefined)
|
|
|
|
.map((v, i) => (
|
|
|
|
<ServerVariableContainer
|
|
|
|
key={i}
|
|
|
|
variable={v}
|
|
|
|
value={
|
|
|
|
server.relationships.variables?.find(
|
|
|
|
v2 => v.eggId === v2.eggId && v.environmentVariable === v2.environmentVariable,
|
|
|
|
)?.serverValue
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
|
2023-09-29 16:33:15 -06:00
|
|
|
<div className="bg-neutral-700 rounded shadow-md py-2 pr-6 mt-6">
|
|
|
|
<div className="flex flex-row">
|
|
|
|
<Button type="submit" size="small" className="ml-auto" disabled={isSubmitting || !isValid}>
|
2022-12-15 19:06:14 -07:00
|
|
|
Save Changes
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default () => {
|
|
|
|
const { data: server } = useServerFromRoute();
|
|
|
|
const { clearFlashes, clearAndAddHttpError } = useStoreActions(
|
|
|
|
(actions: Actions<ApplicationStore>) => actions.flashes,
|
|
|
|
);
|
2023-09-29 16:33:15 -06:00
|
|
|
const [egg, setEgg] = useState<LoadedEgg | undefined>(undefined);
|
2022-12-15 19:06:14 -07:00
|
|
|
|
|
|
|
useEffect(() => {
|
2023-09-29 16:33:15 -06:00
|
|
|
if (!server) {
|
|
|
|
return;
|
|
|
|
}
|
2022-12-15 19:06:14 -07:00
|
|
|
|
|
|
|
getEgg(server.eggId)
|
|
|
|
.then(egg => setEgg(egg))
|
|
|
|
.catch(error => console.error(error));
|
|
|
|
}, [server?.eggId]);
|
|
|
|
|
|
|
|
if (!server) return null;
|
|
|
|
|
|
|
|
const submit = (values: Values, { setSubmitting }: FormikHelpers<Values>) => {
|
|
|
|
clearFlashes('server');
|
|
|
|
|
|
|
|
updateServerStartup(server.id, values)
|
|
|
|
// .then(s => {
|
|
|
|
// mutate(data => { ...data, ...s });
|
|
|
|
// })
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
clearAndAddHttpError({ key: 'server', error });
|
|
|
|
})
|
|
|
|
.then(() => setSubmitting(false));
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Formik
|
|
|
|
onSubmit={submit}
|
|
|
|
initialValues={{
|
|
|
|
startup: server.container.startup || '',
|
|
|
|
environment: [] as Record<string, any>,
|
|
|
|
image: server.container.image,
|
|
|
|
eggId: server.eggId,
|
|
|
|
skipScripts: false,
|
|
|
|
}}
|
|
|
|
validationSchema={object().shape({})}
|
|
|
|
>
|
|
|
|
<ServerStartupForm
|
2023-09-29 16:33:15 -06:00
|
|
|
selectedEggId={egg?.id ?? server.eggId}
|
2022-12-15 19:06:14 -07:00
|
|
|
egg={egg}
|
|
|
|
setEgg={setEgg}
|
2023-09-29 16:33:15 -06:00
|
|
|
server={server as Server}
|
2022-12-15 19:06:14 -07:00
|
|
|
/>
|
|
|
|
</Formik>
|
|
|
|
);
|
|
|
|
};
|