arbret/frontend/app/components/Header.tsx
counterweight 4e6f38e4a1
refactor: Extract SatsDisplay component and fix page IDs
- Extract SatsDisplay component to shared components directory
- Fix page IDs: rename 'admin-appointments' to 'admin-trades'
- Fix trades page using correct 'trades' page ID
2025-12-23 10:44:11 +01:00

90 lines
2.7 KiB
TypeScript

"use client";
import { useRouter } from "next/navigation";
import { useAuth } from "../auth-context";
import { sharedStyles } from "../styles/shared";
import constants from "../../../shared/constants.json";
const { ADMIN, REGULAR } = constants.roles;
type PageId =
| "profile"
| "invites"
| "exchange"
| "trades"
| "admin-invites"
| "admin-availability"
| "admin-trades"
| "admin-price-history";
interface HeaderProps {
currentPage: PageId;
}
interface NavItem {
id: PageId;
label: string;
href: string;
regularOnly?: boolean;
adminOnly?: boolean;
}
const REGULAR_NAV_ITEMS: NavItem[] = [
{ id: "exchange", label: "Exchange", href: "/exchange", regularOnly: true },
{ id: "trades", label: "My Trades", href: "/trades", regularOnly: true },
{ id: "invites", label: "My Invites", href: "/invites", regularOnly: true },
{ id: "profile", label: "My Profile", href: "/profile", regularOnly: true },
];
const ADMIN_NAV_ITEMS: NavItem[] = [
{ id: "admin-trades", label: "Trades", href: "/admin/trades", adminOnly: true },
{ id: "admin-availability", label: "Availability", href: "/admin/availability", adminOnly: true },
{ id: "admin-invites", label: "Invites", href: "/admin/invites", adminOnly: true },
{ id: "admin-price-history", label: "Prices", href: "/admin/price-history", adminOnly: true },
];
export function Header({ currentPage }: HeaderProps) {
const { user, logout, hasRole } = useAuth();
const router = useRouter();
const isRegularUser = hasRole(REGULAR);
const isAdminUser = hasRole(ADMIN);
const handleLogout = async () => {
await logout();
router.push("/login");
};
if (!user) return null;
// Build nav items based on user role
// Admin users see admin nav items, regular users see regular nav items
const navItems = isAdminUser ? ADMIN_NAV_ITEMS : REGULAR_NAV_ITEMS;
const visibleItems = navItems.filter(
(item) => (!item.regularOnly || isRegularUser) && (!item.adminOnly || isAdminUser)
);
return (
<div style={sharedStyles.header}>
<div style={sharedStyles.nav}>
{visibleItems.map((item, index) => (
<span key={item.id}>
{index > 0 && <span style={sharedStyles.navDivider}></span>}
{item.id === currentPage ? (
<span style={sharedStyles.navCurrent}>{item.label}</span>
) : (
<a href={item.href} style={sharedStyles.navLink}>
{item.label}
</a>
)}
</span>
))}
</div>
<div style={sharedStyles.userInfo}>
<span style={sharedStyles.userEmail}>{user.email}</span>
<button onClick={handleLogout} style={sharedStyles.logoutBtn}>
Sign out
</button>
</div>
</div>
);
}