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