Open History to see the last commit
1import { EditorId, type EnvironmentId, 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 VisualStudioCode,
16 VisualStudioCodeInsiders,
17 VSCodium,
18 Zed,
19} from "../Icons";
20import {
21 AquaIcon,
22 CLionIcon,
23 DataGripIcon,
24 DataSpellIcon,
25 GoLandIcon,
26 IntelliJIdeaIcon,
27 PhpStormIcon,
28 PyCharmIcon,
29 RiderIcon,
30 RubyMineIcon,
31 RustRoverIcon,
32 WebStormIcon,
33} from "../JetBrainsIcons";
34import { isMacPlatform, isWindowsPlatform } from "~/lib/utils";
35import { shellEnvironment } from "~/state/shell";
36import { useAtomCommand } from "~/state/use-atom-command";
37
38const resolveOptions = (platform: string, availableEditors: ReadonlyArray<EditorId>) => {
39 const baseOptions: ReadonlyArray<{ label: string; Icon: Icon; value: EditorId }> = [
40 {
41 label: "Cursor",
42 Icon: CursorIcon,
43 value: "cursor",
44 },
45 {
46 label: "Trae",
47 Icon: TraeIcon,
48 value: "trae",
49 },
50 {
51 label: "Kiro",
52 Icon: KiroIcon,
53 value: "kiro",
54 },
55 {
56 label: "VS Code",
57 Icon: VisualStudioCode,
58 value: "vscode",
59 },
60 {
61 label: "VS Code Insiders",
62 Icon: VisualStudioCodeInsiders,
63 value: "vscode-insiders",
64 },
65 {
66 label: "VSCodium",
67 Icon: VSCodium,
68 value: "vscodium",
69 },
70 {
71 label: "Zed",
72 Icon: Zed,
73 value: "zed",
74 },
75 {
76 label: "Antigravity",
77 Icon: AntigravityIcon,
78 value: "antigravity",
79 },
80 {
81 label: "IntelliJ IDEA",
82 Icon: IntelliJIdeaIcon,
83 value: "idea",
84 },
85 {
86 label: "Aqua",
87 Icon: AquaIcon,
88 value: "aqua",
89 },
90 {
91 label: "CLion",
92 Icon: CLionIcon,
93 value: "clion",
94 },
95 {
96 label: "DataGrip",
97 Icon: DataGripIcon,
98 value: "datagrip",
99 },
100 {
101 label: "DataSpell",
102 Icon: DataSpellIcon,
103 value: "dataspell",
104 },
105 {
106 label: "GoLand",
107 Icon: GoLandIcon,
108 value: "goland",
109 },
110 {
111 label: "PhpStorm",
112 Icon: PhpStormIcon,
113 value: "phpstorm",
114 },
115 {
116 label: "PyCharm",
117 Icon: PyCharmIcon,
118 value: "pycharm",
119 },
120 {
121 label: "Rider",
122 Icon: RiderIcon,
123 value: "rider",
124 },
125 {
126 label: "RubyMine",
127 Icon: RubyMineIcon,
128 value: "rubymine",
129 },
130 {
131 label: "RustRover",
132 Icon: RustRoverIcon,
133 value: "rustrover",
134 },
135 {
136 label: "WebStorm",
137 Icon: WebStormIcon,
138 value: "webstorm",
139 },
140 {
141 label: isMacPlatform(platform)
142 ? "Finder"
143 : isWindowsPlatform(platform)
144 ? "Explorer"
145 : "Files",
146 Icon: FolderClosedIcon,
147 value: "file-manager",
148 },
149 ];
150 const availableEditorSet = new Set(availableEditors);
151 return baseOptions.filter((option) => availableEditorSet.has(option.value));
152};
153
154export const OpenInPicker = memo(function OpenInPicker({
155 environmentId,
156 keybindings,
157 availableEditors,
158 openInCwd,
159 compact = false,
160 enableShortcut = true,
161}: {
162 environmentId: EnvironmentId;
163 keybindings: ResolvedKeybindingsConfig;
164 availableEditors: ReadonlyArray<EditorId>;
165 openInCwd: string | null;
166 compact?: boolean;
167 enableShortcut?: boolean;
168}) {
169 const openInEditorMutation = useAtomCommand(shellEnvironment.openInEditor, "open in editor");
170 const [preferredEditor, setPreferredEditor] = usePreferredEditor(availableEditors);
171 const options = useMemo(
172 () => resolveOptions(navigator.platform, availableEditors),
173 [availableEditors],
174 );
175 const primaryOption = options.find(({ value }) => value === preferredEditor) ?? null;
176
177 const openInEditor = useCallback(
178 (editorId: EditorId | null) => {
179 if (!openInCwd) return;
180 const editor = editorId ?? preferredEditor;
181 if (!editor) return;
182 const result = openInEditorMutation({
183 environmentId,
184 input: {
185 cwd: openInCwd,
186 editor,
187 },
188 });
189 setPreferredEditor(editor);
190 return result;
191 },
192 [environmentId, openInCwd, openInEditorMutation, preferredEditor, setPreferredEditor],
193 );
194
195 const openFavoriteEditorShortcutLabel = useMemo(
196 () => shortcutLabelForCommand(keybindings, "editor.openFavorite"),
197 [keybindings],
198 );
199
200 useEffect(() => {
201 if (!enableShortcut) return;
202 const handler = (e: globalThis.KeyboardEvent) => {
203 if (!isOpenFavoriteEditorShortcut(e, keybindings)) return;
204 if (!openInCwd) return;
205 if (!preferredEditor) return;
206
207 e.preventDefault();
208 void openInEditorMutation({
209 environmentId,
210 input: {
211 cwd: openInCwd,
212 editor: preferredEditor,
213 },
214 });
215 };
216 window.addEventListener("keydown", handler);
217 return () => window.removeEventListener("keydown", handler);
218 }, [
219 enableShortcut,
220 environmentId,
221 keybindings,
222 openInCwd,
223 openInEditorMutation,
224 preferredEditor,
225 ]);
226
227 return (
228 <Group aria-label="Open in editor">
229 <Button
230 aria-label={compact ? "Open file in preferred editor" : undefined}
231 size="xs"
232 variant="outline"
233 disabled={!preferredEditor || !openInCwd}
234 onClick={() => openInEditor(preferredEditor)}
235 >
236 {primaryOption?.Icon && <primaryOption.Icon aria-hidden="true" className="size-3.5" />}
237 <span
238 className={
239 compact
240 ? "sr-only"
241 : "sr-only @3xl/header-actions:not-sr-only @3xl/header-actions:ml-0.5"
242 }
243 >
244 Open
245 </span>
246 </Button>
247 <GroupSeparator {...(!compact ? { className: "hidden @3xl/header-actions:block" } : {})} />
248 <Menu>
249 <MenuTrigger
250 render={
251 <Button
252 aria-label={compact ? "Choose editor" : "Copy options"}
253 size="icon-xs"
254 variant="outline"
255 />
256 }
257 >
258 <ChevronDownIcon aria-hidden="true" className="size-4" />
259 </MenuTrigger>
260 <MenuPopup align="end">
261 {options.length === 0 && <MenuItem disabled>No installed editors found</MenuItem>}
262 {options.map(({ label, Icon, value }) => (
263 <MenuItem key={value} onClick={() => openInEditor(value)}>
264 <Icon aria-hidden="true" className="text-muted-foreground" />
265 {label}
266 {value === preferredEditor && openFavoriteEditorShortcutLabel && (
267 <MenuShortcut>{openFavoriteEditorShortcutLabel}</MenuShortcut>
268 )}
269 </MenuItem>
270 ))}
271 </MenuPopup>
272 </Menu>
273 </Group>
274 );
275});