| 1 | import React, { useEffect, useState } from "react"; |
| 2 | import { FolderOpen } from "lucide-react"; |
| 3 | import { api, native, nativeAvailable } from "./api"; |
| 4 | |
| 5 | export function InitializationStorage({ value, onChange, disabled }) { |
| 6 | const [error, setError] = useState(""); |
| 7 | const [attempt, retry] = useState(0); |
| 8 | useEffect(() => { |
| 9 | if (value) return; |
| 10 | let live = true; |
| 11 | api("/environment/storage").then(location => { |
| 12 | if (live) onChange({ location, directory: location.suggested_directory || location.directory }); |
| 13 | }).catch(error => { if (live) setError(error.message); }); |
| 14 | return () => { live = false; }; |
| 15 | }, [Boolean(value), onChange, attempt]); |
| 16 | const locked = disabled || !value || value.location.has_environment; |
| 17 | async function choose() { |
| 18 | try { |
| 19 | const directory = await native("chooseFolder", { purpose: "environmentStorage" }); |
| 20 | if (directory) { onChange({ ...value, directory }); setError(""); } |
| 21 | } catch (error) { setError(error.message); } |
| 22 | } |
| 23 | return <section className="initialization-storage" aria-label="环境存储目录"> |
| 24 | <label htmlFor="initialization-directory">存储目录</label> |
| 25 | <p>系统镜像、下载缓存和虚拟磁盘保存在这里。已填入默认目录,也可以选择其他本地磁盘上的专用空文件夹。</p> |
| 26 | <p>这是 Cage 的环境数据目录。更新、重装或卸载应用会保留这里的数据;请勿手动删除。</p> |
| 27 | <div className="initialization-directory-control"> |
| 28 | <input id="initialization-directory" value={value?.directory || ""} disabled={locked} spellCheck={false} placeholder={value ? "请输入存储目录的完整路径" : "正在读取默认目录…"} onChange={event => onChange({ ...value, directory: event.target.value })} /> |
| 29 | {nativeAvailable() && <button type="button" disabled={locked} onClick={choose}><FolderOpen size={16} />选择文件夹</button>} |
| 30 | </div> |
| 31 | {value?.location.has_environment && <p>已有虚拟磁盘,继续使用原存储目录。完成初始化后可在设置中迁移。</p>} |
| 32 | {error && <p role="alert">{error}<button onClick={() => { setError(""); retry(value => value + 1); }}>重新读取</button></p>} |
| 33 | </section>; |
| 34 | } |