Skip to content

Commit

Permalink
Use link title based default document titles.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyperkid123 committed Apr 18, 2024
1 parent 830ea14 commit f432935
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/components/RootApp/ScalprumRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { populateNotifications } from '../../redux/actions';
import useTrackPendoUsage from '../../hooks/useTrackPendoUsage';
import ChromeAuthContext from '../../auth/ChromeAuthContext';
import { onRegisterModuleWriteAtom } from '../../state/atoms/chromeModuleAtom';
import useTabName from '../../hooks/useTabName';

const ProductSelection = lazy(() => import('../Stratosphere/ProductSelection'));

Expand Down Expand Up @@ -64,6 +65,8 @@ const ScalprumRoot = memo(
useChromeServiceEvents();
// track pendo usage
useTrackPendoUsage();
// setting default tab title
useTabName();

async function getNotifications() {
try {
Expand Down
31 changes: 31 additions & 0 deletions src/hooks/useTabName.ts
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;

0 comments on commit f432935

Please sign in to comment.