Open History to see the last commit
1import { EditorId, type EnvironmentId, type ResolvedKeybindingsConfig } from "@t3tools/contracts";
2import { memo, useCallback, useMemo, useRef } from "react";
3import { 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 { cn, isMacPlatform, isWindowsPlatform } from "~/lib/utils";
35import { shellEnvironment } from "~/state/shell";
36import { useAtomCommand } from "~/state/use-atom-command";
37import {
38 THREAD_DETAILS_PANEL_CHEVRON_CLASS,
39 THREAD_DETAILS_PANEL_ICON_CLASS,
40 THREAD_DETAILS_PANEL_ROW_POPUP_CLASS,
41 THREAD_DETAILS_PANEL_SPLIT_GROUP_CLASS,
42 THREAD_DETAILS_PANEL_SPLIT_PRIMARY_CLASS,
43 THREAD_DETAILS_PANEL_SPLIT_SECONDARY_CLASS,
44 THREAD_DETAILS_PANEL_SPLIT_SEPARATOR_CLASS,
45} from "./threadDetailsPanelStyles";
46
47type OpenInOption = {
48 label: string;
49 Icon: Icon;
50 value: EditorId;
51 kind: "brand" | "generic";
52};
53
54const resolveOptions = (platform: string, availableEditors: ReadonlyArray<EditorId>) => {
55 const baseOptions: ReadonlyArray<OpenInOption> = [
56 {
57 label: "Cursor",
58 Icon: CursorIcon,
59 value: "cursor",
60 kind: "brand",
61 },
62 {
63 label: "Trae",
64 Icon: TraeIcon,
65 value: "trae",
66 kind: "brand",
67 },
68 {
69 label: "Kiro",
70 Icon: KiroIcon,
71 value: "kiro",
72 kind: "brand",
73 },
74 {
75 label: "VS Code",
76 Icon: VisualStudioCode,
77 value: "vscode",
78 kind: "brand",
79 },
80 {
81 label: "VS Code Insiders",
82 Icon: VisualStudioCodeInsiders,
83 value: "vscode-insiders",
84 kind: "brand",
85 },
86 {
87 label: "VSCodium",
88 Icon: VSCodium,
89 value: "vscodium",
90 kind: "brand",
91 },
92 {
93 label: "Zed",
94 Icon: Zed,
95 value: "zed",
96 kind: "brand",
97 },
98 {
99 label: "Antigravity",
100 Icon: AntigravityIcon,
101 value: "antigravity",
102 kind: "brand",
103 },
104 {
105 label: "IntelliJ IDEA",
106 Icon: IntelliJIdeaIcon,
107 value: "idea",
108 kind: "brand",
109 },
110 {
111 label: "Aqua",
112 Icon: AquaIcon,
113 value: "aqua",
114 kind: "brand",
115 },
116 {
117 label: "CLion",
118 Icon: CLionIcon,
119 value: "clion",
120 kind: "brand",
121 },
122 {
123 label: "DataGrip",
124 Icon: DataGripIcon,
125 value: "datagrip",
126 kind: "brand",
127 },
128 {
129 label: "DataSpell",
130 Icon: DataSpellIcon,
131 value: "dataspell",
132 kind: "brand",
133 },
134 {
135 label: "GoLand",
136 Icon: GoLandIcon,
137 value: "goland",
138 kind: "brand",
139 },
140 {
141 label: "PhpStorm",
142 Icon: PhpStormIcon,
143 value: "phpstorm",
144 kind: "brand",
145 },
146 {
147 label: "PyCharm",
148 Icon: PyCharmIcon,
149 value: "pycharm",
150 kind: "brand",
151 },
152 {
153 label: "Rider",
154 Icon: RiderIcon,
155 value: "rider",
156 kind: "brand",
157 },
158 {
159 label: "RubyMine",
160 Icon: RubyMineIcon,
161 value: "rubymine",
162 kind: "brand",
163 },
164 {
165 label: "RustRover",
166 Icon: RustRoverIcon,
167 value: "rustrover",
168 kind: "brand",
169 },
170 {
171 label: "WebStorm",
172 Icon: WebStormIcon,
173 value: "webstorm",
174 kind: "brand",
175 },
176 {
177 label: isMacPlatform(platform)
178 ? "Finder"
179 : isWindowsPlatform(platform)
180 ? "Explorer"
181 : "Files",
182 Icon: FolderClosedIcon,
183 value: "file-manager",
184 kind: "generic",
185 },
186 ];
187 const availableEditorSet = new Set(availableEditors);
188 return baseOptions.filter((option) => availableEditorSet.has(option.value));
189};
190
191function getOpenInIconClass(kind: OpenInOption["kind"]) {
192 return cn(kind === "brand" ? "text-foreground opacity-100" : "text-muted-foreground");
193}
194
195export const OpenInPicker = memo(function OpenInPicker({
196 environmentId,
197 keybindings,
198 availableEditors,
199 openInCwd,
200 compact = false,
201 displayMode = "toolbar",
202}: {
203 environmentId: EnvironmentId;
204 keybindings: ResolvedKeybindingsConfig;
205 availableEditors: ReadonlyArray<EditorId>;
206 openInCwd: string | null;
207 compact?: boolean;
208 displayMode?: "toolbar" | "panel";
209}) {
210 const isPanel = displayMode === "panel";
211 const ActionGroup = isPanel ? "div" : Group;
212 const panelAnchorRef = useRef<HTMLDivElement | null>(null);
213 const openInEditorMutation = useAtomCommand(shellEnvironment.openInEditor, "open in editor");
214 const [preferredEditor, setPreferredEditor] = usePreferredEditor(availableEditors);
215 const options = useMemo(
216 () => resolveOptions(navigator.platform, availableEditors),
217 [availableEditors],
218 );
219 const primaryOption = options.find(({ value }) => value === preferredEditor) ?? null;
220
221 const openInEditor = useCallback(
222 (editorId: EditorId | null) => {
223 if (!openInCwd) return;
224 const editor = editorId ?? preferredEditor;
225 if (!editor) return;
226 const result = openInEditorMutation({
227 environmentId,
228 input: {
229 cwd: openInCwd,
230 editor,
231 },
232 });
233 setPreferredEditor(editor);
234 return result;
235 },
236 [environmentId, openInCwd, openInEditorMutation, preferredEditor, setPreferredEditor],
237 );
238
239 const openFavoriteEditorShortcutLabel = useMemo(
240 () => shortcutLabelForCommand(keybindings, "editor.openFavorite"),
241 [keybindings],
242 );
243 const primaryLabel = isPanel ? `Open in ${primaryOption?.label ?? "editor"}` : "Open";
244
245 return (
246 <ActionGroup
247 aria-label="Open in editor"
248 role="group"
249 {...(isPanel
250 ? { className: THREAD_DETAILS_PANEL_SPLIT_GROUP_CLASS, ref: panelAnchorRef }
251 : {})}
252 >
253 <Button
254 aria-label={compact ? "Open file in preferred editor" : primaryLabel}
255 size={isPanel ? "sm" : "xs"}
256 variant={isPanel ? "ghost" : "outline"}
257 className={isPanel ? THREAD_DETAILS_PANEL_SPLIT_PRIMARY_CLASS : undefined}
258 disabled={!preferredEditor || !openInCwd}
259 onClick={() => openInEditor(preferredEditor)}
260 >
261 {primaryOption?.Icon && (
262 <primaryOption.Icon
263 aria-hidden="true"
264 className={cn(
265 isPanel ? THREAD_DETAILS_PANEL_ICON_CLASS : "size-3.5",
266 getOpenInIconClass(primaryOption.kind),
267 )}
268 />
269 )}
270 <span
271 className={cn(
272 compact
273 ? "sr-only"
274 : "sr-only @3xl/header-actions:not-sr-only @3xl/header-actions:ml-0.5",
275 isPanel && "not-sr-only ml-0.5 min-w-0 truncate",
276 )}
277 >
278 {primaryLabel}
279 </span>
280 </Button>
281 {isPanel ? (
282 <span aria-hidden="true" className={THREAD_DETAILS_PANEL_SPLIT_SEPARATOR_CLASS} />
283 ) : (
284 <GroupSeparator {...(!compact ? { className: "hidden @3xl/header-actions:block" } : {})} />
285 )}
286 <Menu>
287 <MenuTrigger
288 render={
289 <Button
290 aria-label="Choose editor"
291 size={isPanel ? "sm" : "icon-xs"}
292 variant={isPanel ? "ghost" : "outline"}
293 className={isPanel ? THREAD_DETAILS_PANEL_SPLIT_SECONDARY_CLASS : undefined}
294 />
295 }
296 >
297 <ChevronDownIcon
298 aria-hidden="true"
299 className={isPanel ? THREAD_DETAILS_PANEL_CHEVRON_CLASS : "size-4"}
300 />
301 </MenuTrigger>
302 <MenuPopup
303 align="end"
304 {...(isPanel ? { anchor: panelAnchorRef } : {})}
305 className={isPanel ? THREAD_DETAILS_PANEL_ROW_POPUP_CLASS : undefined}
306 >
307 {options.length === 0 && <MenuItem disabled>No installed editors found</MenuItem>}
308 {options.map(({ label, Icon, value, kind }) => (
309 <MenuItem key={value} onClick={() => openInEditor(value)}>
310 <Icon aria-hidden="true" className={getOpenInIconClass(kind)} />
311 {label}
312 {value === preferredEditor && openFavoriteEditorShortcutLabel && (
313 <MenuShortcut>{openFavoriteEditorShortcutLabel}</MenuShortcut>
314 )}
315 </MenuItem>
316 ))}
317 </MenuPopup>
318 </Menu>
319 </ActionGroup>
320 );
321});