|
1 | | -"use client"; |
| 1 | +'use client' |
2 | 2 |
|
3 | 3 | import Link from 'next/link'; |
4 | 4 | import { usePathname } from 'next/navigation'; |
5 | | -import type { ReferencePage } from '../services/referenceService'; |
6 | 5 | import { capitalCase } from 'change-case'; |
| 6 | +import pluralize from 'pluralize'; |
| 7 | +import React, { useEffect, useRef } from 'react'; |
| 8 | + |
| 9 | +type ReferencePage = { |
| 10 | + name: string; |
| 11 | + description: string; |
| 12 | + reference: string; |
| 13 | + type: string; |
| 14 | + category?: string; |
| 15 | +}; |
| 16 | + |
| 17 | +type NavigationStructure = { |
| 18 | + type: string; |
| 19 | + displayName: string; |
| 20 | + categories: { |
| 21 | + category: string; |
| 22 | + displayName: string; |
| 23 | + items: ReferencePage[]; |
| 24 | + }[]; |
| 25 | +}[]; |
| 26 | + |
| 27 | +const icons = { |
| 28 | + chevronRight: ( |
| 29 | + <path |
| 30 | + strokeLinecap="round" |
| 31 | + strokeLinejoin="round" |
| 32 | + strokeWidth={2} |
| 33 | + d="M9 5l7 7-7 7" |
| 34 | + /> |
| 35 | + ), |
| 36 | +}; |
7 | 37 |
|
8 | 38 | export default function Index({ |
9 | 39 | groupedReferences |
10 | 40 | }: { |
11 | 41 | groupedReferences: Record<string, Record<string, ReferencePage[]>>; |
12 | 42 | }) { |
13 | 43 | const pathname = usePathname(); |
| 44 | + const activeItemRef = useRef<HTMLAnchorElement>(null); |
| 45 | + const containerRef = useRef<HTMLElement>(null); |
| 46 | + |
| 47 | + // Auto-scroll to active item when pathname changes |
| 48 | + useEffect(() => { |
| 49 | + if (activeItemRef.current && containerRef.current) { |
| 50 | + const container = containerRef.current; |
| 51 | + const activeItem = activeItemRef.current; |
| 52 | + |
| 53 | + // Calculate position to scroll within the container |
| 54 | + const containerRect = container.getBoundingClientRect(); |
| 55 | + const itemRect = activeItem.getBoundingClientRect(); |
| 56 | + const scrollOffset = itemRect.top - containerRect.top - (containerRect.height / 2) + (itemRect.height / 2); |
| 57 | + |
| 58 | + // Instant scroll without animation |
| 59 | + container.scrollBy({ |
| 60 | + top: scrollOffset, |
| 61 | + behavior: 'instant' |
| 62 | + }); |
| 63 | + } |
| 64 | + }, [pathname]); |
14 | 65 |
|
15 | | - // Get all types and sort them |
16 | | - const types = Object.keys(groupedReferences).sort(); |
| 66 | + // Convert old structure to new structure |
| 67 | + const navigationStructure: NavigationStructure = Object.entries(groupedReferences) |
| 68 | + .sort(([a], [b]) => a.localeCompare(b)) |
| 69 | + .map(([type, categories]) => ({ |
| 70 | + type, |
| 71 | + displayName: type, |
| 72 | + categories: Object.entries(categories) |
| 73 | + .sort(([a], [b]) => a.localeCompare(b)) |
| 74 | + .map(([category, items]) => ({ |
| 75 | + category, |
| 76 | + displayName: category, |
| 77 | + items: items.sort((a, b) => a.name.localeCompare(b.name)) |
| 78 | + })) |
| 79 | + })); |
| 80 | + |
| 81 | + // Parse pathname to determine current location |
| 82 | + const pathParts = pathname.split('/').filter(Boolean); |
| 83 | + const currentType = pathParts[2]; // /docs/reference/[type] |
| 84 | + const currentCategory = pathParts[3]; // /docs/reference/[type]/[category] |
| 85 | + const currentName = pathParts[4] ? decodeURIComponent(pathParts[4]) : undefined; // /docs/reference/[type]/[category]/[name] |
17 | 86 |
|
18 | 87 | return ( |
19 | | - <section className="flex-1 p-4 overflow-y-auto"> |
| 88 | + <section ref={containerRef} className="flex-1 overflow-y-auto p-4 min-h-0"> |
20 | 89 | <nav className="space-y-1"> |
21 | | - {types.map((type, index) => { |
22 | | - const categories = Object.keys(groupedReferences[type]).sort(); |
| 90 | + {navigationStructure.map((typeSection, typeIndex) => { |
| 91 | + // Types are always expanded |
| 92 | + const isTypeExpanded = true; |
| 93 | + const isTypeActive = pathname === `/docs/reference/${typeSection.type}`; |
23 | 94 |
|
24 | 95 | return ( |
25 | | - <div key={type}> |
26 | | - {/* Add horizontal rule between sections (not before the first one) */} |
27 | | - {index > 0 && ( |
| 96 | + <div key={typeSection.type}> |
| 97 | + {/* Separator between type sections (not before first) */} |
| 98 | + {typeIndex > 0 && ( |
28 | 99 | <div className="my-4 border-t border-neutral-700/50"></div> |
29 | 100 | )} |
30 | 101 |
|
31 | | - {/* Type header */} |
32 | | - {categories.length > 0 && ( |
33 | | - <> |
34 | | - <Option |
35 | | - key={type} |
36 | | - target={`/docs/reference/${type}`} |
37 | | - display={capitalCase(type) + 's'} |
38 | | - className="uppercase" |
39 | | - currentPath={pathname} |
40 | | - /> |
| 102 | + {/* Type row */} |
| 103 | + <div className="flex items-center gap-1"> |
| 104 | + <div className="p-1 flex-shrink-0"> |
| 105 | + <svg |
| 106 | + className="w-4 h-4 text-gray-500 rotate-90" |
| 107 | + fill="none" |
| 108 | + stroke="currentColor" |
| 109 | + viewBox="0 0 24 24" |
| 110 | + > |
| 111 | + {icons.chevronRight} |
| 112 | + </svg> |
| 113 | + </div> |
| 114 | + <Link |
| 115 | + href={`/docs/reference/${typeSection.type}`} |
| 116 | + ref={isTypeActive ? activeItemRef : null} |
| 117 | + className={`flex-1 px-3 py-3 rounded-lg text-sm font-semibold uppercase transition-all duration-200 ${ |
| 118 | + isTypeActive |
| 119 | + ? "bg-blue-500/20 text-blue-300 border border-blue-500/30" |
| 120 | + : "text-gray-300 hover:text-white hover:bg-neutral-700/50" |
| 121 | + }`} |
| 122 | + > |
| 123 | + {pluralize(capitalCase(typeSection.displayName))} |
| 124 | + </Link> |
| 125 | + </div> |
41 | 126 |
|
42 | | - {/* Category links */} |
43 | | - {categories.map((category) => ( |
44 | | - <Option |
45 | | - key={`${type}-${category}`} |
46 | | - target={`/docs/reference/${type}/${category}`} |
47 | | - display={capitalCase(category)} |
48 | | - currentPath={pathname} |
49 | | - /> |
50 | | - ))} |
51 | | - </> |
52 | | - )} |
| 127 | + {/* Categories */} |
| 128 | + {isTypeExpanded && typeSection.categories.map((categorySection) => { |
| 129 | + // Category is expanded if we're on that category page or on one of its item pages |
| 130 | + const isCategoryExpanded = currentType === typeSection.type && currentCategory === categorySection.category; |
| 131 | + const isCategoryActive = pathname === `/docs/reference/${typeSection.type}/${categorySection.category}`; |
| 132 | + |
| 133 | + return ( |
| 134 | + <div key={`${typeSection.type}-${categorySection.category}`}> |
| 135 | + {/* Category row */} |
| 136 | + <div className="flex items-center gap-1 pl-4"> |
| 137 | + <div className="p-1 flex-shrink-0"> |
| 138 | + <svg |
| 139 | + className={`w-3 h-3 text-gray-500 transition-transform ${isCategoryExpanded ? 'rotate-90' : ''}`} |
| 140 | + fill="none" |
| 141 | + stroke="currentColor" |
| 142 | + viewBox="0 0 24 24" |
| 143 | + > |
| 144 | + {icons.chevronRight} |
| 145 | + </svg> |
| 146 | + </div> |
| 147 | + <Link |
| 148 | + href={`/docs/reference/${typeSection.type}/${categorySection.category}`} |
| 149 | + ref={isCategoryActive ? activeItemRef : null} |
| 150 | + className={`flex-1 px-3 py-3 rounded-lg text-sm transition-all duration-200 ${ |
| 151 | + isCategoryActive |
| 152 | + ? "bg-blue-500/20 text-blue-300 border border-blue-500/30" |
| 153 | + : "text-gray-300 hover:text-white hover:bg-neutral-700/50" |
| 154 | + }`} |
| 155 | + > |
| 156 | + {capitalCase(categorySection.displayName)} |
| 157 | + </Link> |
| 158 | + </div> |
| 159 | + |
| 160 | + {/* Individual reference items */} |
| 161 | + {isCategoryExpanded && categorySection.items.map((item) => { |
| 162 | + const itemFilename = item.reference.split('/').pop(); |
| 163 | + const itemPath = `/docs/reference/${typeSection.type}/${categorySection.category}/${itemFilename}`; |
| 164 | + // Decode pathname to match against the constructed path |
| 165 | + const decodedPathname = decodeURIComponent(pathname); |
| 166 | + const isItemActive = decodedPathname === itemPath || pathname === itemPath; |
| 167 | + |
| 168 | + return ( |
| 169 | + <div key={item.reference} className="flex items-center pl-10 ml-4"> |
| 170 | + <Link |
| 171 | + href={itemPath} |
| 172 | + ref={isItemActive ? activeItemRef : null} |
| 173 | + className={`flex-1 px-3 py-3 rounded-lg text-sm transition-all duration-200 ${ |
| 174 | + isItemActive |
| 175 | + ? "bg-blue-500/20 text-blue-300 border border-blue-500/30" |
| 176 | + : "text-gray-400 hover:text-white hover:bg-neutral-700/50" |
| 177 | + }`} |
| 178 | + > |
| 179 | + {item.name} |
| 180 | + </Link> |
| 181 | + </div> |
| 182 | + ); |
| 183 | + })} |
| 184 | + </div> |
| 185 | + ); |
| 186 | + })} |
53 | 187 | </div> |
54 | 188 | ); |
55 | 189 | })} |
56 | 190 | </nav> |
57 | 191 | </section> |
58 | 192 | ); |
59 | 193 | } |
60 | | - |
61 | | -function Option({ |
62 | | - target, |
63 | | - display, |
64 | | - className, |
65 | | - currentPath |
66 | | -}: { |
67 | | - target: string; |
68 | | - display: string; |
69 | | - className?: string; |
70 | | - currentPath: string; |
71 | | -}) { |
72 | | - // Determine if this option should be highlighted |
73 | | - // Highlight if: |
74 | | - // 1. Exact match (e.g., on /docs/reference/operator, highlight "Operators") |
75 | | - // 2. Category match (e.g., on /docs/reference/operator/accumulator or /docs/reference/operator/accumulator/avg, highlight "Operators" and "Accumulator") |
76 | | - const isExactMatch = currentPath === target; |
77 | | - const isCategoryMatch = currentPath.startsWith(target + '/'); |
78 | | - const isActive = isExactMatch || isCategoryMatch; |
79 | | - |
80 | | - return ( |
81 | | - <Link |
82 | | - href={target} |
83 | | - className={`block w-full text-left px-4 py-3 rounded-lg text-sm transition-all duration-200 ${ |
84 | | - isActive |
85 | | - ? "bg-blue-500/20 text-blue-300 border border-blue-500/30" |
86 | | - : "text-gray-300 hover:text-white hover:bg-neutral-700/50" |
87 | | - }${className ? ' ' + className : ''}`} |
88 | | - > |
89 | | - {display} |
90 | | - </Link> |
91 | | - ); |
92 | | -} |
|
0 commit comments