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