Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

OpenInPicker.tsx

434 lines12.3 KB

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