import {useConfirmation} from './Confirmation'; import {ModalBackdrop} from './ModalBackdrop'; import {SiteRuntime} from './SiteRuntime'; import {StartupSettings} from './StartupSettings'; import {SiteTools} from './SiteTools'; import { Cloud, FileSearch2, Globe2, LockKeyhole, Play, Plus, RefreshCw, ShieldOff, Square, Trash2, X } from 'lucide-react'; import { useEffect, useState } from 'react'; import { api } from '../api'; import { normalizeRunPath } from '../runPath'; import type { DeploymentCheck, Project, Runtime, RuntimeCatalog, Site, SiteDraft } from '../types'; const runtimes: Runtime[] = ['PHP', 'Node.js', 'Python', 'Go', 'Java']; interface HostSitesModalProps { project: Project; runtimeVersions: RuntimeCatalog; live: boolean; onClose: () => void; } export function HostSitesModal({ project, runtimeVersions, live, onClose }: HostSitesModalProps) { const {confirm,confirmation}=useConfirmation(); const [runtimeSite,setRuntimeSite]=useState(); const [tools,setTools]=useState(),[startup,setStartup]=useState(); const initialRuntime = project.runtime; const [sites, setSites] = useState([]); const [creating, setCreating] = useState(false); const [busy, setBusy] = useState(live); const [busySiteID, setBusySiteID] = useState(); const [error, setError] = useState(''); const [deploymentSite, setDeploymentSite] = useState(); const [deploymentEntry, setDeploymentEntry] = useState(''); const [deploymentCheck, setDeploymentCheck] = useState(); const [certificateSite, setCertificateSite] = useState(); const [certificatePEM, setCertificatePEM] = useState(''); const [privateKeyPEM, setPrivateKeyPEM] = useState(''); const [draft, setDraft] = useState({ name: 'site-two', runtime: initialRuntime, version: runtimeVersions[initialRuntime]?.[0] ?? '', runPath: '/site-two', customDomain: '', autoSSL: true }); const normalizedRunPath = normalizeRunPath(draft.runPath); useEffect(() => { setDraft(current => { if (runtimeVersions[current.runtime]?.includes(current.version)) return current; const runtime = runtimeVersions[current.runtime]?.length ? current.runtime : runtimes.find(item => runtimeVersions[item]?.length); return runtime ? { ...current, runtime, version: runtimeVersions[runtime][0] } : current; }); }, [runtimeVersions]); useEffect(() => { let active = true; if (!live) { setSites([{ id: project.id, projectId: project.id, name: project.name, domain: project.domain, runPath: project.runPath, runtime: project.runtime, version: project.version, status: project.status, sslStatus: project.ssl ? 'active' : 'off', autoSSL: project.ssl, processLimit: 10, primary: true, createdAt: project.createdAt }]); setBusy(false); return () => { active = false; }; } api.projectSites(project.id) .then((result) => { if (active) setSites(result.sites); }) .catch((reason) => { if (active) setError(message(reason, '无法读取网站')); }) .finally(() => { if (active) setBusy(false); }); return () => { active = false; }; }, [live, project.id, project.name, project.domain, project.runPath, project.runtime, project.version, project.status, project.ssl, project.createdAt]); function selectRuntime(runtime: Runtime) { setDraft((current) => ({ ...current, runtime, version: runtimeVersions[runtime]?.[0] ?? '' })); } async function submit() { if (!normalizedRunPath || !draft.version) return; setBusy(true); setError(''); const input = { ...draft, name: draft.name.trim().toLowerCase(), runPath: normalizedRunPath }; try { const site = live ? (await api.createSite(project.id, input)).site : { id: Date.now(), projectId: project.id, domain: `${project.hostName}-demo.vhost.ser888.cn`, status: input.runtime === 'PHP' ? 'running' as const : 'awaiting_upload' as const, sslStatus: input.autoSSL ? 'pending' : 'off', processLimit: 10, primary: false, createdAt: new Date().toISOString(), ...input }; setSites((current) => [...current, site]); setCreating(false); setDraft((current) => ({ ...current, name: 'site-new', runPath: '/site-new', customDomain: '' })); } catch (reason) { setError(message(reason, '创建网站失败')); } finally { setBusy(false); } } async function toggleSite(site: Site) { if (!['running', 'stopped'].includes(site.status)) return; const enabled = site.status !== 'running'; setBusySiteID(site.id); setError(''); try { const updated = live ? (await api.setSiteState(project.id, site.id, enabled)).site : { ...site, status: enabled ? 'running' as const : 'stopped' as const }; setSites((current) => current.map((item) => item.id === updated.id ? updated : item)); } catch (reason) { setError(message(reason, '更新网站状态失败')); } finally { setBusySiteID(undefined); } } async function deleteSite(site: Site) { if (site.primary || !await confirm(`确定删除网站“${site.name}”的宝塔配置?主机目录中的源码会保留。`)) return; setBusySiteID(site.id); setError(''); try { if (live) await api.deleteSite(project.id, site.id); setSites((current) => current.filter((item) => item.id !== site.id)); } catch (reason) { setError(message(reason, '删除网站失败')); } finally { setBusySiteID(undefined); } } function openDeploymentCheck(site: Site) { setDeploymentSite(site); setDeploymentEntry(defaultEntry(site.runtime)); setDeploymentCheck(undefined); setError(''); } async function checkDeployment() { if (!deploymentSite || !deploymentEntry.trim()) return; setBusySiteID(deploymentSite.id); setError(''); setDeploymentCheck(undefined); try { const check = live ? (await api.checkSiteDeployment(project.id, deploymentSite.id, deploymentEntry.trim())).check : { runtime: deploymentSite.runtime, entryFile: deploymentEntry.trim(), ready: true, adapterReady: true, size: 18_432, message: '入口文件检查通过,可以创建宝塔语言项目并验证运行状态' }; setDeploymentCheck(check); } catch (reason) { setError(message(reason, '部署检查失败')); } finally { setBusySiteID(undefined); } } async function deploySite() { if (!deploymentSite || !deploymentCheck?.ready || !deploymentCheck.adapterReady) return; setBusySiteID(deploymentSite.id); setError(''); try { const updated = live ? (await api.deploySite(project.id, deploymentSite.id, deploymentCheck.entryFile)).site : { ...deploymentSite, status: 'running' as const }; setSites((current) => current.map((item) => item.id === updated.id ? updated : item)); setDeploymentSite(undefined); setDeploymentCheck(undefined); } catch (reason) { setError(message(reason, '部署网站失败')); } finally { setBusySiteID(undefined); } } function openCertificate(site: Site) { setCertificateSite(site); setCertificatePEM(''); setPrivateKeyPEM(''); setError(''); } function closeCertificate() { setCertificateSite(undefined); setCertificatePEM(''); setPrivateKeyPEM(''); } async function installCertificate() { if (!certificateSite || !certificatePEM.trim() || !privateKeyPEM.trim()) return; setBusySiteID(certificateSite.id); setError(''); try { const updated = live ? (await api.installManualCertificate(project.id, certificateSite.id, certificatePEM.trim(), privateKeyPEM.trim())).site : { ...certificateSite, sslStatus: 'active', autoSSL: false }; setSites((current) => current.map((item) => item.id === updated.id ? updated : item)); closeCertificate(); } catch (reason) { // Keep the inputs only while the form remains open so the tenant can fix // a validation error; closing the form always discards the private key. setError(message(reason, '安装证书失败')); } finally { setBusySiteID(undefined); } } async function requestAutoCertificate(site: Site) { setBusySiteID(site.id); setError(''); try { const updated = live ? (await api.requestAutoCertificate(project.id, site.id)).site : { ...site, sslStatus: 'active', autoSSL: true }; setSites((current) => current.map((item) => item.id === updated.id ? updated : item)); } catch (reason) { setSites((current) => current.map((item) => item.id === site.id ? { ...item, sslStatus: 'failed', autoSSL: true } : item)); setError(message(reason, '自动证书申请失败')); } finally { setBusySiteID(undefined); } } async function requestDNSCertificate(site: Site) { setBusySiteID(site.id); setError(''); try { const updated = live ? (await api.requestDNSCertificate(project.id, site.id)).site : { ...site, sslStatus: 'active', autoSSL: true }; setSites((current) => current.map((item) => item.id === updated.id ? updated : item)); } catch (reason) { setSites((current) => current.map((item) => item.id === site.id ? { ...item, sslStatus: 'failed', autoSSL: true } : item)); setError(message(reason, 'DNS-01 证书申请失败')); } finally { setBusySiteID(undefined); } } async function disableCertificate(site: Site) { if (!await confirm(`确定关闭“${site.name}”的 SSL 和自动续签?关闭后网站将仅通过 HTTP 提供服务。`)) return; setBusySiteID(site.id); setError(''); try { const updated = live ? (await api.disableCertificate(project.id, site.id)).site : { ...site, sslStatus: 'off', autoSSL: false }; setSites((current) => current.map((item) => item.id === updated.id ? updated : item)); } catch (reason) { setError(message(reason, '关闭 SSL 失败')); } finally { setBusySiteID(undefined); } } if(startup)return setStartup(undefined)}/>; if(tools)return setTools(undefined)}/>; return (
{confirmation}

主机网站

{error ?

{error}

: null} {runtimeSite?s.id===runtimeSite.id)||runtimeSite} project={project} catalog={runtimeVersions} live={live} onClose={()=>setRuntimeSite(undefined)} onChanged={async()=>{if(live)setSites((await api.projectSites(project.id)).sites)}}/>:null} {creating ?
{ event.preventDefault(); void submit(); }}>
: null} {deploymentSite ?
{ event.preventDefault(); void checkDeployment(); }}>
部署前检查{deploymentSite.name} · {deploymentSite.runtime} {deploymentSite.version}
{deploymentCheck?.ready && deploymentCheck.adapterReady ? : null}
{deploymentCheck ?

{deploymentCheck.ready ? deploymentCheck.adapterReady ? '可以继续' : '文件就绪,适配器待验证' : '尚未就绪'}{deploymentCheck.message}{deploymentCheck.size ? ` · ${formatBytes(deploymentCheck.size)}` : ''}

: null}
: null} {certificateSite ?
{ event.preventDefault(); void installCertificate(); }}>
安装自有证书{certificateSite.domain}

证书必须覆盖该网站全部已启用域名。安装后将关闭此网站的自动续签;私钥仅用于本次请求,不会保存在 Sea vHost 数据库或日志中。