Files
newz-qwen-27b/frontend/src/components/ui/Header.tsx
T
Daniel Samson ffe19272b3 feat: add React + TypeScript frontend with Vite, routing, tests and Docker setup
M3 frontend infrastructure for the newzy news application:

- Vite + React 18 + TypeScript project (Node >=18) in frontend/
- React Router v6: BrowserRouter in App.tsx, routes for /, /articles,
  /articles/:id and /categories, Link/NavLink-based navigation
- lucide-react icons (Home, FileText, Settings, Search, ...) in the
  responsive Header (desktop + mobile menu) and article components
- Reusable components: ArticleCard, AuthorAvatar, CategoryBadge (CVA),
  Layout shell with backend API status indicator in the footer
- Typed API client (fetch wrapper, ApiError, endpoint helpers) with
  types matching the backend models; sample-data fallback until the
  backend article endpoints exist
- Tailwind CSS global/component styles, cn() helper (clsx + tailwind-merge)
- Unit tests: Vitest + React Testing Library (23 tests, 100% statement
  coverage on components)
- E2E tests: Playwright (9 tests) covering page shell, feed rendering,
  client-side navigation, article route and API interactions
- ESLint (react + react-hooks + typescript) configured and passing
- Multi-stage Dockerfile (Vite build -> Nginx) with SPA fallback and an
  /api/ proxy to the backend service; docker-compose frontend service on
  port 3000 in both frontend/ and the root docker-compose.yml
- README with setup, API documentation, testing and Docker instructions
2026-10-30 14:10:29 +00:00

100 lines
3.3 KiB
TypeScript

import { FileText, Home, Menu, Settings, X, type LucideIcon } from 'lucide-react';
import { useState } from 'react';
import { NavLink } from 'react-router-dom';
import logo from '../../assets/logo.svg';
import { cn } from '../../lib/utils';
export interface HeaderProps {
className?: string;
}
interface NavItem {
to: string;
label: string;
icon: LucideIcon;
/** Match the route exactly (used for "/"). */
end?: boolean;
}
const NAV_ITEMS: NavItem[] = [
{ to: '/', label: 'Home', icon: Home, end: true },
{ to: '/articles', label: 'Articles', icon: FileText },
{ to: '/categories', label: 'Categories', icon: Settings },
];
/**
* Main navigation header.
*
* Uses react-router-dom <NavLink> (never plain anchors) so navigation stays
* client-side. Lucide icons mark each item; the mobile menu collapses the
* same links into a toggleable panel below <md> breakpoints.
*/
export function Header({ className }: HeaderProps) {
const [menuOpen, setMenuOpen] = useState(false);
const closeMenu = () => setMenuOpen(false);
const linkClasses = ({ isActive }: { isActive: boolean }): string =>
cn(
'inline-flex items-center gap-2 rounded-md px-3 py-2 text-sm font-medium transition-colors',
'text-slate-600 hover:bg-slate-100 hover:text-slate-900',
isActive && 'bg-blue-50 text-blue-700 hover:bg-blue-100 hover:text-blue-800',
);
const renderNav = (id: string, itemClasses?: string) => (
<nav id={id} aria-label={`${id === 'mobile-menu' ? 'Mobile' : 'Main'} navigation`} className={itemClasses}>
{NAV_ITEMS.map(({ to, label, icon: Icon, end }) => (
<NavLink
key={to}
to={to}
end={end}
className={({ isActive }) => linkClasses({ isActive })}
onClick={closeMenu}
>
<Icon className="h-5 w-5" aria-hidden="true" />
<span>{label}</span>
</NavLink>
))}
</nav>
);
return (
<header
data-testid="site-header"
className={cn('sticky top-0 z-40 border-b border-slate-200 bg-white/95 backdrop-blur', className)}
>
<div className="mx-auto flex max-w-6xl items-center justify-between px-4 py-3 sm:px-6">
{/* Brand */}
<NavLink to="/" className="flex items-center gap-2" onClick={closeMenu} aria-label="newzy">
<img src={logo} alt="" className="h-8 w-8" />
<span className="text-xl font-bold tracking-tight text-slate-900">newzy</span>
</NavLink>
{/* Desktop navigation */}
{renderNav('main-nav', 'hidden items-center gap-1 md:flex')}
{/* Mobile menu toggle */}
<button
type="button"
className="inline-flex items-center justify-center rounded-md p-2 text-slate-600 hover:bg-slate-100 hover:text-slate-900 md:hidden"
aria-label={menuOpen ? 'Close menu' : 'Open menu'}
aria-expanded={menuOpen}
aria-controls="mobile-menu"
onClick={() => setMenuOpen((open) => !open)}
>
{menuOpen ? (
<X className="h-6 w-6" aria-hidden="true" />
) : (
<Menu className="h-6 w-6" aria-hidden="true" />
)}
</button>
</div>
{/* Mobile navigation (rendered only while open) */}
{menuOpen && renderNav('mobile-menu', 'flex flex-col gap-1 border-t border-slate-200 px-4 pb-4 pt-2 md:hidden')}
</header>
);
}
export default Header;