All files / src/components/layout sidebar.tsx

100% Statements 33/33
94.59% Branches 70/74
100% Functions 10/10
100% Lines 30/30

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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349                                                                              7x                                 88x 88x 88x 88x 88x   3x 88x 88x 88x                   88x   88x         88x 88x 57x 2x   57x 57x       88x 88x   57x 57x       88x   4x     88x                                                               680x     675x     675x                                                                                                                                                                                                                                               1x 1x                                                                                                                                                                        
import { useEffect, useRef } from "react";
import { Link, useRouterState } from "@tanstack/react-router";
import {
	Home,
	FolderKanban,
	User,
	Users,
	LogOut,
	X,
	Shield,
	BarChart3,
	FileText,
	Globe,
	Settings,
	Contact,
	Bell,
	Clapperboard,
	Store,
	ExternalLink,
	MessageSquare,
} from "lucide-react";
import { useUnreadCount } from "../../hooks/queries/useNotificationQueries";
import { useSocialDrafts } from "../../hooks/queries/useSocialDraftQueries";
import { SidebarPrompts } from "./sidebar-prompts";
import { cn } from "../../lib/utils";
import { useAuth } from "../../lib/auth-context";
import { usePro } from "../../lib/pro-context";
import { getImageUrl } from "../../lib/api/base";
import { MARKETPLACE_URL } from "../../lib/env";
import { LogoIcon } from "../ui/logo-icon";
import { ReleaseBadge } from "../ui/release-badge";
 
type NavItem = {
	name: string;
	href: string;
	icon: typeof Home;
	roles: string[];
};
 
const navigation: NavItem[] = [
	{ name: "Dashboard", href: "/", icon: Home, roles: ["owner", "manager", "staff"] },
	{ name: "Analytics", href: "/analytics", icon: BarChart3, roles: ["owner", "manager"] },
	{ name: "Profile", href: "/profile", icon: User, roles: ["owner", "manager"] },
	{ name: "Projects", href: "/projects", icon: FolderKanban, roles: ["owner", "manager", "staff"] },
	{ name: "CRM", href: "/crm", icon: Contact, roles: ["owner", "manager", "staff"] },
	{ name: "Team", href: "/team", icon: Users, roles: ["owner", "manager"] },
	{ name: "Website", href: "/website", icon: Globe, roles: ["owner"] },
	{ name: "Blog", href: "/blogs", icon: FileText, roles: ["owner", "manager"] },
];
 
type SidebarProps = {
	isOpen: boolean;
	onClose: () => void;
};
 
export function Sidebar({ isOpen, onClose }: SidebarProps) {
	const { signOut, user, isAdmin, hasProAccess } = useAuth();
	const { proRole, pro, proId, isLoading: isProLoading } = usePro();
	const { data: unreadCount = 0 } = useUnreadCount();
	const { data: socialDraftsData } = useSocialDrafts(proId ?? null);
	const socialBadgeCount =
		(socialDraftsData?.pendingCount ?? 0) +
		(socialDraftsData?.drafts.filter((d) => d.status === "ready").length ?? 0);
	const routerState = useRouterState();
	const currentPath = routerState.location.pathname;
	const sidebarRef = useRef<HTMLDivElement>(null);
 
	// #425: desktop sidebar had no path back to the public marketplace. The
	// link deep-links to the pro's own listing (not the marketplace homepage)
	// because the use case is "let me check how MY profile looks" — landing on
	// the homepage forces the pro to hunt for themselves. Plain <a> because the
	// marketplace is a separate Astro app. Only shown after a super-admin
	// publishes the pro AND the pro has a slug — same gate idiom used by
	// website.tsx (`isProPublished && pro?.slug`). Wait for pro data to settle
	// before deciding to render so the link doesn't flash in after first paint.
	const isProPublished = pro?.status === "published";
	const marketplaceProfileUrl =
		!isProLoading && isProPublished && pro?.slug
			? `${MARKETPLACE_URL}/pros/${pro.slug}`
			: 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 = () => {
		// Close sidebar on mobile when navigating
		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-1.5">
					<LogoIcon size={36} className="text-foreground-default" />
					<span className="text-xl font-bold text-foreground-default">
						Interioring
					</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 flex-col flex-1 space-y-1 px-3 py-4">
				{/* Pro navigation - filtered by role (admins see all items) */}
				{hasProAccess &&
					navigation
						.filter((item) => isAdmin || (proRole && item.roles.includes(proRole)))
						.map((item) => {
						const isActive =
							item.href === "/"
								? currentPath === "/"
								: 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-muted group-hover:text-foreground-default",
									)}
								/>
								{item.name}
							</Link>
						);
					})}
 
				{/* Social Studio */}
				{hasProAccess && (
					<Link
						to="/social-studio"
						onClick={handleNavClick}
						aria-current={currentPath.startsWith("/social-studio") ? "page" : undefined}
						className={cn(
							"group flex items-center rounded-md px-3 py-2 text-sm font-medium transition-colors",
							currentPath.startsWith("/social-studio")
								? "bg-primary-100 text-primary-700"
								: "text-foreground-muted hover:bg-background-muted hover:text-foreground-default",
						)}
					>
						<Clapperboard
							className={cn(
								"mr-3 h-5 w-5 flex-shrink-0",
								currentPath.startsWith("/social-studio")
									? "text-primary-600"
									: "text-foreground-muted group-hover:text-foreground-default",
							)}
						/>
						Social Studio
						{socialBadgeCount > 0 && (
							<span className="ml-auto bg-primary-600 text-white text-[10px] font-bold px-1.5 py-0.5 rounded-full min-w-[18px] text-center leading-tight">
								{socialBadgeCount > 99 ? "99+" : socialBadgeCount}
							</span>
						)}
					</Link>
				)}
 
				{/* Admin link - only visible to admins */}
				{isAdmin && (
					<>
						{hasProAccess && (
							<div className="my-4 border-t border-border-default" />
						)}
						<Link
							to="/admin"
							onClick={handleNavClick}
							aria-current={currentPath.startsWith("/admin") ? "page" : undefined}
							className={cn(
								"group flex items-center rounded-md px-3 py-2 text-sm font-medium transition-colors",
								currentPath.startsWith("/admin")
									? "bg-secondary-100 text-secondary-700"
									: "text-secondary-600 hover:bg-secondary-50 hover:text-secondary-700",
							)}
						>
							<Shield
								className={cn(
									"mr-3 h-5 w-5 flex-shrink-0",
									currentPath.startsWith("/admin")
										? "text-secondary-600"
										: "text-secondary-400 group-hover:text-secondary-600",
								)}
							/>
							Admin Panel
						</Link>
					</>
				)}
 
				{/* Spacer */}
				<div className="flex-1" />
 
				{/* Marketplace — deep-link to the pro's public listing (#425).
				   External anchor, not a TanStack <Link>, because the
				   marketplace is a separate Astro app. New tab so pros can peek
				   at their public profile and return to in-flight work in the
				   portal. Hidden when the pro is unpublished or has no slug —
				   either case means there's no public profile URL to link to. */}
				{hasProAccess && marketplaceProfileUrl && (
					<a
						href={marketplaceProfileUrl}
						target="_blank"
						rel="noopener noreferrer"
						className="group flex items-center rounded-md px-3 py-2 text-sm font-medium text-foreground-muted hover:bg-background-muted hover:text-foreground-default transition-colors"
					>
						<Store className="mr-3 h-5 w-5 flex-shrink-0 text-foreground-muted group-hover:text-foreground-default" />
						Marketplace
						<ExternalLink className="ml-auto h-4 w-4 text-foreground-muted/60" />
					</a>
				)}
 
				{/* Push + Install prompts */}
				<SidebarPrompts />
 
				{/* Feedback (#591): desktop sidebar entry that opens the existing
				   FeedbackButton form via the same "open-feedback" event the
				   mobile MoreSheet uses. The floating button alone was reported
				   as undiscoverable — this gives pros a labelled surface in the
				   nav without duplicating the form component. */}
				{hasProAccess && (
					<button
						type="button"
						onClick={() => {
							handleNavClick();
							window.dispatchEvent(new CustomEvent("open-feedback"));
						}}
						className="group flex w-full items-center rounded-md px-3 py-2 text-sm font-medium text-foreground-muted hover:bg-background-muted hover:text-foreground-default transition-colors"
					>
						<MessageSquare className="mr-3 h-5 w-5 flex-shrink-0 text-foreground-muted group-hover:text-foreground-default" />
						Feedback
					</button>
				)}
 
				{/* Notifications */}
				<Link
					to="/notifications"
					onClick={handleNavClick}
					aria-current={currentPath.startsWith("/notifications") ? "page" : undefined}
					className={cn(
						"group flex items-center rounded-md px-3 py-2 text-sm font-medium transition-colors",
						currentPath.startsWith("/notifications")
							? "bg-primary-100 text-primary-700"
							: "text-foreground-muted hover:bg-background-muted hover:text-foreground-default",
					)}
				>
					<Bell
						className={cn(
							"mr-3 h-5 w-5 flex-shrink-0",
							currentPath.startsWith("/notifications")
								? "text-primary-600"
								: "text-foreground-muted group-hover:text-foreground-default",
						)}
					/>
					Notifications
					{unreadCount > 0 && (
						<span className="ml-auto bg-error text-white text-[10px] font-bold px-1.5 py-0.5 rounded-full min-w-[18px] text-center leading-tight">
							{unreadCount > 99 ? "99+" : unreadCount}
						</span>
					)}
				</Link>
			</nav>
 
			{/* User section */}
			<div className="border-t border-border-default p-4">
				<div className="flex items-center">
					<Link
						to="/settings"
						onClick={handleNavClick}
						className="flex items-center flex-1 min-w-0 group rounded-md -m-1 p-1 hover:bg-background-muted transition-colors"
					>
						<div className="flex-shrink-0">
							{user?.image ? (
								<img
									src={getImageUrl(user.image)}
									alt=""
									className="h-8 w-8 rounded-full object-cover"
								/>
							) : (
								<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() || "U"}
									</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>
						<Settings className="h-4 w-4 text-foreground-muted group-hover:text-foreground-default flex-shrink-0" />
					</Link>
					<button
						type="button"
						onClick={signOut}
						className="ml-1 p-1 text-foreground-muted hover:text-foreground-default transition-colors"
						title="Sign out"
					>
						<LogOut className="h-5 w-5" />
					</button>
				</div>
			</div>
		</div>
	);
}