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

PreviewView.tsx

1,165 lines43.6 KB

1import { useAtomValue } from "@effect/atom-react";
2import { scopedThreadKey } from "@t3tools/client-runtime/environment";
3import { settlePromise } from "@t3tools/client-runtime/state/runtime";
4import type { DesktopPreviewAnnotationVoiceConfig } from "@t3tools/contracts";
5import { previewStreamDownloadUrl } from "@t3tools/client-runtime/preview/server-browser-stream";
6import {
7 isAtomCommandInterrupted,
8 squashAtomCommandFailure,
9} from "@t3tools/client-runtime/state/runtime";
10import {
11 AuthOrchestrationOperateScope,
12 DEFAULT_BROWSER_PROFILE_ID,
13 INCOGNITO_BROWSER_PROFILE_ID,
14 FILL_PREVIEW_VIEWPORT,
15 type PreviewAnnotationPayload,
16 type ScopedThreadRef,
17 DEFAULT_PREVIEW_ZOOM_FACTOR,
18 PREVIEW_ZOOM_LEVELS,
19 type PreviewAdjustInput,
20} from "@t3tools/contracts";
21import {
22 normalizePreviewUrl,
23 isPreviewUrlNormalizationError,
24 resolveAddressBarInput,
25 previewPdfAssetUrl,
26 resolveSearchUrlTemplate,
27} from "@t3tools/shared/preview";
28import { useEffect, useEffectEvent, useRef, useState } from "react";
29​
30import {
31 BROWSER_HISTORY_MAX_ENTRIES_PER_PROJECT,
32 recordVisitForThread,
33 removeUrlForThread,
34 useThreadRecentHistory,
35} from "~/browserHistoryStore";
36import { type ComposerImageAttachment, useComposerDraftStore } from "~/composerDraftStore";
37import { capturePreviewAnnotationScreenshot } from "~/lib/previewAnnotation";
38import { ensureLocalApi } from "~/localApi";
39import { getClientSettings } from "~/hooks/useSettings";
40import {
41 rememberPreviewUrl,
42 updatePreviewServerSnapshot,
43 useThreadPreviewState,
44} from "~/previewStateStore";
45import { resolveDiscoveredServerUrl } from "~/browser/browserTargetResolver";
46import { useEnvironmentSupportsServerBrowser } from "~/state/entities";
47import {
48 useEnvironment,
49 useEnvironmentHttpBaseUrl,
50 usePrimaryEnvironmentId,
51} from "~/state/environments";
52import { previewEnvironment } from "~/state/preview";
53import { usePreviewStreamAccess } from "~/state/previewStream";
54import { useAtomCommand } from "~/state/use-atom-command";
55import {
56 browserMiniPlayerSource,
57 selectThreadPreviewMiniPlayerTabId,
58 usePreviewMiniPlayerStore,
59} from "~/previewMiniPlayerStore";
60import { readEnvironmentScope, useEnvironmentScope } from "~/state/session";
61import { useRightPanelStore } from "~/rightPanelStore";
62​
63import { effectiveShortcutsForCommand, shortcutLabelForCommand } from "~/keybindings";
64import { randomUUID } from "~/lib/utils";
65import { primaryServerKeybindingsAtom } from "~/state/server";
66import { PreviewAnnotationSpeech } from "./PreviewAnnotationSpeech";
67import { previewBridge } from "./previewBridge";
68import { subscribePreviewAction } from "./previewActionBus";
69import { closePreviewSession } from "./closePreviewSession";
70import { openPreviewSession } from "./openPreviewSession";
71import { showPreviewPopup } from "./showPreviewPopup";
72import { PreviewChromeRow } from "./PreviewChromeRow";
73import { PreviewEmptyState } from "./PreviewEmptyState";
74import { PreviewMoreMenu, type PreviewMoreMenuActions } from "./PreviewMoreMenu";
75import { commitBrowserViewportChange } from "~/browser/browserViewportActions";
76import { browserResponsiveViewportForToggle, useBrowserDefaults } from "~/browser/browserDefaults";
77import { BrowserDeviceToolbar } from "~/browser/BrowserDeviceToolbar";
78import { BROWSER_DEVICE_TOOLBAR_HEIGHT } from "~/browser/browserViewportLayout";
79import { useBrowserLayoutZoomFactor } from "~/browser/useBrowserLayoutZoomFactor";
80import { previewRuntimeTabId } from "~/browser/previewRuntimeTabId";
81import { isBrowserSettingsReadError } from "~/browser/openFileInPreview";
82import { PreviewFileNotShown, PreviewUnreachable } from "./PreviewUnreachable";
83import { revealInFileExplorerLabel } from "./fileExplorerLabel";
84import { shouldShowPreviewEmptyState } from "./previewEmptyStateLogic";
85import { Badge } from "~/components/ui/badge";
86import { BrowserSurfaceSlot } from "~/browser/BrowserSurfaceSlot";
87import { alternatePreviewRuntime, useRendersServerTabNatively } from "~/browser/previewRuntime";
88import { ServerBrowserSurface, type ServerBrowserHandle } from "~/browser/ServerBrowserSurface";
89import { cn } from "~/lib/utils";
90import { useBrowserSurfaceStore } from "~/browser/browserSurfaceStore";
91import { usePreviewSession } from "./usePreviewSession";
92import { ZoomIndicator } from "./ZoomIndicator";
93import { AgentBrowserCursor } from "./AgentBrowserCursor";
94import { PreviewFindBar } from "./PreviewFindBar";
95import {
96 findActiveBrowserRecordingRuntimeTabId,
97 isBrowserRecordingStartCancelledError,
98 startBrowserRecording,
99 stopBrowserRecording,
100 useActiveBrowserRecordingTabIds,
101} from "~/browser/browserRecording";
102import { stackedThreadToast, toastManager } from "~/components/ui/toast";
103import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
104import { observeResize } from "~/lib/observeResize";
105​
106interface Props {
107 threadRef: ScopedThreadRef;
108 tabId?: string | null;
109 configuredUrls?: ReadonlyArray<string> | undefined;
110 visible: boolean;
111 onSendAnnotation?: (
112 annotation: PreviewAnnotationPayload,
113 image: ComposerImageAttachment | null,
114 ) => void;
116​
117function previewProfileName(
118 profiles: ReadonlyArray<{ readonly id: string; readonly name: string }>,
119 profileId: string,
120): string {
121 return profiles.find((profile) => profile.id === profileId)?.name ?? "Removed profile";
123​
124const localApi = typeof window === "undefined" ? null : ensureLocalApi();
125​
126function ServerTabFileNotShown(props: {
127 readonly threadRef: ScopedThreadRef;
128 readonly tabId: string;
129 readonly url: string;
130 readonly download: { readonly id: string; readonly fileName: string };
131}) {
132 const access = usePreviewStreamAccess(props.threadRef.environmentId);
133 return (
134 <PreviewFileNotShown
135 url={props.url}
136 fileName={props.download.fileName}
137 downloadUrl={
138 access
139 ? previewStreamDownloadUrl(
140 { access, threadId: props.threadRef.threadId, tabId: props.tabId },
141 props.download.id,
142 )
143 : null
144 }
145 onOpen={() => void localApi?.shell.openExternal(props.url).catch(() => undefined)}
146 />
147 );
149​
150function focusPreviewWebview(runtimeTabId: string) {
151 document
152 .querySelector<HTMLElement>(`[data-preview-viewport="${CSS.escape(runtimeTabId)}"] webview`)
153 ?.focus();
155​
156function focusPreviewPage(runtimeTabId: string) {
157 const active = document.activeElement;
158 if (active && active !== document.body && !active.closest("[data-preview-panel-mode]")) return;
159 focusPreviewWebview(runtimeTabId);
161​
162export function PreviewView({
163 threadRef,
164 tabId: requestedTabId,
165 configuredUrls,
166 visible,
167 onSendAnnotation,
168}: Props) {
169 const urlInputRef = useRef<HTMLInputElement | null>(null);
170 const [find, setFind] = useState<{ tabId: string; requestId: number } | null>(null);
171 const [pickActive, setPickActive] = useState(false);
172 const canSendAnnotation =
173 useEnvironmentScope(threadRef.environmentId, AuthOrchestrationOperateScope) &&
174 Boolean(onSendAnnotation);
175 const activeRecordingTabIds = useActiveBrowserRecordingTabIds();
176 const pickActiveRef = useRef<{ cancelled: boolean } | null>(null);
177 const [annotationVoice, setAnnotationVoice] =
178 useState<DesktopPreviewAnnotationVoiceConfig | null>(null);
179 const keybindings = useAtomValue(primaryServerKeybindingsAtom);
180 const previewState = useThreadPreviewState(threadRef);
181 const recentHistoryEntries = useThreadRecentHistory(
182 threadRef,
183 BROWSER_HISTORY_MAX_ENTRIES_PER_PROJECT,
184 );
185 const miniPlayerTabId = usePreviewMiniPlayerStore((state) =>
186 selectThreadPreviewMiniPlayerTabId(state.byThreadKey, threadRef),
187 );
188 const addPreviewAnnotation = useComposerDraftStore((store) => store.addPreviewAnnotation);
189 const addImage = useComposerDraftStore((store) => store.addImage);
190 const environmentHttpBaseUrl = useEnvironmentHttpBaseUrl(threadRef.environmentId);
191 const open = useAtomCommand(previewEnvironment.open);
192 const closePreview = useAtomCommand(previewEnvironment.close, "preview close");
193 const environmentLabel = useEnvironment(threadRef.environmentId)?.label ?? "the environment";
194 const primaryEnvironmentId = usePrimaryEnvironmentId();
195 const serverBrowser = useEnvironmentSupportsServerBrowser(threadRef.environmentId);
196 const adjust = useAtomCommand(previewEnvironment.adjust, "preview appearance or zoom");
197​
198 usePreviewSession(threadRef);
199​
200 const tabId = requestedTabId ?? previewState.activeTabId;
201 const runtimeTabId = tabId
202 ? previewRuntimeTabId(threadRef, previewState.serverEpoch, tabId)
203 : null;
204 if (find !== null && find.tabId !== runtimeTabId) setFind(null);
205 const recordingRuntimeTabId =
206 tabId && runtimeTabId
207 ? activeRecordingTabIds.has(runtimeTabId)
208 ? runtimeTabId
209 : findActiveBrowserRecordingRuntimeTabId(threadRef, tabId)
210 : null;
211 const snapshot = tabId ? (previewState.sessions[tabId] ?? null) : null;
212 const nativeServerTab = useRendersServerTabNatively(threadRef.environmentId, snapshot);
213 const isServerTab = snapshot?.runtime === "server" && !nativeServerTab;
214 const serverOwnsRendering = snapshot?.runtime === "server";
215 const serverSurfaceRef = useRef<ServerBrowserHandle | null>(null);
216 const serverSurfaceBoxRef = useRef<HTMLDivElement | null>(null);
217 const [serverAspectRatioLocked, setServerAspectRatioLocked] = useState(false);
218 const [serverToolbarWidth, setServerToolbarWidth] = useState(0);
219 const serverToolbarShown =
220 snapshot?.runtime === "server" && (snapshot.viewport ?? FILL_PREVIEW_VIEWPORT)._tag !== "fill";
221 const serverBodyRef = useRef<HTMLDivElement | null>(null);
222 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- ResizeObserver on the server preview body sizes the viewport toolbar
223 useEffect(() => {
224 const element = serverBodyRef.current;
225 if (!element || !serverToolbarShown) return;
226 return observeResize(element, ([entry]) =>
227 setServerToolbarWidth(Math.max(1, Math.round(entry?.contentRect.width ?? 0))),
228 );
229 }, [serverToolbarShown]);
230 const [serverControlledTabId, setServerControlledTabId] = useState<string | null>(null);
231 const serverInputDisabled = isServerTab && serverControlledTabId !== runtimeTabId;
232 const [serverFrameTabId, setServerFrameTabId] = useState<string | null>(null);
233 // A desktop page that mounted before its tab went headless leaves its stale state here.
234 const desktopOverlay =
235 tabId && !isServerTab ? (previewState.desktopByTabId[tabId] ?? null) : null;
236 const layoutZoomFactor = useBrowserLayoutZoomFactor(desktopOverlay?.zoomFactor ?? 1);
237 const navStatus = snapshot?.navStatus ?? { _tag: "Idle" as const };
238 const url = navStatus._tag === "Idle" ? "" : navStatus.url;
239 const loading = desktopOverlay?.loading ?? navStatus._tag === "Loading";
240 const canGoBack = desktopOverlay?.canGoBack ?? snapshot?.canGoBack ?? false;
241 const canGoForward = desktopOverlay?.canGoForward ?? snapshot?.canGoForward ?? false;
242 const refreshDisabled = navStatus._tag === "Idle";
243 const isUnreachable = navStatus._tag === "LoadFailed";
244 const showEmptyState = shouldShowPreviewEmptyState(snapshot);
245 const serverStreamPending =
246 isServerTab && !showEmptyState && !isUnreachable && serverFrameTabId !== runtimeTabId;
247 const controller = desktopOverlay?.controller ?? "none";
248 const viewport = snapshot?.viewport ?? FILL_PREVIEW_VIEWPORT;
249 const browserDefaults = useBrowserDefaults();
250 // IPC interprets undefined profile ids as all profiles.
251 const activeProfileId = snapshot?.profileId ?? DEFAULT_BROWSER_PROFILE_ID;
252 const activeProfileName = previewProfileName(browserDefaults.profiles, activeProfileId);
253 const panelRect = useBrowserSurfaceStore((state) =>
254 runtimeTabId ? (state.byTabId[runtimeTabId]?.rect ?? null) : null,
255 );
256​
257 /** Resolves to the profile of the tab that loaded the URL, or null when nothing opened. */
258 const navigateToResolvedUrl = async (resolvedUrl: string) => {
259 if (isServerTab && serverSurfaceRef.current) {
260 if (serverInputDisabled) return null;
261 serverSurfaceRef.current.navigate(resolvedUrl);
262 rememberPreviewUrl(threadRef, resolvedUrl);
263 return activeProfileId;
264 }
265 if (runtimeTabId && previewBridge) {
266 await previewBridge.navigate(runtimeTabId, resolvedUrl);
267 rememberPreviewUrl(threadRef, resolvedUrl);
268 focusPreviewPage(runtimeTabId);
269 return activeProfileId;
270 }
271 const result = await openPreviewSession({ openPreview: open, threadRef, url: resolvedUrl });
272 if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
273 const error = squashAtomCommandFailure(result);
274 if (isBrowserSettingsReadError(error)) {
275 toastManager.add({
276 type: "error",
277 title: "Unable to open browser",
278 description: error.message,
279 });
280 }
281 }
282 return result._tag === "Success"
283 ? (result.value.profileId ?? DEFAULT_BROWSER_PROFILE_ID)
284 : null;
285 };
286​
287 // Incognito visits stay out of the project's persisted history.
288 const isHistoryProfile = (profileId: string | null) =>
289 profileId !== null && profileId !== INCOGNITO_BROWSER_PROFILE_ID;
290​
291 const handleSubmitUrl = async (next: string) => {
292 try {
293 const resolved = resolveAddressBarInput(next, resolveSearchUrlTemplate(getClientSettings()));
294 if (isHistoryProfile(await navigateToResolvedUrl(resolved))) {
295 recordVisitForThread(threadRef, resolved);
296 }
297 } catch (error) {
298 toastManager.add({
299 type: "error",
300 title: "Could not open this address",
301 description: isPreviewUrlNormalizationError(error)
302 ? "Enter an http or https URL."
303 : error instanceof Error
304 ? error.message
305 : "An error occurred.",
306 });
307 }
308 };
309​
310 const handleOpenServerUrl = async (next: string) => {
311 try {
312 const resolved =
313 isServerTab || !previewBridge
314 ? normalizePreviewUrl(next)
315 : resolveDiscoveredServerUrl(threadRef.environmentId, next);
316 if (isHistoryProfile(await navigateToResolvedUrl(resolved))) {
317 recordVisitForThread(threadRef, next);
318 }
319 } catch (error) {
320 toastManager.add({
321 type: "error",
322 title: "Could not open this address",
323 description: isPreviewUrlNormalizationError(error)
324 ? "Enter an http or https URL."
325 : error instanceof Error
326 ? error.message
327 : "An error occurred.",
328 });
329 }
330 };
331​
332 const handleRefresh = () => {
333 if (isServerTab) serverSurfaceRef.current?.reload();
334 else if (previewBridge && runtimeTabId) void previewBridge.refresh(runtimeTabId);
335 };
336​
337 const adjustServerTab = async (change: Omit<PreviewAdjustInput, "threadId" | "tabId">) => {
338 if (!tabId) return;
339 const result = await adjust({
340 environmentId: threadRef.environmentId,
341 input: { threadId: threadRef.threadId, tabId, ...change },
342 });
343 if (result._tag === "Failure") {
344 const error = squashAtomCommandFailure(result);
345 toastManager.add({
346 type: "error",
347 title: "Unable to change the browser tab",
348 description: error instanceof Error ? error.message : "An error occurred.",
349 });
350 return;
351 }
352 updatePreviewServerSnapshot(threadRef, result.value);
353 };
354​
355 const serverZoomFactor = snapshot?.zoomFactor ?? DEFAULT_PREVIEW_ZOOM_FACTOR;
356 const stepServerZoom = (direction: -1 | 0 | 1) => {
357 const index = PREVIEW_ZOOM_LEVELS.indexOf(serverZoomFactor);
358 const next =
359 direction === 0
360 ? DEFAULT_PREVIEW_ZOOM_FACTOR
361 : PREVIEW_ZOOM_LEVELS[
362 Math.min(
363 Math.max((index < 0 ? 7 : index) + direction, 0),
364 PREVIEW_ZOOM_LEVELS.length - 1,
365 )
366 ]!;
367 if (next !== serverZoomFactor) void adjustServerTab({ zoomFactor: next });
368 };
369​
370 const canFindInPage =
371 previewBridge !== null &&
372 runtimeTabId !== null &&
373 desktopOverlay !== null &&
374 !isServerTab &&
375 !showEmptyState &&
376 !isUnreachable;
377 const handleFind = () => {
378 if (!canFindInPage) {
379 toastManager.add({ type: "info", title: "Find on page isn't available for this page" });
380 return;
381 }
382 setFind((previous) => ({ tabId: runtimeTabId, requestId: (previous?.requestId ?? 0) + 1 }));
383 };
384​
385 // Otherwise focus falls to the body and the next find shortcut misses the preview.
386 const closeFind = () => {
387 setFind(null);
388 if (runtimeTabId) focusPreviewPage(runtimeTabId);
389 };
390​
391 const handleZoomIn = () => {
392 if (serverOwnsRendering) stepServerZoom(1);
393 else if (previewBridge && runtimeTabId) void previewBridge.zoomIn(runtimeTabId);
394 };
395​
396 const handleZoomOut = () => {
397 if (serverOwnsRendering) stepServerZoom(-1);
398 else if (previewBridge && runtimeTabId) void previewBridge.zoomOut(runtimeTabId);
399 };
400​
401 const handleResetZoom = () => {
402 if (serverOwnsRendering) stepServerZoom(0);
403 else if (previewBridge && runtimeTabId) void previewBridge.resetZoom(runtimeTabId);
404 };
405​
406 const handleToggleDeviceToolbar = () => {
407 if (!runtimeTabId || !tabId) return;
408 const target = { threadRef, tabId, runtimeTabId };
409 if (viewport._tag !== "fill") {
410 void commitBrowserViewportChange(target, FILL_PREVIEW_VIEWPORT).catch(() => undefined);
411 return;
412 }
413​
414 void commitBrowserViewportChange(
415 target,
416 browserResponsiveViewportForToggle({
417 defaults: browserDefaults,
418 // Only BrowserSurfaceSlot publishes a store rect; streamed tabs measure their own box.
419 panelRect: isServerTab
420 ? (serverSurfaceBoxRef.current?.getBoundingClientRect() ?? null)
421 : panelRect,
422 zoomFactor: isServerTab ? serverZoomFactor : layoutZoomFactor,
423 }),
424 ).catch(() => undefined);
425 };
426​
427 const handleBack = () => {
428 if (isServerTab) serverSurfaceRef.current?.history(-1);
429 else if (previewBridge && runtimeTabId) void previewBridge.goBack(runtimeTabId);
430 };
431​
432 const handleForward = () => {
433 if (isServerTab) serverSurfaceRef.current?.history(1);
434 else if (previewBridge && runtimeTabId) void previewBridge.goForward(runtimeTabId);
435 };
436​
437 const handleOpenInBrowser = () => {
438 if (!localApi || !url) return;
439 void localApi.shell.openExternal(url).catch(() => undefined);
440 };
441​
442 const moveTarget = alternatePreviewRuntime(
443 threadRef.environmentId,
444 primaryEnvironmentId,
445 serverBrowser,
446 snapshot,
447 );
448 const moveLabel =
449 moveTarget === "server" ? `Open in ${environmentLabel}'s browser` : "Open on this computer";
450 const handleMoveTab = async () => {
451 if (!moveTarget || !tabId || !snapshot) return;
452 const result = await openPreviewSession({
453 openPreview: open,
454 threadRef,
455 ...(url
456 ? {
457 url:
458 moveTarget === "desktop"
459 ? resolveDiscoveredServerUrl(threadRef.environmentId, url)
460 : url,
461 }
462 : {}),
463 viewport,
464 ...(snapshot.profileId === undefined ? {} : { profileId: snapshot.profileId }),
465 runtime: moveTarget,
466 });
467 if (result._tag === "Failure") {
468 if (isAtomCommandInterrupted(result)) return;
469 const error = squashAtomCommandFailure(result);
470 toastManager.add({
471 type: "error",
472 title: "Unable to move the browser tab",
473 description: error instanceof Error ? error.message : "An error occurred.",
474 });
475 return;
476 }
477 useRightPanelStore.getState().openBrowser(threadRef, result.value.tabId);
478 await closePreviewSession({ closePreview, snapshot, tabId, threadRef });
479 };
480​
481 const handlePictureInPicture = () => {
482 if (!tabId) return;
483 if (miniPlayerTabId === tabId) {
484 usePreviewMiniPlayerStore.getState().close(threadRef);
485 return;
486 }
487 usePreviewMiniPlayerStore.getState().open(threadRef, browserMiniPlayerSource(tabId));
488 useRightPanelStore.getState().close(threadRef);
489 };
490​
491 const desktopCall = (op: ((tabId: string) => Promise<void>) | undefined) => () => {
492 if (op && runtimeTabId) void op(runtimeTabId).catch(() => undefined);
493 };
494 const moreMenuActions: PreviewMoreMenuActions | null = serverOwnsRendering
495 ? {
496 hardReload: () => void adjustServerTab({ hardReload: true }),
497 setColorScheme: (colorScheme) => void adjustServerTab({ colorScheme }),
498 zoomIn: handleZoomIn,
499 zoomOut: handleZoomOut,
500 resetZoom: handleResetZoom,
501 clearCookies: () => void adjustServerTab({ clear: "cookies" }),
502 clearCache: () => void adjustServerTab({ clear: "cache" }),
503 ...(nativeServerTab && previewBridge
504 ? {
505 openDevTools: desktopCall(previewBridge.openDevTools),
506 toggleNativePictureInPicture: () => handleNativePictureInPicture(),
507 ...(canFindInPage ? { find: handleFind } : {}),
508 }
509 : {}),
510 }
511 : previewBridge
512 ? {
513 hardReload: desktopCall(previewBridge.hardReload),
514 setColorScheme: (colorScheme) => {
515 if (runtimeTabId)
516 void previewBridge?.setColorScheme(runtimeTabId, colorScheme).catch(() => undefined);
517 },
518 zoomIn: handleZoomIn,
519 zoomOut: handleZoomOut,
520 resetZoom: handleResetZoom,
521 clearCookies: () =>
522 void previewBridge
523 ?.clearCookies(threadRef.environmentId, activeProfileId)
524 .catch(() => undefined),
525 clearCache: () =>
526 void previewBridge
527 ?.clearCache(threadRef.environmentId, activeProfileId)
528 .catch(() => undefined),
529 openDevTools: desktopCall(previewBridge.openDevTools),
530 toggleNativePictureInPicture: () => handleNativePictureInPicture(),
531 ...(canFindInPage ? { find: handleFind } : {}),
532 }
533 : null;
534​
535 const handleNativePictureInPicture = () => {
536 if (!previewBridge || !runtimeTabId) return;
537 const operation = desktopOverlay?.pictureInPicture
538 ? previewBridge.pictureInPicture.close
539 : previewBridge.pictureInPicture.open;
540 void operation(runtimeTabId).catch((error) => {
541 toastManager.add({
542 type: "error",
543 title: "Unable to update popped-out preview",
544 description: error instanceof Error ? error.message : "An error occurred.",
545 });
546 });
547 };
548​
549 const handleCapture = (record: boolean) => {
550 if (!previewBridge || !runtimeTabId || !tabId) return;
551 const bridge = previewBridge;
552 if (recordingRuntimeTabId) {
553 void stopBrowserRecording(recordingRuntimeTabId).then(
554 (artifact) => {
555 if (!artifact) return;
556 let pathCopied = false;
557 let toastId: ReturnType<typeof toastManager.add>;
558​
559 const copyPath = () => {
560 if (!navigator.clipboard?.writeText) {
561 toastManager.update(
562 toastId,
563 stackedThreadToast({
564 type: "error",
565 title: "Unable to copy recording path",
566 description: "Clipboard API unavailable.",
567 actionProps: revealAction,
568 }),
569 );
570 return;
571 }
572​
573 void navigator.clipboard.writeText(artifact.path).then(
574 () => {
575 pathCopied = true;
576 updateRecordingToast();
577 window.setTimeout(() => {
578 pathCopied = false;
579 updateRecordingToast();
580 }, 2_000);
581 },
582 (error) => {
583 toastManager.update(
584 toastId,
585 stackedThreadToast({
586 type: "error",
587 title: "Unable to copy recording path",
588 description: error instanceof Error ? error.message : "An error occurred.",
589 actionProps: revealAction,
590 }),
591 );
592 },
593 );
594 };
595​
596 const revealAction = {
597 children: revealInFileExplorerLabel(navigator.platform),
598 onClick: () => void bridge.revealArtifact(artifact.path),
599 };
600 const updateRecordingToast = () => {
601 toastManager.update(
602 toastId,
603 stackedThreadToast({
604 type: "success",
605 title: "Recording saved",
606 actionProps: revealAction,
607 data: {
608 secondaryActionProps: {
609 children: pathCopied ? "Copied!" : "Copy path",
610 disabled: pathCopied,
611 onClick: copyPath,
612 },
613 secondaryActionVariant: "outline",
614 },
615 }),
616 );
617 };
618​
619 toastId = toastManager.add(
620 stackedThreadToast({
621 type: "success",
622 title: "Recording saved",
623 actionProps: revealAction,
624 data: {
625 secondaryActionProps: {
626 children: "Copy path",
627 onClick: copyPath,
628 },
629 secondaryActionVariant: "outline",
630 },
631 }),
632 );
633 },
634 (error) => {
635 toastManager.add({
636 type: "error",
637 title: "Unable to stop recording",
638 description: error instanceof Error ? error.message : "An error occurred.",
639 });
640 },
641 );
642 return;
643 }
644 if (record) {
645 void startBrowserRecording(runtimeTabId, threadRef, tabId).catch((error) => {
646 const description = error instanceof Error ? error.message : "An error occurred.";
647 if (isBrowserRecordingStartCancelledError(error)) return;
648 toastManager.add({
649 type: "error",
650 title: "Unable to start recording",
651 description,
652 });
653 });
654 return;
655 }
656 void bridge.captureScreenshot(runtimeTabId).then(
657 (artifact) => {
658 const revealAction = {
659 children: revealInFileExplorerLabel(navigator.platform),
660 onClick: () => void bridge.revealArtifact(artifact.path),
661 };
662 let pathCopied = false;
663 let imageCopied = false;
664 let toastId: ReturnType<typeof toastManager.add>;
665​
666 const updateScreenshotToast = (
667 type: "success" | "error" = "success",
668 title = "Screenshot saved",
669 description?: string,
670 ) => {
671 toastManager.update(
672 toastId,
673 stackedThreadToast({
674 type,
675 title,
676 description,
677 actionProps: {
678 children: imageCopied ? "Copied!" : "Copy image",
679 disabled: imageCopied,
680 onClick: copyImage,
681 },
682 data: {
683 additionalActions: [
684 {
685 id: "copy-path",
686 props: {
687 children: pathCopied ? "Copied!" : "Copy path",
688 disabled: pathCopied,
689 onClick: copyPath,
690 },
691 },
692 ],
693 secondaryActionProps: {
694 ...revealAction,
695 },
696 secondaryActionVariant: "outline",
697 },
698 }),
699 );
700 };
701​
702 const copyPath = () => {
703 if (!navigator.clipboard?.writeText) {
704 updateScreenshotToast(
705 "error",
706 "Unable to copy screenshot path",
707 "Clipboard API unavailable.",
708 );
709 return;
710 }
711​
712 void navigator.clipboard.writeText(artifact.path).then(
713 () => {
714 pathCopied = true;
715 updateScreenshotToast();
716 window.setTimeout(() => {
717 pathCopied = false;
718 updateScreenshotToast();
719 }, 2_000);
720 },
721 (error) => {
722 updateScreenshotToast(
723 "error",
724 "Unable to copy screenshot path",
725 error instanceof Error ? error.message : "An error occurred.",
726 );
727 },
728 );
729 };
730​
731 const copyImage = () => {
732 void bridge.copyArtifactToClipboard(artifact.path).then(
733 () => {
734 imageCopied = true;
735 updateScreenshotToast();
736 window.setTimeout(() => {
737 imageCopied = false;
738 updateScreenshotToast();
739 }, 2_000);
740 },
741 (error) => {
742 updateScreenshotToast(
743 "error",
744 "Unable to copy screenshot",
745 error instanceof Error ? error.message : "An error occurred.",
746 );
747 },
748 );
749 };
750​
751 toastId = toastManager.add(
752 stackedThreadToast({
753 type: "success",
754 title: "Screenshot saved",
755 actionProps: {
756 children: "Copy image",
757 onClick: copyImage,
758 },
759 data: {
760 additionalActions: [
761 {
762 id: "copy-path",
763 props: {
764 children: "Copy path",
765 onClick: copyPath,
766 },
767 },
768 ],
769 secondaryActionProps: {
770 ...revealAction,
771 },
772 secondaryActionVariant: "outline",
773 },
774 }),
775 );
776 },
777 (error) => {
778 toastManager.add({
779 type: "error",
780 title: "Unable to capture screenshot",
781 description: error instanceof Error ? error.message : "An error occurred.",
782 });
783 },
784 );
785 };
786​
787 const handlePickElement = () => {
788 if (!previewBridge || !runtimeTabId) return;
789 if (pickActiveRef.current) {
790 pickActiveRef.current.cancelled = true;
791 void previewBridge.cancelPickElement(runtimeTabId).catch(() => undefined);
792 return;
793 }
794 // Capture app focus before main moves it into guest webContents.
795 const previouslyFocused =
796 typeof document !== "undefined" ? (document.activeElement as HTMLElement | null) : null;
797 const pickRequest = { cancelled: false };
798 let submitted = false;
799 pickActiveRef.current = pickRequest;
800 setPickActive(true);
801 // The guest learns only the dictation chords, never the whole keybinding config.
802 const voice: DesktopPreviewAnnotationVoiceConfig = {
803 sessionId: randomUUID(),
804 shortcuts: effectiveShortcutsForCommand(keybindings, "composer.dictation", {
805 context: { previewFocus: true, previewOpen: true, editableFocus: true },
806 }),
807 };
808 setAnnotationVoice(voice);
809 const finishPick = () => {
810 const isCurrentPick = pickActiveRef.current === pickRequest;
811 if (isCurrentPick) {
812 pickActiveRef.current = null;
813 setPickActive(false);
814 setAnnotationVoice(null);
815 }
816 if (
817 (isCurrentPick || submitted) &&
818 previouslyFocused &&
819 previouslyFocused.isConnected &&
820 typeof previouslyFocused.focus === "function"
821 ) {
822 try {
823 previouslyFocused.focus({ preventScroll: true });
824 } catch {}
825 }
826 };
827 const bridge = previewBridge;
828 void settlePromise(async () => {
829 await bridge.setAnnotationSendEnabled(
830 runtimeTabId,
831 Boolean(onSendAnnotation) &&
832 readEnvironmentScope(threadRef.environmentId, AuthOrchestrationOperateScope),
833 );
834 if (pickRequest.cancelled) return;
835 // Main's WebContents.focus() cannot move the embedder's DOM focus into a guest, so key
836 // presses would stay in the app; only focusing the <webview> element hands them to the page.
837 focusPreviewWebview(runtimeTabId);
838 const result = await bridge.pickElement(runtimeTabId, voice);
839 if (!result || pickRequest.cancelled) return;
840 // The accepted pick must survive further clicks, tab changes, and unmount; stop cancellation here.
841 if (pickActiveRef.current === pickRequest) {
842 pickActiveRef.current = null;
843 setPickActive(false);
844 setAnnotationVoice(null);
845 submitted = true;
846 }
847 const { annotation: picked, submission, screenshotFailed = false } = result;
848 // Send annotations even if the optional crop fails or stalls; drop failed screenshots so prompts cannot
849 // claim an attachment.
850 const capture = capturePreviewAnnotationScreenshot(picked);
851 const cropDropped = screenshotFailed || capture.status === "failed";
852 const annotation = capture.status === "failed" ? { ...picked, screenshot: null } : picked;
853 addPreviewAnnotation(threadRef, annotation);
854 if (cropDropped) {
855 toastManager.add(
856 stackedThreadToast({
857 type: "error",
858 title: "Could not capture the picked element",
859 description: "The annotation was kept without the screenshot.",
860 }),
861 );
862 }
863 const screenshotFile = capture.status === "captured" ? capture.file : null;
864 const image =
865 screenshotFile && annotation.screenshot
866 ? ({
867 type: "image",
868 id: annotation.id,
869 name: screenshotFile.name,
870 mimeType: screenshotFile.type,
871 sizeBytes: screenshotFile.size,
872 previewUrl: annotation.screenshot.dataUrl,
873 file: screenshotFile,
874 } satisfies ComposerImageAttachment)
875 : null;
876 if (image) {
877 addImage(threadRef, image);
878 }
879 if (
880 submission === "send" &&
881 readEnvironmentScope(threadRef.environmentId, AuthOrchestrationOperateScope)
882 ) {
883 onSendAnnotation?.(annotation, image);
884 }
885 }).then(finishPick);
886 };
887​
888 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- tells the preview bridge whether an element pick may send annotations
889 useEffect(() => {
890 if (!pickActive || !previewBridge || !runtimeTabId) return;
891 void previewBridge
892 .setAnnotationSendEnabled(runtimeTabId, canSendAnnotation)
893 .catch(() => undefined);
894 }, [canSendAnnotation, pickActive, runtimeTabId]);
895​
896 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- cancels an in-progress element pick on the preview bridge when the tab changes or on unmount
897 useEffect(() => {
898 return () => {
899 if (!pickActiveRef.current) return;
900 pickActiveRef.current.cancelled = true;
901 pickActiveRef.current = null;
902 if (previewBridge && runtimeTabId) {
903 void previewBridge.cancelPickElement(runtimeTabId).catch(() => undefined);
904 }
905 setPickActive(false);
906 setAnnotationVoice(null);
907 };
908 }, [runtimeTabId]);
909​
910 // Subscribe only while visible; ChatView always owns URL-aware toggle-panel handling.
911 const onPreviewAction = useEffectEvent(
912 (action: Parameters<Parameters<typeof subscribePreviewAction>[0]>[0]) => {
913 switch (action) {
914 case "refresh":
915 handleRefresh();
916 return;
917 case "back":
918 handleBack();
919 return;
920 case "forward":
921 handleForward();
922 return;
923 case "focus-url":
924 urlInputRef.current?.focus();
925 return;
926 case "zoom-in":
927 handleZoomIn();
928 return;
929 case "zoom-out":
930 handleZoomOut();
931 return;
932 case "reset-zoom":
933 handleResetZoom();
934 return;
935 case "find":
936 handleFind();
937 return;
938 case "toggle-panel":
939 return;
940 }
941 },
942 );
943 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes to preview and desktop menu actions while the view is visible
944 useEffect(() => {
945 if (!visible) return;
946 const unsubscribeAction = subscribePreviewAction((action) => onPreviewAction(action));
947 // A focused page swallows its own keydowns, so the desktop forwards these
948 // shortcuts from the guest as menu actions instead.
949 const unsubscribeMenu = window.desktopBridge?.onMenuAction((action) => {
950 if (action === "preview.focusUrl") onPreviewAction("focus-url");
951 if (action === "preview.find") onPreviewAction("find");
952 });
953 return () => {
954 unsubscribeAction();
955 unsubscribeMenu?.();
956 };
957 }, [visible]);
958​
959 return (
960 <div
961 className="flex min-h-0 flex-1 flex-col bg-background"
962 data-thread-key={scopedThreadKey(threadRef)}
963 >
964 {pickActive && runtimeTabId && annotationVoice ? (
965 <PreviewAnnotationSpeech
966 key={annotationVoice.sessionId}
967 threadRef={threadRef}
968 tabId={runtimeTabId}
969 config={annotationVoice}
970 keybindings={keybindings}
971 />
972 ) : null}
973 <PreviewChromeRow
974 url={url}
975 loading={loading || serverStreamPending}
976 canGoBack={canGoBack && !serverInputDisabled}
977 canGoForward={canGoForward && !serverInputDisabled}
978 refreshDisabled={refreshDisabled || serverInputDisabled}
979 inputDisabled={serverInputDisabled}
980 urlInputRef={urlInputRef}
981 onBack={handleBack}
982 onForward={handleForward}
983 onRefresh={handleRefresh}
984 onSubmit={(next) => void handleSubmitUrl(next)}
985 onOpenInBrowser={tabId ? handleOpenInBrowser : undefined}
986 onCapture={previewBridge && tabId && !isServerTab ? handleCapture : undefined}
987 captureDisabled={!desktopOverlay || isUnreachable}
988 recording={recordingRuntimeTabId !== null}
989 onPictureInPicture={
990 tabId && (isServerTab || previewBridge) ? handlePictureInPicture : undefined
991 }
992 pictureInPicture={miniPlayerTabId === tabId}
993 pictureInPictureDisabled={
994 isUnreachable || (!isServerTab && !desktopOverlay?.hasWebContents)
995 }
996 onPickElement={previewBridge && tabId && !isServerTab ? handlePickElement : undefined}
997 pickActive={pickActive}
998 pickDisabled={!tabId || isUnreachable}
999 pickDisabledReason={
1000 isUnreachable ? "Page didn't load. Pick is unavailable until the page renders" : undefined
1002 leadingActions={
1003 activeProfileId !== browserDefaults.profileId ? (
1004 <Tooltip>
1005 <TooltipTrigger render={<Badge variant="outline" className="max-w-28 shrink-0" />}>
1006 <span className="truncate">{activeProfileName}</span>
1007 </TooltipTrigger>
1008 <TooltipPopup side="top">{activeProfileName}</TooltipPopup>
1009 </Tooltip>
1010 ) : null
1012 trailingActions={
1013 moreMenuActions ? (
1014 <PreviewMoreMenu
1015 enabled={
1016 runtimeTabId !== null &&
1017 (serverOwnsRendering || (desktopOverlay?.hasWebContents ?? false))
1019 actions={moreMenuActions}
1020 {...(moveTarget
1021 ? { move: { label: moveLabel, onMove: () => void handleMoveTab() } }
1022 : {})}
1023 profileName={activeProfileName}
1024 findShortcutLabel={shortcutLabelForCommand(keybindings, "preview.find", {
1025 context: { previewFocus: true },
1026 })}
1027 zoomFactor={
1028 serverOwnsRendering ? serverZoomFactor : (desktopOverlay?.zoomFactor ?? 1)
1030 colorScheme={
1031 serverOwnsRendering
1032 ? (snapshot?.colorScheme ?? "system")
1033 : (desktopOverlay?.colorScheme ?? "system")
1035 deviceToolbarVisible={viewport._tag !== "fill"}
1036 onToggleDeviceToolbar={handleToggleDeviceToolbar}
1037 nativePictureInPicture={desktopOverlay?.pictureInPicture ?? false}
1038 />
1039 ) : null
1041 />
1042​
1043 <div ref={serverBodyRef} className="relative min-h-0 flex-1 overflow-hidden">
1044 {runtimeTabId && snapshot && isServerTab ? (
1045 <>
1046 {viewport._tag !== "fill" && !showEmptyState && !isUnreachable ? (
1047 <div className="absolute inset-x-0 top-0 z-10">
1048 <BrowserDeviceToolbar
1049 setting={viewport}
1050 width={serverToolbarWidth}
1051 aspectRatio={serverAspectRatioLocked ? viewport.width / viewport.height : null}
1052 onAspectRatioChange={(ratio) => setServerAspectRatioLocked(ratio !== null)}
1053 onChange={(setting) =>
1054 commitBrowserViewportChange(
1055 { threadRef, tabId: snapshot.tabId, runtimeTabId },
1056 setting,
1059 />
1060 </div>
1061 ) : null}
1062 <div
1063 ref={serverSurfaceBoxRef}
1064 className={cn(
1065 "absolute inset-x-0 bottom-0",
1066 (showEmptyState || isUnreachable) && "invisible",
1067 )}
1068 style={{ top: viewport._tag !== "fill" ? BROWSER_DEVICE_TOOLBAR_HEIGHT : 0 }}
1070 <ServerBrowserSurface
1071 key={runtimeTabId}
1072 ref={serverSurfaceRef}
1073 environmentId={threadRef.environmentId}
1074 threadId={threadRef.threadId}
1075 tabId={snapshot.tabId}
1076 pdfUrl={
1077 navStatus._tag === "Success"
1078 ? previewPdfAssetUrl(url, environmentHttpBaseUrl)
1079 : null
1081 visible={visible}
1082 onFirstFrame={() => setServerFrameTabId(runtimeTabId)}
1083 onControl={(control) =>
1084 setServerControlledTabId(control?.controller === "you" ? runtimeTabId : null)
1086 onPopup={(popupTabId) => showPreviewPopup(threadRef, popupTabId, "panel")}
1087 // Stays connected under the empty state so a URL picked there reaches the page.
1088 className="absolute inset-0 h-full w-full"
1089 />
1090 </div>
1091 </>
1092 ) : runtimeTabId && snapshot && !showEmptyState ? (
1093 previewBridge ? (
1094 <BrowserSurfaceSlot
1095 key={runtimeTabId}
1096 tabId={runtimeTabId}
1097 visible={visible && !isUnreachable}
1098 className="absolute inset-0 h-full w-full"
1099 />
1100 ) : (
1101 <div className="flex h-full items-center justify-center p-8 text-center">
1102 <p className="max-w-sm text-sm text-muted-foreground">
1103 This tab is open in the T3 Code desktop app.
1104 </p>
1105 </div>
1107 ) : null}
1108 {showEmptyState ? (
1109 <PreviewEmptyState
1110 threadRef={threadRef}
1111 environmentId={threadRef.environmentId}
1112 configuredUrls={configuredUrls}
1113 recentEntries={recentHistoryEntries}
1114 onRemoveRecent={(url) => removeUrlForThread(threadRef, url)}
1115 onOpenUrl={(next) => void handleOpenServerUrl(next)}
1116 />
1117 ) : null}
1118 {snapshot && desktopOverlay ? (
1119 <ZoomIndicator zoomFactor={desktopOverlay.zoomFactor} />
1120 ) : null}
1121 {runtimeTabId &&
1122 desktopOverlay &&
1123 !showEmptyState &&
1124 !isUnreachable &&
1125 !activeRecordingTabIds.has(runtimeTabId) ? (
1126 <AgentBrowserCursor
1127 tabId={runtimeTabId}
1128 zoomFactor={layoutZoomFactor}
1129 controller={controller}
1130 />
1131 ) : null}
1132 {find && canFindInPage && find.tabId === runtimeTabId ? (
1133 <PreviewFindBar
1134 key={find.tabId}
1135 tabId={find.tabId}
1136 focusRequestId={find.requestId}
1137 onClose={closeFind}
1138 />
1139 ) : null}
1140 {navStatus._tag === "LoadFailed" && navStatus.download ? (
1141 <div className="absolute inset-0 z-10 bg-background">
1142 <ServerTabFileNotShown
1143 threadRef={threadRef}
1144 tabId={snapshot?.tabId ?? ""}
1145 url={navStatus.url}
1146 download={navStatus.download}
1147 />
1148 </div>
1149 ) : navStatus._tag === "LoadFailed" ? (
1150 <div className="absolute inset-0 z-10 bg-background">
1151 <PreviewUnreachable
1152 url={navStatus.url}
1153 code={navStatus.code}
1154 description={navStatus.description}
1155 onReload={handleRefresh}
1156 {...(moveTarget === "server"
1157 ? { move: { label: moveLabel, onMove: () => void handleMoveTab() } }
1158 : {})}
1159 />
1160 </div>
1161 ) : null}
1162 </div>
1163 </div>
1164 );