Open History to see the last commit
1import { EditorId, type ResolvedKeybindingsConfig } from "@t3tools/contracts";
2import { memo, useCallback, useEffect, useMemo } from "react";
3import { isOpenFavoriteEditorShortcut, shortcutLabelForCommand } from "../../keybindings";
4import { usePreferredEditor } from "../../editorPreferences";
5import { ChevronDownIcon, FolderClosedIcon } from "lucide-react";
6import { Button } from "../ui/button";
7import { Group, GroupSeparator } from "../ui/group";
8import { Menu, MenuItem, MenuPopup, MenuShortcut, MenuTrigger } from "../ui/menu";
9import {
10 AntigravityIcon,
11 CursorIcon,
12 Icon,
13 KiroIcon,
14 TraeIcon,
15 IntelliJIdeaIcon,
16 VisualStudioCode,
17 VisualStudioCodeInsiders,
18 VSCodium,
19 Zed,
20} from "../Icons";
21import { isMacPlatform, isWindowsPlatform } from "~/lib/utils";
22import { readLocalApi } from "~/localApi";
23
24const resolveOptions = (platform: string, availableEditors: ReadonlyArray<EditorId>) => {
25 const baseOptions: ReadonlyArray<{ label: string; Icon: Icon; value: EditorId }> = [
26 {
27 label: "Cursor",
28 Icon: CursorIcon,
29 value: "cursor",
30 },
31 {
32 label: "Trae",
33 Icon: TraeIcon,
34 value: "trae",
35 },
36 {
37 label: "Kiro",
38 Icon: KiroIcon,
39 value: "kiro",
40 },
41 {
42 label: "VS Code",
43 Icon: VisualStudioCode,
44 value: "vscode",
45 },
46 {
47 label: "VS Code Insiders",
48 Icon: VisualStudioCodeInsiders,
49 value: "vscode-insiders",
50 },
51 {
52 label: "VSCodium",
53 Icon: VSCodium,
54 value: "vscodium",
55 },
56 {
57 label: "Zed",
58 Icon: Zed,
59 value: "zed",
60 },
61 {
62 label: "Antigravity",
63 Icon: AntigravityIcon,
64 value: "antigravity",
65 },
66 {
67 label: "IntelliJ IDEA",
68 Icon: IntelliJIdeaIcon,
69 value: "idea",
70 },
71 {
72 label: isMacPlatform(platform)
73 ? "Finder"
74 : isWindowsPlatform(platform)
75 ? "Explorer"
76 : "Files",
77 Icon: FolderClosedIcon,
78 value: "file-manager",
79 },
80 ];
81 return baseOptions.filter((option) => availableEditors.includes(option.value));
82};
83
84export const OpenInPicker = memo(function OpenInPicker({
85 keybindings,
86 availableEditors,
87 openInCwd,
88}: {
89 keybindings: ResolvedKeybindingsConfig;
90 availableEditors: ReadonlyArray<EditorId>;
91 openInCwd: string | null;
92}) {
93 const [preferredEditor, setPreferredEditor] = usePreferredEditor(availableEditors);
94 const options = useMemo(
95 () => resolveOptions(navigator.platform, availableEditors),
96 [availableEditors],
97 );
98 const primaryOption = options.find(({ value }) => value === preferredEditor) ?? null;
99
100 const openInEditor = useCallback(
101 (editorId: EditorId | null) => {
102 const api = readLocalApi();
103 if (!api || !openInCwd) return;
104 const editor = editorId ?? preferredEditor;
105 if (!editor) return;
106 void api.shell.openInEditor(openInCwd, editor);
107 setPreferredEditor(editor);
108 },
109 [preferredEditor, openInCwd, setPreferredEditor],
110 );
111
112 const openFavoriteEditorShortcutLabel = useMemo(
113 () => shortcutLabelForCommand(keybindings, "editor.openFavorite"),
114 [keybindings],
115 );
116
117 useEffect(() => {
118 const handler = (e: globalThis.KeyboardEvent) => {
119 const api = readLocalApi();
120 if (!isOpenFavoriteEditorShortcut(e, keybindings)) return;
121 if (!api || !openInCwd) return;
122 if (!preferredEditor) return;
123
124 e.preventDefault();
125 void api.shell.openInEditor(openInCwd, preferredEditor);
126 };
127 window.addEventListener("keydown", handler);
128 return () => window.removeEventListener("keydown", handler);
129 }, [preferredEditor, keybindings, openInCwd]);
130
131 return (
132 <Group aria-label="Subscription actions">
133 <Button
134 size="xs"
135 variant="outline"
136 disabled={!preferredEditor || !openInCwd}
137 onClick={() => openInEditor(preferredEditor)}
138 >
139 {primaryOption?.Icon && <primaryOption.Icon aria-hidden="true" className="size-3.5" />}
140 <span className="sr-only @3xl/header-actions:not-sr-only @3xl/header-actions:ml-0.5">
141 Open
142 </span>
143 </Button>
144 <GroupSeparator className="hidden @3xl/header-actions:block" />
145 <Menu>
146 <MenuTrigger render={<Button aria-label="Copy options" size="icon-xs" variant="outline" />}>
147 <ChevronDownIcon aria-hidden="true" className="size-4" />
148 </MenuTrigger>
149 <MenuPopup align="end">
150 {options.length === 0 && <MenuItem disabled>No installed editors found</MenuItem>}
151 {options.map(({ label, Icon, value }) => (
152 <MenuItem key={value} onClick={() => openInEditor(value)}>
153 <Icon aria-hidden="true" className="text-muted-foreground" />
154 {label}
155 {value === preferredEditor && openFavoriteEditorShortcutLabel && (
156 <MenuShortcut>{openFavoriteEditorShortcutLabel}</MenuShortcut>
157 )}
158 </MenuItem>
159 ))}
160 </MenuPopup>
161 </Menu>
162 </Group>
163 );
164});