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

OpenInPicker.tsx

353 lines9.4 KB

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