import { useEffect, useRef, useState, type FormEvent } from 'react'; import { api, selectableImage, type Instance } from './api'; import { ResourceState, useAction, useResource } from './ui'; import { AlertBox } from './components/ui/alert'; import { buttonVariants } from './components/ui/button'; import { inputClass, selectClass, selectStyle } from './components/ui/input'; import { cn } from './lib/utils'; export function CreateInstance({close, refresh}: {close: () => void; refresh: () => Promise}) { const images = useResource(api.images), action = useAction(); const [progress, setProgress] = useState(''); const active = useRef(true); useEffect(() => { active.current = true; return () => { active.current = false; }; }, []); const available = images.data?.filter(selectableImage) || []; async function submit(e: FormEvent) { e.preventDefault(); const form = new FormData(e.currentTarget), name = String(form.get('name')); await action.run(async () => { const image = available.find(i => (i.image_id || i.id) === form.get('image')); if (!active.current) return; if (!image) throw new Error('请选择可用的本地镜像。'); setProgress('正在创建实例,请勿重复提交…'); const created = await api.request('/instances', 'POST', {name, image_id: image.image_id || image.id}); if (!created?.id) throw new Error('未收到实例确认,请刷新实例列表核实,勿重复提交。'); if (active.current) { await refresh(); close(); } }); if (active.current) setProgress(''); } return

创建独立实例

{action.feedback} 请选择已验证并准入的本地镜像。创建使用所选镜像的精确 ID,不会自动构建或切换版本。
{!images.loading && !available.length &&

暂无已准入的可用镜像。请联系管理员构建并验证发布镜像。

}
{progress &&

{progress}

}
; }