fix(web): desktop opens remote environments' browser tabs locally (#17316)
Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
d9f7c0caed
commit
66298aa926
7 files changed
+173
-7
No files matched your search
@@ -0,0 +1,60 @@
|
||||
import type { EnvironmentId } from "@t3tools/contracts";
|
||||
import { afterEach, describe, expect, it, vi } from "vite-plus/test";
|
||||
|
||||
const state = vi.hoisted(() => ({
|
||||
desktop: false,
|
||||
primary: null as string | null,
|
||||
serverBrowser: new Set<string>(),
|
||||
}));
|
||||
|
||||
vi.mock("~/env", () => ({ isElectron: false }));
|
||||
vi.mock("~/previewStateStore", () => ({ isPreviewSupportedInRuntime: () => state.desktop }));
|
||||
vi.mock("~/rpc/atomRegistry", () => ({ appAtomRegistry: { get: () => state.primary } }));
|
||||
vi.mock("~/state/primaryEnvironment", () => ({ primaryEnvironmentIdAtom: {} }));
|
||||
vi.mock("~/state/entities", () => ({
|
||||
readEnvironmentSupportsServerBrowser: (id: string) => state.serverBrowser.has(id),
|
||||
useEnvironmentSupportsServerBrowser: () => false,
|
||||
}));
|
||||
|
||||
import { alternatePreviewRuntime, previewRuntimeFor } from "./previewRuntime";
|
||||
|
||||
const local = "local" as EnvironmentId;
|
||||
const remote = "remote" as EnvironmentId;
|
||||
|
||||
afterEach(() => {
|
||||
state.desktop = false;
|
||||
state.primary = null;
|
||||
state.serverBrowser = new Set();
|
||||
});
|
||||
|
||||
describe("previewRuntimeFor", () => {
|
||||
it("opens a remote environment's tabs on this computer in the desktop app", () => {
|
||||
state.desktop = true;
|
||||
state.primary = local;
|
||||
state.serverBrowser = new Set([local, remote]);
|
||||
|
||||
expect(previewRuntimeFor(remote)).toBeUndefined();
|
||||
expect(previewRuntimeFor(local)).toBe("server");
|
||||
});
|
||||
|
||||
it("uses the environment's browser where the client has none of its own", () => {
|
||||
state.serverBrowser = new Set([remote]);
|
||||
|
||||
expect(previewRuntimeFor(remote)).toBe("server");
|
||||
});
|
||||
});
|
||||
|
||||
describe("alternatePreviewRuntime", () => {
|
||||
it("moves a remote environment's tab between this computer and the environment", () => {
|
||||
state.desktop = true;
|
||||
|
||||
expect(alternatePreviewRuntime(remote, local, true, {})).toBe("server");
|
||||
expect(alternatePreviewRuntime(remote, local, true, { runtime: "server" })).toBe("desktop");
|
||||
expect(alternatePreviewRuntime(local, local, true, {})).toBeNull();
|
||||
expect(alternatePreviewRuntime(remote, local, false, {})).toBeNull();
|
||||
});
|
||||
|
||||
it("offers no move outside the desktop app", () => {
|
||||
expect(alternatePreviewRuntime(remote, null, true, { runtime: "server" })).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -3,14 +3,44 @@ import type { EnvironmentId, PreviewRuntime, PreviewSessionSnapshot } from "@t3t
|
||||
|
||||
import { isElectron } from "~/env";
|
||||
import { isPreviewSupportedInRuntime } from "~/previewStateStore";
|
||||
import { appAtomRegistry } from "~/rpc/atomRegistry";
|
||||
import { primaryEnvironmentIdAtom } from "~/state/primaryEnvironment";
|
||||
import {
|
||||
readEnvironmentSupportsServerBrowser,
|
||||
useEnvironmentSupportsServerBrowser,
|
||||
} from "~/state/entities";
|
||||
|
||||
/**
|
||||
* Where a tab the user opens should run. The desktop app draws its own
|
||||
* server's tabs natively, so those stay agent-drivable at no cost. A remote
|
||||
* environment's tabs would stream, so the desktop opens them in its own
|
||||
* browser instead: no latency, and it reaches what this computer reaches. The
|
||||
* user can move a tab to the environment when only it can reach the page.
|
||||
*/
|
||||
export function previewRuntimeFor(environmentId: EnvironmentId): PreviewRuntime | undefined {
|
||||
return readEnvironmentSupportsServerBrowser(environmentId) ? "server" : undefined;
|
||||
if (!readEnvironmentSupportsServerBrowser(environmentId)) return undefined;
|
||||
if (
|
||||
isPreviewSupportedInRuntime() &&
|
||||
environmentId !== appAtomRegistry.get(primaryEnvironmentIdAtom)
|
||||
) {
|
||||
return undefined;
|
||||
}
|
||||
return "server";
|
||||
}
|
||||
|
||||
/**
|
||||
* The other browser a tab can move to, or null when it has none: a desktop
|
||||
* tab of a remote environment can move to that environment's browser, and back.
|
||||
*/
|
||||
export function alternatePreviewRuntime(
|
||||
environmentId: EnvironmentId,
|
||||
primaryEnvironmentId: EnvironmentId | null,
|
||||
serverBrowser: boolean,
|
||||
snapshot: Pick<PreviewSessionSnapshot, "runtime"> | null | undefined,
|
||||
): PreviewRuntime | null {
|
||||
if (!snapshot || !serverBrowser || !isPreviewSupportedInRuntime()) return null;
|
||||
if (environmentId === primaryEnvironmentId) return null;
|
||||
return snapshot.runtime === "server" ? "desktop" : "server";
|
||||
}
|
||||
|
||||
/** Electron hosts its own browser tabs; other clients need the environment to host them. */
|
||||
|
||||
@@ -64,6 +64,8 @@ interface Props {
|
||||
nativePictureInPicture: boolean;
|
||||
/** Profile display name, shown so the menu says which data is being cleared. */
|
||||
profileName: string | undefined;
|
||||
/** Reopens the tab in the other browser it can run in. */
|
||||
move?: { readonly label: string; readonly onMove: () => void };
|
||||
}
|
||||
|
||||
const MenuTriggerButton = () => (
|
||||
@@ -91,6 +93,7 @@ export function PreviewMoreMenu({
|
||||
onToggleDeviceToolbar,
|
||||
nativePictureInPicture,
|
||||
profileName,
|
||||
move,
|
||||
}: Props) {
|
||||
const zoomLabel = `${Math.round(zoomFactor * 100)}%`;
|
||||
const disabled = !enabled;
|
||||
@@ -113,6 +116,7 @@ export function PreviewMoreMenu({
|
||||
: "Open separate preview window"}
|
||||
</MenuItem>
|
||||
) : null}
|
||||
{move ? <MenuItem onClick={move.onMove}>{move.label}</MenuItem> : null}
|
||||
<MenuItem onClick={onToggleDeviceToolbar} disabled={disabled}>
|
||||
{deviceToolbarVisible ? "Hide device toolbar" : "Show device toolbar"}
|
||||
</MenuItem>
|
||||
|
||||
@@ -11,6 +11,8 @@ interface Props {
|
||||
/** Stringified Chromium error, e.g. "ERR_NAME_NOT_RESOLVED". */
|
||||
description: string;
|
||||
onReload: () => void;
|
||||
/** Reopens the page in a browser that may reach it, such as the environment's. */
|
||||
move?: { readonly label: string; readonly onMove: () => void };
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -58,7 +60,7 @@ export function PreviewFileNotShown({
|
||||
}
|
||||
|
||||
/** Theme-aware tailwind port of Chromium's "This site can't be reached" page. */
|
||||
export function PreviewUnreachable({ url, code, description, onReload }: Props) {
|
||||
export function PreviewUnreachable({ url, code, description, onReload, move }: Props) {
|
||||
const [showDetails, setShowDetails] = useState(false);
|
||||
const host = safeHost(url) ?? url;
|
||||
const friendly = describePreviewError(description);
|
||||
@@ -81,6 +83,9 @@ export function PreviewUnreachable({ url, code, description, onReload }: Props)
|
||||
<ul className="list-disc space-y-1 pl-5 text-muted-foreground">
|
||||
<li>Checking your connection</li>
|
||||
<li>Confirming the dev server is running</li>
|
||||
{move ? (
|
||||
<li>Opening it in the environment’s browser, which reaches its network</li>
|
||||
) : null}
|
||||
<li>Checking the proxy and the firewall</li>
|
||||
</ul>
|
||||
</div>
|
||||
@@ -100,6 +105,11 @@ export function PreviewUnreachable({ url, code, description, onReload }: Props)
|
||||
{showDetails ? "Hide details" : "Details"}
|
||||
</Button>
|
||||
<div className="flex-1" />
|
||||
{move ? (
|
||||
<Button type="button" variant="outline" size="sm" onClick={move.onMove}>
|
||||
{move.label}
|
||||
</Button>
|
||||
) : null}
|
||||
<Button type="button" size="sm" onClick={onReload}>
|
||||
Reload
|
||||
</Button>
|
||||
|
||||
@@ -151,10 +151,11 @@ vi.mock("~/previewStateStore", () => ({
|
||||
vi.mock("~/state/environments", () => ({
|
||||
useEnvironment: () => ({ label: "WSL" }),
|
||||
useEnvironmentHttpBaseUrl: () => "http://172.25.85.75:3773",
|
||||
usePrimaryEnvironmentId: () => null,
|
||||
}));
|
||||
|
||||
vi.mock("~/state/preview", () => ({
|
||||
previewEnvironment: { open: {}, resize: {} },
|
||||
previewEnvironment: { open: {}, close: {}, resize: {} },
|
||||
}));
|
||||
|
||||
vi.mock("~/state/use-atom-command", () => ({
|
||||
|
||||
@@ -36,7 +36,12 @@ import {
|
||||
useThreadPreviewState,
|
||||
} from "~/previewStateStore";
|
||||
import { resolveDiscoveredServerUrl } from "~/browser/browserTargetResolver";
|
||||
import { useEnvironmentHttpBaseUrl } from "~/state/environments";
|
||||
import { useEnvironmentSupportsServerBrowser } from "~/state/entities";
|
||||
import {
|
||||
useEnvironment,
|
||||
useEnvironmentHttpBaseUrl,
|
||||
usePrimaryEnvironmentId,
|
||||
} from "~/state/environments";
|
||||
import { previewEnvironment } from "~/state/preview";
|
||||
import { usePreviewStreamAccess } from "~/state/previewStream";
|
||||
import { useAtomCommand } from "~/state/use-atom-command";
|
||||
@@ -50,6 +55,7 @@ import { useRightPanelStore } from "~/rightPanelStore";
|
||||
|
||||
import { previewBridge } from "./previewBridge";
|
||||
import { subscribePreviewAction } from "./previewActionBus";
|
||||
import { closePreviewSession } from "./closePreviewSession";
|
||||
import { openPreviewSession } from "./openPreviewSession";
|
||||
import { showPreviewPopup } from "./showPreviewPopup";
|
||||
import { PreviewChromeRow } from "./PreviewChromeRow";
|
||||
@@ -69,7 +75,7 @@ import { revealInFileExplorerLabel } from "./fileExplorerLabel";
|
||||
import { shouldShowPreviewEmptyState } from "./previewEmptyStateLogic";
|
||||
import { Badge } from "~/components/ui/badge";
|
||||
import { BrowserSurfaceSlot } from "~/browser/BrowserSurfaceSlot";
|
||||
import { useRendersServerTabNatively } from "~/browser/previewRuntime";
|
||||
import { alternatePreviewRuntime, useRendersServerTabNatively } from "~/browser/previewRuntime";
|
||||
import { ServerBrowserSurface, type ServerBrowserHandle } from "~/browser/ServerBrowserSurface";
|
||||
import { cn } from "~/lib/utils";
|
||||
import { useBrowserSurfaceStore } from "~/browser/browserSurfaceStore";
|
||||
@@ -169,6 +175,10 @@ export function PreviewView({
|
||||
? new URL(environmentHttpBaseUrl).hostname
|
||||
: null;
|
||||
const open = useAtomCommand(previewEnvironment.open);
|
||||
const closePreview = useAtomCommand(previewEnvironment.close, "preview close");
|
||||
const environmentLabel = useEnvironment(threadRef.environmentId)?.label ?? "the environment";
|
||||
const primaryEnvironmentId = usePrimaryEnvironmentId();
|
||||
const serverBrowser = useEnvironmentSupportsServerBrowser(threadRef.environmentId);
|
||||
const resize = useAtomCommand(previewEnvironment.resize, "preview viewport resize");
|
||||
const adjust = useAtomCommand(previewEnvironment.adjust, "preview appearance or zoom");
|
||||
|
||||
@@ -438,6 +448,48 @@ export function PreviewView({
|
||||
void localApi.shell.openExternal(url).catch(() => undefined);
|
||||
}, [url]);
|
||||
|
||||
// A desktop tab of a remote environment can only reach what this computer
|
||||
// reaches; the environment's browser reaches its own network, and its agents.
|
||||
const moveTarget = alternatePreviewRuntime(
|
||||
threadRef.environmentId,
|
||||
primaryEnvironmentId,
|
||||
serverBrowser,
|
||||
snapshot,
|
||||
);
|
||||
const moveLabel =
|
||||
moveTarget === "server" ? `Open in ${environmentLabel}'s browser` : "Open on this computer";
|
||||
const handleMoveTab = useCallback(async () => {
|
||||
if (!moveTarget || !tabId || !snapshot) return;
|
||||
const result = await openPreviewSession({
|
||||
openPreview: open,
|
||||
threadRef,
|
||||
// Loopback means the environment's machine, which this computer reaches by its address.
|
||||
...(url
|
||||
? {
|
||||
url:
|
||||
moveTarget === "desktop"
|
||||
? resolveDiscoveredServerUrl(threadRef.environmentId, url)
|
||||
: url,
|
||||
}
|
||||
: {}),
|
||||
viewport,
|
||||
...(snapshot.profileId === undefined ? {} : { profileId: snapshot.profileId }),
|
||||
runtime: moveTarget,
|
||||
});
|
||||
if (result._tag === "Failure") {
|
||||
if (isAtomCommandInterrupted(result)) return;
|
||||
const error = squashAtomCommandFailure(result);
|
||||
toastManager.add({
|
||||
type: "error",
|
||||
title: "Unable to move the browser tab",
|
||||
description: error instanceof Error ? error.message : "An error occurred.",
|
||||
});
|
||||
return;
|
||||
}
|
||||
useRightPanelStore.getState().openBrowser(threadRef, result.value.tabId);
|
||||
await closePreviewSession({ closePreview, snapshot, tabId, threadRef });
|
||||
}, [closePreview, moveTarget, open, snapshot, tabId, threadRef, url, viewport]);
|
||||
|
||||
const handlePictureInPicture = useCallback(() => {
|
||||
if (!tabId) return;
|
||||
if (miniPlayerTabId === tabId) {
|
||||
@@ -977,6 +1029,9 @@ export function PreviewView({
|
||||
(serverOwnsRendering || (desktopOverlay?.hasWebContents ?? false))
|
||||
}
|
||||
actions={moreMenuActions}
|
||||
{...(moveTarget
|
||||
? { move: { label: moveLabel, onMove: () => void handleMoveTab() } }
|
||||
: {})}
|
||||
profileName={activeProfileName}
|
||||
zoomFactor={
|
||||
serverOwnsRendering ? serverZoomFactor : (desktopOverlay?.zoomFactor ?? 1)
|
||||
@@ -1088,6 +1143,9 @@ export function PreviewView({
|
||||
code={navStatus.code}
|
||||
description={navStatus.description}
|
||||
onReload={handleRefresh}
|
||||
{...(moveTarget === "server"
|
||||
? { move: { label: moveLabel, onMove: () => void handleMoveTab() } }
|
||||
: {})}
|
||||
/>
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import type {
|
||||
EnvironmentId,
|
||||
PreviewOpenInput,
|
||||
PreviewRuntime,
|
||||
PreviewSessionSnapshot,
|
||||
PreviewViewportSetting,
|
||||
ScopedThreadRef,
|
||||
@@ -29,6 +30,8 @@ interface OpenPreviewSessionInput<E> {
|
||||
viewport?: PreviewViewportSetting;
|
||||
/** Overrides the configured default profile. */
|
||||
profileId?: string;
|
||||
/** Overrides where the tab runs, such as when moving a tab between browsers. */
|
||||
runtime?: PreviewRuntime;
|
||||
}
|
||||
|
||||
export async function openPreviewSession<E>(
|
||||
@@ -42,7 +45,7 @@ export async function openPreviewSession<E>(
|
||||
if (defaults instanceof BrowserSettingsReadError) {
|
||||
return AsyncResult.failure(Cause.fail(defaults));
|
||||
}
|
||||
const runtime = previewRuntimeFor(input.threadRef.environmentId);
|
||||
const runtime = input.runtime ?? previewRuntimeFor(input.threadRef.environmentId);
|
||||
const result = await input.openPreview({
|
||||
environmentId: input.threadRef.environmentId,
|
||||
input: {
|
||||
@@ -50,7 +53,7 @@ export async function openPreviewSession<E>(
|
||||
...(input.url === undefined ? {} : { url: input.url }),
|
||||
viewport: input.viewport ?? browserDefaultOpenViewport(defaults),
|
||||
profileId: input.profileId ?? browserDefaultOpenProfileId(defaults),
|
||||
...(runtime === undefined ? {} : { runtime }),
|
||||
...(runtime === undefined || runtime === "desktop" ? {} : { runtime }),
|
||||
},
|
||||
});
|
||||
if (result._tag === "Failure") {
|
||||
|
||||
Reference in new issue
Block a user