All files / hooks useDocumentTitle.ts

78.57% Statements 11/14
100% Branches 2/2
60% Functions 3/5
78.57% Lines 11/14

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                        1x     1x                         1x                                                                                               1x 9x     1x             26x 666x 24x       2x         1x                                
// src/hooks/useDocumentTitle.ts
import {useEffect} from 'react';
import {matchPath, useLocation} from 'react-router-dom';
import {LEGAL_DOC_LIST} from '../content/legal';
 
/**
 * Brand suffix appended to every page title.
 *
 * Sourced from VITE_APP_NAME so non-production builds keep their environment
 * marker — "Sign In · FlowState Capital (test)" — which is how a reviewer
 * tells one deployment's tab from another's.
 */
const BRAND: string = import.meta.env.VITE_APP_NAME;
 
/** Shown when the path matches no known route, i.e. the 404 page. */
const NOT_FOUND_TITLE = 'Page Not Found';
 
/**
 * Route pattern -> page name.
 *
 * Match order matters: the first hit wins, so a static path must be listed
 * before any parameterised pattern that would also match it — "/loans/request"
 * before "/loans/:loanId", which would otherwise capture "request" as an id.
 *
 * Every route in App.tsx should appear here. One that doesn't will fall
 * through to "Page Not Found" in the tab while rendering normally, so add an
 * entry alongside any new route.
 */
const STATIC_TITLES: readonly (readonly [pattern: string, name: string])[] = [
	// Public
	['/', 'Business Treasury & Structured Liquidity'],
	['/login', 'Sign In'],
	['/register', 'Create an Account'],
	['/forgot-password', 'Forgot Password'],
	['/reset-password', 'Reset Password'],
	['/confirm-email', 'Confirm Email Change'],
	['/faq', 'Frequently Asked Questions'],
	['/support', 'Support'],
 
	// Investor
	['/dashboard', 'Dashboard'],
	['/accounts', 'Accounts'],
	['/profile', 'Profile'],
	['/settings/notifications', 'Notification Preferences'],
	['/settings/communications', 'Communication Preferences'],
	['/documents', 'Documents'],
	['/contact', 'Contact Us'],
	['/emails', 'Email History'],
	['/loans/request', 'Request a Loan'],
	['/loans/payment', 'Make a Payment'],
	['/loans/:loanId', 'Loan Details'],
	['/loans', 'Loans'],
 
	// Admin
	['/admin/investors/:investorId', 'Investor Details'],
	['/admin/loans/:loanId', 'Loan Review'],
	['/admin/loans', 'Loan Administration'],
	['/admin/disbursements', 'Disbursements'],
	['/admin/funding-requests', 'Funding Requests'],
	['/admin/transactions', 'Transactions'],
	['/admin/document-reviews', 'Document Reviews'],
	['/admin/configuration', 'Configuration'],
	['/admin', 'Admin Dashboard'],
 
	// Super admin
	['/super-admin/test-data', 'Test Data'],
	['/super-admin/error-logs', 'Error Logs'],
	['/super-admin/settings', 'System Settings'],
	['/super-admin/interest-projection', 'Interest Projection'],
	['/super-admin/users', 'User Management']
];
 
/**
 * Legal routes take their titles straight from the generated content registry,
 * so a title counsel revises flows through without a second edit here.
 */
const LEGAL_TITLES: readonly (readonly [string, string])[] = LEGAL_DOC_LIST.map(
	(doc) => [`/${doc.slug}`, doc.title] as const
);
 
const ROUTE_TITLES: readonly (readonly [string, string])[] = [
	...STATIC_TITLES,
	...LEGAL_TITLES
];
 
/** The page name for a path, or the 404 name when nothing matches. */
export function resolvePageName(pathname: string): string {
	for (const [pattern, name] of ROUTE_TITLES) {
		if (matchPath(pattern, pathname) !== null) {
			return name;
		}
	}
 
	return NOT_FOUND_TITLE;
}
 
/** The full document title for a path, e.g. "Sign In · FlowState Capital". */
export function buildDocumentTitle(pathname: string): string {
	return `${resolvePageName(pathname)} · ${BRAND}`;
}
 
/**
 * Keeps document.title in step with the current route (FSC-165).
 *
 * Previously main.tsx set the title once at boot, so every page in the app —
 * including each legal document — shared the single generic VITE_APP_NAME.
 */
export function useDocumentTitle(): void {
	const {pathname} = useLocation();
 
	useEffect(() => {
		document.title = buildDocumentTitle(pathname);
	}, [pathname]);
}