-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathheader.tsx
More file actions
194 lines (180 loc) · 6.7 KB
/
header.tsx
File metadata and controls
194 lines (180 loc) · 6.7 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
'use client';
import Image from 'next/image';
import Link from 'next/link';
import { usePathname, useRouter } from 'next/navigation';
import { useCallback, useEffect, useRef, useState } from 'react';
import { track } from '@/lib/analytics';
import { ModeToggle } from '@/components/ui/mode-toggle';
import { MinecraftToggles } from '@/components/minecraft/minecraft-toggles';
import { RickMortyToggles } from '@/components/rick-morty/rick-morty-toggles';
import { navigateInApp } from '@/lib/client-navigation';
import { cn } from '@/lib/utils';
import { GitHubStars } from './GithubStars';
/** Dashboard tab paths that should highlight the "Dashboard" nav link. */
const DASHBOARD_TABS = [
'/inference',
'/evaluation',
'/historical',
'/calculator',
'/reliability',
'/gpu-specs',
'/gpu-metrics',
'/submissions',
];
const NAV_LINKS = [
{ href: '/', label: 'Home', testId: 'nav-link-home', event: 'header_home_clicked' },
{
href: '/inference',
label: 'Dashboard',
testId: 'nav-link-dashboard',
event: 'header_dashboard_clicked',
},
{
href: '/quotes',
label: 'Supporters',
testId: 'nav-link-supporters',
event: 'header_supporters_clicked',
},
{ href: '/blog', label: 'Articles', testId: 'nav-link-blog', event: 'header_blog_clicked' },
{ href: '/about', label: 'About', testId: 'nav-link-about', event: 'header_about_clicked' },
] as const;
function isActive(pathname: string, href: string): boolean {
if (href === '/') return pathname === '/';
if (href === '/inference') return DASHBOARD_TABS.some((tab) => pathname.startsWith(tab));
return pathname.startsWith(href);
}
export const Header = ({ starCount }: { starCount?: number | null }) => {
const pathname = usePathname() ?? '/';
const router = useRouter();
const [mobileMenuOpen, setMobileMenuOpen] = useState(false);
const menuRef = useRef<HTMLDivElement>(null);
// Close menu on route change
useEffect(() => {
setMobileMenuOpen(false);
}, [pathname]);
// Close menu on click outside or Escape
useEffect(() => {
if (!mobileMenuOpen) return;
const handleClick = (e: MouseEvent) => {
if (menuRef.current && !menuRef.current.contains(e.target as Node)) {
setMobileMenuOpen(false);
}
};
const handleKey = (e: KeyboardEvent) => {
if (e.key === 'Escape') setMobileMenuOpen(false);
};
document.addEventListener('mousedown', handleClick);
document.addEventListener('keydown', handleKey);
return () => {
document.removeEventListener('mousedown', handleClick);
document.removeEventListener('keydown', handleKey);
};
}, [mobileMenuOpen]);
const toggleMenu = useCallback(() => {
setMobileMenuOpen((prev) => !prev);
track('header_mobile_menu_toggled');
}, []);
return (
<header
data-testid="header"
className="sticky top-0 z-50 border-b border-border/40 mb-4 bg-background/60 backdrop-blur-[2px]"
>
<div className="container mx-auto px-4 lg:px-8">
<div className="flex h-14 items-center gap-6">
{/* Brand */}
<Link href="/" className="flex items-center gap-2 shrink-0">
<span className="text-lg font-bold tracking-tight">InferenceX</span>
<span className="hidden sm:flex items-center gap-1.5 text-xs text-muted-foreground">
by
<Image
src="/brand/logo-color.webp"
alt="SemiAnalysis logo"
width={64}
height={27}
className="inline h-auto lg:w-20"
priority
/>
</span>
</Link>
{/* Desktop nav */}
<nav className="hidden lg:flex items-center gap-1">
{NAV_LINKS.map(({ href, label, testId, event }) => (
<Link
key={href}
data-testid={testId}
href={href}
className={cn(
'px-3 py-1.5 rounded-md text-sm font-medium transition-colors',
isActive(pathname, href)
? 'text-brand bg-brand/10'
: 'text-muted-foreground hover:text-foreground hover:bg-muted',
)}
onClick={(e) => {
track(event);
if (href === '/inference') navigateInApp(e, router, href);
}}
>
{label}
</Link>
))}
</nav>
{/* Right side */}
<div className="ml-auto flex items-center gap-2">
<GitHubStars owner="SemiAnalysisAI" repo="InferenceX" starCount={starCount} />
<MinecraftToggles />
<RickMortyToggles />
<ModeToggle />
{/* Mobile hamburger */}
<div ref={menuRef} className="relative lg:hidden">
<button
type="button"
data-testid="mobile-menu-toggle"
onClick={toggleMenu}
className="flex items-center justify-center size-9 rounded-md transition-colors hover:bg-muted cursor-pointer"
aria-expanded={mobileMenuOpen}
aria-label="Navigation menu"
>
<svg
width="20"
height="20"
viewBox="0 0 20 20"
fill="none"
stroke="currentColor"
strokeWidth="1"
strokeLinecap="round"
strokeLinejoin="round"
>
<line x1="1" y1="4" x2="19" y2="4" />
<line x1="1" y1="10" x2="19" y2="10" />
<line x1="1" y1="16" x2="19" y2="16" />
</svg>
</button>
{mobileMenuOpen && (
<div className="absolute right-0 top-full mt-2 z-50 flex flex-col rounded-lg border border-border bg-background p-1.5 shadow-lg min-w-40">
{NAV_LINKS.map(({ href, label, event }) => (
<Link
key={href}
href={href}
className={cn(
'px-3 py-2 rounded-md text-sm font-medium transition-colors',
isActive(pathname, href)
? 'text-brand bg-brand/10'
: 'text-muted-foreground hover:text-foreground hover:bg-muted',
)}
onClick={(e) => {
track(event);
if (href === '/inference') navigateInApp(e, router, href);
}}
>
{label}
</Link>
))}
</div>
)}
</div>
</div>
</div>
</div>
</header>
);
};