import { useRef } from 'react'; import { Link } from '@tanstack/react-router'; import { useStore } from '@tanstack/react-form'; import { useAppForm, Form } from '@/components/form'; import type { AdminEgg } from '@/api/admin/eggs/queries'; import { updateAdminEggScriptInput, useAdminEggs, useUpdateAdminEggScript } from '@/api/admin/eggs/queries'; import { useEggDetail } from '@/components/admin/eggs/useEggDetail'; import TitledGreyBox from '@/components/elements/TitledGreyBox'; import type { CodemirrorEditorHandle } from '@/components/elements/LazyCodemirrorEditor'; import CodemirrorEditor from '@/components/elements/LazyCodemirrorEditor'; interface Props { egg: AdminEgg; } interface Values { scriptContainer: string; scriptEntry: string; scriptIsPrivileged: boolean; copyScriptFrom: number; } const eggToFormValues = (egg: AdminEgg): Values => ({ scriptContainer: egg.attributes.script.container, scriptEntry: egg.attributes.script.entry, scriptIsPrivileged: egg.attributes.script.privileged ?? false, copyScriptFrom: egg.attributes.script.extends ?? 0, }); function EggScriptForm({ egg }: Props) { const scriptEditor = useRef(null); const { data: eggsResponse } = useAdminEggs(); const eggs = eggsResponse?.data ?? []; const updateEggScript = useUpdateAdminEggScript(); const form = useAppForm({ defaultValues: eggToFormValues(egg), onSubmit: async ({ value }) => { try { const scriptInstall = scriptEditor.current?.getValue() ?? egg.attributes.script.install; await updateEggScript.mutateAsync( updateAdminEggScriptInput(egg.attributes.id, { script_install: scriptInstall, script_is_privileged: value.scriptIsPrivileged, script_entry: value.scriptEntry, script_container: value.scriptContainer, copy_script_from: Number(value.copyScriptFrom) > 0 ? Number(value.copyScriptFrom) : null, }) ); } catch { // Error toast is handled by the mutation. } }, }); const copyScriptFrom = useStore(form.store, (state) => state.values.copyScriptFrom); const copyableEggs = eggs.filter( (option) => option.attributes.id !== egg.attributes.id && option.attributes.script.extends === null ); const copiedFromEgg = eggs.find((option) => option.attributes.id === Number(copyScriptFrom)); const dependentEggs = eggs.filter((option) => option.attributes.script.extends === egg.attributes.id); return (
{Number(copyScriptFrom) > 0 && (

This egg is copying its install script {copiedFromEgg ? ( <> {' '} from{' '} {copiedFromEgg.attributes.name} ) : ( ' from another egg' )} . Changes made to the script below will not apply unless you select "None" from the "Copy Script From" dropdown.

)} {}} />
{(field) => ( ({ value: option.attributes.id, label: option.attributes.name, })), ]} /> )} {(field) => ( )} {(field) => ( )}
{(field) => ( )}
{dependentEggs.length > 0 && (

Changes to this install script affect these eggs because they copy from this one.

{dependentEggs.map((dependent) => ( {dependent.attributes.name} ))}
)}
Save Changes
); } export default function EggScriptTab() { const egg = useEggDetail(); return ; }