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

OpenInPicker.tsx

232 lines6.0 KB

1import { EditorId, type ResolvedKeybindingsConfig } from "@t3tools/contracts";
2import { memo, useCallback, useEffect, useMemo } from "react";
3import { isOpenFavoriteEditorShortcut, shortcutLabelForCommand } from "../../keybindings";
4import { usePreferredEditor } from "../../editorPreferences";
5import { ChevronDownIcon, FolderClosedIcon } from "lucide-react";
6import { Button } from "../ui/button";
7import { Group, GroupSeparator } from "../ui/group";
8import { Menu, MenuItem, MenuPopup, MenuShortcut, MenuTrigger } from "../ui/menu";
9import {
10 AntigravityIcon,
11 CursorIcon,
12 Icon,
13 KiroIcon,
14 TraeIcon,
15 VisualStudioCode,
16 VisualStudioCodeInsiders,
17 VSCodium,
18 Zed,
19} from "../Icons";
20import {
21 AquaIcon,
22 CLionIcon,
23 DataGripIcon,
24 DataSpellIcon,
25 GoLandIcon,
26 IntelliJIdeaIcon,
27 PhpStormIcon,
28 PyCharmIcon,
29 RiderIcon,
30 RubyMineIcon,
31 RustRoverIcon,
32 WebStormIcon,
33} from "../JetBrainsIcons";
34import { isMacPlatform, isWindowsPlatform } from "~/lib/utils";
35import { readLocalApi } from "~/localApi";
36​
37const resolveOptions = (platform: string, availableEditors: ReadonlyArray<EditorId>) => {
38 const baseOptions: ReadonlyArray<{ label: string; Icon: Icon; value: EditorId }> = [
39 {
40 label: "Cursor",
41 Icon: CursorIcon,
42 value: "cursor",
43 },
44 {
45 label: "Trae",
46 Icon: TraeIcon,
47 value: "trae",
48 },
49 {
50 label: "Kiro",
51 Icon: KiroIcon,
52 value: "kiro",
53 },
54 {
55 label: "VS Code",
56 Icon: VisualStudioCode,
57 value: "vscode",
58 },
59 {
60 label: "VS Code Insiders",
61 Icon: VisualStudioCodeInsiders,
62 value: "vscode-insiders",
63 },
64 {
65 label: "VSCodium",
66 Icon: VSCodium,
67 value: "vscodium",
68 },
69 {
70 label: "Zed",
71 Icon: Zed,
72 value: "zed",
73 },
74 {
75 label: "Antigravity",
76 Icon: AntigravityIcon,
77 value: "antigravity",
78 },
79 {
80 label: "IntelliJ IDEA",
81 Icon: IntelliJIdeaIcon,
82 value: "idea",
83 },
84 {
85 label: "Aqua",
86 Icon: AquaIcon,
87 value: "aqua",
88 },
89 {
90 label: "CLion",
91 Icon: CLionIcon,
92 value: "clion",
93 },
94 {
95 label: "DataGrip",
96 Icon: DataGripIcon,
97 value: "datagrip",
98 },
99 {
100 label: "DataSpell",
101 Icon: DataSpellIcon,
102 value: "dataspell",
103 },
104 {
105 label: "GoLand",
106 Icon: GoLandIcon,
107 value: "goland",
108 },
109 {
110 label: "PhpStorm",
111 Icon: PhpStormIcon,
112 value: "phpstorm",
113 },
114 {
115 label: "PyCharm",
116 Icon: PyCharmIcon,
117 value: "pycharm",
118 },
119 {
120 label: "Rider",
121 Icon: RiderIcon,
122 value: "rider",
123 },
124 {
125 label: "RubyMine",
126 Icon: RubyMineIcon,
127 value: "rubymine",
128 },
129 {
130 label: "RustRover",
131 Icon: RustRoverIcon,
132 value: "rustrover",
133 },
134 {
135 label: "WebStorm",
136 Icon: WebStormIcon,
137 value: "webstorm",
138 },
139 {
140 label: isMacPlatform(platform)
141 ? "Finder"
142 : isWindowsPlatform(platform)
143 ? "Explorer"
144 : "Files",
145 Icon: FolderClosedIcon,
146 value: "file-manager",
147 },
148 ];
149 return baseOptions.filter((option) => availableEditors.includes(option.value));
150};
151​
152export const OpenInPicker = memo(function OpenInPicker({
153 keybindings,
154 availableEditors,
155 openInCwd,
156}: {
157 keybindings: ResolvedKeybindingsConfig;
158 availableEditors: ReadonlyArray<EditorId>;
159 openInCwd: string | null;
160}) {
161 const [preferredEditor, setPreferredEditor] = usePreferredEditor(availableEditors);
162 const options = useMemo(
163 () => resolveOptions(navigator.platform, availableEditors),
164 [availableEditors],
165 );
166 const primaryOption = options.find(({ value }) => value === preferredEditor) ?? null;
167​
168 const openInEditor = useCallback(
169 (editorId: EditorId | null) => {
170 const api = readLocalApi();
171 if (!api || !openInCwd) return;
172 const editor = editorId ?? preferredEditor;
173 if (!editor) return;
174 void api.shell.openInEditor(openInCwd, editor);
175 setPreferredEditor(editor);
176 },
177 [preferredEditor, openInCwd, setPreferredEditor],
178 );
179​
180 const openFavoriteEditorShortcutLabel = useMemo(
181 () => shortcutLabelForCommand(keybindings, "editor.openFavorite"),
182 [keybindings],
183 );
184​
185 useEffect(() => {
186 const handler = (e: globalThis.KeyboardEvent) => {
187 const api = readLocalApi();
188 if (!isOpenFavoriteEditorShortcut(e, keybindings)) return;
189 if (!api || !openInCwd) return;
190 if (!preferredEditor) return;
191​
192 e.preventDefault();
193 void api.shell.openInEditor(openInCwd, preferredEditor);
194 };
195 window.addEventListener("keydown", handler);
196 return () => window.removeEventListener("keydown", handler);
197 }, [preferredEditor, keybindings, openInCwd]);
198​
199 return (
200 <Group aria-label="Subscription actions">
201 <Button
202 size="xs"
203 variant="outline"
204 disabled={!preferredEditor || !openInCwd}
205 onClick={() => openInEditor(preferredEditor)}
206 >
207 {primaryOption?.Icon && <primaryOption.Icon aria-hidden="true" className="size-3.5" />}
208 <span className="sr-only @3xl/header-actions:not-sr-only @3xl/header-actions:ml-0.5">
209 Open
210 </span>
211 </Button>
212 <GroupSeparator className="hidden @3xl/header-actions:block" />
213 <Menu>
214 <MenuTrigger render={<Button aria-label="Copy options" size="icon-xs" variant="outline" />}>
215 <ChevronDownIcon aria-hidden="true" className="size-4" />
216 </MenuTrigger>
217 <MenuPopup align="end">
218 {options.length === 0 && <MenuItem disabled>No installed editors found</MenuItem>}
219 {options.map(({ label, Icon, value }) => (
220 <MenuItem key={value} onClick={() => openInEditor(value)}>
221 <Icon aria-hidden="true" className="text-muted-foreground" />
222 {label}
223 {value === preferredEditor && openFavoriteEditorShortcutLabel && (
224 <MenuShortcut>{openFavoriteEditorShortcutLabel}</MenuShortcut>
225 )}
226 </MenuItem>
227 ))}
228 </MenuPopup>
229 </Menu>
230 </Group>
231 );
232});