Skip to content
TypeScript

feat(mobile): close browser tabs from the Browser page (#19579)

Tristan Knight32c930443679
diff --git a/apps/mobile/src/features/browser/BrowserPreviewRouteScreen.tsx b/apps/mobile/src/features/browser/BrowserPreviewRouteScreen.tsx
index cb7a7615fd3..a458046ee2b 100644
--- a/apps/mobile/src/features/browser/BrowserPreviewRouteScreen.tsx
+++ b/apps/mobile/src/features/browser/BrowserPreviewRouteScreen.tsx
@@ -271,7 +271,7 @@ function BrowserAddressBar({
<BrowserTabMenu
environmentId={environmentId}
tab={tab}
- disabled={!streaming}
+ streaming={streaming}
onPaste={ready ? onPaste : undefined}
/>
</View>
diff --git a/apps/mobile/src/features/browser/BrowserTabMenu.tsx b/apps/mobile/src/features/browser/BrowserTabMenu.tsx
index 0ade61a7a1b..916fde2470e 100644
--- a/apps/mobile/src/features/browser/BrowserTabMenu.tsx
+++ b/apps/mobile/src/features/browser/BrowserTabMenu.tsx
@@ -1,4 +1,5 @@
import * as Dialog from "../../components/ui/Dialog";
+import { useAtomValue } from "@effect/atom-react";
import type { MenuAction } from "@react-native-menu/menu";
import {
DEFAULT_PREVIEW_ZOOM_FACTOR,
@@ -40,16 +41,18 @@ const VIEWPORTS = [
export function BrowserTabMenu({
environmentId,
tab,
- disabled,
+ streaming,
onPaste,
}: {
readonly environmentId: EnvironmentId;
readonly tab: PreviewSessionSnapshot;
- readonly disabled: boolean;
+ readonly streaming: boolean;
readonly onPaste?: () => void;
}) {
const adjust = useAtomCommand(previewEnvironment.adjust, "browser tab change");
const resize = useAtomCommand(previewEnvironment.resize, "browser viewport change");
+ const close = useAtomCommand(previewEnvironment.close, "browser tab close");
+ const canClose = useAtomValue(previewEnvironment.close.permissionAtom(environmentId));
const target = { threadId: tab.threadId as PreviewAdjustInput["threadId"], tabId: tab.tabId };
const colorScheme = tab.colorScheme ?? "system";
const zoomFactor = tab.zoomFactor ?? DEFAULT_PREVIEW_ZOOM_FACTOR;
@@ -65,6 +68,12 @@ export function BrowserTabMenu({
? `preset:${tab.viewport.presetId}`
: "custom";
+ const closeAction: MenuAction = {
+ id: "close",
+ title: "Close tab",
+ image: "xmark",
+ attributes: { destructive: true, disabled: !canClose },
+ };
const actions: MenuAction[] = [
...(onPaste ? [{ id: "paste", title: "Paste", image: "doc.on.clipboard" }] : []),
{ id: "hard-reload", title: "Hard reload", image: "arrow.clockwise.circle" },
@@ -107,6 +116,7 @@ export function BrowserTabMenu({
{ id: "clear:cache", title: "Clear cache", attributes: { destructive: true } },
],
},
+ closeAction,
];
const run = async (change: Omit<PreviewAdjustInput, "threadId" | "tabId">) => {
@@ -115,6 +125,12 @@ export function BrowserTabMenu({
};
const onAction = (id: string) => {
+ if (id === "close") {
+ void close({ environmentId, input: target }).then((result) => {
+ if (result._tag === "Failure") Dialog.alert("Could not close this browser tab");
+ });
+ return;
+ }
if (id === "paste") return onPaste?.();
if (id === "hard-reload") return void run({ hardReload: true });
if (id === "zoom:in") return void run({ zoomFactor: zoomTo(1) });
@@ -138,19 +154,18 @@ export function BrowserTabMenu({
}
};
- const pill = (
- <ControlPill icon="ellipsis" accessibilityLabel="Browser tab options" disabled={disabled} />
- );
- if (disabled) return pill;
return (
<Menu
+ // Remount when streaming changes, so an open menu never keeps offering
+ // the other state's actions.
+ key={streaming ? "streaming" : "idle"}
accessible
accessibilityLabel="Browser tab options"
accessibilityRole="button"
- actions={actions}
+ actions={streaming ? actions : [closeAction]}
onPressAction={({ nativeEvent }) => onAction(nativeEvent.event)}
>
- {pill}
+ <ControlPill icon="ellipsis" accessibilityLabel="Browser tab options" />
</Menu>
);
}
diff --git a/packages/client-runtime/src/state/commandPermissions.test.ts b/packages/client-runtime/src/state/commandPermissions.test.ts
index 58a77730a40..efb8aa47722 100644
--- a/packages/client-runtime/src/state/commandPermissions.test.ts
+++ b/packages/client-runtime/src/state/commandPermissions.test.ts
@@ -8,6 +8,7 @@ import { describe, expect, it } from "@effect/vitest";
import { vi } from "vite-plus/test";
import {
AuthOrchestrationOperateScope,
+ AuthPreviewOperateScope,
AuthSettingsWriteScope,
AuthSourceControlWriteScope,
ThreadId,
@@ -59,6 +60,35 @@ const setup = Effect.gen(function* () {
});
describe("command permissions", () => {
+ it.effect("requires the destination preview grant to close a browser tab", () =>
+ Effect.scoped(
+ Effect.gen(function* () {
+ const registry = yield* setup;
+ const close = createCommandPermissions(runtime, WS_METHODS.previewClose);
+ registry.set(sessions(env), AsyncResult.success(grant(true)));
+ expect(registry.get(close.permissionAtom(env))).toBe(false);
+ expect((yield* close.authorize(registry, env).pipe(Effect.flip))._tag).toBe(
+ "EnvironmentAuthorizationError",
+ );
+ registry.set(
+ sessions(env),
+ AsyncResult.success({
+ ...grant(false),
+ scopes: [AuthPreviewOperateScope],
+ permissions: [AuthPreviewOperateScope],
+ }),
+ );
+ expect(registry.get(close.permissionAtom(env))).toBe(true);
+ yield* close.authorize(registry, env);
+ registry.set(sessions(other), AsyncResult.success(grant(false)));
+ expect(registry.get(close.permissionAtom(other))).toBe(false);
+ expect((yield* close.authorize(registry, other).pipe(Effect.flip))._tag).toBe(
+ "EnvironmentAuthorizationError",
+ );
+ }),
+ ),
+ );
+
it.effect("requires the destination settings grant to run storage cleanup", () =>
Effect.scoped(
Effect.gen(function* () {
diff --git a/packages/contracts/src/clientRpcPermissions.ts b/packages/contracts/src/clientRpcPermissions.ts
index e111eb244c5..29df0557c20 100644
--- a/packages/contracts/src/clientRpcPermissions.ts
+++ b/packages/contracts/src/clientRpcPermissions.ts
@@ -2,6 +2,7 @@ import * as Schema from "effect/Schema";
import { GitPreparePullRequestThreadInput } from "./git.ts";
import {
AuthOrchestrationOperateScope,
+ AuthPreviewOperateScope,
AuthSettingsWriteScope,
AuthSourceControlWriteScope,
type AuthEnvironmentScope,
@@ -11,6 +12,7 @@ import { WS_METHODS } from "./rpc.ts";
/** Incremental client enforcement; the server still authorizes every request. */
export const CLIENT_GUARDED_RPC_SCOPES = {
+ [WS_METHODS.previewClose]: AuthPreviewOperateScope,
[WS_METHODS.serverRunStorageCleanup]: AuthSettingsWriteScope,
[WS_METHODS.serverCheckOtlpEndpoint]: AuthSettingsWriteScope,
[WS_METHODS.pullRequestsRunAction]: AuthSourceControlWriteScope,