From ee51478618bb65a9de6ffb326cbe6e59acce1aa3 Mon Sep 17 00:00:00 2001 From: Gao Sun Date: Wed, 29 Mar 2023 01:51:14 +0800 Subject: [PATCH] refactor(console): update page title and manually track pv (#3625) --- packages/console/package.json | 2 ++ packages/console/src/App.tsx | 6 ++-- .../console/src/components/PageMeta/index.tsx | 32 +++++++++++++++++ packages/console/src/consts/tenants.ts | 2 ++ .../src/onboarding/pages/About/index.tsx | 2 ++ .../src/onboarding/pages/Congrats/index.tsx | 2 ++ .../pages/SignInExperience/index.tsx | 2 ++ .../src/onboarding/pages/Welcome/index.tsx | 2 ++ .../src/pages/ApiResourceDetails/index.tsx | 2 ++ .../console/src/pages/ApiResources/index.tsx | 2 ++ .../src/pages/ApplicationDetails/index.tsx | 2 ++ .../console/src/pages/Applications/index.tsx | 2 ++ .../src/pages/AuditLogDetails/index.tsx | 2 ++ .../console/src/pages/AuditLogs/index.tsx | 2 ++ .../src/pages/ConnectorDetails/index.tsx | 2 ++ .../console/src/pages/Connectors/index.tsx | 2 ++ .../console/src/pages/Dashboard/index.tsx | 2 ++ .../console/src/pages/GetStarted/index.tsx | 2 ++ packages/console/src/pages/NotFound/index.tsx | 5 +++ packages/console/src/pages/Profile/index.tsx | 2 ++ packages/console/src/pages/Roles/index.tsx | 2 ++ .../SignInExperience/tabs/Branding/index.tsx | 3 ++ .../SignInExperience/tabs/Others/index.tsx | 3 ++ .../tabs/SignUpAndSignIn/index.tsx | 5 +++ .../console/src/pages/UserDetails/index.tsx | 2 ++ packages/console/src/pages/Users/index.tsx | 2 ++ packages/console/src/utils/app-insights.ts | 2 +- packages/console/src/utils/set-title.ts | 9 ----- .../admin-console/api-resource-details.ts | 1 + .../admin-console/api-resources.ts | 1 + .../admin-console/application-details.ts | 1 + .../translation/admin-console/applications.ts | 1 + .../de/translation/admin-console/cloud.ts | 4 +++ .../admin-console/connector-details.ts | 1 + .../translation/admin-console/connectors.ts | 1 + .../de/translation/admin-console/dashboard.ts | 1 + .../translation/admin-console/get-started.ts | 1 + .../translation/admin-console/log-details.ts | 1 + .../de/translation/admin-console/logs.ts | 1 + .../de/translation/admin-console/profile.ts | 1 + .../de/translation/admin-console/roles.ts | 1 + .../translation/admin-console/sign-in-exp.ts | 7 ++-- .../translation/admin-console/user-details.ts | 1 + .../de/translation/admin-console/users.ts | 1 + .../admin-console/api-resource-details.ts | 1 + .../admin-console/api-resources.ts | 1 + .../admin-console/application-details.ts | 1 + .../translation/admin-console/applications.ts | 1 + .../en/translation/admin-console/cloud.ts | 4 +++ .../admin-console/connector-details.ts | 1 + .../translation/admin-console/connectors.ts | 1 + .../en/translation/admin-console/dashboard.ts | 1 + .../translation/admin-console/get-started.ts | 1 + .../translation/admin-console/log-details.ts | 1 + .../en/translation/admin-console/logs.ts | 1 + .../en/translation/admin-console/profile.ts | 1 + .../en/translation/admin-console/roles.ts | 1 + .../translation/admin-console/sign-in-exp.ts | 7 ++-- .../translation/admin-console/user-details.ts | 1 + .../en/translation/admin-console/users.ts | 1 + .../admin-console/api-resource-details.ts | 1 + .../admin-console/api-resources.ts | 1 + .../admin-console/application-details.ts | 1 + .../translation/admin-console/applications.ts | 1 + .../fr/translation/admin-console/cloud.ts | 4 +++ .../admin-console/connector-details.ts | 1 + .../translation/admin-console/connectors.ts | 1 + .../fr/translation/admin-console/dashboard.ts | 1 + .../translation/admin-console/get-started.ts | 1 + .../translation/admin-console/log-details.ts | 1 + .../fr/translation/admin-console/logs.ts | 1 + .../fr/translation/admin-console/profile.ts | 1 + .../fr/translation/admin-console/roles.ts | 1 + .../translation/admin-console/sign-in-exp.ts | 7 ++-- .../translation/admin-console/user-details.ts | 1 + .../fr/translation/admin-console/users.ts | 1 + .../admin-console/api-resource-details.ts | 1 + .../admin-console/api-resources.ts | 1 + .../admin-console/application-details.ts | 1 + .../translation/admin-console/applications.ts | 1 + .../ko/translation/admin-console/cloud.ts | 4 +++ .../admin-console/connector-details.ts | 1 + .../translation/admin-console/connectors.ts | 1 + .../ko/translation/admin-console/dashboard.ts | 1 + .../translation/admin-console/get-started.ts | 1 + .../translation/admin-console/log-details.ts | 1 + .../ko/translation/admin-console/logs.ts | 1 + .../ko/translation/admin-console/profile.ts | 1 + .../ko/translation/admin-console/roles.ts | 1 + .../translation/admin-console/sign-in-exp.ts | 1 + .../translation/admin-console/user-details.ts | 1 + .../ko/translation/admin-console/users.ts | 1 + .../admin-console/api-resource-details.ts | 1 + .../admin-console/api-resources.ts | 1 + .../admin-console/application-details.ts | 1 + .../translation/admin-console/applications.ts | 1 + .../pt-br/translation/admin-console/cloud.ts | 4 +++ .../admin-console/connector-details.ts | 1 + .../translation/admin-console/connectors.ts | 1 + .../translation/admin-console/dashboard.ts | 1 + .../translation/admin-console/get-started.ts | 1 + .../translation/admin-console/log-details.ts | 1 + .../pt-br/translation/admin-console/logs.ts | 3 +- .../translation/admin-console/profile.ts | 1 + .../pt-br/translation/admin-console/roles.ts | 1 + .../translation/admin-console/sign-in-exp.ts | 1 + .../translation/admin-console/user-details.ts | 1 + .../pt-br/translation/admin-console/users.ts | 1 + .../admin-console/api-resource-details.ts | 1 + .../admin-console/api-resources.ts | 1 + .../admin-console/application-details.ts | 1 + .../translation/admin-console/applications.ts | 1 + .../pt-pt/translation/admin-console/cloud.ts | 4 +++ .../admin-console/connector-details.ts | 1 + .../translation/admin-console/connectors.ts | 1 + .../translation/admin-console/dashboard.ts | 1 + .../translation/admin-console/get-started.ts | 1 + .../translation/admin-console/log-details.ts | 1 + .../pt-pt/translation/admin-console/logs.ts | 1 + .../translation/admin-console/profile.ts | 1 + .../pt-pt/translation/admin-console/roles.ts | 1 + .../translation/admin-console/sign-in-exp.ts | 7 ++-- .../translation/admin-console/user-details.ts | 1 + .../pt-pt/translation/admin-console/users.ts | 1 + .../admin-console/api-resource-details.ts | 1 + .../admin-console/api-resources.ts | 1 + .../admin-console/application-details.ts | 1 + .../translation/admin-console/applications.ts | 1 + .../tr-tr/translation/admin-console/cloud.ts | 4 +++ .../admin-console/connector-details.ts | 1 + .../translation/admin-console/connectors.ts | 1 + .../translation/admin-console/dashboard.ts | 1 + .../translation/admin-console/get-started.ts | 1 + .../translation/admin-console/log-details.ts | 1 + .../tr-tr/translation/admin-console/logs.ts | 1 + .../translation/admin-console/profile.ts | 1 + .../tr-tr/translation/admin-console/roles.ts | 1 + .../translation/admin-console/sign-in-exp.ts | 7 ++-- .../translation/admin-console/user-details.ts | 1 + .../tr-tr/translation/admin-console/users.ts | 1 + .../admin-console/api-resource-details.ts | 1 + .../admin-console/api-resources.ts | 1 + .../admin-console/application-details.ts | 1 + .../translation/admin-console/applications.ts | 1 + .../zh-cn/translation/admin-console/cloud.ts | 4 +++ .../admin-console/connector-details.ts | 1 + .../translation/admin-console/connectors.ts | 1 + .../translation/admin-console/dashboard.ts | 1 + .../translation/admin-console/get-started.ts | 1 + .../translation/admin-console/log-details.ts | 1 + .../zh-cn/translation/admin-console/logs.ts | 1 + .../translation/admin-console/profile.ts | 1 + .../zh-cn/translation/admin-console/roles.ts | 1 + .../translation/admin-console/sign-in-exp.ts | 1 + .../translation/admin-console/user-details.ts | 1 + .../zh-cn/translation/admin-console/users.ts | 1 + pnpm-lock.yaml | 34 +++++++++++++++++++ 157 files changed, 294 insertions(+), 29 deletions(-) create mode 100644 packages/console/src/components/PageMeta/index.tsx delete mode 100644 packages/console/src/utils/set-title.ts diff --git a/packages/console/package.json b/packages/console/package.json index 387152b1c..f7fccc6d8 100644 --- a/packages/console/package.json +++ b/packages/console/package.json @@ -46,6 +46,7 @@ "@types/mdx-js__react": "^1.5.5", "@types/react": "^18.0.0", "@types/react-dom": "^18.0.0", + "@types/react-helmet": "^6.1.6", "@types/react-modal": "^3.13.1", "@types/react-syntax-highlighter": "^15.5.1", "buffer": "^5.7.1", @@ -80,6 +81,7 @@ "react-dnd-html5-backend": "^16.0.0", "react-dom": "^18.0.0", "react-dropzone": "^14.2.3", + "react-helmet": "^6.1.0", "react-hook-form": "^7.34.0", "react-hot-toast": "^2.2.0", "react-i18next": "^11.18.3", diff --git a/packages/console/src/App.tsx b/packages/console/src/App.tsx index 6760a9f6a..2e43933f8 100644 --- a/packages/console/src/App.tsx +++ b/packages/console/src/App.tsx @@ -3,6 +3,7 @@ import { LogtoProvider } from '@logto/react'; import { adminConsoleApplicationId, PredefinedScope } from '@logto/schemas'; import { conditionalArray, deduplicate } from '@silverhand/essentials'; import { useContext } from 'react'; +import { Helmet } from 'react-helmet'; import 'overlayscrollbars/styles/overlayscrollbars.css'; import './scss/normalized.scss'; @@ -15,7 +16,7 @@ import CloudApp from '@/cloud/App'; import { cloudApi, getManagementApi, meApi } from '@/consts/resources'; import initI18n from '@/i18n/init'; -import { adminTenantEndpoint } from './consts'; +import { adminTenantEndpoint, mainTitle } from './consts'; import { isCloud } from './consts/cloud'; import ErrorBoundary from './containers/ErrorBoundary'; import TenantAppContainer from './containers/TenantAppContainer'; @@ -23,10 +24,8 @@ import AppConfirmModalProvider from './contexts/AppConfirmModalProvider'; import AppEndpointsProvider from './contexts/AppEndpointsProvider'; import { AppThemeProvider } from './contexts/AppThemeProvider'; import TenantsProvider, { TenantsContext } from './contexts/TenantsProvider'; -import setTitle from './utils/set-title'; void initI18n(); -setTitle(); function Content() { const { tenants, isSettle, currentTenantId } = useContext(TenantsContext); @@ -64,6 +63,7 @@ function Content() { }} > + {!isCloud || isSettle ? ( diff --git a/packages/console/src/components/PageMeta/index.tsx b/packages/console/src/components/PageMeta/index.tsx new file mode 100644 index 000000000..d06088210 --- /dev/null +++ b/packages/console/src/components/PageMeta/index.tsx @@ -0,0 +1,32 @@ +import type { AdminConsoleKey } from '@logto/phrases'; +import { useEffect, useState } from 'react'; +import { Helmet } from 'react-helmet'; +import { useTranslation } from 'react-i18next'; + +import { getAppInsights } from '@/utils/app-insights'; + +type Props = { + titleKey: AdminConsoleKey | AdminConsoleKey[]; + // eslint-disable-next-line react/boolean-prop-naming + trackPageView?: boolean; +}; + +function PageMeta({ titleKey, trackPageView = true }: Props) { + const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' }); + const [pageViewTracked, setPageViewTracked] = useState(false); + const keys = typeof titleKey === 'string' ? [titleKey] : titleKey; + const rawTitle = keys.map((key) => t(key, { lng: 'en' })).join(' - '); + const title = keys.map((key) => t(key)).join(' - '); + + useEffect(() => { + // Only track once for the same page + if (trackPageView && !pageViewTracked) { + getAppInsights()?.trackPageView({ name: rawTitle }); + setPageViewTracked(true); + } + }, [pageViewTracked, rawTitle, trackPageView]); + + return ; +} + +export default PageMeta; diff --git a/packages/console/src/consts/tenants.ts b/packages/console/src/consts/tenants.ts index b24225730..d41e6d31b 100644 --- a/packages/console/src/consts/tenants.ts +++ b/packages/console/src/consts/tenants.ts @@ -17,6 +17,8 @@ const getAdminTenantEndpoint = () => { export const adminTenantEndpoint = getAdminTenantEndpoint(); +export const mainTitle = isCloud ? 'Logto Cloud' : 'Logto Console'; + export const getUserTenantId = () => { if (isCloud) { const segment = window.location.pathname.split('/')[1]; diff --git a/packages/console/src/onboarding/pages/About/index.tsx b/packages/console/src/onboarding/pages/About/index.tsx index fad96e360..c97fae3cf 100644 --- a/packages/console/src/onboarding/pages/About/index.tsx +++ b/packages/console/src/onboarding/pages/About/index.tsx @@ -8,6 +8,7 @@ import Case from '@/assets/images/case.svg'; import Button from '@/components/Button'; import FormField from '@/components/FormField'; import OverlayScrollbar from '@/components/OverlayScrollbar'; +import PageMeta from '@/components/PageMeta'; import TextInput from '@/components/TextInput'; import useUserOnboardingData from '@/onboarding/hooks/use-user-onboarding-data'; import * as pageLayout from '@/onboarding/scss/layout.module.scss'; @@ -55,6 +56,7 @@ function About() { return (
+
diff --git a/packages/console/src/onboarding/pages/Congrats/index.tsx b/packages/console/src/onboarding/pages/Congrats/index.tsx index c22a274b1..d7d2bc3f3 100644 --- a/packages/console/src/onboarding/pages/Congrats/index.tsx +++ b/packages/console/src/onboarding/pages/Congrats/index.tsx @@ -7,6 +7,7 @@ import CongratsImage from '@/assets/images/congrats.svg'; import Button from '@/components/Button'; import Divider from '@/components/Divider'; import OverlayScrollbar from '@/components/OverlayScrollbar'; +import PageMeta from '@/components/PageMeta'; import { TenantsContext } from '@/contexts/TenantsProvider'; import Reservation from '@/onboarding/components/Reservation'; import useUserOnboardingData from '@/onboarding/hooks/use-user-onboarding-data'; @@ -28,6 +29,7 @@ function Congrats() { return (
+
diff --git a/packages/console/src/onboarding/pages/SignInExperience/index.tsx b/packages/console/src/onboarding/pages/SignInExperience/index.tsx index 0593ce40d..8997b56df 100644 --- a/packages/console/src/onboarding/pages/SignInExperience/index.tsx +++ b/packages/console/src/onboarding/pages/SignInExperience/index.tsx @@ -12,6 +12,7 @@ import Button from '@/components/Button'; import ColorPicker from '@/components/ColorPicker'; import FormField from '@/components/FormField'; import OverlayScrollbar from '@/components/OverlayScrollbar'; +import PageMeta from '@/components/PageMeta'; import TextInput from '@/components/TextInput'; import { ImageUploaderField } from '@/components/Uploader'; import useApi from '@/hooks/use-api'; @@ -106,6 +107,7 @@ function SignInExperience() { return (
+
diff --git a/packages/console/src/onboarding/pages/Welcome/index.tsx b/packages/console/src/onboarding/pages/Welcome/index.tsx index 5c49ea386..ef4c71d58 100644 --- a/packages/console/src/onboarding/pages/Welcome/index.tsx +++ b/packages/console/src/onboarding/pages/Welcome/index.tsx @@ -8,6 +8,7 @@ import WelcomeImage from '@/assets/images/sign-in-experience-welcome.svg'; import Button from '@/components/Button'; import FormField from '@/components/FormField'; import OverlayScrollbar from '@/components/OverlayScrollbar'; +import PageMeta from '@/components/PageMeta'; import ActionBar from '@/onboarding/components/ActionBar'; import { CardSelector } from '@/onboarding/components/CardSelector'; import useUserOnboardingData from '@/onboarding/hooks/use-user-onboarding-data'; @@ -52,6 +53,7 @@ function Welcome() { return (
+
diff --git a/packages/console/src/pages/ApiResourceDetails/index.tsx b/packages/console/src/pages/ApiResourceDetails/index.tsx index da827702d..587c6ed97 100644 --- a/packages/console/src/pages/ApiResourceDetails/index.tsx +++ b/packages/console/src/pages/ApiResourceDetails/index.tsx @@ -16,6 +16,7 @@ import Card from '@/components/Card'; import CopyToClipboard from '@/components/CopyToClipboard'; import DeleteConfirmModal from '@/components/DeleteConfirmModal'; import DetailsPage from '@/components/DetailsPage'; +import PageMeta from '@/components/PageMeta'; import TabNav, { TabNavItem } from '@/components/TabNav'; import { ApiResourceDetailsTabs } from '@/consts/page-tabs'; import type { RequestError } from '@/hooks/use-api'; @@ -74,6 +75,7 @@ function ApiResourceDetails() { className={classNames(isOnPermissionPage && styles.permissionPage)} onRetry={mutate} > + {data && ( <> diff --git a/packages/console/src/pages/ApiResources/index.tsx b/packages/console/src/pages/ApiResources/index.tsx index 1a4cc2755..57150804c 100644 --- a/packages/console/src/pages/ApiResources/index.tsx +++ b/packages/console/src/pages/ApiResources/index.tsx @@ -14,6 +14,7 @@ import CardTitle from '@/components/CardTitle'; import CopyToClipboard from '@/components/CopyToClipboard'; import EmptyDataPlaceholder from '@/components/EmptyDataPlaceholder'; import ItemPreview from '@/components/ItemPreview'; +import PageMeta from '@/components/PageMeta'; import Pagination from '@/components/Pagination'; import Table from '@/components/Table'; import { defaultPageSize } from '@/consts'; @@ -60,6 +61,7 @@ function ApiResources() { return (
+