forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframework-selector.tsx
More file actions
313 lines (297 loc) · 12.1 KB
/
Copy pathframework-selector.tsx
File metadata and controls
313 lines (297 loc) · 12.1 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
"use client";
// FrameworkSelector — persistent "agentic backend" dropdown that anchors
// the docs experience. Opens a panel listing every registry integration
// grouped by category. Selecting an entry navigates to `/<framework>`:
// changing backends is a pivot into that framework's overview, not an
// attempt to preserve the current page's feature slug.
import React, { useEffect, useRef, useState } from "react";
import { usePathname, useRouter } from "next/navigation";
import { usePostHog } from "posthog-js/react";
import { useFramework } from "./framework-provider";
import { FrameworkLogo } from "./icons/framework-icons";
import { compareByDisplayOrder } from "@/lib/framework-order";
export interface FrameworkOption {
slug: string;
name: string;
category: string;
logo?: string | null;
deployed: boolean;
}
export interface FrameworkSelectorProps {
options: FrameworkOption[];
/**
* Ordered category ids (from the registry) used to group entries in the
* dropdown panel. Unknown categories fall through to "Other".
*/
categoryOrder: { id: string; name: string }[];
/** Extra wrapper class (positioning). */
className?: string;
/**
* Presentation flavor.
* - `topbar` (default, legacy): compact pill sized for a horizontal bar.
* - `sidebar`: full-width pill with integration logo left, name center,
* chevron right — styled to match the docs.copilotkit.ai sidebar header.
*/
variant?: "topbar" | "sidebar";
}
export function FrameworkSelector({
options,
categoryOrder,
className,
variant = "topbar",
}: FrameworkSelectorProps) {
const router = useRouter();
const pathname = usePathname() ?? "";
const posthog = usePostHog();
const { effectiveFramework, setStoredFramework } = useFramework();
const [open, setOpen] = useState(false);
const panelRef = useRef<HTMLDivElement>(null);
const buttonRef = useRef<HTMLButtonElement>(null);
// Close on outside-click / Escape
useEffect(() => {
if (!open) return;
const handleClick = (e: MouseEvent) => {
// `e.target` is typed as `EventTarget | null`; `Node.contains`
// requires an actual `Node`. Guard instead of casting so we don't
// silently invoke `contains` with non-DOM targets (e.g. events
// dispatched against `window`).
const target = e.target instanceof Node ? e.target : null;
if (!target) return;
if (
panelRef.current?.contains(target) ||
buttonRef.current?.contains(target)
) {
return;
}
setOpen(false);
};
const handleKey = (e: KeyboardEvent) => {
if (e.key === "Escape") setOpen(false);
};
document.addEventListener("mousedown", handleClick);
document.addEventListener("keydown", handleKey);
return () => {
document.removeEventListener("mousedown", handleClick);
document.removeEventListener("keydown", handleKey);
};
}, [open]);
// Display whatever the page is currently rendering as: URL framework
// when present, then stored choice, then the soft-default
// (Built-in Agent). The selector should never read "Pick a backend"
// when the docs are actually rendering BIA code — that's misleading.
const current = options.find((o) => o.slug === effectiveFramework);
// BIA is the soft-default and the framing on the sidebar is "you're
// reading CopilotKit's docs" rather than "you've picked the Built-in
// Agent backend." Show "CopilotKit" in the sidebar selector chrome
// (closed pill + dropdown row) but keep the registry name elsewhere
// so DocsLandingNext, IntegrationGrid, etc. still call it Built-in
// Agent where the framing is about choosing a backend.
const isSidebar = variant === "sidebar";
const displayNameFor = (opt: FrameworkOption) =>
isSidebar && opt.slug === "built-in-agent" ? "CopilotKit" : opt.name;
const label = current ? displayNameFor(current) : "Pick an agentic backend";
function selectFramework(slug: string) {
setStoredFramework(slug);
// Fire a PostHog event so analytics dashboards can see which
// backend readers pick. Wrapped in try/catch — PostHog can be
// blocked by ad blockers or fail to initialize, and a broken
// analytics call must never break navigation.
try {
const opt = options.find((o) => o.slug === slug);
posthog?.capture("docs.framework_selected", {
framework: slug,
framework_name: opt?.name ?? slug,
category: opt?.category,
from_path: pathname,
});
} catch {
// Swallow — analytics is fire-and-forget.
}
// replace vs push: picking a backend is a pivot on the same logical
// page, not a forward navigation. Using `push` clutters the back
// stack with every framework the user clicked through, which makes
// the browser Back button useless. `replace` keeps history sane.
// Framework changes intentionally drop the current feature slug. The
// selector is a backend pivot, so landing on the framework root gives
// readers the right overview before they drill into framework-specific
// docs.
router.replace(`/${slug}`);
setOpen(false);
}
// Single flat list, ordered by the canonical display order. The
// category buckets ("Most Popular / Agent Frameworks / Enterprise /
// Emerging") used to live here but partners read them as a tier
// list — we now show every backend in one neutral list.
const flatOptions = options
.filter((opt) => !(isSidebar && opt.slug === "built-in-agent"))
.slice()
.sort((a, b) => compareByDisplayOrder(a.slug, b.slug));
// BIA pinned at the top of the sidebar dropdown — only the sidebar
// variant (the topbar selector renders the flat list inline).
const pinnedBIA = isSidebar
? (options.find((o) => o.slug === "built-in-agent") ?? null)
: null;
// Sidebar variant: full-width select with integration logo box on the
// left, framework name center, chevron right. It uses the global
// shadcn radius and a subtle accent wash so it reads as a selected
// docs context control without hardcoding a lavender value.
const sidebarBtnClasses = [
"shell-docs-radius-control w-full flex items-center gap-2 p-1.5 border h-12",
"shadow-[var(--shadow-control)] transition-colors cursor-pointer",
"text-[13px] font-medium text-[var(--text)]",
current
? "bg-[var(--accent-dim)] border-[var(--nav-control-border)] hover:bg-[var(--accent-light)] hover:border-[var(--nav-control-border-hover)]"
: "bg-[var(--bg-surface)]/60 border-[var(--border)] hover:border-[var(--accent)]",
].join(" ");
const topbarBtnClasses =
"shell-docs-radius-control flex items-center gap-1.5 px-2.5 py-1.5 border border-[var(--border)] bg-[var(--bg-surface)] text-[12px] font-medium text-[var(--text)] hover:border-[var(--accent)] transition-colors cursor-pointer max-w-[220px]";
return (
<div className={`relative ${className ?? ""}`}>
<button
ref={buttonRef}
type="button"
onClick={() => setOpen((v) => !v)}
aria-haspopup="listbox"
aria-expanded={open}
className={isSidebar ? sidebarBtnClasses : topbarBtnClasses}
>
{isSidebar ? (
<>
<span
className={`shell-docs-picker-icon-chip h-8 w-8 shrink-0 ${
current ? "" : "border-[var(--border)] text-[var(--text-faint)]"
}`}
aria-hidden="true"
>
{current ? (
<FrameworkLogo
slug={current.slug}
fallbackSrc={current.logo}
size={16}
className="text-[var(--accent)]"
/>
) : (
<span className="h-2.5 w-2.5 bg-current" />
)}
</span>
<span className="flex-1 min-w-0 text-left">
{current ? (
<span className="block truncate leading-tight">{label}</span>
) : (
<span className="block truncate leading-tight text-[var(--text-muted)]">
Pick a backend
</span>
)}
<span className="block text-[9px] uppercase tracking-wider text-[var(--text-faint)] leading-tight mt-0.5">
Agentic backend
</span>
</span>
<svg
className="w-3.5 h-3.5 mr-0.5 shrink-0 text-[var(--text-muted)]"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M19 9l-7 7-7-7"
/>
</svg>
</>
) : (
<>
<span className="w-1.5 h-1.5 rounded-full bg-[var(--accent)] shrink-0" />
<span className="truncate">
{current ? (
<>
<span className="text-[var(--text-faint)] font-mono text-[10px] uppercase tracking-wider mr-1">
Backend
</span>
{label}
</>
) : (
<span className="text-[var(--text-muted)]">{label}</span>
)}
</span>
<svg
className="w-3 h-3 shrink-0 text-[var(--text-muted)]"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M19 9l-7 7-7-7"
/>
</svg>
</>
)}
</button>
{open && (
<div
ref={panelRef}
role="listbox"
className={
isSidebar
? "shell-docs-radius-surface absolute top-full left-0 right-0 mt-1 max-h-[60vh] overflow-y-auto border border-[var(--border)] bg-[var(--bg-surface)] shadow-[var(--shadow-panel)] z-50 p-2"
: "shell-docs-radius-surface absolute top-full left-0 mt-1 w-[340px] max-h-[70vh] overflow-y-auto border border-[var(--border)] bg-[var(--bg-surface)] shadow-[var(--shadow-panel)] z-50 p-2"
}
>
{pinnedBIA && (
<button
key={pinnedBIA.slug}
type="button"
onClick={() => selectFramework(pinnedBIA.slug)}
className={`shell-docs-radius-control w-full flex items-center gap-2 px-2 py-1.5 text-[13px] transition-colors cursor-pointer ${
pinnedBIA.slug === effectiveFramework
? "bg-[var(--accent-dim)] text-[var(--accent)]"
: "text-[var(--text-secondary)] hover:bg-[var(--bg-elevated)] hover:text-[var(--text)]"
}`}
>
<FrameworkLogo
slug={pinnedBIA.slug}
fallbackSrc={pinnedBIA.logo}
size={16}
className="shrink-0 text-[var(--accent)]"
/>
<span className="flex-1 text-left truncate">
{displayNameFor(pinnedBIA)}
</span>
</button>
)}
<div>
{flatOptions.map((opt) => {
const isActive = opt.slug === effectiveFramework;
return (
<button
key={opt.slug}
type="button"
onClick={() => selectFramework(opt.slug)}
className={`shell-docs-radius-control w-full flex items-center gap-2 px-2 py-1.5 text-[13px] transition-colors cursor-pointer ${
isActive
? "bg-[var(--accent-dim)] text-[var(--accent)]"
: "text-[var(--text-secondary)] hover:bg-[var(--bg-elevated)] hover:text-[var(--text)]"
}`}
>
<FrameworkLogo
slug={opt.slug}
fallbackSrc={opt.logo}
size={16}
className="shrink-0 text-[var(--accent)]"
/>
<span className="flex-1 text-left truncate">
{displayNameFor(opt)}
</span>
</button>
);
})}
</div>
</div>
)}
</div>
);
}