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