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