From 6c26113526acf990deb92c4f0331e52732a78553 Mon Sep 17 00:00:00 2001 From: Johnnadel Salita Uy Date: Thu, 18 Jul 2024 11:29:41 +0200 Subject: [PATCH] [MIM-1986] Mim 1986 prepend environments in title (#2827) * Add a processor for environment prepending in title MIM-1986 * Add functionality to prepend environment into title tag in head (wip) * Minor adjustments to header and title regex * Add logging for testing (in test) * Fetch environment from baseUrl in app config * Fix environment string on baseUrl match * Fetch environment string from app config instead * Minor code refactoring * Use optional chain for app config * Move getting environment string functionality to utils * Add environment string to admin tool applications dashboard and bestbet too --- .../admin/tools/bestbet/bestbet.html | 2 +- .../resources/admin/tools/bestbet/bestbet.ts | 4 +++- .../admin/tools/dashboard/dashboard.es6 | 4 +++- src/main/resources/lib/ssb/utils/utils.ts | 10 ++++++++++ .../react4xp/_entries/DashboardEntry.tsx | 4 +++- .../site/processors/prependEnvironment.ts | 20 +++++++++++++++++++ src/main/resources/site/site.xml | 1 + 7 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 src/main/resources/site/processors/prependEnvironment.ts diff --git a/src/main/resources/admin/tools/bestbet/bestbet.html b/src/main/resources/admin/tools/bestbet/bestbet.html index ccafce0f2..239faef3a 100644 --- a/src/main/resources/admin/tools/bestbet/bestbet.html +++ b/src/main/resources/admin/tools/bestbet/bestbet.html @@ -2,7 +2,7 @@ - SSB Best bet + <meta http-equiv="Cache-control" content="NO-CACHE"></meta> <meta name="viewport" content="width=device-width, initial-scale=1"></meta> <link rel="stylesheet" diff --git a/src/main/resources/admin/tools/bestbet/bestbet.ts b/src/main/resources/admin/tools/bestbet/bestbet.ts index f04949a5b..31d126b21 100644 --- a/src/main/resources/admin/tools/bestbet/bestbet.ts +++ b/src/main/resources/admin/tools/bestbet/bestbet.ts @@ -4,7 +4,7 @@ import { localize } from '/lib/xp/i18n' import { getToolUrl } from '/lib/xp/admin' import { render } from '/lib/thymeleaf' import { getMainSubjects } from '/lib/ssb/utils/subjectUtils' -import { parseContributions } from '/lib/ssb/utils/utils' +import { getEnvironmentString, parseContributions } from '/lib/ssb/utils/utils' import { type DropdownItems } from '/lib/types/components' import { render as r4XpRender } from '/lib/enonic/react4xp' @@ -114,10 +114,12 @@ function renderPart(req: XP.Request): XP.Response { } ) + const environmentString = getEnvironmentString() return { body: render(view, { ...getAssets(), pageContributions: parseContributions(bestBetComponent.pageContributions), + title: `${environmentString ? `${environmentString}: ` : ''}SSB Bestbet`, }), } } diff --git a/src/main/resources/admin/tools/dashboard/dashboard.es6 b/src/main/resources/admin/tools/dashboard/dashboard.es6 index 7bdf5f16f..7746b3d81 100644 --- a/src/main/resources/admin/tools/dashboard/dashboard.es6 +++ b/src/main/resources/admin/tools/dashboard/dashboard.es6 @@ -1,7 +1,7 @@ import { assetUrl, serviceUrl } from '/lib/xp/portal' import { getUser, hasRole } from '/lib/xp/auth' import { getToolUrl } from '/lib/xp/admin' -import { parseContributions } from '/lib/ssb/utils/utils' +import { parseContributions, getEnvironmentString } from '/lib/ssb/utils/utils' import { render } from '/lib/thymeleaf' import { renderError } from '/lib/ssb/error/error' @@ -53,6 +53,7 @@ function renderPart(req) { statisticRegister: userHasAdmin, } + const environmentString = getEnvironmentString() const dashboardDataset = new React4xp('DashboardEntry') .setProps({ user, @@ -63,6 +64,7 @@ function renderPart(req) { internalStatbankUrl: `${INTERNAL_STATBANK_URL}`, statregRapportUrl: `${STATREG_RAPPORT_URL}`, toggleDebugging: isEnabled('dashboard-redux-logging-debugging', true, 'ssb'), + title: `${environmentString ? `${environmentString}: ` : ''}SSB Dashboard`, }) .setId('dashboard') diff --git a/src/main/resources/lib/ssb/utils/utils.ts b/src/main/resources/lib/ssb/utils/utils.ts index 287613661..df978c6be 100644 --- a/src/main/resources/lib/ssb/utils/utils.ts +++ b/src/main/resources/lib/ssb/utils/utils.ts @@ -171,6 +171,16 @@ export function scriptAsset(path: string): string { export const XP_RUN_MODE = ''.concat(Java.type('com.enonic.xp.server.RunMode').get()) // ^ check for DEV mode +export function getEnvironmentString(): string { + let environment = '' + if (XP_RUN_MODE === 'DEV') { + environment = XP_RUN_MODE + } else { + environment = app.config?.['ssb.env'] ? app.config['ssb.env'].toUpperCase() : '' + } + return environment +} + interface ContentSearchPageResult { contentId?: string } diff --git a/src/main/resources/react4xp/_entries/DashboardEntry.tsx b/src/main/resources/react4xp/_entries/DashboardEntry.tsx index 341bd17ff..04f99368a 100644 --- a/src/main/resources/react4xp/_entries/DashboardEntry.tsx +++ b/src/main/resources/react4xp/_entries/DashboardEntry.tsx @@ -37,6 +37,7 @@ function Dashboard(props: DashboardProps) { internalBaseUrl={props.internalBaseUrl} internalStatbankUrl={props.internalStatbankUrl} statregRapportUrl={props.statregRapportUrl} + title={props.title} /> </HelmetProvider> </WebsocketProvider> @@ -58,6 +59,7 @@ interface DashboardRouterProps { internalBaseUrl?: string internalStatbankUrl?: string statregRapportUrl?: string + title: string } function DashboardRouter(props: DashboardRouterProps) { @@ -101,7 +103,7 @@ function DashboardRouter(props: DashboardRouterProps) { requestServerTime(dispatch, io) return ( <BrowserRouter> - <Helmet titleTemplate='SSB Dashboard' defaultTitle='SSB Dashboard'></Helmet> + <Helmet titleTemplate={props.title} defaultTitle={props.title}></Helmet> <Switch> <Route path='/' component={HomePage} /> diff --git a/src/main/resources/site/processors/prependEnvironment.ts b/src/main/resources/site/processors/prependEnvironment.ts new file mode 100644 index 000000000..15d85c6d6 --- /dev/null +++ b/src/main/resources/site/processors/prependEnvironment.ts @@ -0,0 +1,20 @@ +import { getEnvironmentString } from '/lib/ssb/utils/utils' + +exports.responseProcessor = (req: XP.Request, res: XP.Response) => { + const environment = getEnvironmentString() + + if (environment) { + // Prepend environment to title tag in head for DEV, UTV, TEST, and QA + const headRegex = /<head>([\s\S]*?)<\/head>/ + const titleRegex = /<title>(.*?)<\/title>/ + + res.body = (res.body as string).replace(headRegex, (match, headContent) => { + const modifiedHeadContent = headContent.replace(titleRegex, (match: string, titleText: string) => { + return match.replace(titleText, `${environment}: ${titleText}`) + }) + return match.replace(headContent, modifiedHeadContent) + }) + } + + return res +} diff --git a/src/main/resources/site/site.xml b/src/main/resources/site/site.xml index 8bec992b4..0f5eb7eaa 100644 --- a/src/main/resources/site/site.xml +++ b/src/main/resources/site/site.xml @@ -148,5 +148,6 @@ <response-processor name="searchableText" order="10"/> <response-processor name="react4xpAssetSource" order="11"/> <response-processor name="browserSync" order="12"/> + <response-processor name="prependEnvironment" order="13"/> </processors> </site>