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

OpenInPicker.tsx

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