import type { EditorId, EnvironmentId, ResolvedKeybindingsConfig } from "@t3tools/contracts"; import { useEffect } from "react"; import { usePreferredEditor } from "../../editorPreferences"; import { isOpenFavoriteEditorShortcut } from "../../keybindings"; import { shellEnvironment } from "../../state/shell"; import { useAtomCommand } from "../../state/use-atom-command"; export function useOpenFavoriteEditorShortcut({ enabled, environmentId, keybindings, availableEditors, openInCwd, }: { enabled: boolean; environmentId: EnvironmentId; keybindings: ResolvedKeybindingsConfig; availableEditors: ReadonlyArray; openInCwd: string | null; }) { const openInEditorMutation = useAtomCommand(shellEnvironment.openInEditor, "open in editor"); const [preferredEditor] = usePreferredEditor(availableEditors); useEffect(() => { if (!enabled) return; const handler = (event: globalThis.KeyboardEvent) => { if (!isOpenFavoriteEditorShortcut(event, keybindings)) return; if (!openInCwd || !preferredEditor) return; event.preventDefault(); void openInEditorMutation({ environmentId, input: { cwd: openInCwd, editor: preferredEditor, }, }); }; window.addEventListener("keydown", handler); return () => window.removeEventListener("keydown", handler); }, [enabled, environmentId, keybindings, openInCwd, openInEditorMutation, preferredEditor]); }