Open History to see the last commit
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 TraeIcon,
14 IntelliJIdeaIcon,
15 VisualStudioCode,
16 VisualStudioCodeInsiders,
17 VSCodium,
18 Zed,
19} from "../Icons";
20import { isMacPlatform, isWindowsPlatform } from "~/lib/utils";
21import { readLocalApi } from "~/localApi";
22
23const resolveOptions = (platform: string, availableEditors: ReadonlyArray<EditorId>) => {
24 const baseOptions: ReadonlyArray<{ label: string; Icon: Icon; value: EditorId }> = [
25 {
26 label: "Cursor",
27 Icon: CursorIcon,
28 value: "cursor",
29 },
30 {
31 label: "Trae",
32 Icon: TraeIcon,
33 value: "trae",
34 },
35 {
36 label: "VS Code",
37 Icon: VisualStudioCode,
38 value: "vscode",
39 },
40 {
41 label: "VS Code Insiders",
42 Icon: VisualStudioCodeInsiders,
43 value: "vscode-insiders",
44 },
45 {
46 label: "VSCodium",
47 Icon: VSCodium,
48 value: "vscodium",
49 },
50 {
51 label: "Zed",
52 Icon: Zed,
53 value: "zed",
54 },
55 {
56 label: "Antigravity",
57 Icon: AntigravityIcon,
58 value: "antigravity",
59 },
60 {
61 label: "IntelliJ IDEA",
62 Icon: IntelliJIdeaIcon,
63 value: "idea",
64 },
65 {
66 label: isMacPlatform(platform)
67 ? "Finder"
68 : isWindowsPlatform(platform)
69 ? "Explorer"
70 : "Files",
71 Icon: FolderClosedIcon,
72 value: "file-manager",
73 },
74 ];
75 return baseOptions.filter((option) => availableEditors.includes(option.value));
76};
77
78export const OpenInPicker = memo(function OpenInPicker({
79 keybindings,
80 availableEditors,
81 openInCwd,
82}: {
83 keybindings: ResolvedKeybindingsConfig;
84 availableEditors: ReadonlyArray<EditorId>;
85 openInCwd: string | null;
86}) {
87 const [preferredEditor, setPreferredEditor] = usePreferredEditor(availableEditors);
88 const options = useMemo(
89 () => resolveOptions(navigator.platform, availableEditors),
90 [availableEditors],
91 );
92 const primaryOption = options.find(({ value }) => value === preferredEditor) ?? null;
93
94 const openInEditor = useCallback(
95 (editorId: EditorId | null) => {
96 const api = readLocalApi();
97 if (!api || !openInCwd) return;
98 const editor = editorId ?? preferredEditor;
99 if (!editor) return;
100 void api.shell.openInEditor(openInCwd, editor);
101 setPreferredEditor(editor);
102 },
103 [preferredEditor, openInCwd, setPreferredEditor],
104 );
105
106 const openFavoriteEditorShortcutLabel = useMemo(
107 () => shortcutLabelForCommand(keybindings, "editor.openFavorite"),
108 [keybindings],
109 );
110
111 useEffect(() => {
112 const handler = (e: globalThis.KeyboardEvent) => {
113 const api = readLocalApi();
114 if (!isOpenFavoriteEditorShortcut(e, keybindings)) return;
115 if (!api || !openInCwd) return;
116 if (!preferredEditor) return;
117
118 e.preventDefault();
119 void api.shell.openInEditor(openInCwd, preferredEditor);
120 };
121 window.addEventListener("keydown", handler);
122 return () => window.removeEventListener("keydown", handler);
123 }, [preferredEditor, keybindings, openInCwd]);
124
125 return (
126 <Group aria-label="Subscription actions">
127 <Button
128 size="xs"
129 variant="outline"
130 disabled={!preferredEditor || !openInCwd}
131 onClick={() => openInEditor(preferredEditor)}
132 >
133 {primaryOption?.Icon && <primaryOption.Icon aria-hidden="true" className="size-3.5" />}
134 <span className="sr-only @3xl/header-actions:not-sr-only @3xl/header-actions:ml-0.5">
135 Open
136 </span>
137 </Button>
138 <GroupSeparator className="hidden @3xl/header-actions:block" />
139 <Menu>
140 <MenuTrigger render={<Button aria-label="Copy options" size="icon-xs" variant="outline" />}>
141 <ChevronDownIcon aria-hidden="true" className="size-4" />
142 </MenuTrigger>
143 <MenuPopup align="end">
144 {options.length === 0 && <MenuItem disabled>No installed editors found</MenuItem>}
145 {options.map(({ label, Icon, value }) => (
146 <MenuItem key={value} onClick={() => openInEditor(value)}>
147 <Icon aria-hidden="true" className="text-muted-foreground" />
148 {label}
149 {value === preferredEditor && openFavoriteEditorShortcutLabel && (
150 <MenuShortcut>{openFavoriteEditorShortcutLabel}</MenuShortcut>
151 )}
152 </MenuItem>
153 ))}
154 </MenuPopup>
155 </Menu>
156 </Group>
157 );
158});