|
1 | | -import { useEffect, useRef, useState } from 'react'; |
| 1 | +import { useState, useEffect, useRef } from 'react'; |
2 | 2 | import { isAdministrator, useAuth } from '@/useAuth'; |
3 | | -import { |
4 | | - Bars3Icon, |
5 | | - BuildingOffice2Icon, |
6 | | - HomeIcon |
7 | | -} from '@heroicons/react/24/solid'; |
| 3 | +import { Bars3Icon, BuildingOffice2Icon } from '@heroicons/react/24/solid'; |
8 | 4 | import ULIComponent from '@/Components/ULIComponent.tsx'; |
9 | | -import { Facility } from '@/common'; |
10 | | -import { useLoaderData } from 'react-router-dom'; |
| 5 | +import { Facility, TitleHandler } from '@/common'; |
| 6 | +import { useMatches, useLoaderData } from 'react-router-dom'; |
11 | 7 | import API from '@/api/api'; |
12 | | -import { usePathValue } from '@/Context/PathValueCtx'; |
| 8 | + |
| 9 | +let setGlobalPageTitle: (newTitle: string) => void; |
13 | 10 |
|
14 | 11 | export default function PageNav({ |
15 | | - path, |
16 | 12 | showOpenMenu, |
17 | 13 | onShowNav |
18 | 14 | }: { |
19 | | - path: string[]; |
20 | 15 | showOpenMenu: boolean; |
21 | 16 | onShowNav?: () => void; |
22 | 17 | }) { |
23 | 18 | const { user } = useAuth(); |
24 | 19 | const detailsRef = useRef<HTMLDetailsElement>(null); |
25 | | - const { pathVal } = usePathValue(); |
26 | | - const [customPath, setCustomPath] = useState<string[]>(path ?? []); |
27 | 20 | const facilityNames = useLoaderData() as Facility[] | null; |
28 | | - |
29 | | - useEffect(() => { |
30 | | - const handlePathChange = () => { |
31 | | - const newPath = [...path]; |
32 | | - if (newPath && newPath.length > 0) { |
33 | | - setCustomPath( |
34 | | - newPath.map((p) => { |
35 | | - return ( |
36 | | - pathVal?.find((pv) => pv.path_id === p)?.value ?? p |
37 | | - ); |
38 | | - }) |
39 | | - ); |
40 | | - return; |
41 | | - } |
42 | | - setCustomPath(path); |
43 | | - }; |
44 | | - handlePathChange(); |
45 | | - }, [path, pathVal]); |
| 21 | + const matches = useMatches(); |
| 22 | + const currentRoute = matches[matches.length - 1]; |
| 23 | + const pageTitle = (currentRoute?.handle as TitleHandler)?.title; |
| 24 | + const [globalPageTitle, _setGlobalPageTitle] = useState<string>( |
| 25 | + pageTitle || 'Library Viewer' |
| 26 | + ); |
| 27 | + setGlobalPageTitle = _setGlobalPageTitle; |
46 | 28 |
|
47 | 29 | useEffect(() => { |
48 | 30 | const closeDropdown = ({ target }: MouseEvent) => { |
@@ -72,40 +54,25 @@ export default function PageNav({ |
72 | 54 |
|
73 | 55 | return ( |
74 | 56 | <div className="px-6 py-3 flex justify-between items-center"> |
75 | | - <div className="breadcrumbs"> |
76 | | - <ul> |
77 | | - {showOpenMenu ? ( |
78 | | - <li> |
79 | | - <ULIComponent |
80 | | - onClick={() => { |
81 | | - if (onShowNav) onShowNav(); |
82 | | - }} |
83 | | - icon={Bars3Icon} |
84 | | - iconClassName={'cursor-pointer'} |
85 | | - /> |
86 | | - </li> |
87 | | - ) : ( |
88 | | - <li> |
89 | | - <ULIComponent |
90 | | - onClick={() => { |
91 | | - if (onShowNav) onShowNav(); |
92 | | - }} |
93 | | - icon={Bars3Icon} |
94 | | - iconClassName={'lg:hidden cursor-pointer'} |
95 | | - /> |
96 | | - <ULIComponent |
97 | | - icon={HomeIcon} |
98 | | - iconClassName={'hidden lg:block'} |
99 | | - /> |
100 | | - </li> |
101 | | - )} |
102 | | - |
103 | | - {customPath?.map((p) => ( |
104 | | - <li className="capitalize body" key={p}> |
105 | | - {p} |
106 | | - </li> |
107 | | - ))} |
108 | | - </ul> |
| 57 | + <div className="flex items-center gap-3"> |
| 58 | + {showOpenMenu ? ( |
| 59 | + <ULIComponent |
| 60 | + onClick={onShowNav} |
| 61 | + icon={Bars3Icon} |
| 62 | + iconClassName="cursor-pointer" |
| 63 | + /> |
| 64 | + ) : ( |
| 65 | + <ULIComponent |
| 66 | + onClick={onShowNav} |
| 67 | + icon={Bars3Icon} |
| 68 | + iconClassName="lg:hidden cursor-pointer" |
| 69 | + /> |
| 70 | + )} |
| 71 | + <h1> |
| 72 | + {pageTitle == 'Library Viewer' |
| 73 | + ? globalPageTitle |
| 74 | + : pageTitle} |
| 75 | + </h1> |
109 | 76 | </div> |
110 | 77 | {user && isAdministrator(user) ? ( |
111 | 78 | <ul className="menu menu-horizontal px-1"> |
@@ -148,3 +115,4 @@ export default function PageNav({ |
148 | 115 | </div> |
149 | 116 | ); |
150 | 117 | } |
| 118 | +export { setGlobalPageTitle }; |
0 commit comments