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