forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmobile-sidebar.tsx
More file actions
337 lines (301 loc) · 10.9 KB
/
Copy pathmobile-sidebar.tsx
File metadata and controls
337 lines (301 loc) · 10.9 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
"use client";
import Image from "next/image";
import Link from "fumadocs-core/link";
import { usePathname } from "next/navigation";
import { useMemo, useState, useEffect, useCallback } from "react";
import { usePostHog } from "posthog-js/react";
// Components
import Separator from "@/components/ui/sidebar/separator";
import Page from "@/components/ui/sidebar/page";
import Folder from "@/components/ui/sidebar/folder";
import Dropdown from "@/components/ui/mobile-sidebar/dropdown";
import IntegrationSelector from "@/components/ui/integrations-sidebar/integration-selector";
import IntegrationSelectorSkeleton from "@/components/ui/integrations-sidebar/skeleton";
import { OpenedFoldersProvider } from "@/lib/hooks/use-opened-folders";
// Icons
import DiscordIcon from "@/components/ui/icons/discord";
import GithubIcon from "@/components/ui/icons/github";
import CrossIcon from "@/components/ui/icons/cross";
// Types
import { NavbarLink } from "./navbar";
import { DocsLayoutProps } from "fumadocs-ui/layouts/docs";
import { Integration } from "../ui/integrations-sidebar/integration-selector";
import { INTEGRATION_ORDER, INTEGRATION_METADATA } from "@/lib/integrations";
import { normalizeUrl } from "@/lib/analytics-utils";
interface MobileSidebarProps {
pageTree: DocsLayoutProps["tree"];
setIsOpen: (isOpen: boolean) => void;
handleToggleTheme: () => void;
}
type Node = DocsLayoutProps["tree"]["children"][number] & {
url: string;
name?: string;
index?: { url: string };
children?: Node[];
};
const LEFT_LINKS: NavbarLink[] = [
{
icon: <GithubIcon />,
href: "https://github.com/copilotkit/copilotkit",
target: "_blank",
},
{
icon: <DiscordIcon />,
href: "https://discord.gg/6dffbvGU3D",
target: "_blank",
},
];
const NODE_COMPONENTS: Record<
Node["type"],
React.ComponentType<{ node: Node; onNavigate?: () => void }>
> = {
separator: Separator,
page: Page,
folder: Folder,
};
const ANIMATION_DURATION = 300; // ms
const MobileSidebar = ({
pageTree,
setIsOpen,
handleToggleTheme,
}: MobileSidebarProps) => {
const pathname = usePathname();
const posthog = usePostHog();
const [selectedIntegration, setSelectedIntegration] =
useState<Integration | null>(null);
const [isVisible, setIsVisible] = useState(false);
const handleTalkToEngineerClick = () => {
posthog?.capture("talk_to_us_clicked", {
location: "docs_navbar_mobile",
});
window.location.href = "https://copilotkit.ai/talk-to-an-engineer";
};
// Trigger slide-in animation on mount
useEffect(() => {
// Small delay to ensure the initial state is rendered before animating
const timer = setTimeout(() => setIsVisible(true), 10);
return () => clearTimeout(timer);
}, []);
// Handle closing with animation
const handleClose = useCallback(() => {
setIsVisible(false);
setTimeout(() => {
setIsOpen(false);
}, ANIMATION_DURATION);
}, [setIsOpen]);
// Determine route type from pathname
const normalizedPathname = normalizeUrl(pathname);
const firstSegment = normalizedPathname.replace(/^\//, "").split("/")[0];
const isIntegrationRoute = INTEGRATION_ORDER.includes(
firstSegment as (typeof INTEGRATION_ORDER)[number],
);
const isReferenceRoute = firstSegment === "reference";
// Get integration-specific pages when an integration is selected
const integrationPages = useMemo(() => {
if (!selectedIntegration) return [];
const integrationMeta = INTEGRATION_METADATA[selectedIntegration];
const integrationLabel = integrationMeta?.label;
const possiblePaths = [
`/${selectedIntegration}`,
`/integrations/${selectedIntegration}`,
];
const FOLDER_NAME_MAPPINGS: Record<string, string> = {
AutoGen2: "ag2",
autogen2: "ag2",
};
const matchesIntegration = (folderNode: Node): boolean => {
if (folderNode.type !== "folder") return false;
const url = folderNode.index?.url || folderNode.url;
if (url && possiblePaths.includes(url)) {
return true;
}
if (folderNode.name) {
const folderNameLower = folderNode.name.toLowerCase();
const labelLower = integrationLabel?.toLowerCase() || "";
const idLower = selectedIntegration.toLowerCase();
const mappedId =
FOLDER_NAME_MAPPINGS[folderNode.name] ||
FOLDER_NAME_MAPPINGS[folderNameLower];
if (mappedId && mappedId === selectedIntegration.toLowerCase()) {
return true;
}
if (folderNameLower === labelLower || folderNameLower === idLower) {
return true;
}
}
return false;
};
let integrationFolder = pageTree.children.find((node) =>
matchesIntegration(node as Node),
) as Node | undefined;
if (!integrationFolder) {
const integrationsParent = pageTree.children.find((node) => {
const folderNode = node as Node;
return (
folderNode.type === "folder" &&
(folderNode.index?.url === "/integrations" ||
folderNode.name?.toLowerCase() === "integrations")
);
}) as Node | undefined;
if (integrationsParent?.children) {
integrationFolder = integrationsParent.children.find((node) =>
matchesIntegration(node as Node),
) as Node | undefined;
}
}
return integrationFolder?.children ?? [];
}, [selectedIntegration, pageTree.children]);
// Get reference-specific pages
const referencePages = useMemo(() => {
if (!isReferenceRoute) return [];
const referenceFolder = pageTree.children.find((node) => {
if (node.type !== "folder") return false;
const folderNode = node as Node;
const url = folderNode.index?.url || folderNode.url;
const name =
typeof folderNode.name === "string" ? folderNode.name : undefined;
return url === "/reference" || name?.toLowerCase() === "reference";
}) as Node | undefined;
if (referenceFolder && "children" in referenceFolder) {
return (referenceFolder as Node).children || [];
}
return [];
}, [isReferenceRoute, pageTree.children]);
// Determine which pages to show
const pagesToShow = useMemo(() => {
if (isIntegrationRoute && selectedIntegration) {
return integrationPages;
}
if (isReferenceRoute) {
return referencePages;
}
return pageTree.children;
}, [
isIntegrationRoute,
selectedIntegration,
integrationPages,
isReferenceRoute,
referencePages,
pageTree.children,
]);
return (
<div
className={`flex fixed top-0 left-0 z-50 justify-end p-1 w-full h-full transition-colors duration-300 ${
isVisible ? "bg-black/30" : "bg-black/0"
}`}
onClick={(e) => {
// Close when clicking the backdrop (outside the sidebar)
if (e.target === e.currentTarget) handleClose();
}}
>
<OpenedFoldersProvider>
<aside
className={`flex flex-col w-full max-w-[280px] h-[calc(100vh-8px)] border border-r-0 border-border bg-sidebar rounded-2xl pl-3 pr-1 transition-transform duration-300 ease-out ${
isVisible ? "translate-x-0" : "translate-x-full"
}`}
>
<div className="flex justify-between items-center my-2 w-full">
<div className="flex gap-1 items-center">
{LEFT_LINKS.map((link) => (
<Link
key={link.href}
href={link.href}
target={link.target}
className="flex justify-center items-center w-11 h-11 shrink-0"
>
<span className="flex items-center h-full">{link.icon}</span>
</Link>
))}
<button
className="flex justify-center items-center w-11 h-11 cursor-pointer"
onClick={handleToggleTheme}
>
<Image
src="/images/navbar/theme-moon.svg"
alt="Theme icon"
width={20}
height={20}
className="hidden dark:inline-block"
/>
<Image
src="/images/navbar/theme-sun.svg"
alt="Theme icon"
width={20}
height={20}
className="dark:hidden"
/>
</button>
</div>
<button
className="flex justify-center items-center w-11 h-full cursor-pointer"
onClick={handleClose}
>
<CrossIcon />
</button>
</div>
{/* Talk to an Engineer — pinned at the top of the mobile
* drawer as a primary CTA. */}
<button
type="button"
onClick={() => {
handleClose();
handleTalkToEngineerClick();
}}
className="text-left rounded-md px-3 py-2.5 mb-2 text-[14px] font-medium bg-indigo-500/10 text-indigo-500 hover:bg-indigo-500/20 transition-all cursor-pointer"
aria-label="Talk to an engineer"
>
Talk to an engineer
</button>
<Dropdown onSelect={handleClose} />
{!isReferenceRoute && (
<IntegrationSelector
selectedIntegration={selectedIntegration}
setSelectedIntegration={setSelectedIntegration}
onNavigate={handleClose}
/>
)}
{isIntegrationRoute && selectedIntegration ? (
<ul className="flex overflow-y-auto flex-col mt-6 max-h-full custom-scrollbar [&>*:first-child]:mt-0">
{integrationPages.map((page, index) => {
const Component = NODE_COMPONENTS[page.type];
const pageUrl =
(page as Node).index?.url ||
(page as Node).url ||
`page-${index}`;
const key = `${page.type}-${pageUrl}`;
return (
<Component
key={key}
node={page as Node}
onNavigate={handleClose}
/>
);
})}
</ul>
) : isIntegrationRoute && !selectedIntegration ? (
<IntegrationSelectorSkeleton />
) : (
<ul className="flex overflow-y-auto flex-col mt-6 max-h-full custom-scrollbar [&>*:first-child]:mt-0">
{pagesToShow.map((page, index) => {
const Component = NODE_COMPONENTS[page.type];
const pageUrl =
(page as Node).index?.url ||
(page as Node).url ||
`page-${index}`;
const key = `${page.type}-${pageUrl}`;
return (
<Component
key={key}
node={page as Node}
onNavigate={handleClose}
/>
);
})}
</ul>
)}
</aside>
</OpenedFoldersProvider>
</div>
);
};
export default MobileSidebar;