Open History to see the last commit
1"use client";
2
3import type { ScopedThreadRef } from "@t3tools/contracts";
4import { useShallow } from "zustand/react/shallow";
5import { useCallback, useEffect, useRef } from "react";
6
7import { previewBridge } from "~/components/preview/previewBridge";
8import { usePreviewBridge } from "~/components/preview/usePreviewBridge";
9
10import { useActiveBrowserRecordingTabId } from "./browserRecording";
11import { useBrowserSurfaceStore } from "./browserSurfaceStore";
12import { acquireDesktopTab, type AcquiredDesktopTab } from "./desktopTabLifetime";
13import { usePreviewWebviewConfig } from "./previewWebviewConfigState";
14
15interface ElectronWebview extends HTMLElement {
16 src: string;
17 partition: string;
18 preload?: string;
19 webpreferences?: string;
20 getWebContentsId: () => number;
21}
22
23declare global {
24 interface HTMLElementTagNameMap {
25 webview: ElectronWebview;
26 }
27}
28
29export function HostedBrowserWebview(props: {
30 readonly threadRef: ScopedThreadRef;
31 readonly tabId: string;
32 readonly initialUrl: string | null;
33}) {
34 const { threadRef, tabId, initialUrl } = props;
35 const config = usePreviewWebviewConfig(threadRef.environmentId);
36 const initialSrcRef = useRef(initialUrl ?? "about:blank");
37 const tabLeaseRef = useRef<AcquiredDesktopTab | null>(null);
38 const webviewRef = useRef<ElectronWebview | null>(null);
39 const presentation = useBrowserSurfaceStore(useShallow((state) => state.byTabId[tabId] ?? null));
40 const recording = useActiveBrowserRecordingTabId() === tabId;
41
42 usePreviewBridge({ threadRef, tabId });
43
44 useEffect(() => {
45 const lease = acquireDesktopTab(tabId);
46 tabLeaseRef.current = lease;
47 return () => {
48 if (tabLeaseRef.current === lease) tabLeaseRef.current = null;
49 lease.release();
50 };
51 }, [tabId]);
52
53 const setWebviewRef = useCallback((node: HTMLElement | null) => {
54 webviewRef.current = node as ElectronWebview | null;
55 if (node && !node.hasAttribute("allowpopups")) node.setAttribute("allowpopups", "true");
56 }, []);
57
58 useEffect(() => {
59 const webview = webviewRef.current;
60 const bridge = previewBridge;
61 if (!webview || !config || !bridge) return;
62 let disposed = false;
63 const register = () => {
64 const lease = tabLeaseRef.current;
65 if (!lease) return;
66 void (async () => {
67 try {
68 // The main-process tab and the DOM webview are created by separate
69 // effects. Wait for the former so registration cannot race and fail
70 // with PreviewTabNotFoundError on a fast about:blank attachment.
71 await lease.ready;
72 if (disposed || webviewRef.current !== webview) return;
73 const webContentsId = webview.getWebContentsId();
74 if (Number.isInteger(webContentsId) && webContentsId > 0) {
75 await bridge.registerWebview(tabId, webContentsId);
76 }
77 } catch {
78 // did-attach/dom-ready will retry if the guest was not ready yet.
79 }
80 })();
81 };
82 webview.addEventListener("did-attach", register);
83 webview.addEventListener("dom-ready", register);
84 register();
85 return () => {
86 disposed = true;
87 webview.removeEventListener("did-attach", register);
88 webview.removeEventListener("dom-ready", register);
89 };
90 }, [config, tabId]);
91
92 if (!config) return null;
93 const active = presentation?.visible === true && presentation.rect !== null;
94 const lastRect = presentation?.rect;
95 const style =
96 active && lastRect
97 ? {
98 left: lastRect.x,
99 top: lastRect.y,
100 width: lastRect.width,
101 height: lastRect.height,
102 zIndex: 30,
103 pointerEvents: "auto" as const,
104 }
105 : {
106 left: 0,
107 top: 0,
108 width: lastRect?.width ?? 1280,
109 height: lastRect?.height ?? 800,
110 zIndex: recording ? 0 : -1,
111 pointerEvents: "none" as const,
112 };
113
114 return (
115 <webview
116 ref={setWebviewRef}
117 src={initialSrcRef.current}
118 partition={config.partition}
119 webpreferences={config.webPreferences}
120 {...(config.preloadUrl ? { preload: config.preloadUrl } : {})}
121 data-preview-tab={tabId}
122 aria-hidden={active ? undefined : true}
123 className="fixed flex overflow-hidden bg-background"
124 style={style}
125 />
126 );
127}