| 1 | import React, { useEffect, useRef, useState } from "react"; |
| 2 | import { api, native } from "./api"; |
| 3 | import { InlineSelect } from "./inline-select"; |
| 4 | import "./guest-system-picker.css"; |
| 5 | |
| 6 | const names = { "debian-12": "Debian 12", "debian-13": "Debian 13", "ubuntu-22.04": "Ubuntu 22.04", "ubuntu-24.04": "Ubuntu 24.04", macos: "macOS", "windows-11": "Windows 11" }; |
| 7 | const editions = { Professional: "专业版", ProfessionalN: "专业版 N", Enterprise: "企业版", EnterpriseN: "企业版 N", Education: "教育版", EducationN: "教育版 N", Core: "家庭版", CoreSingleLanguage: "家庭单语言版" }; |
| 8 | const languages = { "zh-CN": "简体中文", "zh-TW": "繁体中文", "en-US": "英语(美国)" }; |
| 9 | |
| 10 | export function HostCloneSummary({ pending, onChange }) { |
| 11 | const [result, setResult] = useState(null); |
| 12 | const [error, setError] = useState(""); |
| 13 | const [attempt, retry] = useState(0); |
| 14 | useEffect(() => { |
| 15 | let active = true; |
| 16 | api("/environment/guest-systems/clone").then((next) => { |
| 17 | if (!active) return; |
| 18 | setResult(next); |
| 19 | onChange((current) => current?.clone ? { ...current, clone_key: next.key, clone_ready: next.enough_space } : current); |
| 20 | }).catch((failure) => { if (active) setError(failure.message); }); |
| 21 | return () => { active = false; }; |
| 22 | }, [attempt, onChange]); |
| 23 | if (error) return <div className="guest-install-fields" role="alert"><small>{error}</small> |
| 24 | <button type="button" onClick={() => { setError(""); retry((value) => value + 1); }}>重新检测并匹配</button></div>; |
| 25 | if (!result) return <small className="guest-install-fields" role="status">正在检测本机系统版本并匹配官方镜像…</small>; |
| 26 | const plan = result.plan; |
| 27 | const host = plan.host; |
| 28 | const download = result.download_bytes > 0 ? `${(result.download_bytes / 1e9).toFixed(2)} GB` : ""; |
| 29 | const source = host.system === "macos" ? "Apple" : host.system === "windows-11" ? "微软" : "发行版官方源"; |
| 30 | const cached = Boolean(plan.path || result.download_bytes === 0 && plan.bytes > 0); |
| 31 | return <div className="guest-install-fields host-clone-summary" aria-live="polite"> |
| 32 | <strong>将安装:{names[host.system]} {host.system === "windows-11" ? `${host.version} · ${editions[host.edition] || host.edition} · ${languages[host.language] || host.language}` : host.system === "macos" ? host.version : ""}</strong> |
| 33 | <small>{pending ? "沿用首次选择的系统镜像,继续安装独立客机。" : "根据本机系统版本匹配官方镜像,安装到独立客机中。"}</small> |
| 34 | {host.build && <small>{pending ? "首次检测的本机构建号" : "本机构建号"} {host.build} · 镜像构建号 {plan.build}</small>} |
| 35 | {host.system === "windows-11" && host.build !== plan.build && <small>Windows 的发行版本、版本类型和语言与本机一致,补丁版本有所不同。</small>} |
| 36 | <small>本机的应用、个人文件和登录状态不会复制;客机中的工具、账户和文件会持续保存。</small> |
| 37 | <small>{cached ? "已找到匹配的本地镜像,校验后直接使用,无需重新下载系统镜像。" : `从 ${source} 下载官方系统镜像,${download ? `预计还需下载 ${download}` : "下载大小将在连接后显示"}。`}</small> |
| 38 | <small>{result.installation_started ? "继续使用原虚拟磁盘,保留已有安装进度。" : `至少需要 ${result.minimum_free_gib} GiB 可用空间,虚拟磁盘按实际使用增长。`}浏览器、编辑器等工作台组件按需下载。</small> |
| 39 | {!result.enough_space && <small role="alert">当前可用 {result.free_gib} GiB,请在存储设置中选择空间充足的位置,再重新检查。</small>} |
| 40 | {!pending && <button type="button" onClick={() => { |
| 41 | setResult(null); |
| 42 | onChange((current) => current?.clone ? { ...current, clone_key: "", clone_ready: false } : current); |
| 43 | retry((value) => value + 1); |
| 44 | }}>重新检测并匹配</button>} |
| 45 | </div>; |
| 46 | } |
| 47 | |
| 48 | export function GuestSystemPicker({ system, onChange, preserveOnUnmount = false, deferMatch = false }) { |
| 49 | const [view, setView] = useState(null); |
| 50 | const [error, setError] = useState(""); |
| 51 | const [attempt, retry] = useState(0); |
| 52 | const activated = useRef(false); |
| 53 | useEffect(() => { |
| 54 | if (!system?.activate || activated.current) return; |
| 55 | activated.current = true; |
| 56 | api("/environment/guest-systems/activate",{}).then(() => native("reconnect")).catch(e => setError(e.message)); |
| 57 | }, [system?.activate]); |
| 58 | useEffect(() => { |
| 59 | let active = true; |
| 60 | setError(""); |
| 61 | api("/environment/guest-systems").then((next) => { |
| 62 | if (!active) return; |
| 63 | setView(next); |
| 64 | if (next.fresh || next.pending) { |
| 65 | onChange(current => !next.pending && current && !current.pending ? current : ({ guest_system: next.pending?.system || "debian-13", media: next.pending?.media || "", |
| 66 | sha256: next.pending?.sha256 || "", editor_terms: Boolean(next.pending?.editor_terms), |
| 67 | clone: Boolean(next.pending?.source && !next.pending?.source_kind), clone_key: "", clone_ready: false, |
| 68 | activate: next.activate, pending: Boolean(next.pending) })); |
| 69 | } else onChange(null); |
| 70 | }).catch((e) => { |
| 71 | if (!active) return; |
| 72 | if (e.status === 403 || e.status === 404) onChange(null); |
| 73 | else setError(e.message); |
| 74 | }); |
| 75 | return () => { active = false; if (!preserveOnUnmount) onChange(undefined); }; |
| 76 | }, [onChange, attempt, preserveOnUnmount]); |
| 77 | if (!view || !system) return error ? <div className="guest-system-picker"><span role="alert">{error}</span> |
| 78 | <button type="button" onClick={() => retry((n) => n + 1)}>重新检查安装选项</button></div> : null; |
| 79 | const os = system.guest_system; |
| 80 | const change = (changes) => onChange((current) => current?.guest_system === os ? { ...current, ...changes } : current); |
| 81 | async function choose() { |
| 82 | try { |
| 83 | const path = await native("chooseGuestMedia"); |
| 84 | if (path) change({ media: path }); |
| 85 | } catch (e) { setError(e.message); } |
| 86 | } |
| 87 | async function reference(kind) { |
| 88 | try { await native("guestInstallReference", { kind }); } |
| 89 | catch (e) { setError(e.message); } |
| 90 | } |
| 91 | return ( |
| 92 | <div className="guest-system-picker"> |
| 93 | <span>客户机系统</span> |
| 94 | <InlineSelect aria-label="客户机系统" value={system.clone ? "host" : os} disabled={system.pending} |
| 95 | onValueChange={(value) => onChange({ guest_system: value === "host" ? view.system : value, clone: value === "host", clone_key: "", clone_ready: false, media: "", sha256: "", editor_terms: false })}> |
| 96 | {Boolean(view.system) && <option value="host" disabled={!view.available}>自动匹配本机系统版本{view.available ? "" : "(暂不可用)"}</option>} |
| 97 | <option value="debian-13">Debian 13</option> |
| 98 | <option value="ubuntu-22.04" disabled={!view.linux_systems?.includes("ubuntu-22.04")}>Ubuntu 22.04{view.linux_systems?.includes("ubuntu-22.04") ? "" : "(此构建暂不可用)"}</option> |
| 99 | <option value="macos" disabled={view.system !== "macos" || !view.available}>macOS{view.system === "macos" ? view.available ? "" : "(暂不可用)" : "(需要 Apple Silicon Mac)"}</option> |
| 100 | <option value="windows-11" disabled={view.system !== "windows-11" || !view.available}>Windows 11{view.system === "windows-11" ? view.available ? "" : "(暂不可用)" : "(需要 Windows 11 与 Hyper-V)"}</option> |
| 101 | </InlineSelect> |
| 102 | {system.clone && !system.activate && (deferMatch ? <small className="guest-install-fields">Cage 会检测本机系统版本,在下一步显示匹配的官方镜像、下载量与空间要求。</small> : <HostCloneSummary pending={system.pending} onChange={onChange} />)} |
| 103 | {!system.activate && <small className="guest-install-fields">系统镜像通过主机网络下载,遵循主机系统代理或 TUN 设置,不使用 Cage 的客机代理。使用 Clash 等软件时,请先开启系统代理或 TUN;代理规则决定镜像网站的实际出口。</small>} |
| 104 | {!system.clone && os !== "debian-13" && !system.activate && <div className="guest-install-fields"> |
| 105 | {os === "ubuntu-22.04" ? <> |
| 106 | <small>从 Ubuntu 官方源下载 Ubuntu 22.04 LTS 镜像,并校验 SHA-256。Cage 按主机架构选择 ARM64 或 x86_64。</small> |
| 107 | <small>自动安装开发工具、浏览器和编辑器。客机账户、文件和登录状态保存在独立的持久虚拟磁盘中。</small> |
| 108 | <small>至少需要 17 GiB 可用空间。失败后可继续原安装,不会改用其他系统或覆盖已有环境。</small> |
| 109 | </> : os === "macos" ? <> |
| 110 | <small>{system.media || "将从 Apple 下载 macOS 26.5.2(19.77 GB),并创建 64 GiB 稀疏虚拟磁盘。"}</small> |
| 111 | {!system.pending && <div> |
| 112 | <button type="button" onClick={choose}>选择本机 IPSW</button> |
| 113 | {system.media && <button type="button" onClick={() => change({ media: "" })}>改为下载</button>} |
| 114 | </div>} |
| 115 | <small>客机账户和独立密码由 Cage 自动准备并加密保管,启动时自动登录。</small> |
| 116 | </> : <> |
| 117 | <small>Windows 11 x64 需要 Hyper-V 和至少 64 GiB 可用磁盘空间。安装前在授权窗口设置客机本地管理员,Cage 会生成离线账户应答。进入客机桌面后回 Cage 继续,使用该账户确认组件安装。Cage 随后创建专用普通账户并重启客机。</small> |
| 118 | {!system.pending && <div> |
| 119 | <button type="button" onClick={() => reference("windows-media")}>从微软下载 ISO</button> |
| 120 | <button type="button" onClick={choose}>选择本机 ISO</button> |
| 121 | </div>} |
| 122 | {system.media && <small>{system.media}</small>} |
| 123 | <label>ISO 的 SHA-256<input value={system.sha256} readOnly={system.pending} spellCheck={false} |
| 124 | onChange={(e) => change({ sha256: e.target.value.trim() })} placeholder="微软公布的 64 位十六进制摘要" /></label> |
| 125 | <button type="button" onClick={() => reference("editor-license")}>阅读 Microsoft VS Code Server 许可条款</button> |
| 126 | <label><input type="checkbox" checked={system.editor_terms} disabled={system.pending} |
| 127 | onChange={(e) => change({ editor_terms: e.target.checked })} />我接受上述编辑器许可条款</label> |
| 128 | </>} |
| 129 | </div>} |
| 130 | {system.clone && os === "windows-11" && <div className="guest-install-fields"> |
| 131 | <small>安装时需要你确认 Windows 管理员授权,Cage 随后自动安装系统并创建独立客机账户。</small> |
| 132 | <button type="button" onClick={() => reference("editor-license")}>阅读 Microsoft VS Code Server 许可条款</button> |
| 133 | <label><input type="checkbox" checked={system.editor_terms} disabled={system.pending} |
| 134 | onChange={(e) => change({ editor_terms: e.target.checked })} />我接受上述编辑器许可条款</label> |
| 135 | </div>} |
| 136 | {view.reason && !view.available && <small className="guest-install-fields">{view.reason}</small>} |
| 137 | {view.reselect && <button type="button" className="guest-install-fields" onClick={async () => { |
| 138 | try { |
| 139 | await api("/environment/guest-systems", { id: view.pending.id }, "DELETE"); |
| 140 | setView(await api("/environment/guest-systems")); |
| 141 | onChange({ guest_system: "debian-13", media: "", sha256: "", editor_terms: false }); |
| 142 | } catch (e) { setError(e.message); } |
| 143 | }}>重新选择系统或镜像</button>} |
| 144 | {system.activate && <small className="guest-install-fields">客机已安装,可以打开工作台。</small>} |
| 145 | {error && <p role="alert" className="guest-install-fields">{error}</p>} |
| 146 | </div> |
| 147 | ); |
| 148 | } |
| 149 | |
| 150 | export function validGuestChoice(value) { |
| 151 | if (value?.clone && !value.activate) return Boolean(value.clone_key && value.clone_ready && (value.guest_system !== "windows-11" || value.editor_terms)); |
| 152 | if (!value || value.activate || value.guest_system === "debian-13" || value.guest_system === "ubuntu-22.04") return true; |
| 153 | if (value.guest_system === "macos") return true; |
| 154 | return value.media && /^[a-f\d]{64}$/i.test(value.sha256) && value.editor_terms; |
| 155 | } |