📦 EqualifyEverything / equalify-dashboard

📄 announcer.tsx · 57 lines
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
57import { useEffect, useRef } from 'react';
import { useLocation, matchPath } from 'react-router-dom';

const routeNames: Record<string, string> = {
 '/reports': 'Reports',
  '/reports/create': 'Create Report',
  '/reports/:reportId': 'Report Details',
  '/reports/:reportId/edit': 'Edit Report',
  '/reports/:reportId/messages/:messageId': 'Message Details',
  '/reports/:reportId/tags/:tagId': 'Tag Details',
  '/reports/:reportId/pages/:pageId': 'Page Details',
  '/account': 'Account',
  '/scans': 'Scans',
  '/properties': 'Properties',
  '/properties/add': 'Add Property',
  '/properties/:propertyId/edit': 'Edit Property',
  '/login': 'Login',
  '/signup': 'Signup',
  '/forgot': 'Forgot Password',
  '/reset': 'Reset Password',
};


const getPageName = (pathname: string) => {
  for (const [pattern, name] of Object.entries(routeNames)) {
    if (matchPath(pattern, pathname)) {
      return name;
    }
  }
  return 'Reports';
};


const Announcer: React.FC = () => {
  const location = useLocation();
  const announcer = useRef<HTMLDivElement>(null);

  useEffect(() => {
    const pageName = getPageName(location.pathname);
    if (announcer.current) {
      announcer.current.textContent = `${pageName} page loaded`;
      announcer.current.focus({ preventScroll: true });
    }
  }, [location]);

  return (
    <div
    ref={announcer}
    tabIndex={-1}
      id="page-announcer"
      className="sr-only"
    />
  );
};

export default Announcer;