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