The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | buildRemoteOpenUrl, |
| 3 | type EditorId, |
| 4 | type EnvironmentId, |
| 5 | type ResolvedKeybindingsConfig, |
| 6 | } from "@t3tools/contracts"; |
| 7 | import { msg } from "gt-react"; |
| 8 | import { useEffect } from "react"; |
| 9 | |
| 10 | import { usePreferredEditor } from "../../editorPreferences"; |
| 11 | import { editorLabelForPlatform } from "../../editorLabels"; |
| 12 | import { isOpenFavoriteEditorShortcut } from "../../keybindings"; |
| 13 | import { m } from "../../i18n/messages"; |
| 14 | import { announce } from "../../lib/announce"; |
| 15 | import { openRemoteEditorUrl, useRemoteCapableEditors, useRemoteOpenState } from "../../remoteOpen"; |
| 16 | import { registerCommands } from "../../shortcuts/dispatcher"; |
| 17 | import { shellEnvironment } from "../../state/shell"; |
| 18 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 19 | |
| 20 | /** Marks an OpenInPicker's primary button as the target of `editor.openFavorite`. */ |
| 21 | export const OPEN_FAVORITE_EDITOR_ATTRIBUTE = "data-open-favorite-editor"; |
| 22 | |
| 23 | /** |
| 24 | * `editor.openFavorite` presses the marked OpenInPicker primary button, so the shortcut opens |
| 25 | * exactly what the button on screen would. A missing or disabled button leaves the chord to the |
| 26 | * bubble-phase fallback below. Capture phase, so a focused terminal does not swallow the chord. |
| 27 | */ |
| 28 | export function registerOpenFavoriteEditorCommand(): () => void { |
| 29 | return registerCommands( |
| 30 | { |
| 31 | "editor.openFavorite": (event) => { |
| 32 | if (event.repeat) return false; |
| 33 | const button = [ |
| 34 | ...document.querySelectorAll<HTMLButtonElement>(`[${OPEN_FAVORITE_EDITOR_ATTRIBUTE}]`), |
| 35 | ].at(-1); |
| 36 | if (button === undefined || button.disabled) return false; |
| 37 | event.preventDefault(); |
| 38 | button.click(); |
| 39 | }, |
| 40 | }, |
| 41 | { capture: true }, |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | export function useOpenFavoriteEditorShortcut({ |
| 46 | enabled, |
| 47 | environmentId, |
| 48 | keybindings, |
| 49 | availableEditors, |
| 50 | openInCwd, |
| 51 | }: { |
| 52 | enabled: boolean; |
| 53 | environmentId: EnvironmentId; |
| 54 | keybindings: ResolvedKeybindingsConfig; |
| 55 | availableEditors: ReadonlyArray<EditorId>; |
| 56 | openInCwd: string | null; |
| 57 | }) { |
| 58 | const openInEditorMutation = useAtomCommand(shellEnvironment.openInEditor, "open in editor"); |
| 59 | const remote = useRemoteOpenState(environmentId); |
| 60 | const remoteCapableEditors = useRemoteCapableEditors(); |
| 61 | // Matches the picker: remote editors run on the viewing machine. |
| 62 | const [preferredEditor] = usePreferredEditor( |
| 63 | remote.mode === "local-exec" ? availableEditors : remoteCapableEditors, |
| 64 | ); |
| 65 | |
| 66 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes a window keydown listener for the open-in-editor shortcut while enabled |
| 67 | useEffect(() => { |
| 68 | if (!enabled || remote.mode === "remote-unavailable") return; |
| 69 | const handler = (event: globalThis.KeyboardEvent) => { |
| 70 | if ( |
| 71 | event.repeat || |
| 72 | event.defaultPrevented || |
| 73 | !isOpenFavoriteEditorShortcut(event, keybindings) |
| 74 | ) |
| 75 | return; |
| 76 | if (!openInCwd || !preferredEditor) return; |
| 77 | |
| 78 | event.preventDefault(); |
| 79 | const label = editorLabelForPlatform(preferredEditor, navigator.platform); |
| 80 | if (remote.mode === "remote-links") { |
| 81 | const url = buildRemoteOpenUrl({ |
| 82 | editor: preferredEditor, |
| 83 | ...remote.host, |
| 84 | absolutePath: openInCwd, |
| 85 | }); |
| 86 | if (url === undefined) return; |
| 87 | void openRemoteEditorUrl(url).then((opened) => { |
| 88 | if (opened) announce(m(msg("Opened in {editor}", { editor: label }))); |
| 89 | }); |
| 90 | return; |
| 91 | } |
| 92 | void openInEditorMutation({ |
| 93 | environmentId, |
| 94 | input: { |
| 95 | cwd: openInCwd, |
| 96 | editor: preferredEditor, |
| 97 | }, |
| 98 | }).then((result) => { |
| 99 | if (result._tag === "Success") announce(m(msg("Opened in {editor}", { editor: label }))); |
| 100 | }); |
| 101 | }; |
| 102 | window.addEventListener("keydown", handler); |
| 103 | return () => window.removeEventListener("keydown", handler); |
| 104 | }, [ |
| 105 | enabled, |
| 106 | environmentId, |
| 107 | keybindings, |
| 108 | openInCwd, |
| 109 | openInEditorMutation, |
| 110 | preferredEditor, |
| 111 | remote, |
| 112 | ]); |
| 113 | } |