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 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 | 5x 35x 3032x 94x 212x 5x 58x 58x 58x 58x 63x 63x 94x 94x 94x 94x 94x 94x 94x 94x 58x 58x 58x 1x 1x 94x 63x 94x 662x 94x 4x 94x 74x 50x 1x 50x 50x 94x 74x 50x 50x 94x 3x 94x 658x 658x 658x 2561x 658x 2547x 658x 4x 2820x 2820x 2820x | import { Link, useRouterState } from "@tanstack/react-router";
import {
BadgeCheck,
Banknote,
BarChart3,
Bell,
Calculator,
CalendarClock,
ChevronDown,
Contact,
FileText,
FolderKanban,
Gauge,
Handshake,
Home,
Inbox,
LayoutDashboard,
LayoutGrid,
LayoutTemplate,
ListChecks,
LogOut,
type LucideIcon,
MessageCircle,
MessageSquare,
Music,
Search,
Send,
Share2,
Store,
Tags,
TrendingDown,
Upload,
UserPlus,
Users,
UsersRound,
X,
} from "lucide-react";
import { useEffect, useRef, useState } from "react";
import type { AdminNavBadges } from "../../hooks/queries/useAdminNavBadges";
import { useIsMobile } from "../../hooks/useIsMobile";
import { useAuth } from "../../lib/auth-context";
import { cn } from "../../lib/utils";
import { ReleaseBadge } from "../ui/release-badge";
import { ThemeSwitcher } from "../ui/theme-switcher";
/**
* Admin navigation, grouped into sections.
*
* This was a flat list of 24 links, which is past the point where anyone
* scans it — you hunt. The grouping is not decoration: each section is a
* different *job*, and the order is by how often a section is opened, not
* alphabetical. Overview and Professionals are daily; Platform is monthly.
*
* RRM is one section rather than five more entries. It is a time-boxed
* campaign, so it sits third while it is running and can drop — or be deleted
* outright — when it is not.
*
* Note what is deliberately NOT together: Messaging → WhatsApp is the
* product's transactional sender (OTP, inquiry notifications) on the
* production number. RRM sends recruitment marketing from a *different*
* number. Filing them in one section would invite exactly the mistake the
* whole design forbids — cold marketing leaving the number that carries every
* user's login OTP.
*/
type NavItem = {
name: string;
href: string;
icon: LucideIcon;
/**
* Match the path exactly instead of by prefix. Needed where an item's href
* is a prefix of its siblings' — `/admin/rrm` would otherwise stay lit on
* every RRM page, so "Overview" would never look like a distinct place.
*/
exact?: boolean;
/** Which live count to show, if any. See NAV_BADGES. */
badge?: "rrmInbox" | "rrmTasks" | "rrmSignups";
};
type NavSection = { section: string; items: NavItem[] };
const navigation: NavSection[] = [
{
section: "Overview",
items: [
{ name: "Dashboard", href: "/admin", icon: LayoutDashboard, exact: true },
{ name: "Analytics", href: "/admin/analytics", icon: BarChart3 },
],
},
{
section: "Professionals",
items: [
{ name: "Pros", href: "/admin/pros", icon: Store },
{ name: "Projects", href: "/admin/projects", icon: FolderKanban },
{ name: "Pro imports", href: "/admin/imports", icon: Upload },
],
},
{
section: "Partner recruitment",
items: [
{ name: "Overview", href: "/admin/rrm", icon: Gauge, exact: true },
{ name: "Prospects", href: "/admin/rrm/prospects", icon: UsersRound },
// People who came to us through the landing page. They are also in
// Prospects, but buried among 455 scraped names — and a warm inbound
// is the one row the operator most needs to find quickly. The badge
// is a backlog count (unreplied), not an SLA breach like the inbox —
// neutral styling, not the error tone.
{
name: "Sign-ups",
href: "/admin/rrm/signups",
icon: UserPlus,
badge: "rrmSignups",
},
// The two counts that decay if nobody looks. Inbox backlog over 24h
// auto-pauses the campaign (P1-0.10), and unconfirmed intents are
// what the 14-day gate is measured on — so both surface here rather
// than requiring a click to discover.
{
name: "Inbox",
href: "/admin/rrm/inbox",
icon: Inbox,
badge: "rrmInbox",
},
// ── The partner PROGRAMME, below the recruitment screens above.
// Recruitment is about getting agents to join; these are about what
// they produce once they have. The queue leads because it is the
// only screen with someone waiting on the other end of it.
{
name: "Verify referrals",
href: "/admin/rrm/verify",
icon: BadgeCheck,
},
{ name: "Referrals", href: "/admin/rrm/referrals", icon: Share2 },
{ name: "Partners", href: "/admin/rrm/partners", icon: Handshake },
{ name: "Payouts", href: "/admin/rrm/payouts", icon: Banknote },
{
name: "Referral funnel",
href: "/admin/rrm/funnel",
icon: TrendingDown,
},
{ name: "Ramp & pacing", href: "/admin/rrm/ramp", icon: Contact },
{
name: "Tasks",
href: "/admin/rrm/tasks",
icon: ListChecks,
badge: "rrmTasks",
},
],
},
{
section: "Marketplace",
items: [
{ name: "Home page", href: "/admin/home-page", icon: Home },
{
name: "Room categories",
href: "/admin/room-categories",
icon: LayoutGrid,
},
{
name: "Cost calculator",
href: "/admin/calculator-config",
icon: Calculator,
},
{ name: "Taxonomy", href: "/admin/taxonomy", icon: Tags },
{
name: "Consultations",
href: "/admin/consultations",
icon: CalendarClock,
},
{ name: "Blogs", href: "/admin/blogs", icon: FileText },
{ name: "Search", href: "/admin/search", icon: Search },
],
},
{
section: "Messaging",
items: [
{ name: "WhatsApp", href: "/admin/whatsapp", icon: MessageCircle },
{ name: "Communications", href: "/admin/communications", icon: Send },
{
name: "Notification test",
href: "/admin/notifications/test",
icon: Bell,
},
],
},
{
section: "Social studio",
items: [
{
name: "Music library",
href: "/admin/social-studio/music",
icon: Music,
},
{
name: "Templates",
href: "/admin/social-studio/templates",
icon: LayoutTemplate,
},
],
},
{
section: "Platform",
items: [
{ name: "Users", href: "/admin/users", icon: Users },
{ name: "Feedback", href: "/admin/feedback", icon: MessageSquare },
],
},
];
/** Flat view, for tests and for anything that needs every destination. */
export const ADMIN_NAV_ITEMS: NavItem[] = navigation.flatMap((g) => g.items);
function isActiveItem(item: NavItem, currentPath: string): boolean {
return item.exact
? currentPath === item.href
: currentPath.startsWith(item.href);
}
/** The section that owns the current page, if any. */
function sectionForPath(currentPath: string): string | undefined {
return navigation.find((g) =>
g.items.some((item) => isActiveItem(item, currentPath)),
)?.section;
}
/**
* Sections collapse so the rail fits on one screen without scrolling.
*
* Which sections are open is resolved in two layers: an explicit choice the
* operator made by clicking a header (remembered across visits), and
* underneath that, "the section I am currently in". Only choices are stored —
* the active section is derived — so visiting every section over a week does
* not slowly re-expand the whole rail. Navigating into a section you had
* collapsed forgets that one choice, because a nav that hides the page you
* are on has stopped being a map.
*/
export const SIDEBAR_SECTIONS_STORAGE_KEY = "admin-sidebar:sections";
type SectionChoices = Record<string, boolean>;
function readSectionChoices(): SectionChoices {
try {
const raw = localStorage.getItem(SIDEBAR_SECTIONS_STORAGE_KEY);
const parsed: unknown = raw ? JSON.parse(raw) : {};
return parsed && typeof parsed === "object"
? (parsed as SectionChoices)
: {};
} catch {
return {};
}
}
function writeSectionChoices(choices: SectionChoices): void {
try {
localStorage.setItem(SIDEBAR_SECTIONS_STORAGE_KEY, JSON.stringify(choices));
} catch {
// Private mode or storage disabled: the rail still works, it just
// stops remembering.
}
}
type AdminSidebarProps = {
isOpen: boolean;
onClose: () => void;
/**
* Live counts, supplied by AdminLayout.
*
* Passed in rather than fetched here on purpose: this component renders on
* every admin page, and calling a query hook inside it would both couple a
* presentational component to React Query — breaking every test that
* renders it without a provider — and poll the RRM funnel from screens
* that have nothing to do with the campaign.
*/
badges?: AdminNavBadges;
};
export function AdminSidebar({
isOpen,
onClose,
badges = {},
}: AdminSidebarProps) {
const { signOut, user, hasProAccess } = useAuth();
const routerState = useRouterState();
const currentPath = routerState.location.pathname;
const sidebarRef = useRef<HTMLDivElement>(null);
const activeSection = sectionForPath(currentPath);
const [choices, setChoices] = useState<SectionChoices>(readSectionChoices);
// Collapsing exists to make the DESKTOP rail fit on one screen. On a phone
// the drawer covers the screen anyway, and a fold there only adds a second
// tap before every destination — so the drawer always shows everything.
const isMobile = useIsMobile();
// Arriving in a section you had collapsed drops that one choice, so the
// active page is never hidden. Other choices are untouched.
useEffect(() => {
Iif (!activeSection) return;
setChoices((prev) => {
if (prev[activeSection] !== false) return prev;
const { [activeSection]: _dropped, ...rest } = prev;
return rest;
});
}, [activeSection]);
useEffect(() => {
writeSectionChoices(choices);
}, [choices]);
const isSectionOpen = (section: string) =>
choices[section] ?? section === activeSection;
const toggleSection = (section: string) =>
setChoices((prev) => ({ ...prev, [section]: !isSectionOpen(section) }));
// 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 overflow-y-auto px-3 py-4">
{navigation.map((group) => {
const open = isMobile || isSectionOpen(group.section);
const listId = `admin-nav-${group.section
.toLowerCase()
.replace(/[^a-z0-9]+/g, "-")}`;
// A collapsed section must not swallow its counts — the badges
// exist so a backlog is visible WITHOUT a click. Roll them up
// onto the header while the items are hidden.
const rolledUp = open
? 0
: group.items.reduce(
(sum, item) =>
sum + (item.badge ? (badges[item.badge] ?? 0) : 0),
0,
);
const rolledUpIsUrgent =
!open &&
group.items.some(
(item) => item.badge === "rrmInbox" && (badges.rrmInbox ?? 0) > 0,
);
return (
<div key={group.section} className="mb-2 last:mb-0">
<button
type="button"
onClick={() => toggleSection(group.section)}
aria-expanded={open}
aria-controls={listId}
data-testid={`admin-nav-section-${group.section}`}
className={cn(
// A real control, not a caption: 40px tall, readable text,
// a hover surface, and a state you can tell apart at a glance.
"flex min-h-10 w-full items-center justify-between rounded-md px-3 py-2 text-left text-sm font-semibold transition-colors",
"hover:bg-background-muted focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-primary-500",
open
? "text-foreground-default"
: "text-foreground-muted hover:text-foreground-default",
group.section === activeSection && "text-primary-700",
)}
>
<span>{group.section}</span>
<span className="flex items-center gap-1.5">
{rolledUp > 0 && (
<span
data-testid={`admin-nav-section-badge-${group.section}`}
className={cn(
"min-w-5 rounded-full px-1.5 py-0.5 text-center text-[10px] font-semibold normal-case tracking-normal tabular-nums",
rolledUpIsUrgent
? "bg-notification-error-bg text-notification-error-text"
: "bg-background-muted text-foreground-muted",
)}
>
{rolledUp}
<span className="sr-only"> waiting</span>
</span>
)}
{/* The fold is a desktop affordance; the drawer never folds. */}
{!isMobile && (
<ChevronDown
aria-hidden="true"
className={cn(
"h-4 w-4 text-foreground-subtle transition-transform duration-200",
open ? "" : "-rotate-90",
)}
/>
)}
</span>
</button>
<div id={listId} hidden={!open} className="space-y-1">
{group.items.map((item) => {
const isActive = isActiveItem(item, currentPath);
const count = item.badge ? badges[item.badge] : undefined;
return (
<Link
key={item.href}
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",
)}
/>
<span className="flex-1">{item.name}</span>
{/* Count is announced, not just coloured — a backlog that only
reads as red is invisible in greyscale and to a screen reader. */}
{count !== undefined && count > 0 && (
<span
data-testid={`admin-nav-badge-${item.badge}`}
className={cn(
"ml-2 min-w-5 rounded-full px-1.5 py-0.5 text-center text-[11px] font-semibold tabular-nums",
item.badge === "rrmInbox"
? "bg-notification-error-bg text-notification-error-text"
: "bg-background-muted text-foreground-muted",
)}
>
{count}
<span className="sr-only"> waiting</span>
</span>
)}
</Link>
);
})}
</div>
</div>
);
})}
{/* Pro Portal link — only visible to dual-role users who also have Pro access */}
{hasProAccess && (
<>
<hr className="my-4 border-t border-border-default" />
<Link
to="/"
onClick={handleNavClick}
className="group flex items-center rounded-md px-3 py-2 text-sm font-medium transition-colors text-secondary-600 hover:bg-secondary-50 hover:text-secondary-700"
>
<Home className="mr-3 h-5 w-5 flex-shrink-0 text-secondary-400 group-hover:text-secondary-600" />
Pro Portal
</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>
);
}
|