forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfolder.tsx
More file actions
200 lines (170 loc) · 6.08 KB
/
Copy pathfolder.tsx
File metadata and controls
200 lines (170 loc) · 6.08 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
"use client";
import { useCallback } from "react";
import { DocsLayoutProps } from "fumadocs-ui/layouts/docs";
import { usePathname, useRouter } from "next/navigation";
import Page from "./page";
import ChevronDownIcon from "../icons/chevron";
import { cn } from "@/lib/utils";
import Separator from "./separator";
import { useOpenedFolders } from "@/lib/hooks/use-opened-folders";
import { normalizeUrl, normalizeUrlForMatching } from "@/lib/analytics-utils";
type Node = DocsLayoutProps["tree"]["children"][number] & {
url: string;
$id?: string;
};
interface FolderProps {
node: Node & { index?: { url: string; $id?: string } };
onNavigate?: () => void;
}
function getFolderDisplayName(node: Node & { children?: Node[] }): string {
const folderName = typeof node.name === "string" ? node.name : "";
const folderUrl =
typeof (node as any).index?.url === "string"
? (node as any).index.url
: typeof node.url === "string"
? node.url
: "";
const childUrls = (node.children || [])
.map((child) => {
if (
child.type === "folder" &&
typeof (child as any).index?.url === "string"
) {
return (child as any).index.url;
}
return typeof child.url === "string" ? child.url : "";
})
.filter(Boolean);
const isA2UIFolder =
folderName === "A2UI" ||
folderUrl.endsWith("/a2ui") ||
childUrls.some((url) => url.includes("/a2ui/"));
if (isA2UIFolder) {
return "Declerative Gen-UI (A2UI)";
}
return folderName;
}
/**
* Checks if a folder's index URL matches the current pathname.
* Handles:
* - Index page normalization (e.g., /langgraph matches /langgraph/index)
* - Rewrite matches (e.g., /langgraph matches /integrations/langgraph)
* - Normalizes integration URLs and relative URLs
*/
function isFolderActive(
indexUrl: string | undefined,
pathname: string,
): boolean {
if (!indexUrl) return false;
const normalizedIndexUrl = normalizeUrlForMatching(indexUrl);
const normalizedPathname = normalizeUrlForMatching(pathname);
// Exact match
if (normalizedIndexUrl === normalizedPathname) {
return true;
}
// Handle index pages: /langgraph should match /langgraph/index
if (normalizedPathname === normalizedIndexUrl.replace(/\/index$/, "")) {
return true;
}
// Handle reverse: /langgraph/index should match /langgraph
if (normalizedIndexUrl === `${normalizedPathname}/index`) {
return true;
}
// Handle rewrite patterns: /langgraph should match /integrations/langgraph
const pathnameBase = normalizedPathname.replace(/^\/integrations\//, "/");
const indexUrlBase = normalizedIndexUrl.replace(/^\/integrations\//, "/");
if (pathnameBase === indexUrlBase) {
return true;
}
// Handle index with rewrite: /langgraph should match /integrations/langgraph/index
if (pathnameBase === indexUrlBase.replace(/\/index$/, "")) {
return true;
}
if (indexUrlBase === `${pathnameBase}/index`) {
return true;
}
return false;
}
const Folder = ({ node }: FolderProps) => {
const { isFolderOpen, toggleFolder } = useOpenedFolders();
const pathname = usePathname();
const router = useRouter();
const folderUrl = node.index?.url;
const folderId = node.$id;
const displayName = getFolderDisplayName(
node as Node & { children?: Node[] },
);
// Check if folder should be open by default from meta.json defaultOpen property
// Fumadocs exposes this property from meta.json files
const defaultOpen = (node as any).defaultOpen === true;
const isOpen = folderId ? isFolderOpen(folderId) || defaultOpen : false;
// Check if any child page is active - if so, don't mark the folder as active
const folderChildren = (node as { children?: Node[] }).children || [];
const normalizedPathname = normalizeUrlForMatching(pathname);
const hasActiveChild = folderChildren.some((child: any) => {
if (child.type === "page" && child.url) {
const childUrl = normalizeUrlForMatching(child.url);
return (
normalizedPathname === childUrl ||
normalizedPathname.startsWith(childUrl + "/")
);
}
return false;
});
// Only mark folder as active if we're on the folder's index page AND no child is active
const isActive =
!hasActiveChild && isFolderActive(node?.index?.url, pathname);
const NODE_COMPONENTS = {
separator: Separator,
page: Page,
folder: Folder,
};
const handleLinkClick = useCallback(
(e: React.MouseEvent<HTMLButtonElement>) => {
e.preventDefault();
e.stopPropagation();
if (folderId) {
toggleFolder(folderId);
}
if (folderUrl) {
const normalizedUrl = normalizeUrl(folderUrl);
router.push(normalizedUrl);
}
},
[folderUrl, router, folderId, toggleFolder],
);
return (
<div className="w-full">
<li
className={cn(
"w-full shrink-0 opacity-60 transition-all duration-300 hover:opacity-100 hover:bg-white dark:hover:bg-white/10 rounded-lg",
isActive && "opacity-100 bg-white dark:bg-white/10",
)}
>
<button
type="button"
onClick={handleLinkClick}
className="flex gap-2 justify-between items-center px-3 w-full h-10 cursor-pointer"
>
<span className="w-max text-sm shrink-0">{displayName}</span>
<ChevronDownIcon className={cn(isOpen ? "rotate-180" : "")} />
</button>
</li>
{isOpen && (
<ul className="flex relative flex-col gap-2 ml-4">
<div className="absolute top-1/2 -translate-y-1/2 -left-2 w-px h-[calc(100%-8px)] bg-foreground/10" />
{(node as { children: Node[] }).children.map((page, index) => {
const Component =
NODE_COMPONENTS[page.type as keyof typeof NODE_COMPONENTS];
const pageWithIndex = page as Node & { index?: { url: string } };
const pageUrl =
pageWithIndex.index?.url || page.url || `page-${index}`;
const key = `${page.type}-${pageUrl}`;
return <Component key={key} node={page as Node} minimal={true} />;
})}
</ul>
)}
</div>
);
};
export default Folder;