2020-08-26 04:25:31 +00:00
|
|
|
import React, { memo, useState } from 'react';
|
2020-08-23 01:13:59 +00:00
|
|
|
import { ServerEggVariable } from '@/api/server/types';
|
|
|
|
import TitledGreyBox from '@/components/elements/TitledGreyBox';
|
|
|
|
import { usePermissions } from '@/plugins/usePermissions';
|
|
|
|
import InputSpinner from '@/components/elements/InputSpinner';
|
|
|
|
import Input from '@/components/elements/Input';
|
|
|
|
import tw from 'twin.macro';
|
|
|
|
import { debounce } from 'debounce';
|
|
|
|
import updateStartupVariable from '@/api/server/updateStartupVariable';
|
|
|
|
import useFlash from '@/plugins/useFlash';
|
|
|
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
2020-08-26 04:25:31 +00:00
|
|
|
import getServerStartup from '@/api/swr/getServerStartup';
|
|
|
|
import isEqual from 'react-fast-compare';
|
|
|
|
import { ServerContext } from '@/state/server';
|
2020-08-23 01:13:59 +00:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
variable: ServerEggVariable;
|
|
|
|
}
|
|
|
|
|
|
|
|
const VariableBox = ({ variable }: Props) => {
|
|
|
|
const FLASH_KEY = `server:startup:${variable.envVariable}`;
|
|
|
|
|
2020-08-26 04:25:31 +00:00
|
|
|
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
|
2020-08-23 01:13:59 +00:00
|
|
|
const [ loading, setLoading ] = useState(false);
|
|
|
|
const [ canEdit ] = usePermissions([ 'startup.update' ]);
|
|
|
|
const { clearFlashes, clearAndAddHttpError } = useFlash();
|
2020-08-26 04:25:31 +00:00
|
|
|
const { mutate } = getServerStartup(uuid);
|
2020-08-23 01:13:59 +00:00
|
|
|
|
|
|
|
const setVariableValue = debounce((value: string) => {
|
|
|
|
setLoading(true);
|
|
|
|
clearFlashes(FLASH_KEY);
|
|
|
|
|
2020-08-26 04:25:31 +00:00
|
|
|
updateStartupVariable(uuid, variable.envVariable, value)
|
|
|
|
.then(([ response, invocation ]) => mutate(data => ({
|
|
|
|
invocation,
|
|
|
|
variables: data.variables.map(v => v.envVariable === response.envVariable ? response : v),
|
|
|
|
}), false))
|
2020-08-23 01:13:59 +00:00
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
clearAndAddHttpError({ error, key: FLASH_KEY });
|
|
|
|
})
|
|
|
|
.then(() => setLoading(false));
|
|
|
|
}, 500);
|
|
|
|
|
|
|
|
return (
|
2020-08-23 21:56:05 +00:00
|
|
|
<TitledGreyBox
|
|
|
|
title={
|
|
|
|
<p css={tw`text-sm uppercase`}>
|
|
|
|
{!variable.isEditable &&
|
2020-09-13 17:02:25 +00:00
|
|
|
<span css={tw`bg-neutral-700 text-xs py-1 px-2 rounded-full mr-2 mb-1`}>Read Only</span>
|
2020-08-23 21:56:05 +00:00
|
|
|
}
|
|
|
|
{variable.name}
|
|
|
|
</p>
|
|
|
|
}
|
|
|
|
>
|
2020-09-13 17:02:25 +00:00
|
|
|
<FlashMessageRender byKey={FLASH_KEY} css={tw`mb-2 md:mb-4`}/>
|
2020-08-23 01:13:59 +00:00
|
|
|
<InputSpinner visible={loading}>
|
|
|
|
<Input
|
2020-08-23 21:56:05 +00:00
|
|
|
onKeyUp={e => {
|
|
|
|
if (canEdit && variable.isEditable) {
|
|
|
|
setVariableValue(e.currentTarget.value);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
readOnly={!canEdit || !variable.isEditable}
|
2020-08-23 01:13:59 +00:00
|
|
|
name={variable.envVariable}
|
|
|
|
defaultValue={variable.serverValue}
|
|
|
|
placeholder={variable.defaultValue}
|
|
|
|
/>
|
|
|
|
</InputSpinner>
|
|
|
|
<p css={tw`mt-1 text-xs text-neutral-400`}>
|
|
|
|
{variable.description}
|
|
|
|
</p>
|
|
|
|
</TitledGreyBox>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-08-26 04:25:31 +00:00
|
|
|
export default memo(VariableBox, isEqual);
|