2022-11-25 20:25:03 +00:00
|
|
|
import { 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}`;
|
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
|
2022-06-26 19:13:52 +00:00
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
const [canEdit] = usePermissions(['startup.update']);
|
2020-08-23 01:13:59 +00:00
|
|
|
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)
|
2022-06-26 19:13:52 +00:00
|
|
|
.then(([response, invocation]) =>
|
|
|
|
mutate(
|
2022-11-25 20:25:03 +00:00
|
|
|
data => ({
|
|
|
|
...data!,
|
2022-06-26 19:13:52 +00:00
|
|
|
invocation,
|
2022-11-25 20:25:03 +00:00
|
|
|
variables: (data!.variables || []).map(v =>
|
|
|
|
v.envVariable === response.envVariable ? response : v,
|
2022-06-26 19:13:52 +00:00
|
|
|
),
|
|
|
|
}),
|
2022-11-25 20:25:03 +00:00
|
|
|
false,
|
|
|
|
),
|
2022-06-26 19:13:52 +00:00
|
|
|
)
|
2022-11-25 20:25:03 +00:00
|
|
|
.catch(error => {
|
2020-08-23 01:13:59 +00:00
|
|
|
console.error(error);
|
2022-11-25 20:25:03 +00:00
|
|
|
clearAndAddHttpError({ key: FLASH_KEY, error });
|
2020-08-23 01:13:59 +00:00
|
|
|
})
|
|
|
|
.then(() => setLoading(false));
|
|
|
|
}, 500);
|
|
|
|
|
2022-09-25 19:18:13 +00:00
|
|
|
const useSwitch = variable.rules.some(
|
2022-11-25 20:25:03 +00:00
|
|
|
v => v === 'boolean' || v === 'in:0,1' || v === 'in:1,0' || v === 'in:true,false' || v === 'in:false,true',
|
2022-09-25 19:18:13 +00:00
|
|
|
);
|
2022-11-25 20:25:03 +00:00
|
|
|
const isStringSwitch = variable.rules.some(v => v === 'string');
|
|
|
|
const selectValues = variable.rules.find(v => v.startsWith('in:'))?.split(',') || [];
|
2022-01-18 03:08:53 +00:00
|
|
|
|
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`}>
|
2022-06-26 19:13:52 +00:00
|
|
|
{!variable.isEditable && (
|
|
|
|
<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>
|
|
|
|
}
|
|
|
|
>
|
2022-06-26 19:13:52 +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-06-26 19:13:52 +00:00
|
|
|
{useSwitch ? (
|
2022-01-18 03:08:53 +00:00
|
|
|
<>
|
|
|
|
<Switch
|
|
|
|
readOnly={!canEdit || !variable.isEditable}
|
|
|
|
name={variable.envVariable}
|
2022-09-25 19:18:13 +00:00
|
|
|
defaultChecked={
|
|
|
|
isStringSwitch ? variable.serverValue === 'true' : variable.serverValue === '1'
|
|
|
|
}
|
2022-01-18 03:08:53 +00:00
|
|
|
onChange={() => {
|
|
|
|
if (canEdit && variable.isEditable) {
|
2022-09-25 19:18:13 +00:00
|
|
|
if (isStringSwitch) {
|
|
|
|
setVariableValue(variable.serverValue === 'true' ? 'false' : 'true');
|
|
|
|
} else {
|
|
|
|
setVariableValue(variable.serverValue === '1' ? '0' : '1');
|
|
|
|
}
|
2022-01-18 03:08:53 +00:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</>
|
2022-06-26 19:13:52 +00:00
|
|
|
) : (
|
2022-01-18 03:08:53 +00:00
|
|
|
<>
|
2022-06-26 19:13:52 +00:00
|
|
|
{selectValues.length > 0 ? (
|
2022-01-18 03:08:53 +00:00
|
|
|
<>
|
|
|
|
<Select
|
2022-11-25 20:25:03 +00:00
|
|
|
onChange={e => setVariableValue(e.target.value)}
|
2022-01-18 03:08:53 +00:00
|
|
|
name={variable.envVariable}
|
|
|
|
defaultValue={variable.serverValue}
|
|
|
|
disabled={!canEdit || !variable.isEditable}
|
|
|
|
>
|
2022-11-25 20:25:03 +00:00
|
|
|
{selectValues.map(selectValue => (
|
2022-06-26 19:13:52 +00:00
|
|
|
<option
|
|
|
|
key={selectValue.replace('in:', '')}
|
|
|
|
value={selectValue.replace('in:', '')}
|
|
|
|
>
|
|
|
|
{selectValue.replace('in:', '')}
|
|
|
|
</option>
|
2022-01-18 03:08:53 +00:00
|
|
|
))}
|
|
|
|
</Select>
|
|
|
|
</>
|
2022-06-26 19:13:52 +00:00
|
|
|
) : (
|
2022-01-18 03:08:53 +00:00
|
|
|
<>
|
|
|
|
<Input
|
2022-11-25 20:25:03 +00:00
|
|
|
onKeyUp={e => {
|
2022-01-18 03:08:53 +00:00
|
|
|
if (canEdit && variable.isEditable) {
|
|
|
|
setVariableValue(e.currentTarget.value);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
readOnly={!canEdit || !variable.isEditable}
|
|
|
|
name={variable.envVariable}
|
|
|
|
defaultValue={variable.serverValue}
|
|
|
|
placeholder={variable.defaultValue}
|
|
|
|
/>
|
|
|
|
</>
|
2022-06-26 19:13:52 +00:00
|
|
|
)}
|
2022-01-18 03:08:53 +00:00
|
|
|
</>
|
2022-06-26 19:13:52 +00:00
|
|
|
)}
|
2020-08-23 01:13:59 +00:00
|
|
|
</InputSpinner>
|
2022-06-26 19:13:52 +00:00
|
|
|
<p css={tw`mt-1 text-xs text-neutral-300`}>{variable.description}</p>
|
2020-08-23 01:13:59 +00:00
|
|
|
</TitledGreyBox>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-08-26 04:25:31 +00:00
|
|
|
export default memo(VariableBox, isEqual);
|