"use client"; import { scopedThreadKey } from "@t3tools/client-runtime/environment"; import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; import { FILL_PREVIEW_VIEWPORT, type PreviewAnnotationPayload, type PreviewViewportSetting, type ScopedThreadRef, } from "@t3tools/contracts"; import { normalizePreviewUrl } from "@t3tools/shared/preview"; import { useCallback, useEffect, useRef, useState } from "react"; import { type ComposerImageAttachment, useComposerDraftStore } from "~/composerDraftStore"; import { previewAnnotationScreenshotFile } from "~/lib/previewAnnotation"; import { ensureLocalApi } from "~/localApi"; import { rememberPreviewUrl, updatePreviewServerSnapshot, useThreadPreviewState, } from "~/previewStateStore"; import { resolveDiscoveredServerUrl } from "~/browser/browserTargetResolver"; import { previewEnvironment } from "~/state/preview"; import { useAtomCommand } from "~/state/use-atom-command"; import { selectThreadPreviewMiniPlayer, usePreviewMiniPlayerStore } from "~/previewMiniPlayerStore"; import { useRightPanelStore } from "~/rightPanelStore"; import { previewBridge } from "./previewBridge"; import { subscribePreviewAction } from "./previewActionBus"; import { openPreviewSession } from "./openPreviewSession"; import { PreviewChromeRow } from "./PreviewChromeRow"; import { PreviewEmptyState } from "./PreviewEmptyState"; import { PreviewMoreMenu } from "./PreviewMoreMenu"; import { commitBrowserViewportChange, subscribeBrowserViewportChange, } from "~/browser/browserViewportActions"; import { resolveResponsiveBrowserViewportSize } from "~/browser/browserViewportLayout"; import { previewRuntimeTabId } from "~/browser/previewRuntimeTabId"; import { PreviewUnreachable } from "./PreviewUnreachable"; import { revealInFileExplorerLabel } from "./fileExplorerLabel"; import { shouldShowPreviewEmptyState } from "./previewEmptyStateLogic"; import { BrowserSurfaceSlot } from "~/browser/BrowserSurfaceSlot"; import { useBrowserSurfaceStore } from "~/browser/browserSurfaceStore"; import { useLoadingProgress } from "./useLoadingProgress"; import { usePreviewSession } from "./usePreviewSession"; import { ZoomIndicator } from "./ZoomIndicator"; import { AgentBrowserCursor } from "./AgentBrowserCursor"; import { findActiveBrowserRecordingRuntimeTabId, startBrowserRecording, stopBrowserRecording, useActiveBrowserRecordingTabIds, } from "~/browser/browserRecording"; import { stackedThreadToast, toastManager } from "~/components/ui/toast"; interface Props { threadRef: ScopedThreadRef; tabId?: string | null; configuredUrls?: ReadonlyArray | undefined; visible: boolean; onSendAnnotation?: ( annotation: PreviewAnnotationPayload, image: ComposerImageAttachment | null, ) => void; } const localApi = typeof window === "undefined" ? null : ensureLocalApi(); /** * Single-tab preview surface: chrome row on top, one webview below, empty * state when no session exists for the thread. */ export function PreviewView({ threadRef, tabId: requestedTabId, configuredUrls, visible, onSendAnnotation, }: Props) { const [focusUrlNonce, setFocusUrlNonce] = useState(undefined); const [pickActive, setPickActive] = useState(false); const activeRecordingTabIds = useActiveBrowserRecordingTabIds(); const pickActiveRef = useRef(false); const isMountedRef = useRef(true); const previewState = useThreadPreviewState(threadRef); const miniPlayer = usePreviewMiniPlayerStore((state) => selectThreadPreviewMiniPlayer(state.byThreadKey, threadRef), ); const addPreviewAnnotation = useComposerDraftStore((store) => store.addPreviewAnnotation); const addImage = useComposerDraftStore((store) => store.addImage); const open = useAtomCommand(previewEnvironment.open); const resize = useAtomCommand(previewEnvironment.resize, "preview viewport resize"); usePreviewSession(threadRef); useEffect(() => { isMountedRef.current = true; return () => { isMountedRef.current = false; }; }, []); const tabId = requestedTabId ?? previewState.activeTabId; const runtimeTabId = tabId ? previewRuntimeTabId(threadRef, previewState.serverEpoch, tabId) : null; const recordingRuntimeTabId = tabId && runtimeTabId ? activeRecordingTabIds.has(runtimeTabId) ? runtimeTabId : findActiveBrowserRecordingRuntimeTabId(threadRef, tabId) : null; const snapshot = tabId ? (previewState.sessions[tabId] ?? null) : null; const desktopOverlay = tabId ? (previewState.desktopByTabId[tabId] ?? null) : null; const navStatus = snapshot?.navStatus ?? { _tag: "Idle" as const }; const url = navStatus._tag === "Idle" ? "" : navStatus.url; const loading = desktopOverlay?.loading ?? navStatus._tag === "Loading"; const canGoBack = desktopOverlay?.canGoBack ?? snapshot?.canGoBack ?? false; const canGoForward = desktopOverlay?.canGoForward ?? snapshot?.canGoForward ?? false; const refreshDisabled = navStatus._tag === "Idle"; const isUnreachable = navStatus._tag === "LoadFailed"; const showEmptyState = shouldShowPreviewEmptyState(snapshot); const controller = desktopOverlay?.controller ?? "none"; const loadProgress = useLoadingProgress(loading); const viewport = snapshot?.viewport ?? FILL_PREVIEW_VIEWPORT; const panelRect = useBrowserSurfaceStore((state) => runtimeTabId ? (state.byTabId[runtimeTabId]?.rect ?? null) : null, ); const navigateToResolvedUrl = useCallback( async (resolvedUrl: string) => { if (runtimeTabId && previewBridge) { // Drive the webview imperatively; `usePreviewBridge` mirrors the // resolved URL back to the server so other clients stay in sync. await previewBridge.navigate(runtimeTabId, resolvedUrl); rememberPreviewUrl(threadRef, resolvedUrl); } else { await openPreviewSession({ openPreview: open, threadRef, url: resolvedUrl, }); } }, [open, runtimeTabId, threadRef], ); const handleSubmitUrl = useCallback( async (next: string) => { try { await navigateToResolvedUrl(normalizePreviewUrl(next)); } catch { // Server-side `failed` event renders the unreachable view. } }, [navigateToResolvedUrl], ); const handleOpenServerUrl = useCallback( async (next: string) => { try { await navigateToResolvedUrl(resolveDiscoveredServerUrl(threadRef.environmentId, next)); } catch { // Server-side `failed` event renders the unreachable view. } }, [navigateToResolvedUrl, threadRef.environmentId], ); const handleRefresh = useCallback(() => { if (previewBridge && runtimeTabId) void previewBridge.refresh(runtimeTabId); }, [runtimeTabId]); const handleZoomIn = useCallback(() => { if (previewBridge && runtimeTabId) void previewBridge.zoomIn(runtimeTabId); }, [runtimeTabId]); const handleZoomOut = useCallback(() => { if (previewBridge && runtimeTabId) void previewBridge.zoomOut(runtimeTabId); }, [runtimeTabId]); const handleResetZoom = useCallback(() => { if (previewBridge && runtimeTabId) void previewBridge.resetZoom(runtimeTabId); }, [runtimeTabId]); const handleViewportChange = useCallback( async (nextViewport: PreviewViewportSetting) => { if (!tabId) return; const result = await resize({ environmentId: threadRef.environmentId, input: { threadId: threadRef.threadId, tabId, viewport: nextViewport, }, }); if (result._tag === "Failure") { const error = squashAtomCommandFailure(result); toastManager.add({ type: "error", title: "Unable to resize browser viewport", description: error instanceof Error ? error.message : "An error occurred.", }); throw error; } updatePreviewServerSnapshot(threadRef, result.value); }, [resize, tabId, threadRef], ); const handleToggleDeviceToolbar = () => { if (!runtimeTabId) return; if (viewport._tag !== "fill") { void commitBrowserViewportChange(runtimeTabId, FILL_PREVIEW_VIEWPORT).catch(() => undefined); return; } const responsiveSize = panelRect ? resolveResponsiveBrowserViewportSize(panelRect, desktopOverlay?.zoomFactor) : { width: 1024, height: 768 }; void commitBrowserViewportChange(runtimeTabId, { _tag: "freeform", ...responsiveSize }).catch( () => undefined, ); }; useEffect(() => { if (!runtimeTabId) return; return subscribeBrowserViewportChange(runtimeTabId, handleViewportChange); }, [handleViewportChange, runtimeTabId]); const handleBack = useCallback(() => { if (previewBridge && runtimeTabId) void previewBridge.goBack(runtimeTabId); }, [runtimeTabId]); const handleForward = useCallback(() => { if (previewBridge && runtimeTabId) void previewBridge.goForward(runtimeTabId); }, [runtimeTabId]); const handleOpenInBrowser = useCallback(() => { if (!localApi || !url) return; void localApi.shell.openExternal(url).catch(() => undefined); }, [url]); const handlePictureInPicture = useCallback(() => { if (!tabId) return; if (miniPlayer?.tabId === tabId) { usePreviewMiniPlayerStore.getState().close(threadRef); return; } usePreviewMiniPlayerStore.getState().open(threadRef, tabId); useRightPanelStore.getState().close(threadRef); }, [miniPlayer?.tabId, tabId, threadRef]); const handleNativePictureInPicture = useCallback(() => { if (!previewBridge || !runtimeTabId) return; const operation = desktopOverlay?.pictureInPicture ? previewBridge.pictureInPicture.close : previewBridge.pictureInPicture.open; void operation(runtimeTabId).catch((error) => { toastManager.add({ type: "error", title: "Unable to update popped-out preview", description: error instanceof Error ? error.message : "An error occurred.", }); }); }, [desktopOverlay?.pictureInPicture, runtimeTabId]); const handleCapture = useCallback( (record: boolean) => { if (!previewBridge || !runtimeTabId || !tabId) return; const bridge = previewBridge; if (recordingRuntimeTabId) { void stopBrowserRecording(recordingRuntimeTabId).then( (artifact) => { if (!artifact) return; let pathCopied = false; let toastId: ReturnType; const copyPath = () => { if (!navigator.clipboard?.writeText) { toastManager.update( toastId, stackedThreadToast({ type: "error", title: "Unable to copy recording path", description: "Clipboard API unavailable.", actionProps: revealAction, }), ); return; } void navigator.clipboard.writeText(artifact.path).then( () => { pathCopied = true; updateRecordingToast(); window.setTimeout(() => { pathCopied = false; updateRecordingToast(); }, 2_000); }, (error) => { toastManager.update( toastId, stackedThreadToast({ type: "error", title: "Unable to copy recording path", description: error instanceof Error ? error.message : "An error occurred.", actionProps: revealAction, }), ); }, ); }; const revealAction = { children: revealInFileExplorerLabel(navigator.platform), onClick: () => void bridge.revealArtifact(artifact.path), }; const updateRecordingToast = () => { toastManager.update( toastId, stackedThreadToast({ type: "success", title: "Recording saved", actionProps: revealAction, data: { secondaryActionProps: { children: pathCopied ? "Copied!" : "Copy path", disabled: pathCopied, onClick: copyPath, }, secondaryActionVariant: "outline", }, }), ); }; toastId = toastManager.add( stackedThreadToast({ type: "success", title: "Recording saved", actionProps: revealAction, data: { secondaryActionProps: { children: "Copy path", onClick: copyPath, }, secondaryActionVariant: "outline", }, }), ); }, (error) => { toastManager.add({ type: "error", title: "Unable to stop recording", description: error instanceof Error ? error.message : "An error occurred.", }); }, ); return; } if (record) { void startBrowserRecording(runtimeTabId, threadRef, tabId).catch((error) => { toastManager.add({ type: "error", title: "Unable to start recording", description: error instanceof Error ? error.message : "An error occurred.", }); }); return; } void bridge.captureScreenshot(runtimeTabId).then( (artifact) => { const revealAction = { children: revealInFileExplorerLabel(navigator.platform), onClick: () => void bridge.revealArtifact(artifact.path), }; let pathCopied = false; let imageCopied = false; let toastId: ReturnType; const updateScreenshotToast = ( type: "success" | "error" = "success", title = "Screenshot saved", description?: string, ) => { toastManager.update( toastId, stackedThreadToast({ type, title, description, actionProps: { children: imageCopied ? "Copied!" : "Copy image", disabled: imageCopied, onClick: copyImage, }, data: { additionalActions: [ { id: "copy-path", props: { children: pathCopied ? "Copied!" : "Copy path", disabled: pathCopied, onClick: copyPath, }, }, ], secondaryActionProps: { ...revealAction, }, secondaryActionVariant: "outline", }, }), ); }; const copyPath = () => { if (!navigator.clipboard?.writeText) { updateScreenshotToast( "error", "Unable to copy screenshot path", "Clipboard API unavailable.", ); return; } void navigator.clipboard.writeText(artifact.path).then( () => { pathCopied = true; updateScreenshotToast(); window.setTimeout(() => { pathCopied = false; updateScreenshotToast(); }, 2_000); }, (error) => { updateScreenshotToast( "error", "Unable to copy screenshot path", error instanceof Error ? error.message : "An error occurred.", ); }, ); }; const copyImage = () => { void bridge.copyArtifactToClipboard(artifact.path).then( () => { imageCopied = true; updateScreenshotToast(); window.setTimeout(() => { imageCopied = false; updateScreenshotToast(); }, 2_000); }, (error) => { updateScreenshotToast( "error", "Unable to copy screenshot", error instanceof Error ? error.message : "An error occurred.", ); }, ); }; toastId = toastManager.add( stackedThreadToast({ type: "success", title: "Screenshot saved", actionProps: { children: "Copy image", onClick: copyImage, }, data: { additionalActions: [ { id: "copy-path", props: { children: "Copy path", onClick: copyPath, }, }, ], secondaryActionProps: { ...revealAction, }, secondaryActionVariant: "outline", }, }), ); }, (error) => { toastManager.add({ type: "error", title: "Unable to capture screenshot", description: error instanceof Error ? error.message : "An error occurred.", }); }, ); }, [recordingRuntimeTabId, runtimeTabId, tabId, threadRef], ); const handlePickElement = useCallback(() => { if (!previewBridge || !runtimeTabId) return; if (pickActiveRef.current) { void previewBridge.cancelPickElement(runtimeTabId).catch(() => undefined); return; } // Snapshot whatever the user was focused on (typically the chat // composer textarea or the chrome-row pick button) BEFORE main steals // focus into the guest webContents. We restore it when the pick // resolves so the user's typing context isn't lost — otherwise after // every pick they'd have to click back into the textarea. const previouslyFocused = typeof document !== "undefined" ? (document.activeElement as HTMLElement | null) : null; pickActiveRef.current = true; setPickActive(true); void (async () => { try { const result = await previewBridge.pickElement(runtimeTabId); if (!result) return; const { annotation, submission } = result; addPreviewAnnotation(threadRef, annotation); let screenshotFile: File | null = null; try { screenshotFile = await previewAnnotationScreenshotFile(annotation); } catch { // The structured annotation is still sendable when converting its // optional screenshot into a composer attachment fails. } const image = screenshotFile && annotation.screenshot ? ({ type: "image", id: annotation.id, name: screenshotFile.name, mimeType: screenshotFile.type, sizeBytes: screenshotFile.size, previewUrl: annotation.screenshot.dataUrl, file: screenshotFile, } satisfies ComposerImageAttachment) : null; if (image) { addImage(threadRef, image); } if (submission === "send") { onSendAnnotation?.(annotation, image); } } catch { // Picker failed (e.g. webview navigated). Treat as silent cancel. } finally { pickActiveRef.current = false; // Avoid `setState on unmounted component` if the panel/thread closed // while the pick was in flight. if (isMountedRef.current) setPickActive(false); // Best-effort: restore focus to whatever the user had before the // pick stole it into the guest webContents. Skip if the previously- // focused element was unmounted or is no longer focusable. if ( previouslyFocused && previouslyFocused.isConnected && typeof previouslyFocused.focus === "function" ) { try { previouslyFocused.focus({ preventScroll: true }); } catch { // Some elements throw on .focus() (detached iframes, etc.). } } } })(); }, [addImage, addPreviewAnnotation, onSendAnnotation, runtimeTabId, threadRef]); // If the active tab changes mid-pick (close, thread switch, hot restart), // tell main to tear down the in-flight session AND reset our local toggle // state so the button doesn't get stuck pressed against a stale tab id. useEffect(() => { return () => { if (!pickActiveRef.current) return; pickActiveRef.current = false; if (previewBridge && runtimeTabId) { void previewBridge.cancelPickElement(runtimeTabId).catch(() => undefined); } if (isMountedRef.current) setPickActive(false); }; }, [runtimeTabId]); // Subscribe only while visible; `toggle-panel` is owned by ChatView's // URL-aware handler regardless of whether the panel is currently mounted. useEffect(() => { if (!visible) return; return subscribePreviewAction((action) => { switch (action) { case "refresh": handleRefresh(); return; case "focus-url": setFocusUrlNonce((value) => (value ?? 0) + 1); return; case "zoom-in": handleZoomIn(); return; case "zoom-out": handleZoomOut(); return; case "reset-zoom": handleResetZoom(); return; case "toggle-panel": return; } }); }, [handleRefresh, handleResetZoom, handleZoomIn, handleZoomOut, visible]); return (
void handleSubmitUrl(next)} onOpenInBrowser={tabId ? handleOpenInBrowser : undefined} onCapture={previewBridge && tabId ? handleCapture : undefined} captureDisabled={!desktopOverlay || isUnreachable} recording={recordingRuntimeTabId !== null} onPictureInPicture={previewBridge && tabId ? handlePictureInPicture : undefined} pictureInPicture={miniPlayer?.tabId === tabId} pictureInPictureDisabled={!desktopOverlay?.hasWebContents || isUnreachable} onPickElement={previewBridge && tabId ? handlePickElement : undefined} pickActive={pickActive} // Disable when there's no tab (nothing to pick on) OR the page // failed to load (a React overlay covers the webview, so the // user wouldn't be able to actually click anything underneath). pickDisabled={!tabId || isUnreachable} pickDisabledReason={ isUnreachable ? "Page didn't load — pick unavailable until the page renders" : undefined } trailingActions={ previewBridge ? ( ) : null } />
{runtimeTabId && snapshot && !showEmptyState ? ( ) : null} {showEmptyState ? ( void handleOpenServerUrl(next)} /> ) : null} {snapshot && desktopOverlay ? ( ) : null} {runtimeTabId && desktopOverlay && !showEmptyState && !isUnreachable ? ( ) : null} {controller !== "none" ? (
{controller === "agent" ? "Agent controlling browser" : "Human control"}
) : null} {navStatus._tag === "LoadFailed" ? (
) : null}
); }