All files / src/components/layout admin-sidebar.tsx

100% Statements 23/23
94.44% Branches 17/18
100% Functions 7/7
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                    5x                                                 60x 60x 60x 60x     60x 60x 36x 1x   36x 36x       60x 60x   36x 36x       60x 2x     60x                                                           900x     900x                                                                                                                      
import { useEffect, useRef } from "react";
import { Link, useRouterState } from "@tanstack/react-router";
import {
	LayoutDashboard,
	Store,
	FolderKanban,
	Users,
	LogOut,
	X,
	Tags,
	BarChart3,
	FileText,
	MessageCircle,
	MessageSquare,
	Send,
	Bell,
	Search,
	Music,
	LayoutTemplate,
	LayoutGrid,
} from "lucide-react";
import { cn } from "../../lib/utils";
import { useAuth } from "../../lib/auth-context";
import { ThemeSwitcher } from "../ui/theme-switcher";
import { ReleaseBadge } from "../ui/release-badge";
 
const navigation = [
	{ name: "Dashboard", href: "/admin", icon: LayoutDashboard },
	{ name: "Analytics", href: "/admin/analytics", icon: BarChart3 },
	{ name: "Pros", href: "/admin/pros", icon: Store },
	{ name: "Projects", href: "/admin/projects", icon: FolderKanban },
	{ name: "Room categories", href: "/admin/room-categories", icon: LayoutGrid },
 
	{ name: "Users", href: "/admin/users", icon: Users },
	{ name: "Taxonomy", href: "/admin/taxonomy", icon: Tags },
	{ name: "Blogs", href: "/admin/blogs", icon: FileText },
	{ name: "WhatsApp", href: "/admin/whatsapp", icon: MessageCircle },
	{ name: "Communications", href: "/admin/communications", icon: Send },
	{ name: "Notification Test", href: "/admin/notifications/test", icon: Bell },
	{ name: "Feedback", href: "/admin/feedback", icon: MessageSquare },
	{ name: "Search", href: "/admin/search", icon: Search },
	{ name: "Music Library", href: "/admin/social-studio/music", icon: Music },
	{ name: "Templates", href: "/admin/social-studio/templates", icon: LayoutTemplate },
];
 
type AdminSidebarProps = {
	isOpen: boolean;
	onClose: () => void;
};
 
export function AdminSidebar({ isOpen, onClose }: AdminSidebarProps) {
	const { signOut, user } = useAuth();
	const routerState = useRouterState();
	const currentPath = routerState.location.pathname;
	const sidebarRef = useRef<HTMLDivElement>(null);
 
	// Escape key closes sidebar on mobile
	useEffect(() => {
		if (!isOpen) return;
		const handleKeyDown = (e: KeyboardEvent) => {
			Eif (e.key === "Escape") onClose();
		};
		document.addEventListener("keydown", handleKeyDown);
		return () => document.removeEventListener("keydown", handleKeyDown);
	}, [isOpen, onClose]);
 
	// Auto-focus sidebar when opened on mobile
	useEffect(() => {
		if (isOpen) {
			const firstLink =
				sidebarRef.current?.querySelector<HTMLAnchorElement>("nav a");
			firstLink?.focus();
		}
	}, [isOpen]);
 
	const handleNavClick = () => {
		onClose();
	};
 
	return (
		<div
			ref={sidebarRef}
			className={cn(
				"fixed inset-y-0 left-0 z-50 flex w-64 flex-col bg-background-subtle border-r border-border-default transition-transform duration-300 ease-in-out lg:static lg:translate-x-0",
				isOpen ? "translate-x-0" : "-translate-x-full",
			)}
		>
			{/* Logo */}
			<div className="flex h-16 items-center justify-between px-6 border-b border-border-default">
				<div className="flex items-center gap-2">
					<span className="text-xl font-bold text-foreground-default">
						Admin Panel
					</span>
					<ReleaseBadge />
				</div>
				<button
					type="button"
					onClick={onClose}
					aria-label="Close sidebar"
					className="p-2 text-foreground-muted hover:text-foreground-default lg:hidden"
				>
					<X className="h-5 w-5" />
				</button>
			</div>
 
			{/* Navigation */}
			<nav className="flex-1 space-y-1 px-3 py-4">
				{navigation.map((item) => {
					const isActive =
						item.href === "/admin"
							? currentPath === "/admin"
							: currentPath.startsWith(item.href);
					return (
						<Link
							key={item.name}
							to={item.href}
							onClick={handleNavClick}
							aria-current={isActive ? "page" : undefined}
							className={cn(
								"group flex items-center rounded-md px-3 py-2 text-sm font-medium transition-colors",
								isActive
									? "bg-primary-100 text-primary-700"
									: "text-foreground-muted hover:bg-background-muted hover:text-foreground-default",
							)}
						>
							<item.icon
								className={cn(
									"mr-3 h-5 w-5 flex-shrink-0",
									isActive
										? "text-primary-600"
										: "text-foreground-subtle group-hover:text-foreground-muted",
								)}
							/>
							{item.name}
						</Link>
					);
				})}
			</nav>
 
			{/* User section */}
			<div className="border-t border-border-default p-4">
				<div className="flex items-center">
					<div className="flex-shrink-0">
						<div className="h-8 w-8 rounded-full bg-primary-100 flex items-center justify-center">
							<span className="text-sm font-medium text-primary-700">
								{user?.name?.charAt(0).toUpperCase() || "A"}
							</span>
						</div>
					</div>
					<div className="ml-3 flex-1 min-w-0">
						<p className="text-sm font-medium text-foreground-default truncate">
							{user?.name}
						</p>
						<p className="text-xs text-foreground-muted truncate">
							{user?.email}
						</p>
					</div>
					<ThemeSwitcher variant="icon" size="sm" className="ml-1" />
					<button
						type="button"
						onClick={signOut}
						className="ml-1 p-1 text-foreground-subtle hover:text-foreground-default transition-colors"
						title="Sign out"
					>
						<LogOut className="h-5 w-5" />
					</button>
				</div>
			</div>
		</div>
	);
}