Skip to content

Commit

Permalink
[MIM-1986] Mim 1986 prepend environments in title (#2827)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
johnnadeluy authored and Carl-OW committed Jul 24, 2024
1 parent c879d8b commit 6c26113
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/main/resources/admin/tools/bestbet/bestbet.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html data-th-lang="no">
<head>
<meta charset="UTF-8"></meta>
<title>SSB Best bet</title>
<title data-th-utext="${title}" />
<meta http-equiv="Cache-control" content="NO-CACHE"></meta>
<meta name="viewport" content="width=device-width, initial-scale=1"></meta>
<link rel="stylesheet"
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/admin/tools/bestbet/bestbet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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`,
}),
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/admin/tools/dashboard/dashboard.es6
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -53,6 +53,7 @@ function renderPart(req) {
statisticRegister: userHasAdmin,
}

const environmentString = getEnvironmentString()
const dashboardDataset = new React4xp('DashboardEntry')
.setProps({
user,
Expand All @@ -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')

Expand Down
10 changes: 10 additions & 0 deletions src/main/resources/lib/ssb/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/react4xp/_entries/DashboardEntry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function Dashboard(props: DashboardProps) {
internalBaseUrl={props.internalBaseUrl}
internalStatbankUrl={props.internalStatbankUrl}
statregRapportUrl={props.statregRapportUrl}
title={props.title}
/>
</HelmetProvider>
</WebsocketProvider>
Expand All @@ -58,6 +59,7 @@ interface DashboardRouterProps {
internalBaseUrl?: string
internalStatbankUrl?: string
statregRapportUrl?: string
title: string
}

function DashboardRouter(props: DashboardRouterProps) {
Expand Down Expand Up @@ -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} />
Expand Down
20 changes: 20 additions & 0 deletions src/main/resources/site/processors/prependEnvironment.ts
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions src/main/resources/site/site.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>

0 comments on commit 6c26113

Please sign in to comment.