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