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

OpenInPicker.tsx

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