All files / src/components/layout nav-config.ts

100% Statements 83/83
98.38% Branches 61/62
100% Functions 11/11
100% Lines 64/64

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                                                                                                                                                                                                                    8x                                                                                                                                                                                                                 8x                                                                                                                                                                                                                     8x                   8x     1266x 1229x 1209x   227x 215x         1035x 964x 41x 1035x 32x 32x         68x 68x 657x 385x 385x   272x 272x   68x       52x   68x                     68x 68x 725x 272x 272x   453x 231x 228x 813x 813x   228x 228x   222x 222x   68x         281x 281x 2688x 1798x 937x 937x   861x   281x         3226x                           233x 233x 3548x   3381x 3226x 231x   233x               99x 99x 97x 703x 238x 745x   238x   60x    
/**
 * The navigation, as DATA.
 *
 * Adding a destination is a config line here, not a renderer. Before this
 * module the pro rail rendered its nav SIX ways in one file (only the first
 * of them data-driven) and the admin rail was a second 393-line file with its
 * own copy of the badge machinery. One `<NavRail>` reads this.
 *
 * Shape notes, all of them load-bearing:
 *
 * - **No `exact` flag.** Which row is current is resolved by LONGEST matching
 *   href (`resolveActiveHref`), segment-aware. That kills the whole class of
 *   prefix collisions the old flag existed to patch — `/settings/account` no
 *   longer lights on `/settings/security`, the admin `/admin` row no longer
 *   lights on `/admin/pros`, and `/` is naturally exact because nothing can be
 *   a segment-prefix of the root.
 * - **No icons.** The Job Book rail is text and figures: a row is a label and
 *   a right-aligned tabular count. The mockup (`.context/mockups/shell.html`)
 *   draws no icons in the rail, so the config carries none. The mobile bottom
 *   nav does have them, and owns its own icon map.
 * - **Counts are a key, not a number.** An entry declares *that* it can carry
 *   a count (`badge: "leads"`); the layout passes whatever counts it actually
 *   has. A key with no data renders no count — so `today` and `leads` are
 *   declared (they are in the mockup) without this module inventing a query
 *   for them. Wire them up where the data lands, not here.
 */
 
/** Which counted things a row can show. The layout supplies the numbers. */
export type NavBadgeKey = "today" | "leads" | "studio" | "notifications";
 
/** Counts by key. Absent or <= 0 renders nothing. */
export type NavBadges = Partial<Record<NavBadgeKey, number>>;
 
/** An href that is only known at runtime (it depends on fetched data). */
export type NavDynamicHref = "marketplaceProfile";
 
/** A non-navigating row. The rail fires it; it never changes the URL. */
export type NavActionId = "open-feedback";
 
/** Gates beyond the pro role, for the two cross-rail links. */
export type NavVisibility = "isAdmin" | "hasProAccess";
 
type NavGate = {
	/** Pro roles that may see this. Undefined = every role. Admins see all. */
	roles?: string[];
	when?: NavVisibility;
};
 
export type NavLink = NavGate & {
	kind: "link";
	name: string;
	href: string;
	badge?: NavBadgeKey;
};
 
export type NavExternalLink = NavGate & {
	kind: "external";
	name: string;
	dynamic: NavDynamicHref;
};
 
export type NavActionItem = NavGate & {
	kind: "action";
	name: string;
	action: NavActionId;
};
 
export type NavGroup = NavGate & {
	kind: "group";
	name: string;
	/**
	 * `section` is a `.label` head (caps, tabular) over indented children —
	 * SELL, SHOW, OVERVIEW. `submenu` is an item-styled disclosure that sits
	 * in the flow of ordinary rows: `Settings ▾` is one row, not four.
	 */
	style: "section" | "submenu";
	items: NavItem[];
};
 
/** A hairline. Not a box. Dropped when it would lead, trail or double up. */
export type NavDivider = { kind: "divider" };
 
export type NavItem = NavLink | NavExternalLink | NavActionItem;
export type NavEntry = NavItem | NavGroup | NavDivider;
 
/** Same as `NavExternalLink` once its runtime href is known. */
export type ResolvedExternalLink = Omit<NavExternalLink, "dynamic"> & {
	href: string;
};
export type ResolvedItem = NavLink | ResolvedExternalLink | NavActionItem;
export type ResolvedGroup = Omit<NavGroup, "items"> & {
	items: ResolvedItem[];
};
export type ResolvedEntry = ResolvedItem | ResolvedGroup | NavDivider;
 
export type NavContext = {
	proRole?: string | null;
	isAdmin?: boolean;
	hasProAccess?: boolean;
	hrefs?: Partial<Record<NavDynamicHref, string | null>>;
};
 
/**
 * The pro rail. Five rows for a solo owner: Today, Leads, the four SHOW rows
 * it cares about, the three utility rows, and `Settings ▾` closed.
 */
export const PRO_NAV: NavEntry[] = [
	{
		kind: "link",
		name: "Today",
		href: "/",
		badge: "today",
		when: "hasProAccess",
	},
	{ kind: "divider" },
	{
		kind: "group",
		name: "Sell",
		style: "section",
		when: "hasProAccess",
		items: [{ kind: "link", name: "Leads", href: "/leads", badge: "leads" }],
	},
	{ kind: "divider" },
	{
		kind: "group",
		name: "Show",
		style: "section",
		when: "hasProAccess",
		items: [
			{ kind: "link", name: "Projects", href: "/projects" },
			{ kind: "link", name: "Website", href: "/website", roles: ["owner"] },
			{
				kind: "link",
				name: "Blog",
				href: "/blog",
				roles: ["owner", "manager"],
			},
			{
				kind: "link",
				name: "Studio",
				href: "/studio",
				badge: "studio",
			},
			{
				kind: "link",
				name: "Insights",
				href: "/insights",
				roles: ["owner", "manager"],
			},
		],
	},
	{ kind: "divider" },
	{
		kind: "link",
		name: "Notifications",
		href: "/notifications",
		badge: "notifications",
	},
	{
		kind: "action",
		name: "Feedback",
		action: "open-feedback",
		when: "hasProAccess",
	},
	{
		kind: "external",
		name: "Marketplace",
		dynamic: "marketplaceProfile",
		when: "hasProAccess",
	},
	{
		kind: "group",
		name: "Settings",
		style: "submenu",
		items: [
			{
				kind: "link",
				name: "Studio profile",
				href: "/settings/studio",
				roles: ["owner", "manager"],
			},
			{ kind: "link", name: "Account", href: "/settings/account" },
			{ kind: "link", name: "Security", href: "/settings/security" },
			{
				kind: "link",
				name: "Team",
				href: "/settings/team",
				roles: ["owner", "manager"],
			},
			{
				kind: "link",
				name: "Pipeline & sources",
				href: "/settings/pipeline",
				roles: ["owner", "manager"],
			},
		],
	},
	{ kind: "divider" },
	{ kind: "link", name: "Admin panel", href: "/admin", when: "isAdmin" },
];
 
/**
 * The admin rail: 21 destinations across 6 sections, down from 33 — the whole
 * 12-item "Partner recruitment" section went with the partner programme in
 * phase 1.
 *
 * Plan §1.8 states 21 and names 20, and the mockup draws the 20 it names with
 * "Social studio assets" as one placeholder row. There is no
 * `/admin/social-studio` index route for that row to point at, so the two real
 * routes are listed as two rows — which is also what makes the count 21.
 */
export const ADMIN_NAV: NavEntry[] = [
	{
		kind: "group",
		name: "Overview",
		style: "section",
		items: [
			{ kind: "link", name: "Dashboard", href: "/admin" },
			{ kind: "link", name: "Analytics", href: "/admin/analytics" },
			{
				kind: "link",
				name: "Leadership metrics",
				href: "/internal/metrics",
			},
		],
	},
	{ kind: "divider" },
	{
		kind: "group",
		name: "Supply",
		style: "section",
		items: [
			{ kind: "link", name: "Pros", href: "/admin/pros" },
			{ kind: "link", name: "Projects", href: "/admin/projects" },
			{ kind: "link", name: "Pro imports", href: "/admin/imports" },
		],
	},
	{ kind: "divider" },
	{
		kind: "group",
		name: "Demand",
		style: "section",
		items: [
			{ kind: "link", name: "Web leads", href: "/admin/web-leads" },
			{ kind: "link", name: "Consultations", href: "/admin/consultations" },
		],
	},
	{ kind: "divider" },
	{
		kind: "group",
		name: "Marketplace",
		style: "section",
		items: [
			{ kind: "link", name: "Home page", href: "/admin/home-page" },
			{
				kind: "link",
				name: "Room categories",
				href: "/admin/room-categories",
			},
			{ kind: "link", name: "Taxonomy", href: "/admin/taxonomy" },
			{
				kind: "link",
				name: "Cost calculator",
				href: "/admin/calculator-config",
			},
			{ kind: "link", name: "Blogs", href: "/admin/blogs" },
			{ kind: "link", name: "Search", href: "/admin/search" },
		],
	},
	{ kind: "divider" },
	{
		kind: "group",
		name: "Messaging",
		style: "section",
		items: [
			{ kind: "link", name: "WhatsApp", href: "/admin/whatsapp" },
			{
				kind: "link",
				name: "Communications",
				href: "/admin/communications",
			},
			{
				kind: "link",
				name: "Notification test",
				href: "/admin/notifications/test",
			},
		],
	},
	{ kind: "divider" },
	{
		kind: "group",
		name: "Platform",
		style: "section",
		items: [
			{ kind: "link", name: "Users & roles", href: "/admin/users" },
			{ kind: "link", name: "Feedback", href: "/admin/feedback" },
			{
				kind: "link",
				name: "Studio music",
				href: "/admin/social-studio/music",
			},
			{
				kind: "link",
				name: "Studio templates",
				href: "/admin/social-studio/templates",
			},
		],
	},
	{ kind: "divider" },
	{ kind: "link", name: "Pro portal", href: "/", when: "hasProAccess" },
];
 
/**
 * Which sections are folded is remembered per rail.
 *
 * The admin rail keeps its original key so operators do not lose the folds
 * they already set; the pro rail is new and gets a new one.
 */
export const NAV_STORAGE_KEYS = {
	pro: "nav-rail:sections:pro",
	admin: "admin-sidebar:sections",
} as const;
 
/**
 * The four fixed mobile slots (plan §1.9). The same four for everyone,
 * because there is one role shape in scope — no supervisor rail, no
 * procurement rail. Everything else is behind More.
 */
export const BOTTOM_NAV_HREFS = ["/", "/leads", "/projects"] as const;
 
function isVisible(gate: NavGate, ctx: NavContext): boolean {
	if (gate.when === "isAdmin" && !ctx.isAdmin) return false;
	if (gate.when === "hasProAccess" && !ctx.hasProAccess) return false;
	if (!gate.roles) return true;
	// Admins see every pro row, exactly as the old rail did.
	if (ctx.isAdmin) return true;
	return !!ctx.proRole && gate.roles.includes(ctx.proRole);
}
 
/** An item with its runtime href filled in, or null if it has nowhere to go. */
function resolveItem(item: NavItem, ctx: NavContext): ResolvedItem | null {
	if (!isVisible(item, ctx)) return null;
	if (item.kind !== "external") return item;
	const href = ctx.hrefs?.[item.dynamic];
	if (!href) return null;
	const { dynamic: _dynamic, ...rest } = item;
	return { ...rest, href };
}
 
/** Leading, trailing and doubled-up hairlines are noise. Drop them. */
function collapseDividers(entries: ResolvedEntry[]): ResolvedEntry[] {
	const out: ResolvedEntry[] = [];
	for (const entry of entries) {
		if (entry.kind !== "divider") {
			out.push(entry);
			continue;
		}
		const previous = out[out.length - 1];
		if (previous && previous.kind !== "divider") out.push(entry);
	}
	while (
		out.length > 0 &&
		(out[out.length - 1] as ResolvedEntry).kind === "divider"
	) {
		out.pop();
	}
	return out;
}
 
/**
 * Config + who is looking → what the rail renders. Pure, so the gating rules
 * are testable without a DOM.
 */
export function resolveNav(
	entries: NavEntry[],
	ctx: NavContext,
): ResolvedEntry[] {
	const resolved: ResolvedEntry[] = [];
	for (const entry of entries) {
		if (entry.kind === "divider") {
			resolved.push(entry);
			continue;
		}
		if (entry.kind === "group") {
			if (!isVisible(entry, ctx)) continue;
			const items = entry.items
				.map((item) => resolveItem(item, ctx))
				.filter((item): item is ResolvedItem => item !== null);
			// A head with nothing under it is a lie about where you can go.
			if (items.length > 0) resolved.push({ ...entry, items });
			continue;
		}
		const item = resolveItem(entry, ctx);
		if (item) resolved.push(item);
	}
	return collapseDividers(resolved);
}
 
/** Every navigable row, groups flattened, in rail order. */
export function flattenNav(entries: ResolvedEntry[]): ResolvedItem[] {
	const out: ResolvedItem[] = [];
	for (const entry of entries) {
		if (entry.kind === "divider") continue;
		if (entry.kind === "group") {
			out.push(...entry.items);
			continue;
		}
		out.push(entry);
	}
	return out;
}
 
/** Segment-aware prefix match. `/blog` does not match `/blog-roundup`. */
function matchesPath(href: string, path: string): boolean {
	return path === href || path.startsWith(`${href}/`);
}
 
/**
 * The current row, resolved by LONGEST match.
 *
 * Longest-match is what lets the config drop the old `exact` flag: on
 * `/admin/pros` both `/admin` and `/admin/pros` match, and the deeper one
 * wins, so exactly one row is ever current.
 */
export function resolveActiveHref(
	entries: ResolvedEntry[],
	path: string,
): string | null {
	let best: string | null = null;
	for (const item of flattenNav(entries)) {
		if (item.kind === "action") continue;
		// An external link leaves the app; it is never "the page you are on".
		if (item.kind === "external") continue;
		if (!matchesPath(item.href, path)) continue;
		Eif (best === null || item.href.length > best.length) best = item.href;
	}
	return best;
}
 
/** The group that owns the current page, if any. Used to derive the fold. */
export function groupForPath(
	entries: ResolvedEntry[],
	path: string,
): string | undefined {
	const active = resolveActiveHref(entries, path);
	if (active === null) return undefined;
	for (const entry of entries) {
		if (entry.kind !== "group") continue;
		const owns = entry.items.some(
			(item) => item.kind === "link" && item.href === active,
		);
		if (owns) return entry.name;
	}
	return undefined;
}