-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use link title based default document titles.
- Loading branch information
1 parent
830ea14
commit f432935
Showing
2 changed files
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { useAtomValue } from 'jotai'; | ||
import { useLocation } from 'react-router-dom'; | ||
|
||
import { activeModuleAtom } from '../state/atoms/activeModuleAtom'; | ||
import useBreadcrumbsLinks from './useBreadcrumbsLinks'; | ||
import useBundle from './useBundle'; | ||
import { useEffect, useMemo } from 'react'; | ||
|
||
const useTabName = () => { | ||
const { bundleTitle } = useBundle(); | ||
const activeModule = useAtomValue(activeModuleAtom); | ||
const { pathname } = useLocation(); | ||
const fragments = useBreadcrumbsLinks(); | ||
|
||
const title = useMemo(() => { | ||
const fragmentsWithoutBundle = fragments | ||
.slice(1) // remove the bundle | ||
.slice(-2) // limit to first parent | ||
.map(({ title }) => title) | ||
.toReversed(); // get the right order of fragments | ||
return `${fragmentsWithoutBundle.join(' - ')} | ${bundleTitle}`; | ||
}, [activeModule, pathname, fragments]); | ||
useEffect(() => { | ||
// sometimes the nav files are not loaded yet and the first section is empty | ||
if (title.split(' | ')?.[0].length > 1) { | ||
document.title = title; | ||
} | ||
}, [title]); | ||
}; | ||
|
||
export default useTabName; |