| 1 | import React, { useState } from "react"; |
| 2 | import { api } from "./api"; |
| 3 | import { InlineSelect } from "./inline-select"; |
| 4 | import { Modal } from "./modal"; |
| 5 | |
| 6 | const emptyHost = () => ({ |
| 7 | name: "", |
| 8 | host: "", |
| 9 | port: 22, |
| 10 | user: "", |
| 11 | auth: { kind: "key_file", identity_file: "" }, |
| 12 | executable: "cage", |
| 13 | config_path: "", |
| 14 | }); |
| 15 | |
| 16 | export function HostForm({ initial, platform, onSaved, onClose }) { |
| 17 | const [form, setForm] = useState(() => |
| 18 | initial ? structuredClone(initial) : emptyHost(), |
| 19 | ); |
| 20 | const [busy, setBusy] = useState(false); |
| 21 | const [error, setError] = useState(""); |
| 22 | const update = (field, value) => |
| 23 | setForm((old) => ({ |
| 24 | ...old, |
| 25 | [field]: value, |
| 26 | ...(["host", "port"].includes(field) ? { host_key: undefined } : {}), |
| 27 | })); |
| 28 | async function save(event) { |
| 29 | event.preventDefault(); |
| 30 | if (busy) return; |
| 31 | setBusy(true); |
| 32 | setError(""); |
| 33 | try { |
| 34 | const saved = await api( |
| 35 | form.id ? `/client/hosts/${form.id}` : "/client/hosts", |
| 36 | { ...form, port: Number(form.port) }, |
| 37 | form.id ? "PUT" : "POST", |
| 38 | ); |
| 39 | await onSaved(saved); |
| 40 | } catch (e) { |
| 41 | setError(e.message); |
| 42 | } finally { |
| 43 | setBusy(false); |
| 44 | } |
| 45 | } |
| 46 | return ( |
| 47 | <Modal |
| 48 | title={form.id ? "编辑远程主机" : "添加远程主机"} |
| 49 | onClose={busy ? undefined : onClose} |
| 50 | > |
| 51 | <form className="remote-host-form" onSubmit={save}> |
| 52 | {error && ( |
| 53 | <p className="error-message" role="alert"> |
| 54 | {error} |
| 55 | </p> |
| 56 | )} |
| 57 | <fieldset disabled={busy}> |
| 58 | <label> |
| 59 | 主机名称 |
| 60 | <input |
| 61 | required |
| 62 | value={form.name} |
| 63 | onChange={(event) => update("name", event.target.value)} |
| 64 | placeholder="例如:东京开发机" |
| 65 | maxLength={200} |
| 66 | /> |
| 67 | </label> |
| 68 | <div className="remote-form-row"> |
| 69 | <label> |
| 70 | 服务器地址 |
| 71 | <input |
| 72 | required |
| 73 | value={form.host} |
| 74 | onChange={(event) => update("host", event.target.value)} |
| 75 | placeholder="IP 地址或域名" |
| 76 | autoComplete="off" |
| 77 | spellCheck="false" |
| 78 | /> |
| 79 | </label> |
| 80 | <label className="remote-port"> |
| 81 | SSH 端口 |
| 82 | <input |
| 83 | required |
| 84 | type="number" |
| 85 | min="1" |
| 86 | max="65535" |
| 87 | value={form.port} |
| 88 | onChange={(event) => update("port", event.target.value)} |
| 89 | /> |
| 90 | </label> |
| 91 | </div> |
| 92 | <label> |
| 93 | SSH 用户名 |
| 94 | <input |
| 95 | required |
| 96 | value={form.user} |
| 97 | onChange={(event) => update("user", event.target.value)} |
| 98 | placeholder="cage" |
| 99 | autoComplete="off" |
| 100 | spellCheck="false" |
| 101 | /> |
| 102 | </label> |
| 103 | <label> |
| 104 | SSH 登录方式 |
| 105 | <InlineSelect |
| 106 | aria-label="SSH 登录方式" |
| 107 | value={form.auth.kind} |
| 108 | onValueChange={(kind) => |
| 109 | update("auth", { |
| 110 | kind, |
| 111 | identity_file: |
| 112 | kind === "key_file" ? form.auth.identity_file || "" : "", |
| 113 | }) |
| 114 | } |
| 115 | > |
| 116 | <option value="key_file">本机密钥文件</option> |
| 117 | <option value="agent">本机 SSH agent</option> |
| 118 | </InlineSelect> |
| 119 | </label> |
| 120 | {form.auth.kind === "key_file" && ( |
| 121 | <label> |
| 122 | 本机私钥文件 |
| 123 | <input |
| 124 | aria-label="本机私钥文件" |
| 125 | required |
| 126 | value={form.auth.identity_file} |
| 127 | onChange={(event) => |
| 128 | update("auth", { |
| 129 | ...form.auth, |
| 130 | identity_file: event.target.value, |
| 131 | }) |
| 132 | } |
| 133 | placeholder={ |
| 134 | platform === "windows" |
| 135 | ? "C:\\Users\\你的名字\\.ssh\\id_ed25519" |
| 136 | : "/Users/你的名字/.ssh/id_ed25519" |
| 137 | } |
| 138 | autoComplete="off" |
| 139 | spellCheck="false" |
| 140 | /> |
| 141 | <small>填写绝对路径。密钥留在本机,不上传到云端。</small> |
| 142 | </label> |
| 143 | )} |
| 144 | <details> |
| 145 | <summary>云端安装路径</summary> |
| 146 | <div className="remote-advanced"> |
| 147 | <label> |
| 148 | Cage 程序路径 |
| 149 | <input |
| 150 | value={form.executable || ""} |
| 151 | onChange={(event) => update("executable", event.target.value)} |
| 152 | placeholder="cage" |
| 153 | spellCheck="false" |
| 154 | /> |
| 155 | <small>默认使用 cage,也可填写云端程序的绝对路径。</small> |
| 156 | </label> |
| 157 | <label> |
| 158 | Cage 配置文件 |
| 159 | <input |
| 160 | value={form.config_path || ""} |
| 161 | onChange={(event) => |
| 162 | update("config_path", event.target.value) |
| 163 | } |
| 164 | placeholder="默认使用云端账户的 ~/.claude-cage/config.json" |
| 165 | spellCheck="false" |
| 166 | /> |
| 167 | <small>自定义时填写云端绝对路径。</small> |
| 168 | </label> |
| 169 | </div> |
| 170 | </details> |
| 171 | <div className="remote-button-row"> |
| 172 | <button className="primary" type="submit"> |
| 173 | 保存主机 |
| 174 | </button> |
| 175 | <button type="button" onClick={onClose}> |
| 176 | 取消 |
| 177 | </button> |
| 178 | </div> |
| 179 | </fieldset> |
| 180 | </form> |
| 181 | </Modal> |
| 182 | ); |
| 183 | } |