Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

OpenInPicker.tsx

143 lines4.6 KB

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