Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

usePreviewBridge.ts

160 lines5.4 KB

1import type {
2 DesktopPreviewTabState,
3 PreviewReportStatusInput,
4 ScopedThreadRef,
5 ThreadId,
6} from "@t3tools/contracts";
7import { useEffect, useEffectEvent, useRef } from "react";
8​
9import { recordFaviconForThread } from "~/browserFaviconStore";
10import { useBrowserPointerStore } from "~/browser/browserPointerStore";
11import { applyPreviewDesktopState, type DesktopPreviewOverlay } from "~/previewStateStore";
12import { previewEnvironment } from "~/state/preview";
13import { useAtomCommand } from "~/state/use-atom-command";
14​
15import { previewBridge } from "./previewBridge";
16​
17function originOf(url: string): string | null {
18 try {
19 return new URL(url).origin;
20 } catch {
21 return null;
22 }
24​
25/** ElectronBrowserHost owns webview lifetime. */
26export function usePreviewBridge(input: {
27 threadRef: ScopedThreadRef;
28 tabId: string;
29 runtimeTabId: string;
30 serverDriven?: boolean;
31}): void {
32 const { threadRef, tabId, runtimeTabId, serverDriven = false } = input;
33 const clearBrowserPointer = useBrowserPointerStore((state) => state.clear);
34 const reportStatus = useAtomCommand(previewEnvironment.reportStatus, "preview status report");
35 const { environmentId, threadId } = threadRef;
36 const stableThreadRef: ScopedThreadRef = { environmentId, threadId };
37​
38 const lastReportedUrl = useRef<string | null>(null);
39 const lastReportedKind = useRef<DesktopPreviewTabState["navStatus"]["kind"] | null>(null);
40 const lastDesktopNavStatus = useRef<DesktopPreviewTabState["navStatus"] | null>(null);
41 const handleStateChange = useEffectEvent(
42 (changedTabId: string, state: DesktopPreviewTabState): void => {
43 if (changedTabId !== runtimeTabId) return;
44 if (shouldClearBrowserPointer(lastDesktopNavStatus.current, state.navStatus)) {
45 clearBrowserPointer(runtimeTabId);
46 }
47 lastDesktopNavStatus.current = state.navStatus;
48 applyPreviewDesktopState(stableThreadRef, tabId, projectDesktopState(state));
49 if (state.favicon) {
50 recordFaviconForThread(stableThreadRef, state.favicon);
51 }
52 const reported = buildReportInput({
53 threadId: stableThreadRef.threadId,
54 tabId,
55 state,
56 lastReportedUrl: lastReportedUrl.current,
57 lastReportedKind: lastReportedKind.current,
58 });
59 if (!reported || serverDriven) return;
60 lastReportedUrl.current = reported.lastReportedUrl;
61 lastReportedKind.current = reported.lastReportedKind;
62 void reportStatus({
63 environmentId: stableThreadRef.environmentId,
64 input: reported.input,
65 });
66 },
67 );
68 useEffect(() => {
69 if (!previewBridge || typeof window === "undefined") return;
70 lastReportedUrl.current = null;
71 lastReportedKind.current = null;
72 lastDesktopNavStatus.current = null;
73 return previewBridge.onStateChange(handleStateChange);
74 }, [
75 // oxlint-disable-next-line react/exhaustive-effect-dependencies -- A different tab or thread starts its status reports and pointer tracking from scratch.
76 runtimeTabId,
77 stableThreadRef,
78 tabId,
79 ]);
81​
82function shouldClearBrowserPointer(
83 previous: DesktopPreviewTabState["navStatus"] | null,
84 current: DesktopPreviewTabState["navStatus"],
85): boolean {
86 if (!previous) return false;
87 if (current.kind === "Loading" && previous.kind !== "Loading") return true;
88 if (current.kind === "Idle" || previous.kind === "Idle") return false;
89 return current.url !== previous.url;
91​
92export function projectDesktopState(state: DesktopPreviewTabState): DesktopPreviewOverlay {
93 const navOrigin = state.navStatus.kind === "Idle" ? null : originOf(state.navStatus.url);
94 return {
95 hasWebContents: state.webContentsId !== null,
96 canGoBack: state.canGoBack,
97 canGoForward: state.canGoForward,
98 loading: state.navStatus.kind === "Loading",
99 zoomFactor: state.zoomFactor,
100 pictureInPicture: state.pictureInPicture,
101 colorScheme: state.colorScheme,
102 audioMuted: state.audioMuted,
103 audible: state.audible,
104 controller: state.controller,
105 favicon: state.favicon && originOf(state.favicon.pageUrl) === navOrigin ? state.favicon : null,
106 };
108​
109function buildReportInput(args: {
110 readonly threadId: ThreadId;
111 readonly tabId: string;
112 readonly state: DesktopPreviewTabState;
113 readonly lastReportedUrl: string | null;
114 readonly lastReportedKind: DesktopPreviewTabState["navStatus"]["kind"] | null;
115}): {
116 readonly input: PreviewReportStatusInput;
117 readonly lastReportedUrl: string;
118 readonly lastReportedKind: DesktopPreviewTabState["navStatus"]["kind"];
119} | null {
120 const { threadId, tabId, state, lastReportedUrl, lastReportedKind } = args;
121 const status = state.navStatus;
122 if (status.kind === "Idle") return null;
123​
124 const sameAsLast =
125 status.kind !== "LoadFailed" &&
126 status.kind === lastReportedKind &&
127 status.url === lastReportedUrl;
128 if (sameAsLast) return null;
129​
130 const base = {
131 threadId,
132 tabId,
133 canGoBack: state.canGoBack,
134 canGoForward: state.canGoForward,
135 };
136 if (status.kind === "LoadFailed") {
137 return {
138 input: {
139 ...base,
140 navStatus: {
141 _tag: "LoadFailed",
142 url: status.url,
143 title: status.title,
144 code: status.code,
145 description: status.description,
146 },
147 },
148 lastReportedUrl: status.url,
149 lastReportedKind: "LoadFailed",
150 };
151 }
152 return {
153 input: {
154 ...base,
155 navStatus: { _tag: status.kind, url: status.url, title: status.title },
156 },
157 lastReportedUrl: status.url,
158 lastReportedKind: status.kind,
159 };