| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| import { useEffect, useRef, useState, type ReactNode } from "react"; |
|
|
| const LOCK_NAME = "agent-gui-single-tab"; |
| const TAKEOVER_CHANNEL = "agent-gui-tab-takeover"; |
|
|
| type GateStatus = "pending" | "active" | "blocked" | "deactivated"; |
|
|
| const hasLocks = typeof navigator !== "undefined" && "locks" in navigator; |
|
|
| export default function SingleTabGate({ children }: { children: ReactNode }) { |
| const [status, setStatus] = useState<GateStatus>(hasLocks ? "pending" : "active"); |
| |
| const releaseRef = useRef<(() => void) | null>(null); |
|
|
| useEffect(() => { |
| if (!hasLocks) return; |
| const abort = new AbortController(); |
| const bc = new BroadcastChannel(TAKEOVER_CHANNEL); |
| let dead = false; |
|
|
| |
| |
| navigator.locks |
| .request(LOCK_NAME, { signal: abort.signal }, async () => { |
| if (dead) return; |
| setStatus("active"); |
| await new Promise<void>((resolve) => { releaseRef.current = resolve; }); |
| }) |
| .catch(() => { }); |
|
|
| |
| |
| navigator.locks.query().then((state) => { |
| if (dead) return; |
| if (state.held?.some((l) => l.name === LOCK_NAME)) { |
| setStatus((s) => (s === "pending" ? "blocked" : s)); |
| } |
| }).catch(() => {}); |
|
|
| bc.onmessage = (e) => { |
| |
| |
| |
| if (e.data === "takeover" && releaseRef.current) { |
| releaseRef.current(); |
| releaseRef.current = null; |
| setStatus("deactivated"); |
| } |
| }; |
|
|
| return () => { |
| dead = true; |
| abort.abort(); |
| releaseRef.current?.(); |
| releaseRef.current = null; |
| bc.close(); |
| }; |
| }, []); |
|
|
| if (status === "active") return <>{children}</>; |
| if (status === "pending") return null; |
|
|
| const blocked = status === "blocked"; |
| return ( |
| <div style={{ |
| height: "100vh", display: "flex", flexDirection: "column", |
| alignItems: "center", justifyContent: "center", gap: 14, |
| background: "var(--bg)", color: "var(--text)", textAlign: "center", padding: 24, |
| }}> |
| <div style={{ fontSize: 42 }}>{blocked ? "๐" : "๐"}</div> |
| <div style={{ fontSize: 18, fontWeight: 600 }}> |
| {blocked |
| ? "AgentGUI is already open in another tab or window" |
| : "This session moved to another tab"} |
| </div> |
| <div style={{ color: "var(--text-dim)", maxWidth: 440, fontSize: 13, lineHeight: 1.5 }}> |
| {blocked |
| ? "Running two copies splits the live agent streams between them and " + |
| "doubles manager patrols, so only one tab can be active at a time. " + |
| "Close the other tab to continue here, or take over now." |
| : "Another tab took over this AgentGUI session. Reload to claim it back."} |
| </div> |
| {blocked ? ( |
| <button |
| onClick={() => { |
| // The waiting lock request from the effect is already queued; the |
| // holder releases on this message and we activate automatically. |
| const c = new BroadcastChannel(TAKEOVER_CHANNEL); |
| c.postMessage("takeover"); |
| c.close(); |
| }} |
| style={btnStyle} |
| > |
| Use here instead |
| </button> |
| ) : ( |
| <button onClick={() => location.reload()} style={btnStyle}> |
| Reload & claim this tab |
| </button> |
| )} |
| </div> |
| ); |
| } |
|
|
| const btnStyle: React.CSSProperties = { |
| background: "var(--accent2)", color: "#fff", border: "none", |
| borderRadius: "var(--radius)", padding: "10px 18px", fontSize: 14, |
| fontWeight: 600, cursor: "pointer", |
| }; |
|
|