Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

OpenInPickerShortcut.ts

53 lines1.6 KB

1import type { EditorId, EnvironmentId, ResolvedKeybindingsConfig } from "@t3tools/contracts";
2import { useEffect } from "react";
3​
4import { usePreferredEditor } from "../../editorPreferences";
5import { isOpenFavoriteEditorShortcut } from "../../keybindings";
6import { useRemoteOpenState } from "../../remoteOpen";
7import { shellEnvironment } from "../../state/shell";
8import { useAtomCommand } from "../../state/use-atom-command";
9​
10export function useOpenFavoriteEditorShortcut({
11 enabled,
12 environmentId,
13 keybindings,
14 availableEditors,
15 openInCwd,
16}: {
17 enabled: boolean;
18 environmentId: EnvironmentId;
19 keybindings: ResolvedKeybindingsConfig;
20 availableEditors: ReadonlyArray<EditorId>;
21 openInCwd: string | null;
22}) {
23 const openInEditorMutation = useAtomCommand(shellEnvironment.openInEditor, "open in editor");
24 const [preferredEditor] = usePreferredEditor(availableEditors);
25 const remote = useRemoteOpenState(environmentId);
26​
27 useEffect(() => {
28 if (!enabled || remote.mode !== "local-exec") return;
29 const handler = (event: globalThis.KeyboardEvent) => {
30 if (event.defaultPrevented || !isOpenFavoriteEditorShortcut(event, keybindings)) return;
31 if (!openInCwd || !preferredEditor) return;
32​
33 event.preventDefault();
34 void openInEditorMutation({
35 environmentId,
36 input: {
37 cwd: openInCwd,
38 editor: preferredEditor,
39 },
40 });
41 };
42 window.addEventListener("keydown", handler);
43 return () => window.removeEventListener("keydown", handler);
44 }, [
45 enabled,
46 environmentId,
47 keybindings,
48 openInCwd,
49 openInEditorMutation,
50 preferredEditor,
51 remote.mode,
52 ]);