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';
|
2022-01-18 03:08:53 +00:00
|
|
|
import Switch from '@/components/elements/Switch';
|
2020-08-23 01:13:59 +00:00
|
|
|
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';
|
2022-01-18 03:08:53 +00:00
|
|
|
import Select from '@/components/elements/Select';
|
2020-08-26 04:25:31 +00:00
|
|
|
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 => ({
|
2020-12-13 19:07:29 +00:00
|
|
|
...data,
|
2020-08-26 04:25:31 +00:00
|
|
|
invocation,
|
2020-12-13 19:07:29 +00:00
|
|
|
variables: (data.variables || []).map(v => v.envVariable === response.envVariable ? response : v),
|
2020-08-26 04:25:31 +00:00
|
|
|
}), false))
|
2020-08-23 01:13:59 +00:00
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
clearAndAddHttpError({ error, key: FLASH_KEY });
|
|
|
|
})
|
|
|
|
.then(() => setLoading(false));
|
|
|
|
}, 500);
|
|
|
|
|
2022-01-18 03:08:53 +00:00
|
|
|
const useSwitch = variable.rules.some(v => v === 'boolean' || v === 'in:0,1');
|
|
|
|
const selectValues = variable.rules.find(v => v.startsWith('in:'))?.split(',') || [];
|
|
|
|
|
2020-08-23 01:13:59 +00:00
|
|
|
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}>
|
2022-01-18 03:08:53 +00:00
|
|
|
{useSwitch ?
|
|
|
|
<>
|
|
|
|
<Switch
|
|
|
|
readOnly={!canEdit || !variable.isEditable}
|
|
|
|
name={variable.envVariable}
|
|
|
|
defaultChecked={variable.serverValue === '1'}
|
|
|
|
onChange={() => {
|
|
|
|
if (canEdit && variable.isEditable) {
|
|
|
|
setVariableValue(variable.serverValue === '1' ? '0' : '1');
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
:
|
|
|
|
<>
|
|
|
|
{selectValues.length > 0 ?
|
|
|
|
<>
|
|
|
|
<Select
|
|
|
|
onChange={e => setVariableValue(e.target.value)}
|
|
|
|
name={variable.envVariable}
|
|
|
|
defaultValue={variable.serverValue}
|
|
|
|
disabled={!canEdit || !variable.isEditable}
|
|
|
|
>
|
|
|
|
{selectValues.map(selectValue => (
|
|
|
|
<option key={selectValue.replace('in:', '')} value={selectValue.replace('in:', '')}>{selectValue.replace('in:', '')}</option>
|
|
|
|
))}
|
|
|
|
</Select>
|
|
|
|
|
|
|
|
</>
|
|
|
|
:
|
|
|
|
<>
|
|
|
|
<Input
|
|
|
|
onKeyUp={e => {
|
|
|
|
if (canEdit && variable.isEditable) {
|
|
|
|
setVariableValue(e.currentTarget.value);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
readOnly={!canEdit || !variable.isEditable}
|
|
|
|
name={variable.envVariable}
|
|
|
|
defaultValue={variable.serverValue}
|
|
|
|
placeholder={variable.defaultValue}
|
|
|
|
/>
|
|
|
|
</>
|
2020-08-23 21:56:05 +00:00
|
|
|
}
|
2022-01-18 03:08:53 +00:00
|
|
|
</>
|
|
|
|
}
|
2020-08-23 01:13:59 +00:00
|
|
|
</InputSpinner>
|
2020-12-13 19:07:29 +00:00
|
|
|
<p css={tw`mt-1 text-xs text-neutral-300`}>
|
2020-08-23 01:13:59 +00:00
|
|
|
{variable.description}
|
|
|
|
</p>
|
|
|
|
</TitledGreyBox>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-08-26 04:25:31 +00:00
|
|
|
export default memo(VariableBox, isEqual);
|