| 1 | import React, { useEffect, useRef, useState } from "react"; |
| 2 | import { LoaderCircle, ShieldCheck } from "lucide-react"; |
| 3 | import { api } from "./api"; |
| 4 | import { isRemoteClient } from "./client-context.mjs"; |
| 5 | import { Modal } from "./views"; |
| 6 | |
| 7 | export function HostGitPreparation({ onClose, onReady }) { |
| 8 | const machine = isRemoteClient() ? "云端主机" : "本机"; |
| 9 | const [state, setState] = useState(null); |
| 10 | const [busy, setBusy] = useState(true); |
| 11 | const [error, setError] = useState(""); |
| 12 | const alive = useRef(true); |
| 13 | const check = async (enable = false) => { |
| 14 | setBusy(true); |
| 15 | setError(""); |
| 16 | try { |
| 17 | const next = await api( |
| 18 | "/host/git", |
| 19 | enable ? { confirm: true } : undefined, |
| 20 | ); |
| 21 | if (!alive.current) return; |
| 22 | setState(next); |
| 23 | if (next.ready) onReady(); |
| 24 | } catch (e) { |
| 25 | if (alive.current) setError(e.message); |
| 26 | } finally { |
| 27 | if (alive.current) setBusy(false); |
| 28 | } |
| 29 | }; |
| 30 | useEffect(() => { |
| 31 | alive.current = true; |
| 32 | check(); |
| 33 | return () => { |
| 34 | alive.current = false; |
| 35 | }; |
| 36 | }, []); |
| 37 | |
| 38 | if (!state && !error) return null; |
| 39 | return ( |
| 40 | <Modal |
| 41 | title={`启动前,保护${machine} Git`} |
| 42 | onClose={busy ? undefined : onClose} |
| 43 | footer={ |
| 44 | <> |
| 45 | <button disabled={busy} onClick={onClose}> |
| 46 | 暂不启动 |
| 47 | </button> |
| 48 | <button disabled={busy} onClick={() => check()}> |
| 49 | 重新检查 |
| 50 | </button> |
| 51 | {state?.can_enable && ( |
| 52 | <button |
| 53 | className="primary" |
| 54 | disabled={busy} |
| 55 | onClick={() => check(true)} |
| 56 | > |
| 57 | {busy ? ( |
| 58 | <LoaderCircle size={16} className="spin" /> |
| 59 | ) : ( |
| 60 | <ShieldCheck size={16} /> |
| 61 | )} |
| 62 | 启用保护并继续 |
| 63 | </button> |
| 64 | )} |
| 65 | </> |
| 66 | } |
| 67 | > |
| 68 | <div className="modal-body host-git-preparation"> |
| 69 | {busy && !state ? ( |
| 70 | <p role="status">正在检查{machine} Git,尚未修改任何设置…</p> |
| 71 | ) : ( |
| 72 | <> |
| 73 | <p>{state?.message}</p> |
| 74 | {state?.can_enable && ( |
| 75 | <> |
| 76 | <p> |
| 77 | Claude 可以修改共享文件。启用这项保护后,{machine} Git |
| 78 | 会忽略未经你明确指定的裸仓库,避免共享目录里的配置在{machine} |
| 79 | 上执行命令。 |
| 80 | </p> |
| 81 | <p> |
| 82 | 点击“启用保护并继续”将修改{machine}当前用户的一项全局 Git 设置: |
| 83 | </p> |
| 84 | <pre>git config --global safe.bareRepository explicit</pre> |
| 85 | <p> |
| 86 | 普通的 <code>.git</code>{" "} |
| 87 | 项目、提交记录和远程地址保持不变。使用裸仓库时,需要通过{" "} |
| 88 | <code>--git-dir</code> 明确指定。设置会影响{machine}其他使用 Git |
| 89 | 的工具。 |
| 90 | </p> |
| 91 | </> |
| 92 | )} |
| 93 | {!state?.can_enable && !error && ( |
| 94 | <p>完成安装或修复后点击“重新检查”,无需重新配置代理。</p> |
| 95 | )} |
| 96 | </> |
| 97 | )} |
| 98 | {error && ( |
| 99 | <p className="start-error" role="alert"> |
| 100 | {error} |
| 101 | </p> |
| 102 | )} |
| 103 | </div> |
| 104 | </Modal> |
| 105 | ); |
| 106 | } |