Open History to see the last commit
1import {
2 buildRemoteOpenUrl,
3 EditorId,
4 type EnvironmentId,
5 type ResolvedKeybindingsConfig,
6} from "@t3tools/contracts";
7import { memo, useCallback, useEffect, useMemo } from "react";
8import { isOpenFavoriteEditorShortcut, shortcutLabelForCommand } from "../../keybindings";
9import { usePreferredEditor } from "../../editorPreferences";
10import { editorLabelForPlatform } from "../../editorLabels";
11import {
12 openRemoteEditorUrl,
13 useRemoteCapableEditors,
14 useRemoteOpenHint,
15 useRemoteOpenState,
16} from "../../remoteOpen";
17import { useEnvironment } from "../../state/environments";
18import { ChevronDownIcon, FolderClosedIcon, SquareArrowOutUpRightIcon } from "lucide-react";
19import { Button } from "../ui/button";
20import { Group, GroupSeparator } from "../ui/group";
21import {
22 Menu,
23 MenuItem,
24 MenuItemLabel,
25 MenuPopup,
26 MenuShortcut,
27 MenuSub,
28 MenuSubTrigger,
29 MenuSubPopup,
30 MenuTrigger,
31} from "../ui/menu";
32import {
33 AntigravityIcon,
34 CursorIcon,
35 FileExplorerIcon,
36 FinderIcon,
37 Icon,
38 KiroIcon,
39 TraeIcon,
40 VisualStudioCode,
41 VisualStudioCodeInsiders,
42 VSCodium,
43 Zed,
44} from "../Icons";
45import {
46 AquaIcon,
47 CLionIcon,
48 DataGripIcon,
49 DataSpellIcon,
50 GoLandIcon,
51 IntelliJIdeaIcon,
52 PhpStormIcon,
53 PyCharmIcon,
54 RiderIcon,
55 RubyMineIcon,
56 RustRoverIcon,
57 WebStormIcon,
58} from "../JetBrainsIcons";
59import { cn, isMacPlatform, isWindowsPlatform } from "~/lib/utils";
60import { shellEnvironment } from "~/state/shell";
61import { useAtomCommand } from "~/state/use-atom-command";
62
63type OpenInOption = {
64 label: string;
65 Icon: Icon;
66 value: EditorId;
67 kind: "brand" | "generic";
68};
69
70export const resolveOpenInOptions = (
71 platform: string,
72 availableEditors: ReadonlyArray<EditorId>,
73) => {
74 const baseOptions: ReadonlyArray<Omit<OpenInOption, "label">> = [
75 {
76 Icon: CursorIcon,
77 value: "cursor",
78 kind: "brand",
79 },
80 {
81 Icon: TraeIcon,
82 value: "trae",
83 kind: "brand",
84 },
85 {
86 Icon: KiroIcon,
87 value: "kiro",
88 kind: "brand",
89 },
90 {
91 Icon: VisualStudioCode,
92 value: "vscode",
93 kind: "brand",
94 },
95 {
96 Icon: VisualStudioCodeInsiders,
97 value: "vscode-insiders",
98 kind: "brand",
99 },
100 {
101 Icon: VSCodium,
102 value: "vscodium",
103 kind: "brand",
104 },
105 {
106 Icon: Zed,
107 value: "zed",
108 kind: "brand",
109 },
110 {
111 Icon: AntigravityIcon,
112 value: "antigravity",
113 kind: "brand",
114 },
115 {
116 Icon: IntelliJIdeaIcon,
117 value: "idea",
118 kind: "brand",
119 },
120 {
121 Icon: AquaIcon,
122 value: "aqua",
123 kind: "brand",
124 },
125 {
126 Icon: CLionIcon,
127 value: "clion",
128 kind: "brand",
129 },
130 {
131 Icon: DataGripIcon,
132 value: "datagrip",
133 kind: "brand",
134 },
135 {
136 Icon: DataSpellIcon,
137 value: "dataspell",
138 kind: "brand",
139 },
140 {
141 Icon: GoLandIcon,
142 value: "goland",
143 kind: "brand",
144 },
145 {
146 Icon: PhpStormIcon,
147 value: "phpstorm",
148 kind: "brand",
149 },
150 {
151 Icon: PyCharmIcon,
152 value: "pycharm",
153 kind: "brand",
154 },
155 {
156 Icon: RiderIcon,
157 value: "rider",
158 kind: "brand",
159 },
160 {
161 Icon: RubyMineIcon,
162 value: "rubymine",
163 kind: "brand",
164 },
165 {
166 Icon: RustRoverIcon,
167 value: "rustrover",
168 kind: "brand",
169 },
170 {
171 Icon: WebStormIcon,
172 value: "webstorm",
173 kind: "brand",
174 },
175 {
176 Icon: isMacPlatform(platform)
177 ? FinderIcon
178 : isWindowsPlatform(platform)
179 ? FileExplorerIcon
180 : FolderClosedIcon,
181 value: "file-manager",
182 kind: isMacPlatform(platform) || isWindowsPlatform(platform) ? "brand" : "generic",
183 },
184 ];
185 const availableEditorSet = new Set(availableEditors);
186 return baseOptions
187 .filter((option) => availableEditorSet.has(option.value))
188 .map((option) => ({ ...option, label: editorLabelForPlatform(option.value, platform) }));
189};
190
191function getOpenInIconClass(kind: OpenInOption["kind"]) {
192 return cn(kind === "brand" ? "text-foreground opacity-100" : "text-muted-foreground");
193}
194
195export const OpenInPicker = memo(function OpenInPicker({
196 environmentId,
197 keybindings,
198 availableEditors,
199 openInCwd,
200 presentation = "toolbar",
201 compact = false,
202 enableShortcut = true,
203}: {
204 environmentId: EnvironmentId;
205 keybindings: ResolvedKeybindingsConfig;
206 availableEditors: ReadonlyArray<EditorId>;
207 openInCwd: string | null;
208 presentation?: "toolbar" | "menu";
209 compact?: boolean;
210 enableShortcut?: boolean;
211}) {
212 const openInEditorMutation = useAtomCommand(shellEnvironment.openInEditor, "open in editor");
213 const remote = useRemoteOpenState(environmentId);
214 const remoteCapableEditors = useRemoteCapableEditors();
215 const [remoteHintSeen, markRemoteHintSeen] = useRemoteOpenHint();
216 const environmentLabel = useEnvironment(environmentId)?.label ?? "this machine";
217 // Remote mode ignores the server's PATH probe: what matters is what runs on
218 // the viewing machine, which only the desktop app can probe.
219 const effectiveEditors = remote.mode === "local-exec" ? availableEditors : remoteCapableEditors;
220 const [preferredEditor, setPreferredEditor] = usePreferredEditor(effectiveEditors);
221 const options = useMemo(
222 () => resolveOpenInOptions(navigator.platform, effectiveEditors),
223 [effectiveEditors],
224 );
225 const primaryOption = options.find(({ value }) => value === preferredEditor) ?? null;
226
227 const openInEditor = useCallback(
228 (editorId: EditorId | null) => {
229 if (!openInCwd) return;
230 const editor = editorId ?? preferredEditor;
231 if (!editor) return;
232 if (remote.mode === "remote-unavailable") return;
233 if (remote.mode === "remote-links") {
234 const url = buildRemoteOpenUrl({
235 editor,
236 host: remote.host.host,
237 absolutePath: openInCwd,
238 });
239 if (url === undefined) return;
240 // Only record hint-seen/preferred when the shell actually accepted
241 // the URL (an older desktop build can refuse the editor scheme).
242 void openRemoteEditorUrl(url).then((opened) => {
243 if (!opened) return;
244 markRemoteHintSeen();
245 setPreferredEditor(editor);
246 });
247 return;
248 }
249 const result = openInEditorMutation({
250 environmentId,
251 input: {
252 cwd: openInCwd,
253 editor,
254 },
255 });
256 setPreferredEditor(editor);
257 return result;
258 },
259 [
260 environmentId,
261 markRemoteHintSeen,
262 openInCwd,
263 openInEditorMutation,
264 preferredEditor,
265 remote,
266 setPreferredEditor,
267 ],
268 );
269
270 const openFavoriteEditorShortcutLabel = useMemo(
271 () => shortcutLabelForCommand(keybindings, "editor.openFavorite"),
272 [keybindings],
273 );
274
275 useEffect(() => {
276 if (!enableShortcut) return;
277 const handler = (e: globalThis.KeyboardEvent) => {
278 if (!isOpenFavoriteEditorShortcut(e, keybindings)) return;
279 if (!openInCwd) return;
280 if (!preferredEditor) return;
281
282 e.preventDefault();
283 void openInEditor(preferredEditor);
284 };
285 window.addEventListener("keydown", handler);
286 return () => window.removeEventListener("keydown", handler);
287 }, [enableShortcut, keybindings, openInCwd, openInEditor, preferredEditor]);
288
289 const editorItems = (
290 <>
291 {remote.mode === "remote-unavailable" ? (
292 <MenuItem density={presentation === "menu" ? "touch" : "default"} disabled>
293 No SSH route to {environmentLabel}
294 </MenuItem>
295 ) : (
296 <>
297 {options.length === 0 && (
298 <MenuItem density={presentation === "menu" ? "touch" : "default"} disabled>
299 No installed editors found
300 </MenuItem>
301 )}
302 {options.map(({ label, Icon, value, kind }) => (
303 <MenuItem
304 density={presentation === "menu" ? "touch" : "default"}
305 key={value}
306 onClick={() => openInEditor(value)}
307 >
308 <Icon aria-hidden="true" className={getOpenInIconClass(kind)} />
309 <MenuItemLabel>{label}</MenuItemLabel>
310 {value === preferredEditor && openFavoriteEditorShortcutLabel && (
311 <MenuShortcut>{openFavoriteEditorShortcutLabel}</MenuShortcut>
312 )}
313 </MenuItem>
314 ))}
315 {remote.mode === "remote-links" && !remoteHintSeen && (
316 <MenuItem density={presentation === "menu" ? "touch" : "default"} disabled>
317 Opens over SSH. Needs your key on {environmentLabel}
318 </MenuItem>
319 )}
320 </>
321 )}
322 </>
323 );
324 if (presentation === "menu") {
325 return (
326 <>
327 {primaryOption && (
328 <MenuItem
329 density={presentation === "menu" ? "touch" : "default"}
330
331 disabled={!openInCwd || remote.mode === "remote-unavailable"}
332 onClick={() => openInEditor(preferredEditor)}
333 >
334 <primaryOption.Icon className={cn("size-4", getOpenInIconClass(primaryOption.kind))} />
335 <MenuItemLabel className="truncate">Open in {primaryOption.label}</MenuItemLabel>
336 {openFavoriteEditorShortcutLabel && (
337 <MenuShortcut>{openFavoriteEditorShortcutLabel}</MenuShortcut>
338 )}
339 </MenuItem>
340 )}
341 <MenuSub>
342 <MenuSubTrigger density="touch">
343 <SquareArrowOutUpRightIcon className="size-4" />
344 <MenuItemLabel>Open in…</MenuItemLabel>
345 </MenuSubTrigger>
346 <MenuSubPopup className="min-w-32 max-w-[calc(100vw-2rem)]">{editorItems}</MenuSubPopup>
347 </MenuSub>
348 </>
349 );
350 }
351
352 return (
353 <Group aria-label="Open in editor">
354 <Button
355 aria-label={compact ? "Open file in preferred editor" : undefined}
356 className="ps-[8.5px]"
357 size="xs"
358 variant="outline"
359 disabled={!preferredEditor || !openInCwd || remote.mode === "remote-unavailable"}
360 onClick={() => openInEditor(preferredEditor)}
361 >
362 {primaryOption?.Icon && (
363 <primaryOption.Icon
364 aria-hidden="true"
365 className={cn("size-3.5", getOpenInIconClass(primaryOption.kind))}
366 />
367 )}
368 <span
369 className={
370 compact
371 ? "sr-only"
372 : "sr-only @3xl/header-actions:not-sr-only @3xl/header-actions:ml-0.5"
373 }
374 >
375 Open
376 </span>
377 </Button>
378 <GroupSeparator {...(!compact ? { className: "hidden @3xl/header-actions:block" } : {})} />
379 <Menu>
380 <MenuTrigger
381 render={<Button aria-label="Choose editor" size="icon-xs" variant="outline" />}
382 >
383 <ChevronDownIcon aria-hidden="true" className="size-4" />
384 </MenuTrigger>
385 <MenuPopup align="end">{editorItems}</MenuPopup>
386 </Menu>
387 </Group>
388 );
389});