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

OpenInPicker.tsx

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