Files
pterodactyl-panel/resources/scripts/components/server/startup/VariableBox.tsx
T

115 lines
4.9 KiB
TypeScript
Raw Normal View History

2026-10-05 18:10:18 -04:00
import { useState } from 'react';
import TitledGreyBox from '@/components/elements/TitledGreyBox';
import { usePermissions } from '@/plugins/usePermissions';
import InputSpinner from '@/components/elements/InputSpinner';
2026-10-05 18:10:18 -04:00
import { TextInput } from '@/components/form/controls';
import Switch from '@/components/ui/Switch';
import Select from '@/components/ui/Select';
import { useCurrentServerUuid } from '@/api/server/queries';
import { type ServerStartupVariable, useUpdateStartupVariable } from '@/api/server/startup/queries';
import { useDebouncedCallback } from '@/plugins/useDebouncedCallback';
interface Props {
2026-10-05 18:10:18 -04:00
variable: ServerStartupVariable;
}
2026-10-05 18:10:18 -04:00
const VariableEditor = ({ variable, uuid }: Props & { uuid: string }) => {
const { attributes } = variable;
const [draft, setDraft] = useState<string | null>(null);
const value = draft ?? attributes.server_value ?? attributes.default_value;
const [canEdit] = usePermissions(['startup.update']);
2026-10-05 18:10:18 -04:00
const updateVariable = useUpdateStartupVariable(uuid);
2026-10-05 18:10:18 -04:00
const saveVariable = useDebouncedCallback((value: string) => {
updateVariable.mutate(
{ path: { server_uuid: uuid }, body: { key: attributes.env_variable, value } },
{ onSuccess: () => setDraft((current) => (current === value ? null : current)) }
);
}, 500);
2026-10-05 18:10:18 -04:00
const setVariableValue = (value: string) => {
setDraft(value);
saveVariable(value);
};
const rules = attributes.rules.split('|');
const useSwitch = rules.some(
(v) => v === 'boolean' || v === 'in:0,1' || v === 'in:1,0' || v === 'in:true,false' || v === 'in:false,true'
);
2026-10-05 18:10:18 -04:00
const isStringSwitch = rules.some((v) => v === 'string');
const selectValues = rules.find((v) => v.startsWith('in:'))?.split(',') || [];
return (
<TitledGreyBox
title={
<p className='text-sm uppercase'>
2026-10-05 18:10:18 -04:00
{!attributes.is_editable && (
<span className='bg-card text-xs py-1 px-2 rounded-full mr-2 mb-1'>Read Only</span>
)}
2026-10-05 18:10:18 -04:00
{attributes.name}
</p>
}
>
2026-10-05 18:10:18 -04:00
<InputSpinner visible={updateVariable.isPending}>
{useSwitch ? (
<>
<Switch
2026-10-05 18:10:18 -04:00
disabled={!canEdit || !attributes.is_editable}
checked={isStringSwitch ? value === 'true' : value === '1'}
onChange={() => {
2026-10-05 18:10:18 -04:00
if (canEdit && attributes.is_editable) {
if (isStringSwitch) {
2026-10-05 18:10:18 -04:00
setVariableValue(value === 'true' ? 'false' : 'true');
} else {
2026-10-05 18:10:18 -04:00
setVariableValue(value === '1' ? '0' : '1');
}
}
}}
/>
</>
) : (
<>
{selectValues.length > 0 ? (
<>
<Select
2026-10-05 18:10:18 -04:00
value={value}
onChange={(value) => setVariableValue(String(value))}
disabled={!canEdit || !attributes.is_editable}
options={selectValues.map((rule) => {
const clean = rule.replace('in:', '');
return { value: clean, label: clean };
})}
/>
</>
) : (
<>
2026-10-05 18:10:18 -04:00
<TextInput
onChange={(e) => {
if (canEdit && attributes.is_editable) {
setVariableValue(e.currentTarget.value);
}
}}
2026-10-05 18:10:18 -04:00
onBlur={saveVariable.flush}
readOnly={!canEdit || !attributes.is_editable}
name={attributes.env_variable}
value={value}
placeholder={attributes.default_value}
/>
</>
)}
</>
)}
</InputSpinner>
2026-10-05 18:10:18 -04:00
<p className='mt-1 text-xs text-muted-foreground'>{attributes.description}</p>
</TitledGreyBox>
);
};
2026-10-05 18:10:18 -04:00
const VariableBox = ({ variable }: Props) => {
const uuid = useCurrentServerUuid()!;
return <VariableEditor key={`${uuid}:${variable.attributes.env_variable}`} uuid={uuid} variable={variable} />;
};
export default VariableBox;