| 1 | import React, { useEffect, useRef, useState } from "react"; |
| 2 | import { api } from "./api"; |
| 3 | import { Modal } from "./modal"; |
| 4 | import { MacOSLogin } from "./macos-login"; |
| 5 | import "./macos-project-approval.css"; |
| 6 | |
| 7 | const permissionMessages = { |
| 8 | project_screen_permission: "无法显示文件夹授权窗口。请在虚拟 Mac 的“系统设置 → 隐私与安全性 → 屏幕与系统音频录制”中允许 Cage Guest Window,并按系统提示重新打开助手。", |
| 9 | accessibility_permission: "无法点击文件夹授权窗口。请在虚拟 Mac 的“系统设置 → 隐私与安全性 → 辅助功能”中允许 Cage Guest Window,然后重试。", |
| 10 | requires_gui_login: "请先登录并解锁虚拟 Mac 的普通账户,然后重新打开项目。", |
| 11 | }; |
| 12 | |
| 13 | export function InitializationProjectApproval({ workspace, jobRunning, onClose, onApproved = onClose }) { |
| 14 | const [pending, setPending] = useState(null); |
| 15 | const [error, setError] = useState(""); |
| 16 | const [requesting, setRequesting] = useState(false); |
| 17 | const approved = useRef(onApproved); |
| 18 | approved.current = onApproved; |
| 19 | |
| 20 | useEffect(() => { |
| 21 | if (jobRunning || pending || error) return; |
| 22 | let live = true, timer; |
| 23 | async function open() { |
| 24 | setRequesting(true); |
| 25 | try { |
| 26 | const result = await api("/projects/open", { workspace, add: false, activate: false }); |
| 27 | if (!live) return; |
| 28 | if (result.approval) setPending({ workspace: result.workspace, grant: result.approval }); |
| 29 | else approved.current(); |
| 30 | } catch (e) { |
| 31 | if (!live) return; |
| 32 | if (e.recovery === "project-operation-busy") timer = setTimeout(open, 1200); |
| 33 | else setError(e.message); |
| 34 | } finally { |
| 35 | if (live) setRequesting(false); |
| 36 | } |
| 37 | } |
| 38 | open(); |
| 39 | return () => { live = false; clearTimeout(timer); setRequesting(false); }; |
| 40 | }, [workspace, jobRunning, pending, error]); |
| 41 | |
| 42 | if (pending) return <MacOSProjectApproval pending={pending} onClose={onClose} onCheck={async () => { |
| 43 | const result = await api("/projects/open", { workspace, add: false, activate: false }); |
| 44 | if (result.approval) setPending({ workspace: result.workspace, grant: result.approval }); |
| 45 | else onApproved(); |
| 46 | }} />; |
| 47 | return <Modal title="允许客机访问所选项目" onClose={requesting ? undefined : onClose} footer={<> |
| 48 | <button disabled={requesting} onClick={onClose}>取消</button> |
| 49 | {error && <button onClick={() => setError("")}>重新请求授权</button>} |
| 50 | </>}> |
| 51 | <p className="macos-approval-path">{workspace}</p> |
| 52 | {error ? <p role="alert">{error}</p> : <p role="status">{jobRunning ? "当前客机操作结束后,会显示项目授权窗口。" : "正在连接客机的项目授权窗口…"}</p>} |
| 53 | </Modal>; |
| 54 | } |
| 55 | |
| 56 | export function MacOSProjectApproval({ pending, onCheck, onClose, onSettings }) { |
| 57 | const [frame, setFrame] = useState(null), |
| 58 | [notice, setNotice] = useState(""); |
| 59 | const [busy, setBusy] = useState(false); |
| 60 | const [actionError, setActionError] = useState(""); |
| 61 | const [ended, setEnded] = useState(false); |
| 62 | const [maintenance, setMaintenance] = useState(""); |
| 63 | const active = useRef(false), |
| 64 | inFlight = useRef(null); |
| 65 | const call = (action, fields = {}) => |
| 66 | api("/macos/project-approval", { |
| 67 | workspace: pending.workspace, |
| 68 | grant: pending.grant, |
| 69 | action, |
| 70 | ...fields, |
| 71 | }); |
| 72 | |
| 73 | useEffect(() => { |
| 74 | let live = true, |
| 75 | timer; |
| 76 | async function poll() { |
| 77 | if (!active.current) { |
| 78 | try { |
| 79 | const request = call("frame"); |
| 80 | inFlight.current = request; |
| 81 | const result = await request; |
| 82 | if (live && !active.current) { |
| 83 | if (result.error === "project_screen_permission") setMaintenance("screen"); |
| 84 | if (result.error === "accessibility_permission") setMaintenance("input"); |
| 85 | const valid = Boolean( |
| 86 | !result.error && |
| 87 | result.image && |
| 88 | result.window && |
| 89 | result.pid && |
| 90 | result.nonce, |
| 91 | ); |
| 92 | setFrame(valid ? result : null); |
| 93 | setNotice( |
| 94 | valid |
| 95 | ? "" |
| 96 | : permissionMessages[result.error] || "等待虚拟 Mac 的项目授权窗口。已允许此文件夹时,可以直接验证授权。", |
| 97 | ); |
| 98 | } |
| 99 | } catch (e) { |
| 100 | if (live && !active.current) { |
| 101 | setFrame(null); |
| 102 | setNotice(e.message); |
| 103 | if (e.recovery === "project-approval-ended") { |
| 104 | setEnded(true); |
| 105 | return; |
| 106 | } |
| 107 | } |
| 108 | } finally { |
| 109 | inFlight.current = null; |
| 110 | } |
| 111 | } |
| 112 | if (live) timer = setTimeout(poll, 650); |
| 113 | } |
| 114 | setFrame(null); |
| 115 | setEnded(false); |
| 116 | setMaintenance(""); |
| 117 | setNotice(""); |
| 118 | setActionError(""); |
| 119 | poll(); |
| 120 | return () => { |
| 121 | live = false; |
| 122 | clearTimeout(timer); |
| 123 | }; |
| 124 | }, [pending.workspace, pending.grant]); |
| 125 | |
| 126 | async function act(fn) { |
| 127 | if (active.current) return; |
| 128 | active.current = true; |
| 129 | setBusy(true); |
| 130 | setActionError(""); |
| 131 | try { |
| 132 | await inFlight.current?.catch(() => {}); |
| 133 | await fn(); |
| 134 | } catch (e) { |
| 135 | setFrame(null); |
| 136 | if (e.recovery === "project-approval-ended") setEnded(true); |
| 137 | setActionError(e.message); |
| 138 | } finally { |
| 139 | active.current = false; |
| 140 | setBusy(false); |
| 141 | } |
| 142 | } |
| 143 | const cancel = () => |
| 144 | act(async () => { |
| 145 | try { |
| 146 | await call("cancel"); |
| 147 | } catch (error) { |
| 148 | if (error.recovery !== "project-approval-ended") throw error; |
| 149 | } |
| 150 | onClose(); |
| 151 | }); |
| 152 | const click = (event) => { |
| 153 | if (!frame) return; |
| 154 | const rect = event.currentTarget.getBoundingClientRect(); |
| 155 | const x = (event.clientX - rect.left) / rect.width, |
| 156 | y = (event.clientY - rect.top) / rect.height; |
| 157 | if (!(x >= 0 && x < 1 && y >= 0 && y < 1)) return; |
| 158 | void act(async () => { |
| 159 | const result = await call("click", { |
| 160 | window: frame.window, |
| 161 | pid: frame.pid, |
| 162 | nonce: frame.nonce, |
| 163 | x, |
| 164 | y, |
| 165 | }); |
| 166 | setFrame(null); |
| 167 | if (result.error === "accessibility_permission") setMaintenance("input"); |
| 168 | if (result.error) throw new Error(permissionMessages[result.error] || "客机授权窗口已变化,请等待新画面。"); |
| 169 | await onCheck(); |
| 170 | }); |
| 171 | }; |
| 172 | return ( |
| 173 | <Modal |
| 174 | title="允许客机访问所选项目" |
| 175 | onClose={busy ? undefined : cancel} |
| 176 | footer={ |
| 177 | <> |
| 178 | <button disabled={busy} onClick={cancel}> |
| 179 | 取消 |
| 180 | </button> |
| 181 | <button disabled={busy} onClick={() => act(onCheck)}> |
| 182 | {ended ? "重新请求授权" : "验证授权"} |
| 183 | </button> |
| 184 | </> |
| 185 | } |
| 186 | > |
| 187 | <p>在下方的 macOS 授权窗口中确认所选项目。</p> |
| 188 | <p className="macos-approval-path">{pending.workspace}</p> |
| 189 | {frame && ( |
| 190 | <img |
| 191 | src={"data:image/png;base64," + frame.image} |
| 192 | alt="虚拟 Mac 的当前项目授权窗口" |
| 193 | onClick={click} |
| 194 | className="macos-approval-frame" |
| 195 | draggable={false} |
| 196 | /> |
| 197 | )} |
| 198 | {notice && <p role="status">{notice}</p>} |
| 199 | {maintenance && !ended && <MacOSLogin maintenance={maintenance} onSettings={onSettings} onSessionClosed={() => void act(onCheck)} />} |
| 200 | {actionError && <p role="alert">{actionError}</p>} |
| 201 | </Modal> |
| 202 | ); |
| 203 | } |