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

feat(core): 19315 implement dynamic titles #836

Merged
merged 1 commit into from
Sep 18, 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
2 changes: 2 additions & 0 deletions src/core/localization/translations/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,9 @@
"modes": {
"map": "Map",
"about": "About",
"cookies": "Cookies",
"reports": "Reports",
"report": "Report",
"profile": "Profile",
"privacy": "Privacy",
"terms": "Terms"
Expand Down
21 changes: 13 additions & 8 deletions src/core/router/atoms/currentRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@ export const currentRouteAtom = createAtom(
const routesConfig = get('availableRoutesAtom');
if (routesConfig === null) return null;
const location = get('currentLocationAtom');
const normalizedLocation = stripBasename(location.pathname, configRepo.get().baseUrl);

const matchedRoute = routesConfig.routes.find((route) => {
const path = getAbsoluteRoute(route);
return matchPath({ path, exact: true }, normalizedLocation);
});

if (matchedRoute) {
return matchedRoute;
}

// fallback to default route
return (
routesConfig.routes.find((route) => {
const path = getAbsoluteRoute(route);
const normalizedLocation = stripBasename(
location.pathname,
configRepo.get().baseUrl,
);
return matchPath({ path, exact: true }, normalizedLocation);
}) ?? null
routesConfig.routes.find((route) => route.id === routesConfig.defaultRoute) ?? null
);
},
'currentRouteAtom',
Expand Down
5 changes: 5 additions & 0 deletions src/core/router/components/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ globalThis.addEventListener(NAVIGATE_EVENT, ((e: CustomEvent) => {
routerInstance.navigate(getAbsoluteRoute(slug) + globalThis.location.search);
}) as EventListener);

// update Title
currentRouteAtom.v3atom.onChange((ctx, route) => {
document.title = `${configRepo.get().name} - ${route?.title || ''}`;
});

export function Router() {
return <RouterProvider router={routerInstance} />;
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/router/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const routerConfig: AppRouterConfig = {
{
id: 'report',
slug: ':reportId',
title: 'modes.report',
title: i18n.t('modes.report'),
icon: <Reports16 />,
view: <ReportPage />,
requiredFeature: AppFeature.REPORTS,
Expand Down Expand Up @@ -112,7 +112,7 @@ export const routerConfig: AppRouterConfig = {
{
id: 'cookies',
slug: 'cookies',
title: 'modes.cookies',
title: i18n.t('modes.cookies'),
icon: <Reports16 />,
view: <PagesDocument doc={[{ type: 'md', url: 'cookies.md' }]} key="cookies" />,
parentRouteId: 'about',
Expand Down
2 changes: 1 addition & 1 deletion src/core/router/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface AppRoute {
view: JSX.Element;
icon: JSX.Element;
/** How to name this route in navigation */
title: string | JSX.Element;
title: string;
/** Redirect to this route users that see it first time */
showForNewUsers?: boolean;
/** Should we unmount (false) or just hide (true) route when it deactivated */
Expand Down
7 changes: 0 additions & 7 deletions src/utils/hooks/useTabNameUpdate.tsx

This file was deleted.

2 changes: 0 additions & 2 deletions src/views/CommonView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { useAtom } from '@reatom/react-v2';
import { configRepo } from '~core/config';
import { OriginalLogo } from '~components/KonturLogo/KonturLogo';
import { CookieConsentBanner } from '~features/cookie_consent_banner';
import { useTabNameUpdate } from '~utils/hooks/useTabNameUpdate';
import { featureFlagsAtom, FeatureFlag } from '~core/shared_state';
import { FullScreenLoader } from '~components/LoadingSpinner/LoadingSpinner';
import s from './CommonView.module.css';
Expand All @@ -26,7 +25,6 @@ export function CommonView({
getAbsoluteRoute: (path: string | AppRoute) => string;
}>) {
const [featureFlags] = useAtom(featureFlagsAtom);
useTabNameUpdate(configRepo.get().name);

useEffect(() => {
if (featureFlags[FeatureFlag.INTERCOM]) {
Expand Down
Loading