Skip to content

Commit 7d0d71b

Browse files
authored
Checkbox & Dropdown for Startup Variables (pterodactyl#3769)
1 parent e7884e4 commit 7d0d71b

File tree

2 files changed

+53
-11
lines changed

2 files changed

+53
-11
lines changed

resources/scripts/components/elements/Switch.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,12 @@ export interface SwitchProps {
4141
label?: string;
4242
description?: string;
4343
defaultChecked?: boolean;
44+
readOnly?: boolean;
4445
onChange?: (e: React.ChangeEvent<HTMLInputElement>) => void;
4546
children?: React.ReactNode;
4647
}
4748

48-
const Switch = ({ name, label, description, defaultChecked, onChange, children }: SwitchProps) => {
49+
const Switch = ({ name, label, description, defaultChecked, readOnly, onChange, children }: SwitchProps) => {
4950
const uuid = useMemo(() => v4(), []);
5051

5152
return (
@@ -58,6 +59,7 @@ const Switch = ({ name, label, description, defaultChecked, onChange, children }
5859
type={'checkbox'}
5960
onChange={e => onChange && onChange(e)}
6061
defaultChecked={defaultChecked}
62+
disabled={readOnly}
6163
/>
6264
}
6365
<Label htmlFor={uuid}/>

resources/scripts/components/server/startup/VariableBox.tsx

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import TitledGreyBox from '@/components/elements/TitledGreyBox';
44
import { usePermissions } from '@/plugins/usePermissions';
55
import InputSpinner from '@/components/elements/InputSpinner';
66
import Input from '@/components/elements/Input';
7+
import Switch from '@/components/elements/Switch';
78
import tw from 'twin.macro';
89
import { debounce } from 'debounce';
910
import updateStartupVariable from '@/api/server/updateStartupVariable';
1011
import useFlash from '@/plugins/useFlash';
1112
import FlashMessageRender from '@/components/FlashMessageRender';
1213
import getServerStartup from '@/api/swr/getServerStartup';
14+
import Select from '@/components/elements/Select';
1315
import isEqual from 'react-fast-compare';
1416
import { ServerContext } from '@/state/server';
1517

@@ -43,6 +45,9 @@ const VariableBox = ({ variable }: Props) => {
4345
.then(() => setLoading(false));
4446
}, 500);
4547

48+
const useSwitch = variable.rules.some(v => v === 'boolean' || v === 'in:0,1');
49+
const selectValues = variable.rules.find(v => v.startsWith('in:'))?.split(',') || [];
50+
4651
return (
4752
<TitledGreyBox
4853
title={
@@ -56,17 +61,52 @@ const VariableBox = ({ variable }: Props) => {
5661
>
5762
<FlashMessageRender byKey={FLASH_KEY} css={tw`mb-2 md:mb-4`}/>
5863
<InputSpinner visible={loading}>
59-
<Input
60-
onKeyUp={e => {
61-
if (canEdit && variable.isEditable) {
62-
setVariableValue(e.currentTarget.value);
64+
{useSwitch ?
65+
<>
66+
<Switch
67+
readOnly={!canEdit || !variable.isEditable}
68+
name={variable.envVariable}
69+
defaultChecked={variable.serverValue === '1'}
70+
onChange={() => {
71+
if (canEdit && variable.isEditable) {
72+
setVariableValue(variable.serverValue === '1' ? '0' : '1');
73+
}
74+
}}
75+
/>
76+
</>
77+
:
78+
<>
79+
{selectValues.length > 0 ?
80+
<>
81+
<Select
82+
onChange={e => setVariableValue(e.target.value)}
83+
name={variable.envVariable}
84+
defaultValue={variable.serverValue}
85+
disabled={!canEdit || !variable.isEditable}
86+
>
87+
{selectValues.map(selectValue => (
88+
<option key={selectValue.replace('in:', '')} value={selectValue.replace('in:', '')}>{selectValue.replace('in:', '')}</option>
89+
))}
90+
</Select>
91+
92+
</>
93+
:
94+
<>
95+
<Input
96+
onKeyUp={e => {
97+
if (canEdit && variable.isEditable) {
98+
setVariableValue(e.currentTarget.value);
99+
}
100+
}}
101+
readOnly={!canEdit || !variable.isEditable}
102+
name={variable.envVariable}
103+
defaultValue={variable.serverValue}
104+
placeholder={variable.defaultValue}
105+
/>
106+
</>
63107
}
64-
}}
65-
readOnly={!canEdit || !variable.isEditable}
66-
name={variable.envVariable}
67-
defaultValue={variable.serverValue}
68-
placeholder={variable.defaultValue}
69-
/>
108+
</>
109+
}
70110
</InputSpinner>
71111
<p css={tw`mt-1 text-xs text-neutral-300`}>
72112
{variable.description}

0 commit comments

Comments
 (0)