| 1 | import React, { useEffect, useState } from "react"; |
| 2 | import { FolderOpen } from "lucide-react"; |
| 3 | import { api, native, nativeAvailable } from "./api"; |
| 4 | import { SafetyMessage } from "./safety-message"; |
| 5 | |
| 6 | export function EnvironmentStorage({ status, onConfirm }) { |
| 7 | const [location, setLocation] = useState(null); |
| 8 | const [pending, setPending] = useState(null); |
| 9 | const [directory, setDirectory] = useState(""); |
| 10 | const [submitting, setSubmitting] = useState(false); |
| 11 | const [error, setError] = useState(""); |
| 12 | const job = status.job; |
| 13 | const recovery = status.storage_recovery?.required ? status.storage_recovery : location?.recovery; |
| 14 | const storageJob = job?.kind === "storage" || job?.kind === "storage-recovery"; |
| 15 | const busy = submitting || Boolean(pending) || Boolean(job?.running); |
| 16 | useEffect(() => { |
| 17 | let live = true; |
| 18 | if (pending && job?.id === pending.previousJob) return; |
| 19 | if (storageJob && job.running) return; |
| 20 | api("/environment/storage").then((next) => { |
| 21 | if (!live) return; |
| 22 | setLocation(next); |
| 23 | if (pending) { |
| 24 | setPending(null); |
| 25 | if (job?.id === pending.id && job.error) setError(job.error); |
| 26 | else if (job?.id === pending.id || next.directory && next.directory === pending.path) { setDirectory(""); setError(""); } |
| 27 | else setError("操作已结束,请核对当前环境目录后重试。"); |
| 28 | } |
| 29 | }).catch((e) => { |
| 30 | if (live) { setError(e.message); setPending(null); } |
| 31 | }); |
| 32 | return () => { live = false; }; |
| 33 | }, [pending, job?.id, job?.running, storageJob, status.storage_recovery?.required]); |
| 34 | |
| 35 | const choose = async () => { |
| 36 | setError(""); |
| 37 | try { |
| 38 | const selected = await native("chooseFolder", { purpose: "environmentStorage" }); |
| 39 | if (selected) setDirectory(selected); |
| 40 | } catch { setError("未能选择文件夹,请直接填写绝对路径。"); } |
| 41 | }; |
| 42 | const apply = async () => { |
| 43 | if (!location?.directory || busy || recovery?.required) return; |
| 44 | const path = directory.trim(); |
| 45 | if (location.has_environment && !(await onConfirm({ |
| 46 | title: "迁移本机环境?", |
| 47 | body: `请先保存文件。Cage 会停止环境和终端,将虚拟磁盘复制到 ${path},校验后再切换位置。原数据保留为备份,迁移完成后可手动启动环境。`, |
| 48 | confirm: "迁移环境", |
| 49 | }))) return; |
| 50 | setSubmitting(true); |
| 51 | setError(""); |
| 52 | try { |
| 53 | const accepted = await api("/environment/storage", { |
| 54 | directory: path, previous: location.directory, migrate: location.has_environment, |
| 55 | }); |
| 56 | setPending({ id: accepted.id, previousJob: job?.id, path }); |
| 57 | } catch (e) { setError(e.message); } |
| 58 | finally { setSubmitting(false); } |
| 59 | }; |
| 60 | const recover = async (repair) => { |
| 61 | if (busy) return; |
| 62 | setSubmitting(true); |
| 63 | setError(""); |
| 64 | try { |
| 65 | const accepted = await api("/environment/storage/recover", { repair }); |
| 66 | setPending({ id: accepted.id, previousJob: job?.id }); |
| 67 | } catch (e) { setError(e.message); } |
| 68 | finally { setSubmitting(false); } |
| 69 | }; |
| 70 | return <section className="environment-versions environment-storage" aria-label="本机环境存储"> |
| 71 | <h4>本机环境存储</h4> |
| 72 | {recovery?.required ? <> |
| 73 | <p className="field-help">环境存储需要恢复。请连接迁移使用的磁盘后重试。恢复完成后可以启动环境。</p> |
| 74 | <p role="alert" className="error-message"><SafetyMessage message={recovery.error} /></p> |
| 75 | <div className="storage-actions"> |
| 76 | <button type="button" disabled={busy} onClick={() => recover(false)}>重试恢复</button> |
| 77 | {recovery.repair_allowed && <button type="button" disabled={busy} onClick={() => recover(true)}>修复系统助手并恢复</button>} |
| 78 | </div> |
| 79 | {recovery.repair_allowed && <p className="field-help storage-repair-help">修复系统助手需要 Windows 系统授权。完成后会继续恢复原环境。</p>} |
| 80 | </> : <> |
| 81 | <p className="field-help">系统镜像、下载缓存和虚拟磁盘可以放在另一块本地磁盘。项目文件保存在各自的项目文件夹。</p> |
| 82 | <div className="setting-summary-row"><span>当前环境目录</span><strong>{location?.directory || "正在读取…"}</strong></div> |
| 83 | {status.environment_identity_directory && status.environment_identity_directory !== status.environment_directory && |
| 84 | <p className="field-help">升级前的默认项目文件位于 <code>{status.environment_identity_directory}</code>,当前默认项目位于 <code>{status.environment_directory}</code>。如需使用旧目录中的项目,请在主机文件管理器中将项目移到 Cage 配置目录之外,再从工作台打开。</p>} |
| 85 | {location?.backup && <p className="field-help">迁移前的备份保存在 <code>{location.backup}</code>。确认新环境能正常启动、文件和登录状态齐全后,可自行清理备份。</p>} |
| 86 | <label>新存储位置 |
| 87 | <input value={directory} onChange={(e) => { setDirectory(e.target.value); setError(""); }} spellCheck="false" |
| 88 | disabled={busy || !location?.directory} placeholder="本地磁盘上专用空文件夹的完整路径" /> |
| 89 | </label> |
| 90 | <div className="storage-actions"> |
| 91 | {nativeAvailable() && <button type="button" disabled={busy || !location?.directory} onClick={choose}><FolderOpen size={15} />选择文件夹</button>} |
| 92 | <button type="button" disabled={busy || !location?.directory || !directory.trim() || directory.trim() === location.directory} onClick={apply}> |
| 93 | {location?.has_environment ? "迁移到此位置" : "使用此位置"} |
| 94 | </button> |
| 95 | </div> |
| 96 | </>} |
| 97 | {storageJob && job.running && <p role="status" className="field-help">{job.message}</p>} |
| 98 | {error && error !== recovery?.error && <p role="alert" className="error-message"><SafetyMessage message={error} /></p>} |
| 99 | </section>; |
| 100 | } |