TypeScript
perf(web): the browser preview view compiles with the React Compiler (#20476)
Julius Marmingee96db6f0c36b
diff --git a/apps/web/reactCompiledFiles.ts b/apps/web/reactCompiledFiles.tsindex fdfad99fdae..3e9d7dcae27 100644--- a/apps/web/reactCompiledFiles.ts+++ b/apps/web/reactCompiledFiles.ts@@ -51,4 +51,5 @@ export const REACT_COMPILED_FILES = ["apps/web/src/hooks/useTheme.ts","apps/web/src/hooks/useSettings.ts","apps/web/src/components/chat/QueuedRunsControl.tsx",+ "apps/web/src/components/preview/PreviewView.tsx",] as const;diff --git a/apps/web/src/components/preview/PreviewView.tsx b/apps/web/src/components/preview/PreviewView.tsxindex 12e31337b8c..85b743763f2 100644--- a/apps/web/src/components/preview/PreviewView.tsx+++ b/apps/web/src/components/preview/PreviewView.tsx@@ -1,6 +1,7 @@"use client";-import { scopedThreadKey } from "@t3tools/client-runtime/environment";+import { scopeThreadRef, scopedThreadKey } from "@t3tools/client-runtime/environment";+import { settlePromise } from "@t3tools/client-runtime/state/runtime";import { previewStreamDownloadUrl } from "@t3tools/client-runtime/preview/server-browser-stream";import {isAtomCommandInterrupted,@@ -18,7 +19,7 @@ import {type PreviewAdjustInput,} from "@t3tools/contracts";import { normalizePreviewUrl, resolveAddressBarInput } from "@t3tools/shared/preview";-import { useCallback, useEffect, useRef, useState } from "react";+import { useEffect, useEffectEvent, useRef, useState } from "react";import {BROWSER_HISTORY_MAX_ENTRIES_PER_PROJECT,@@ -152,10 +153,6 @@ export function PreviewView({const activeRecordingTabIds = useActiveBrowserRecordingTabIds();const pickActiveRef = useRef<{ cancelled: boolean } | null>(null);const isMountedRef = useRef(true);- // Kept in sync so the title effect can depend on the stable thread key- // instead of the thread object, which is recreated on every update.- const threadRefRef = useRef(threadRef);- threadRefRef.current = threadRef;const previewState = useThreadPreviewState(threadRef);const recentHistoryEntries = useThreadRecentHistory(threadRef,@@ -242,151 +239,139 @@ export function PreviewView({const navUrl = navStatus._tag === "Success" ? navStatus.url : null;const navTitle = navStatus._tag === "Success" ? navStatus.title : null;const latestHistoryUrl = recentHistoryEntries[0]?.url;- const threadKey = scopedThreadKey(threadRef);+ // Keyed on the ids rather than threadRef, which is recreated on every update.+ const { environmentId: threadEnvironmentId, threadId } = threadRef;useEffect(() => {if (!navUrl || !navTitle || !latestHistoryUrl) return;- setTitleForThreadUrl(threadRefRef.current, navUrl, navTitle, environmentHostname);- }, [environmentHostname, latestHistoryUrl, navTitle, navUrl, threadKey]);+ setTitleForThreadUrl(+ scopeThreadRef(threadEnvironmentId, threadId),+ navUrl,+ navTitle,+ environmentHostname,+ );+ }, [environmentHostname, latestHistoryUrl, navTitle, navUrl, threadEnvironmentId, threadId]);- const navigateToResolvedUrl = useCallback(- async (resolvedUrl: string) => {- if (isServerTab && serverSurfaceRef.current) {- if (serverInputDisabled) return false;- serverSurfaceRef.current.navigate(resolvedUrl);- rememberPreviewUrl(threadRef, resolvedUrl);- return true;- }- if (runtimeTabId && previewBridge) {- await previewBridge.navigate(runtimeTabId, resolvedUrl);- rememberPreviewUrl(threadRef, resolvedUrl);- return true;- }- const result = await openPreviewSession({ openPreview: open, threadRef, url: resolvedUrl });- if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {- const error = squashAtomCommandFailure(result);- if (error instanceof BrowserSettingsReadError) {- toastManager.add({- type: "error",- title: "Unable to open browser",- description: error.message,- });- }+ const navigateToResolvedUrl = async (resolvedUrl: string) => {+ if (isServerTab && serverSurfaceRef.current) {+ if (serverInputDisabled) return false;+ serverSurfaceRef.current.navigate(resolvedUrl);+ rememberPreviewUrl(threadRef, resolvedUrl);+ return true;+ }+ if (runtimeTabId && previewBridge) {+ await previewBridge.navigate(runtimeTabId, resolvedUrl);+ rememberPreviewUrl(threadRef, resolvedUrl);+ return true;+ }+ const result = await openPreviewSession({ openPreview: open, threadRef, url: resolvedUrl });+ if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {+ const error = squashAtomCommandFailure(result);+ if (error instanceof BrowserSettingsReadError) {+ toastManager.add({+ type: "error",+ title: "Unable to open browser",+ description: error.message,+ });}- return result._tag === "Success";- },- [isServerTab, open, runtimeTabId, serverInputDisabled, threadRef],- );+ }+ return result._tag === "Success";+ };- const handleSubmitUrl = useCallback(- async (next: string) => {- try {- const resolved = resolveAddressBarInput(next);- if (await navigateToResolvedUrl(resolved)) {- recordVisitForThread(threadRef, resolved);- }- } catch {}- },- [navigateToResolvedUrl, threadRef],- );+ const handleSubmitUrl = async (next: string) => {+ try {+ const resolved = resolveAddressBarInput(next);+ if (await navigateToResolvedUrl(resolved)) {+ recordVisitForThread(threadRef, resolved);+ }+ } catch {}+ };- const handleOpenServerUrl = useCallback(- async (next: string) => {- try {- const resolved =- isServerTab || !previewBridge- ? normalizePreviewUrl(next)- : resolveDiscoveredServerUrl(threadRef.environmentId, next);- if (await navigateToResolvedUrl(resolved)) {- recordVisitForThread(threadRef, next);- }- } catch {}- },- [isServerTab, navigateToResolvedUrl, threadRef],- );+ const handleOpenServerUrl = async (next: string) => {+ try {+ const resolved =+ isServerTab || !previewBridge+ ? normalizePreviewUrl(next)+ : resolveDiscoveredServerUrl(threadRef.environmentId, next);+ if (await navigateToResolvedUrl(resolved)) {+ recordVisitForThread(threadRef, next);+ }+ } catch {}+ };- const handleRefresh = useCallback(() => {+ const handleRefresh = () => {if (isServerTab) serverSurfaceRef.current?.reload();else if (previewBridge && runtimeTabId) void previewBridge.refresh(runtimeTabId);- }, [isServerTab, runtimeTabId]);+ };- const adjustServerTab = useCallback(- async (change: Omit<PreviewAdjustInput, "threadId" | "tabId">) => {- if (!tabId) return;- const result = await adjust({- environmentId: threadRef.environmentId,- input: { threadId: threadRef.threadId, tabId, ...change },+ const adjustServerTab = async (change: Omit<PreviewAdjustInput, "threadId" | "tabId">) => {+ if (!tabId) return;+ const result = await adjust({+ environmentId: threadRef.environmentId,+ input: { threadId: threadRef.threadId, tabId, ...change },+ });+ if (result._tag === "Failure") {+ const error = squashAtomCommandFailure(result);+ toastManager.add({+ type: "error",+ title: "Unable to change the browser tab",+ description: error instanceof Error ? error.message : "An error occurred.",});- if (result._tag === "Failure") {- const error = squashAtomCommandFailure(result);- toastManager.add({- type: "error",- title: "Unable to change the browser tab",- description: error instanceof Error ? error.message : "An error occurred.",- });- return;- }- updatePreviewServerSnapshot(threadRef, result.value);- },- [adjust, tabId, threadRef],- );+ return;+ }+ updatePreviewServerSnapshot(threadRef, result.value);+ };const serverZoomFactor = snapshot?.zoomFactor ?? DEFAULT_PREVIEW_ZOOM_FACTOR;- const stepServerZoom = useCallback(- (direction: -1 | 0 | 1) => {- const index = PREVIEW_ZOOM_LEVELS.indexOf(serverZoomFactor);- const next =- direction === 0- ? DEFAULT_PREVIEW_ZOOM_FACTOR- : PREVIEW_ZOOM_LEVELS[- Math.min(- Math.max((index < 0 ? 7 : index) + direction, 0),- PREVIEW_ZOOM_LEVELS.length - 1,- )- ]!;- if (next !== serverZoomFactor) void adjustServerTab({ zoomFactor: next });- },- [adjustServerTab, serverZoomFactor],- );+ const stepServerZoom = (direction: -1 | 0 | 1) => {+ const index = PREVIEW_ZOOM_LEVELS.indexOf(serverZoomFactor);+ const next =+ direction === 0+ ? DEFAULT_PREVIEW_ZOOM_FACTOR+ : PREVIEW_ZOOM_LEVELS[+ Math.min(+ Math.max((index < 0 ? 7 : index) + direction, 0),+ PREVIEW_ZOOM_LEVELS.length - 1,+ )+ ]!;+ if (next !== serverZoomFactor) void adjustServerTab({ zoomFactor: next });+ };- const handleZoomIn = useCallback(() => {+ const handleZoomIn = () => {if (serverOwnsRendering) stepServerZoom(1);else if (previewBridge && runtimeTabId) void previewBridge.zoomIn(runtimeTabId);- }, [runtimeTabId, serverOwnsRendering, stepServerZoom]);+ };- const handleZoomOut = useCallback(() => {+ const handleZoomOut = () => {if (serverOwnsRendering) stepServerZoom(-1);else if (previewBridge && runtimeTabId) void previewBridge.zoomOut(runtimeTabId);- }, [runtimeTabId, serverOwnsRendering, stepServerZoom]);+ };- const handleResetZoom = useCallback(() => {+ const handleResetZoom = () => {if (serverOwnsRendering) stepServerZoom(0);else if (previewBridge && runtimeTabId) void previewBridge.resetZoom(runtimeTabId);- }, [runtimeTabId, serverOwnsRendering, stepServerZoom]);+ };- const handleViewportChange = useCallback(- async (nextViewport: PreviewViewportSetting) => {- if (!tabId) return;- const result = await resize({- environmentId: threadRef.environmentId,- input: {- threadId: threadRef.threadId,- tabId,- viewport: nextViewport,- },+ const handleViewportChange = 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.",});- 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],- );+ throw error;+ }+ updatePreviewServerSnapshot(threadRef, result.value);+ };const handleToggleDeviceToolbar = () => {if (!runtimeTabId) return;@@ -405,25 +390,28 @@ export function PreviewView({).catch(() => undefined);};+ const onViewportChange = useEffectEvent(handleViewportChange);useEffect(() => {if (!runtimeTabId) return;- return subscribeBrowserViewportChange(runtimeTabId, handleViewportChange);- }, [handleViewportChange, runtimeTabId]);+ return subscribeBrowserViewportChange(runtimeTabId, (nextViewport) =>+ onViewportChange(nextViewport),+ );+ }, [runtimeTabId]);- const handleBack = useCallback(() => {+ const handleBack = () => {if (isServerTab) serverSurfaceRef.current?.history(-1);else if (previewBridge && runtimeTabId) void previewBridge.goBack(runtimeTabId);- }, [isServerTab, runtimeTabId]);+ };- const handleForward = useCallback(() => {+ const handleForward = () => {if (isServerTab) serverSurfaceRef.current?.history(1);else if (previewBridge && runtimeTabId) void previewBridge.goForward(runtimeTabId);- }, [isServerTab, runtimeTabId]);+ };- const handleOpenInBrowser = useCallback(() => {+ const handleOpenInBrowser = () => {if (!localApi || !url) return;void localApi.shell.openExternal(url).catch(() => undefined);- }, [url]);+ };const moveTarget = alternatePreviewRuntime(threadRef.environmentId,@@ -433,7 +421,7 @@ export function PreviewView({);const moveLabel =moveTarget === "server" ? `Open in ${environmentLabel}'s browser` : "Open on this computer";- const handleMoveTab = useCallback(async () => {+ const handleMoveTab = async () => {if (!moveTarget || !tabId || !snapshot) return;const result = await openPreviewSession({openPreview: open,@@ -462,9 +450,9 @@ export function PreviewView({}useRightPanelStore.getState().openBrowser(threadRef, result.value.tabId);await closePreviewSession({ closePreview, snapshot, tabId, threadRef });- }, [closePreview, moveTarget, open, snapshot, tabId, threadRef, url, viewport]);+ };- const handlePictureInPicture = useCallback(() => {+ const handlePictureInPicture = () => {if (!tabId) return;if (miniPlayerTabId === tabId) {usePreviewMiniPlayerStore.getState().close(threadRef);@@ -472,7 +460,7 @@ export function PreviewView({}usePreviewMiniPlayerStore.getState().open(threadRef, browserMiniPlayerSource(tabId));useRightPanelStore.getState().close(threadRef);- }, [miniPlayerTabId, tabId, threadRef]);+ };const desktopCall = (op: ((tabId: string) => Promise<void>) | undefined) => () => {if (op && runtimeTabId) void op(runtimeTabId).catch(() => undefined);@@ -516,7 +504,7 @@ export function PreviewView({}: null;- const handleNativePictureInPicture = useCallback(() => {+ const handleNativePictureInPicture = () => {if (!previewBridge || !runtimeTabId) return;const operation = desktopOverlay?.pictureInPicture? previewBridge.pictureInPicture.close@@ -528,168 +516,28 @@ export function PreviewView({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<typeof toastManager.add>;-- 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) => {- const description = error instanceof Error ? error.message : "An error occurred.";- if (isBrowserRecordingStartCancelledError(error)) return;- toastManager.add({- type: "error",- title: "Unable to start recording",- description,- });- });- return;- }- void bridge.captureScreenshot(runtimeTabId).then(+ const handleCapture = (record: boolean) => {+ if (!previewBridge || !runtimeTabId || !tabId) return;+ const bridge = previewBridge;+ if (recordingRuntimeTabId) {+ void stopBrowserRecording(recordingRuntimeTabId).then((artifact) => {- const revealAction = {- children: revealInFileExplorerLabel(navigator.platform),- onClick: () => void bridge.revealArtifact(artifact.path),- };+ if (!artifact) return;let pathCopied = false;- let imageCopied = false;let toastId: ReturnType<typeof toastManager.add>;- 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.",+ toastManager.update(+ toastId,+ stackedThreadToast({+ type: "error",+ title: "Unable to copy recording path",+ description: "Clipboard API unavailable.",+ actionProps: revealAction,+ }),);return;}@@ -697,48 +545,110 @@ export function PreviewView({void navigator.clipboard.writeText(artifact.path).then(() => {pathCopied = true;- updateScreenshotToast();+ updateRecordingToast();window.setTimeout(() => {pathCopied = false;- updateScreenshotToast();+ updateRecordingToast();}, 2_000);},(error) => {- updateScreenshotToast(- "error",- "Unable to copy screenshot path",- error instanceof Error ? error.message : "An error occurred.",+ toastManager.update(+ toastId,+ stackedThreadToast({+ type: "error",+ title: "Unable to copy recording path",+ description: error instanceof Error ? error.message : "An error occurred.",+ actionProps: revealAction,+ }),);},);};- 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.",- );- },+ 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: "Screenshot saved",+ 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) => {+ const description = error instanceof Error ? error.message : "An error occurred.";+ if (isBrowserRecordingStartCancelledError(error)) return;+ toastManager.add({+ type: "error",+ title: "Unable to start recording",+ description,+ });+ });+ 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<typeof toastManager.add>;++ const updateScreenshotToast = (+ type: "success" | "error" = "success",+ title = "Screenshot saved",+ description?: string,+ ) => {+ toastManager.update(+ toastId,+ stackedThreadToast({+ type,+ title,+ description,actionProps: {- children: "Copy image",+ children: imageCopied ? "Copied!" : "Copy image",+ disabled: imageCopied,onClick: copyImage,},data: {@@ -746,7 +656,8 @@ export function PreviewView({{id: "copy-path",props: {- children: "Copy path",+ children: pathCopied ? "Copied!" : "Copy path",+ disabled: pathCopied,onClick: copyPath,},},@@ -758,20 +669,94 @@ export function PreviewView({},}),);- },- (error) => {- toastManager.add({- type: "error",- title: "Unable to capture screenshot",- description: error instanceof Error ? error.message : "An error occurred.",- });- },- );- },- [recordingRuntimeTabId, runtimeTabId, tabId, threadRef],- );+ };++ const copyPath = () => {+ if (!navigator.clipboard?.writeText) {+ updateScreenshotToast(+ "error",+ "Unable to copy screenshot path",+ "Clipboard API unavailable.",+ );+ return;+ }- const handlePickElement = useCallback(() => {+ 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.",+ });+ },+ );+ };++ const handlePickElement = () => {if (!previewBridge || !runtimeTabId) return;if (pickActiveRef.current) {pickActiveRef.current.cancelled = true;@@ -787,85 +772,85 @@ export function PreviewView({let submitted = false;pickActiveRef.current = pickRequest;setPickActive(true);- void (async () => {- try {- await previewBridge.setAnnotationSendEnabled?.(- runtimeTabId,- Boolean(onSendAnnotation) &&- readEnvironmentScope(threadRef.environmentId, AuthOrchestrationOperateScope),+ const finishPick = () => {+ const isCurrentPick = pickActiveRef.current === pickRequest;+ if (isCurrentPick) {+ pickActiveRef.current = null;+ if (isMountedRef.current) setPickActive(false);+ }+ if (+ (isCurrentPick || submitted) &&+ previouslyFocused &&+ previouslyFocused.isConnected &&+ typeof previouslyFocused.focus === "function"+ ) {+ try {+ previouslyFocused.focus({ preventScroll: true });+ } catch {}+ }+ };+ // A failed pick is dropped silently; finishPick still releases the picker.+ const bridge = previewBridge;+ void settlePromise(async () => {+ await bridge.setAnnotationSendEnabled?.(+ runtimeTabId,+ Boolean(onSendAnnotation) &&+ readEnvironmentScope(threadRef.environmentId, AuthOrchestrationOperateScope),+ );+ if (pickRequest.cancelled) return;+ const result = await bridge.pickElement(runtimeTabId);+ if (!result || pickRequest.cancelled) return;+ // Nothing that happens after this point (a second picker click, a tab+ // change, unmount) may discard it, so the pick stops being cancellable+ // here rather than in finishPick.+ if (pickActiveRef.current === pickRequest) {+ pickActiveRef.current = null;+ if (isMountedRef.current) setPickActive(false);+ submitted = true;+ }+ const { annotation: picked, submission, screenshotFailed = false } = result;+ // The structured annotation is still sendable when its optional crop+ // stalls or fails, so tell the user what they lost and keep going+ // instead of holding the composer for an attachment that never lands.+ // The stored copy drops the screenshot on failure, otherwise the prompt+ // would tell the agent a crop is attached when none was sent.+ const capture = capturePreviewAnnotationScreenshot(picked);+ const cropDropped = screenshotFailed || capture.status === "failed";+ const annotation = capture.status === "failed" ? { ...picked, screenshot: null } : picked;+ addPreviewAnnotation(threadRef, annotation);+ if (cropDropped) {+ toastManager.add(+ stackedThreadToast({+ type: "error",+ title: "Could not capture the picked element",+ description: "The annotation was kept without the screenshot.",+ }),);- if (pickRequest.cancelled) return;- const result = await previewBridge.pickElement(runtimeTabId);- if (!result || pickRequest.cancelled) return;- // Nothing that happens after this point (a second picker click, a tab- // change, unmount) may discard it, so the pick stops being cancellable- // here rather than in `finally`.- if (pickActiveRef.current === pickRequest) {- pickActiveRef.current = null;- if (isMountedRef.current) setPickActive(false);- submitted = true;- }- const { annotation: picked, submission, screenshotFailed = false } = result;- // The structured annotation is still sendable when its optional crop- // stalls or fails, so tell the user what they lost and keep going- // instead of holding the composer for an attachment that never lands.- // The stored copy drops the screenshot on failure, otherwise the prompt- // would tell the agent a crop is attached when none was sent.- const capture = capturePreviewAnnotationScreenshot(picked);- const cropDropped = screenshotFailed || capture.status === "failed";- const annotation = capture.status === "failed" ? { ...picked, screenshot: null } : picked;- addPreviewAnnotation(threadRef, annotation);- if (cropDropped) {- toastManager.add(- stackedThreadToast({- type: "error",- title: "Could not capture the picked element",- description: "The annotation was kept without the screenshot.",- }),- );- }- const screenshotFile = capture.status === "captured" ? capture.file : null;- 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" &&- readEnvironmentScope(threadRef.environmentId, AuthOrchestrationOperateScope)- ) {- onSendAnnotation?.(annotation, image);- }- } catch {- } finally {- const isCurrentPick = pickActiveRef.current === pickRequest;- if (isCurrentPick) {- pickActiveRef.current = null;- if (isMountedRef.current) setPickActive(false);- }- if (- (isCurrentPick || submitted) &&- previouslyFocused &&- previouslyFocused.isConnected &&- typeof previouslyFocused.focus === "function"- ) {- try {- previouslyFocused.focus({ preventScroll: true });- } catch {}- }}- })();- }, [addImage, addPreviewAnnotation, onSendAnnotation, runtimeTabId, threadRef]);+ const screenshotFile = capture.status === "captured" ? capture.file : null;+ 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" &&+ readEnvironmentScope(threadRef.environmentId, AuthOrchestrationOperateScope)+ ) {+ onSendAnnotation?.(annotation, image);+ }+ }).then(finishPick);+ };useEffect(() => {if (!pickActive || !previewBridge || !runtimeTabId) return;@@ -888,9 +873,8 @@ export function PreviewView({// 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) => {+ const onPreviewAction = useEffectEvent(+ (action: Parameters<Parameters<typeof subscribePreviewAction>[0]>[0]) => {switch (action) {case "refresh":handleRefresh();@@ -910,8 +894,12 @@ export function PreviewView({case "toggle-panel":return;}- });- }, [handleRefresh, handleResetZoom, handleZoomIn, handleZoomOut, visible]);+ },+ );+ useEffect(() => {+ if (!visible) return;+ return subscribePreviewAction((action) => onPreviewAction(action));+ }, [visible]);return (<div