TypeScript
fix(web): Cmd+O no longer opens the editor more than once (#18983)
Samuel Soellerfad7202f9ca6
diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsxindex fbbaa17a42d..5096063c74d 100644--- a/apps/web/src/components/ChatView.tsx+++ b/apps/web/src/components/ChatView.tsx@@ -8451,6 +8451,13 @@ export default function ChatView(props: ChatViewProps) {keybindings={keybindings}availableEditors={availableEditors}showOpenInPicker={showOpenInPicker}+ openInShortcutEnabled={+ !(+ rightPanelOpen &&+ renderedRightPanelSurface?.kind === "file" &&+ renderedRightPanelSurface.attachment === undefined+ )+ }gitCwd={gitCwd}isGitRepo={isGitRepo}envLocked={envLocked}diff --git a/apps/web/src/components/chat/OpenInPicker.browser.test.tsx b/apps/web/src/components/chat/OpenInPicker.browser.test.tsxindex 6f418a2bbab..82603fc3fca 100644--- a/apps/web/src/components/chat/OpenInPicker.browser.test.tsx+++ b/apps/web/src/components/chat/OpenInPicker.browser.test.tsx@@ -66,6 +66,7 @@ vi.mock(import("../../remoteOpen"), async (importOriginal) => ({}));import { OpenInPicker } from "./OpenInPicker";+import { useOpenFavoriteEditorShortcut } from "./OpenInPickerShortcut";const primary = EnvironmentId.make("primary");const selected = EnvironmentId.make("selected");@@ -133,17 +134,105 @@ async function click(element: HTMLElement) {await act(async () => element.click());}-async function pressOpenFavorite() {+async function pressOpenFavorite(mac = false) {const event = new KeyboardEvent("keydown", {key: "o",- ctrlKey: true,+ ctrlKey: !mac,+ metaKey: mac,bubbles: true,cancelable: true,});- await act(async () => window.dispatchEvent(event));+ await act(async () => document.body.dispatchEvent(event));return event;}+function FavoriteEditorFallback() {+ useOpenFavoriteEditorShortcut({+ enabled: true,+ environmentId: selected,+ keybindings: DEFAULT_RESOLVED_KEYBINDINGS,+ availableEditors: editors,+ openInCwd: "/work/project",+ });+ return null;+}++it.each(["local-exec", "remote-links"] as const)(+ "opens exactly one %s target as the file preview and workspace pickers mount and unmount",+ async (mode) => {+ state.allowed.add(selected);+ state.remote =+ mode === "local-exec" ? { mode } : { mode, host: { kind: "ssh-alias", host: "test-host" } };+ const mac = mode === "remote-links";+ vi.spyOn(navigator, "platform", "get").mockReturnValue(mac ? "MacIntel" : "Linux");+ const surfaces = ["workspace", "file", "workspace"];+ if (mode === "local-exec") surfaces.push("fallback");++ for (const surface of surfaces) {+ const previewVisible = surface === "file";+ await act(async () => {+ root.render(+ <>+ {/* Mount the fallback first so picker priority cannot rely on listener order. */}+ <FavoriteEditorFallback />+ {surface !== "fallback" && (+ <OpenInPicker+ environmentId={selected}+ keybindings={DEFAULT_RESOLVED_KEYBINDINGS}+ availableEditors={editors}+ openInCwd="/work/project"+ displayMode="panel"+ enableShortcut={!previewVisible}+ />+ )}+ {previewVisible && (+ <OpenInPicker+ environmentId={selected}+ keybindings={DEFAULT_RESOLVED_KEYBINDINGS}+ availableEditors={editors}+ openInCwd="/work/project/README.md"+ compact+ />+ )}+ </>,+ );+ });+ state.run.mockClear();+ state.openUrl.mockClear();+ const event = await pressOpenFavorite(mac);+ const cwd = previewVisible ? "/work/project/README.md" : "/work/project";+ expect.soft(event.defaultPrevented, surface).toBe(true);+ if (mode === "local-exec") {+ expect.soft(state.run, surface).toHaveBeenCalledExactlyOnceWith({+ environmentId: selected,+ input: { cwd, editor: "vscode" },+ });+ expect.soft(state.openUrl, surface).not.toHaveBeenCalled();+ } else {+ expect+ .soft(state.openUrl, surface)+ .toHaveBeenCalledExactlyOnceWith(`vscode://vscode-remote/ssh-remote+test-host${cwd}`);+ expect.soft(state.run, surface).not.toHaveBeenCalled();+ }+ }+ },+);++it.each(["remote-links", "remote-unavailable"] as const)(+ "does not launch an editor from the %s fallback when no picker is mounted",+ async (mode) => {+ state.allowed.add(selected);+ state.remote =+ mode === "remote-links" ? { mode, host: { kind: "ssh-alias", host: "test-host" } } : { mode };+ await act(async () => root.render(<FavoriteEditorFallback />));++ await pressOpenFavorite();++ expect.soft(state.run).not.toHaveBeenCalled();+ expect.soft(state.openUrl).not.toHaveBeenCalled();+ },+);+it.each(["local-exec", "remote-links"] as const)("selects the preferred editor without launching a %s workspace",async (mode) => {diff --git a/apps/web/src/components/chat/OpenInPicker.tsx b/apps/web/src/components/chat/OpenInPicker.tsxindex c7fdf24f10e..050725964af 100644--- a/apps/web/src/components/chat/OpenInPicker.tsx+++ b/apps/web/src/components/chat/OpenInPicker.tsx@@ -283,7 +283,7 @@ export const OpenInPicker = memo(function OpenInPicker({useEffect(() => {if (!enableShortcut || !canOpenEditor) return;const handler = (e: globalThis.KeyboardEvent) => {- if (!isOpenFavoriteEditorShortcut(e, keybindings)) return;+ if (e.defaultPrevented || !isOpenFavoriteEditorShortcut(e, keybindings)) return;if (!openInCwd) return;if (!preferredEditor) return;if (@@ -295,8 +295,8 @@ export const OpenInPicker = memo(function OpenInPicker({e.preventDefault();void openInEditor(preferredEditor);};- window.addEventListener("keydown", handler);- return () => window.removeEventListener("keydown", handler);+ window.addEventListener("keydown", handler, true);+ return () => window.removeEventListener("keydown", handler, true);}, [canOpenEditor,enableShortcut,diff --git a/apps/web/src/components/chat/OpenInPickerShortcut.ts b/apps/web/src/components/chat/OpenInPickerShortcut.tsindex 91102b40c50..6d567b160d7 100644--- a/apps/web/src/components/chat/OpenInPickerShortcut.ts+++ b/apps/web/src/components/chat/OpenInPickerShortcut.ts@@ -3,6 +3,7 @@ import { useEffect } from "react";import { usePreferredEditor } from "../../editorPreferences";import { isOpenFavoriteEditorShortcut } from "../../keybindings";+import { useRemoteOpenState } from "../../remoteOpen";import { shellEnvironment } from "../../state/shell";import { useAtomCommand } from "../../state/use-atom-command";@@ -21,11 +22,12 @@ export function useOpenFavoriteEditorShortcut({}) {const openInEditorMutation = useAtomCommand(shellEnvironment.openInEditor, "open in editor");const [preferredEditor] = usePreferredEditor(availableEditors);+ const remote = useRemoteOpenState(environmentId);useEffect(() => {- if (!enabled) return;+ if (!enabled || remote.mode !== "local-exec") return;const handler = (event: globalThis.KeyboardEvent) => {- if (!isOpenFavoriteEditorShortcut(event, keybindings)) return;+ if (event.defaultPrevented || !isOpenFavoriteEditorShortcut(event, keybindings)) return;if (!openInCwd || !preferredEditor) return;event.preventDefault();@@ -39,5 +41,13 @@ export function useOpenFavoriteEditorShortcut({};window.addEventListener("keydown", handler);return () => window.removeEventListener("keydown", handler);- }, [enabled, environmentId, keybindings, openInCwd, openInEditorMutation, preferredEditor]);+ }, [+ enabled,+ environmentId,+ keybindings,+ openInCwd,+ openInEditorMutation,+ preferredEditor,+ remote.mode,+ ]);}diff --git a/apps/web/src/components/chat/RightPanelSurfaceContent.tsx b/apps/web/src/components/chat/RightPanelSurfaceContent.tsxindex 2fc588777a7..3fed509f5dd 100644--- a/apps/web/src/components/chat/RightPanelSurfaceContent.tsx+++ b/apps/web/src/components/chat/RightPanelSurfaceContent.tsx@@ -196,6 +196,7 @@ export function RightPanelSurfaceContent(props: {composerDraftTarget={props.composerDraftTarget}keybindings={props.keybindings}availableEditors={files.availableEditors}+ openInShortcutEnabled={visible}relativePath={surface.kind === "file" ? surface.relativePath : null}{...(attachment ? { attachment } : {})}revealLine={surface.kind === "file" ? (surface.revealLine ?? null) : null}diff --git a/apps/web/src/components/chat/ThreadDetailsPanel.tsx b/apps/web/src/components/chat/ThreadDetailsPanel.tsxindex 5bc2a4c4525..0d610a5f934 100644--- a/apps/web/src/components/chat/ThreadDetailsPanel.tsx+++ b/apps/web/src/components/chat/ThreadDetailsPanel.tsx@@ -38,6 +38,7 @@ export interface ThreadDetailsPanelProps extends Pick<keybindings: ResolvedKeybindingsConfig;availableEditors: ReadonlyArray<EditorId>;showOpenInPicker: boolean;+ openInShortcutEnabled: boolean;gitCwd: string | null;isGitRepo: boolean;envLocked: boolean;@@ -126,6 +127,7 @@ export function ThreadDetailsPanel(props: ThreadDetailsPanelProps) {environmentId={props.environmentId}availableEditors={props.availableEditors}openInCwd={props.gitCwd}+ enableShortcut={props.openInShortcutEnabled}displayMode="panel"/>) : null}diff --git a/apps/web/src/components/files/FilePreviewPanel.tsx b/apps/web/src/components/files/FilePreviewPanel.tsxindex dfbe629e350..07cee367aae 100644--- a/apps/web/src/components/files/FilePreviewPanel.tsx+++ b/apps/web/src/components/files/FilePreviewPanel.tsx@@ -129,6 +129,7 @@ interface FilePreviewPanelProps {composerDraftTarget: ScopedThreadRef | DraftId;keybindings: ResolvedKeybindingsConfig;availableEditors: ReadonlyArray<EditorId>;+ openInShortcutEnabled: boolean;revealLine: number | null;revealRequestId: number;onOpenFile: (relativePath: string) => void;@@ -979,6 +980,7 @@ export default function FilePreviewPanel({composerDraftTarget,keybindings,availableEditors,+ openInShortcutEnabled,revealLine,revealRequestId,onOpenFile,@@ -1239,6 +1241,7 @@ export default function FilePreviewPanel({environmentId={environmentId}keybindings={keybindings}availableEditors={availableEditors}+ enableShortcut={openInShortcutEnabled}openInCwd={absolutePath}compactrevealFileManager={revealsInFileManager}