-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathHeader.tsx
More file actions
343 lines (317 loc) · 11.6 KB
/
Copy pathHeader.tsx
File metadata and controls
343 lines (317 loc) · 11.6 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
338
339
340
341
342
343
'use client'
import { Command } from 'lucide-react'
import Image from 'next/image'
import { useParams } from 'next/navigation'
import { useTranslations } from 'next-intl'
import { memo, useCallback, useEffect, useMemo, useState } from 'react'
import SearchDialog from '@/components/controls/SearchDialog'
import { RankingMegaMenu, rankingMenuItems } from '@/components/navigation/RankingMegaMenu'
import { StackMegaMenu } from '@/components/navigation/StackMegaMenu'
import { Link } from '@/i18n/navigation'
// Menu item configuration type
interface MenuItem {
href: string
translationKey: string
namespace?: 'header' | 'shared'
isExternal?: boolean
hasMegaMenu?: boolean
megaMenuType?: 'aiCodingStack' | 'ranking'
}
// Common CSS class names - extracted to constants for DRY
const DESKTOP_LINK_CLASSES =
'text-sm border-b border-transparent hover:border-[var(--color-border-strong)] pb-[var(--spacing-xs)] transition-all'
const MOBILE_LINK_CLASSES =
'block text-sm py-[var(--spacing-xs)] hover:text-[var(--color-text-secondary)] transition-colors'
function Header() {
const params = useParams()
const locale = params?.locale as string | undefined
const [isMenuOpen, setIsMenuOpen] = useState(false)
const [activeMegaMenu, setActiveMegaMenu] = useState<'aiCodingStack' | 'ranking' | null>(null)
const [isSearchDialogOpen, setIsSearchDialogOpen] = useState(false)
const tComponent = useTranslations('components.common')
const tShared = useTranslations('shared')
// Menu items configuration - memoized to avoid recreation on each render
const menuItems = useMemo<MenuItem[]>(
() => [
{ href: '/manifesto', translationKey: 'terms.manifesto', namespace: 'shared' },
{
href: '/ai-coding-stack',
translationKey: 'terms.aiCodingStack',
namespace: 'shared',
hasMegaMenu: true,
megaMenuType: 'aiCodingStack',
},
{ href: '/ai-coding-landscape', translationKey: 'header.landscape', namespace: 'header' },
{
href: '/model-intelligence-index',
translationKey: 'header.ranking',
namespace: 'header',
hasMegaMenu: true,
megaMenuType: 'ranking',
},
{ href: '/curated-collections', translationKey: 'terms.collections', namespace: 'shared' },
],
[]
)
// Event handlers - memoized with useCallback to prevent unnecessary re-renders
const handleMenuToggle = useCallback(() => {
setIsMenuOpen(prev => !prev)
}, [])
const handleMenuClose = useCallback(() => {
setIsMenuOpen(false)
}, [])
const handleMegaMenuOpen = useCallback((type: 'aiCodingStack' | 'ranking') => {
setActiveMegaMenu(type)
}, [])
const handleMegaMenuClose = useCallback(() => {
setActiveMegaMenu(null)
}, [])
// Handle keyboard shortcuts for search
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
// CMD+K (Mac) or CTRL+K (Windows/Linux)
if ((e.metaKey || e.ctrlKey) && e.key === 'k') {
e.preventDefault()
setIsSearchDialogOpen(true)
}
}
document.addEventListener('keydown', handleKeyDown)
return () => document.removeEventListener('keydown', handleKeyDown)
}, [])
// Get translated text based on namespace
const getTranslation = useCallback(
(item: MenuItem) => {
return item.namespace === 'shared'
? tShared(item.translationKey)
: tComponent(item.translationKey)
},
[tComponent, tShared]
)
// Render desktop menu item
const renderDesktopMenuItem = useCallback(
(item: MenuItem) => {
const translatedText = getTranslation(item)
if (item.hasMegaMenu && item.megaMenuType) {
const isActive = activeMegaMenu === item.megaMenuType
return (
<li
key={item.href}
className="relative"
onMouseEnter={() => handleMegaMenuOpen(item.megaMenuType!)}
onMouseLeave={handleMegaMenuClose}
onFocus={() => handleMegaMenuOpen(item.megaMenuType!)}
onBlur={event => {
if (!event.currentTarget.contains(event.relatedTarget)) handleMegaMenuClose()
}}
>
<Link
href={item.href}
className={DESKTOP_LINK_CLASSES}
aria-expanded={isActive}
aria-haspopup="true"
>
{translatedText}
</Link>
{item.megaMenuType === 'aiCodingStack' && (
<StackMegaMenu isOpen={isActive} onClose={handleMegaMenuClose} />
)}
{item.megaMenuType === 'ranking' && (
<RankingMegaMenu isOpen={isActive} onClose={handleMegaMenuClose} />
)}
</li>
)
}
return (
<li key={item.href}>
{item.isExternal ? (
<a href={item.href} target="_blank" rel="noopener" className={DESKTOP_LINK_CLASSES}>
→ {translatedText}
</a>
) : (
<Link href={item.href} className={DESKTOP_LINK_CLASSES}>
{translatedText}
</Link>
)}
</li>
)
},
[activeMegaMenu, handleMegaMenuOpen, handleMegaMenuClose, getTranslation]
)
// Render mobile menu item
const renderMobileMenuItem = useCallback(
(item: MenuItem) => {
const translatedText = getTranslation(item)
const rankingChildItems =
item.megaMenuType === 'ranking'
? rankingMenuItems.filter(rankingItem => rankingItem.href !== item.href)
: []
return (
<li key={item.href}>
{item.isExternal ? (
<a href={item.href} target="_blank" rel="noopener" className={MOBILE_LINK_CLASSES}>
→ {translatedText}
</a>
) : (
<Link href={item.href} className={MOBILE_LINK_CLASSES} onClick={handleMenuClose}>
{translatedText}
</Link>
)}
{rankingChildItems.length > 0 && (
<ul className="ml-[var(--spacing-sm)] mt-[var(--spacing-xs)] flex list-none flex-col gap-[var(--spacing-xs)] border-l border-[var(--color-border)] pl-[var(--spacing-sm)]">
{rankingChildItems.map(rankingItem => (
<li key={rankingItem.href}>
<Link
href={rankingItem.href}
className={MOBILE_LINK_CLASSES}
onClick={handleMenuClose}
>
{tComponent(rankingItem.titleKey)}
</Link>
</li>
))}
</ul>
)}
</li>
)
},
[handleMenuClose, getTranslation, tComponent]
)
// Memoized menu button label
const menuButtonLabel = useMemo(
() => (isMenuOpen ? tComponent('header.closeMenu') : tComponent('header.openMenu')),
[isMenuOpen, tComponent]
)
return (
<header className="sticky top-0 bg-[var(--color-bg)]/95 backdrop-blur-sm border-b border-[var(--color-border)] z-50">
<div className="max-w-8xl mx-auto px-[var(--spacing-md)]">
<nav className="flex items-center py-[var(--spacing-sm)]">
<Link
href="/"
className="flex items-center gap-2 text-lg font-semibold tracking-tight hover:text-[var(--color-text-secondary)] transition-colors"
>
<Image
src="/icon.svg"
alt="AI Coding Stack Logo"
width={24}
height={24}
className="inline-block"
/>
AI Coding Stack
</Link>
{/* Desktop Menu - Centered */}
<div className="hidden md:flex flex-1 justify-center">
<ul className="flex gap-[var(--spacing-md)] list-none">
{menuItems.map(renderDesktopMenuItem)}
</ul>
</div>
{/* Desktop Search Button - Right aligned */}
<div className="hidden md:block">
<button
type="button"
onClick={() => setIsSearchDialogOpen(true)}
className="flex items-center gap-2 px-3 py-1.5 text-sm border border-[var(--color-border)] bg-[var(--color-background)] text-[var(--color-text-muted)] hover:border-[var(--color-border-strong)] transition-colors min-w-[140px]"
>
<svg
className="w-4 h-4"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
strokeWidth={2}
aria-hidden="true"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"
/>
</svg>
<span className="flex-1 text-left">{tShared('actions.searchPlaceholder')}</span>
<kbd className="flex items-center gap-1 px-1.5 py-0.5 text-xs border border-[var(--color-border)]">
<Command className="w-3 h-3" />
<span>K</span>
</kbd>
</button>
</div>
{/* Mobile Menu Buttons - Right aligned */}
<div className="flex md:hidden items-center gap-2 ml-auto">
{/* Mobile Search Toggle */}
<button
type="button"
onClick={() => setIsSearchDialogOpen(true)}
className="p-[var(--spacing-xs)] hover:bg-[var(--color-hover)] transition-colors"
aria-label={tShared('actions.search')}
>
<svg
className="w-5 h-5"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
strokeWidth={2}
aria-hidden="true"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"
/>
</svg>
</button>
{/* Mobile Menu Toggle */}
<button
type="button"
onClick={handleMenuToggle}
className="p-[var(--spacing-xs)] hover:bg-[var(--color-hover)] transition-colors"
aria-label={tComponent('header.toggleMenu')}
aria-expanded={isMenuOpen}
aria-controls="mobile-navigation"
>
<svg
className="w-6 h-6"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
role="img"
>
<title>{menuButtonLabel}</title>
{isMenuOpen ? (
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M6 18L18 6M6 6l12 12"
/>
) : (
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M4 6h16M4 12h16M4 18h16"
/>
)}
</svg>
</button>
</div>
</nav>
{/* Mobile Menu */}
{isMenuOpen && (
<nav
id="mobile-navigation"
aria-label={tComponent('header.toggleMenu')}
className="md:hidden border-t border-[var(--color-border)] py-[var(--spacing-sm)]"
>
<ul className="flex flex-col gap-[var(--spacing-sm)] list-none">
{menuItems.map(renderMobileMenuItem)}
</ul>
</nav>
)}
</div>
{/* Search Dialog */}
<SearchDialog
isOpen={isSearchDialogOpen}
onClose={() => setIsSearchDialogOpen(false)}
locale={locale}
/>
</header>
)
}
export default memo(Header)