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