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