-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathcommand-palette.tsx
More file actions
215 lines (199 loc) · 7.31 KB
/
Copy pathcommand-palette.tsx
File metadata and controls
215 lines (199 loc) · 7.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { useCallback, useEffect, useMemo } from "react";
import { useNavigate } from "@tanstack/react-router";
import { useAtomValue } from "@effect/atom-react";
import * as AsyncResult from "effect/unstable/reactivity/AsyncResult";
import { PlusIcon } from "lucide-react";
import { trackEvent } from "../api/analytics";
import type { Integration } from "@executor-js/sdk/shared";
import { IntegrationFavicon, integrationPresetIconUrl } from "./integration-favicon";
import { integrationsOptimisticAtom } from "../api/atoms";
import { useIntegrationPlugins } from "@executor-js/sdk/client";
import {
CommandDialog,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
CommandSeparator,
CommandShortcut,
} from "./command";
// ---------------------------------------------------------------------------
// CommandPalette — global ⌘K navigator.
//
// Order of entries:
// 1. Connected sources (priority, shown first)
// 2. Add <Plugin> actions for each available source plugin
// 3. Popular integrations (plugin presets)
// ---------------------------------------------------------------------------
export function CommandPalette(props: { open: boolean; onOpenChange: (open: boolean) => void }) {
const { open, onOpenChange } = props;
const integrationPlugins = useIntegrationPlugins();
const navigate = useNavigate();
const integrationsResult = useAtomValue(integrationsOptimisticAtom);
// Toggle with ⌘K / Ctrl+K
useEffect(() => {
const onKeyDown = (e: KeyboardEvent) => {
if (e.key === "k" && (e.metaKey || e.ctrlKey)) {
e.preventDefault();
onOpenChange(!open);
}
};
document.addEventListener("keydown", onKeyDown);
return () => document.removeEventListener("keydown", onKeyDown);
}, [onOpenChange, open]);
const connectedSources = useMemo(
() =>
AsyncResult.match(integrationsResult, {
onInitial: () => [] as Array<{ id: string; name: string; kind: string; url?: string }>,
onFailure: () => [] as Array<{ id: string; name: string; kind: string; url?: string }>,
onSuccess: ({ value }) =>
value.map((integration: Integration) => ({
id: String(integration.slug),
name: integration.name || String(integration.slug),
kind: integration.kind,
url: integration.displayUrl,
})),
}),
[integrationsResult],
);
const presetEntries = useMemo(() => {
const entries: Array<{
pluginKey: string;
pluginLabel: string;
presetId: string;
presetName: string;
presetSummary?: string;
presetUrl?: string;
presetIcon?: string;
}> = [];
for (const plugin of integrationPlugins) {
for (const preset of plugin.presets ?? []) {
entries.push({
pluginKey: plugin.key,
pluginLabel: plugin.label,
presetId: preset.id,
presetName: preset.name,
presetSummary: preset.summary,
presetUrl: preset.url,
presetIcon: preset.icon,
});
}
}
return entries;
}, [integrationPlugins]);
const close = useCallback(() => onOpenChange(false), [onOpenChange]);
const goToIntegration = useCallback(
(id: string) => {
close();
trackEvent("command_palette_navigated", { kind: "integration", plugin_key: id });
void navigate({ to: "/{-$orgSlug}/integrations/$namespace", params: { namespace: id } });
},
[close, navigate],
);
const goToAdd = useCallback(
(pluginKey: string) => {
close();
trackEvent("command_palette_navigated", { kind: "add_integration", plugin_key: pluginKey });
trackEvent("integration_add_started", { plugin_key: pluginKey, via: "command_palette" });
void navigate({
to: "/{-$orgSlug}/integrations/add/$pluginKey",
params: { pluginKey },
});
},
[close, navigate],
);
const goToPreset = useCallback(
(pluginKey: string, presetId: string, presetUrl?: string) => {
close();
trackEvent("command_palette_navigated", { kind: "preset", plugin_key: pluginKey });
trackEvent("integration_add_started", {
plugin_key: pluginKey,
via: "command_palette",
preset_id: presetId,
});
const search: Record<string, string> = { preset: presetId };
if (presetUrl) search.url = presetUrl;
void navigate({
to: "/{-$orgSlug}/integrations/add/$pluginKey",
params: { pluginKey },
search,
});
},
[close, navigate],
);
if (!open) return null;
return (
<CommandDialog open={open} onOpenChange={onOpenChange}>
<CommandInput placeholder="Search integrations or jump to add…" />
<CommandList>
<CommandEmpty>No results found.</CommandEmpty>
{connectedSources.length > 0 && (
<CommandGroup heading="Connected">
{connectedSources.map(
(s: {
readonly id: string;
readonly name: string;
readonly kind: string;
readonly url?: string;
}) => (
<CommandItem
key={`source-${s.id}`}
value={`connected ${s.name} ${s.id} ${s.kind}`}
onSelect={() => goToIntegration(s.id)}
>
<IntegrationFavicon
icon={integrationPresetIconUrl(s, integrationPlugins)}
url={s.url}
/>
<span className="flex-1 truncate">{s.name}</span>
<CommandShortcut>{s.kind}</CommandShortcut>
</CommandItem>
),
)}
</CommandGroup>
)}
{connectedSources.length > 0 && integrationPlugins.length > 0 && <CommandSeparator />}
{integrationPlugins.length > 0 && (
<CommandGroup heading="Add integration">
{integrationPlugins.map((plugin) => (
<CommandItem
key={`add-${plugin.key}`}
value={`add ${plugin.label} ${plugin.key}`}
onSelect={() => goToAdd(plugin.key)}
>
<PlusIcon />
<span className="flex-1 truncate">Add {plugin.label}</span>
</CommandItem>
))}
</CommandGroup>
)}
{presetEntries.length > 0 && <CommandSeparator />}
{presetEntries.length > 0 && (
<CommandGroup heading="Popular integrations">
{presetEntries.map((e) => (
<CommandItem
key={`preset-${e.pluginKey}-${e.presetId}`}
value={`preset ${e.presetName} ${e.presetSummary ?? ""} ${e.pluginLabel}`}
onSelect={() => goToPreset(e.pluginKey, e.presetId, e.presetUrl)}
>
{e.presetIcon ? (
<img
src={e.presetIcon}
alt=""
className="size-4 shrink-0 object-contain"
loading="lazy"
/>
) : (
<span aria-hidden className="size-4 shrink-0 rounded-sm bg-muted-foreground/20" />
)}
<span className="flex-1 truncate">{e.presetName}</span>
<CommandShortcut>{e.pluginLabel}</CommandShortcut>
</CommandItem>
))}
</CommandGroup>
)}
</CommandList>
</CommandDialog>
);
}