Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use link title based default document titles. #2820

Merged
merged 2 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
32 changes: 32 additions & 0 deletions src/hooks/useTabName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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 closest link parent
.map(({ title }) => title);
// toReversed is not properly supported in all envs we run this code
fragmentsWithoutBundle.reverse();
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;
Loading