import { useMatches, useLocation, Link, Form } from "react-router"; import { ChevronRight, LayoutDashboard, LogOut } from "lucide-react"; interface Breadcrumb { label: string; href?: string; } interface BreadcrumbHandle { breadcrumbs: (data: unknown) => Breadcrumb[]; } function isBreadcrumbHandle(handle: unknown): handle is BreadcrumbHandle { return ( typeof handle === "object" && handle !== null && "breadcrumbs" in handle && typeof (handle as BreadcrumbHandle).breadcrumbs === "function" ); } function getInitials(name?: string | null): string { if (!name) return "?"; return name.split(" ").map((n) => n[0]).join("").toUpperCase().slice(0, 2); } export function Topbar({ userName }: { userName?: string | null }) { const matches = useMatches(); const location = useLocation(); const isOnDashboard = location.pathname === "/"; const activeMatch = [...matches].reverse().find((m) => isBreadcrumbHandle(m.handle)); const breadcrumbs: Breadcrumb[] = activeMatch && isBreadcrumbHandle(activeMatch.handle) ? activeMatch.handle.breadcrumbs(activeMatch.data) : []; return (
{/* Breadcrumbs */} {/* Dashboard Button */} {!isOnDashboard && ( Dashboard )} {/* User + Logout */} {userName && (
{userName}
{getInitials(userName)}
)}
); }