All files / pages/public NotFoundPage.tsx

100% Statements 11/11
50% Branches 1/2
100% Functions 4/4
100% Lines 11/11

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                                5x 5x 5x 5x 5x   5x 5x                         5x 5x   5x   5x                                                                    
// src/pages/public/NotFoundPage.tsx
import {type ReactElement, useEffect} from 'react';
import {Link as RouterLink, useLocation} from 'react-router-dom';
import {Box, Button, Stack, Typography} from '@mui/material';
import LegalPageShell from '../../components/layout/LegalPageShell';
import {selectIsAuthenticated} from '../../features/auth/authSlice';
import {useAppSelector} from '../../store/hooks';
 
/**
 * Tells crawlers not to index this page for as long as it is on screen.
 *
 * A single-page app answers every URL with HTTP 200, so without this a
 * mistyped or retired link becomes a "soft 404" that search engines will
 * happily index.
 */
function useNoIndex(): void {
	useEffect(() => {
		const meta = document.createElement('meta');
		meta.name = 'robots';
		meta.content = 'noindex';
		document.head.appendChild(meta);
 
		return () => {
			document.head.removeChild(meta);
		};
	}, []);
}
 
/**
 * Shown for any URL that matches no route (FSC-167).
 *
 * Unknown paths used to redirect silently to the landing page, which left a
 * visitor who mistyped a URL — or followed a stale link — believing they had
 * arrived somewhere real, with the address bar quietly rewritten.
 */
export default function NotFoundPage(): ReactElement {
	const {pathname} = useLocation();
	const isAuthenticated = useAppSelector(selectIsAuthenticated);
 
	useNoIndex();
 
	return (
		<LegalPageShell title="Page Not Found">
			<Typography variant="body1" sx={{color: 'text.secondary', mb: 2}}>
				We couldn&apos;t find a page at{' '}
				<Box component="code" sx={{px: 0.75, py: 0.25, borderRadius: 0.5, backgroundColor: 'action.hover', wordBreak: 'break-all'}}>
					{pathname}
				</Box>
				.
			</Typography>
			<Typography variant="body1" sx={{color: 'text.secondary', mb: 3}}>
				The address may have been mistyped, or the page may have moved since the
				link was created.
			</Typography>
 
			<Stack direction={{xs: 'column', sm: 'row'}} spacing={2}>
				<Button component={RouterLink} to="/" variant="contained">
					Go to Homepage
				</Button>
				{isAuthenticated ? (
					<Button component={RouterLink} to="/dashboard" variant="outlined">
						Go to Dashboard
					</Button>
				) : (
					<Button component={RouterLink} to="/login" variant="outlined">
						Sign In
					</Button>
				)}
				<Button component={RouterLink} to="/support" variant="text">
					Contact Support
				</Button>
			</Stack>
		</LegalPageShell>
	);
}