Skip to content

Commit 8d867c6

Browse files
Add environment banner (#202)
* Add environment banner * Detect environment
1 parent 8a76a03 commit 8d867c6

File tree

8 files changed

+106
-1
lines changed

8 files changed

+106
-1
lines changed

app.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const routes = require('./app/routes')
3333
const exampleTemplatesRoutes = require('./lib/example_templates_routes')
3434
const authentication = require('./lib/middleware/authentication')
3535
const automaticRouting = require('./lib/middleware/auto-routing')
36+
const environmentMiddleware = require('./app/lib/middleware/environment')
3637
const production = require('./lib/middleware/production')
3738
const prototypeAdminRoutes = require('./lib/middleware/prototype-admin-routes')
3839
const utils = require('./lib/utils')
@@ -241,6 +242,9 @@ if (!sessionDataDefaultsFileExists) {
241242
// Local variables
242243
app.use(locals(config))
243244

245+
// Environment banner
246+
app.use(environmentMiddleware)
247+
244248
// View engine
245249
app.set('view engine', 'html')
246250
exampleTemplatesApp.set('view engine', 'html')
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Stolen (with thanks) from https://github.com/nhsuk/manage-vaccinations-in-schools-prototype/blob/c294744d6fba958f1ee9a4d51ab4ef8e425d893d/app/assets/stylesheets/components/_environment.scss
2+
3+
// @use "../vendor/nhsuk-frontend" as *;
4+
@use "nhsuk-frontend/dist/nhsuk/core/settings" as *;
5+
@use "nhsuk-frontend/dist/nhsuk/core/tools" as *;
6+
7+
.app-environment {
8+
@include nhsuk-font-size(14);
9+
@include nhsuk-responsive-padding(2, bottom);
10+
@include nhsuk-responsive-padding(2, top);
11+
12+
.nhsuk-link {
13+
color: inherit;
14+
}
15+
16+
.nhsuk-width-container {
17+
align-items: baseline;
18+
display: flex;
19+
flex-wrap: wrap;
20+
gap: nhsuk-spacing(1) nhsuk-spacing(2);
21+
}
22+
}

app/assets/sass/main.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ $nhsuk-page-width: 1260px;
2020
@forward "components/status-bar";
2121
@forward "components/summary-card";
2222
@forward "components/reading";
23+
@forward "components/environment";
2324

2425
@forward "components/overrides";
2526
@forward "components/checkboxes";

app/lib/middleware/environment.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// app/lib/middleware/environment.js
2+
3+
/**
4+
* Format a link as HTML
5+
*
6+
* @param {string} url - The URL to link to
7+
* @param {string} text - The link text
8+
* @returns {string} HTML anchor tag
9+
*/
10+
const formatLink = (url, text) => {
11+
return `<a href="${url}">${text}</a>`
12+
}
13+
14+
/**
15+
* Environment middleware
16+
* Sets res.locals.environment based on the current environment
17+
* Supports: development, production, and review (for Heroku PRs)
18+
*/
19+
const environment = (req, res, next) => {
20+
const { HEROKU_BRANCH, HEROKU_PR_NUMBER, NODE_ENV } = process.env
21+
22+
let environment = 'development' // Default to development
23+
24+
if (NODE_ENV === 'production') {
25+
environment = 'production'
26+
}
27+
28+
// Review apps on Heroku override the above
29+
if (HEROKU_PR_NUMBER || HEROKU_BRANCH) {
30+
environment = 'review'
31+
}
32+
33+
const pullRequestUrl =
34+
HEROKU_PR_NUMBER &&
35+
`https://github.com/NHSDigital/manage-breast-screening-prototype/pull/${HEROKU_PR_NUMBER}`
36+
37+
const environments = {
38+
development: {
39+
colour: 'white',
40+
name: 'Prototype',
41+
text: 'This is a prototype in development.'
42+
},
43+
production: {
44+
colour: 'grey',
45+
name: 'Prototype',
46+
text: 'This is a prototype for research.'
47+
},
48+
review: {
49+
colour: 'purple',
50+
name: HEROKU_PR_NUMBER ? `Prototype PR ${HEROKU_PR_NUMBER}` : 'Prototype',
51+
html: HEROKU_PR_NUMBER
52+
? `This is a prototype for review. ${formatLink(pullRequestUrl, 'View pull request')}`
53+
: 'This is a prototype for review.'
54+
}
55+
}
56+
57+
res.locals.environment = environments[environment]
58+
59+
next()
60+
}
61+
62+
module.exports = environment
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{%- macro appEnvironment(params) -%}
2+
{%- include "./template.njk" -%}
3+
{% endmacro %}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{%- from "nhsuk/components/tag/macro.njk" import tag -%}
2+
3+
{%- set colour = params.colour or "blue" -%}
4+
<div class="app-environment nhsuk-tag--{{ colour }}">
5+
<div class="nhsuk-width-container">
6+
{{ tag({
7+
classes: "nhsuk-tag--" + colour,
8+
text: params.name
9+
}) if params.name }}
10+
{{ params.html | safe if params.html else params.text }}
11+
</div>
12+
</div>

app/views/_templates/layout-base.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
{% endblock %}
1717

1818
{% block header %}
19-
19+
{{ appEnvironment(environment) if environment }}
2020
{% set useMinimalNav = useMinimalNav | default(false) %}
2121

2222
{% set rootHref = "/" %}

app/views/_templates/layout.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
{%- from '_components/summary-list/macro.njk' import appSummaryList as summaryList %}
2323
{%- from '_components/status-bar/macro.njk' import appStatusBar %}
2424
{%- from "_components/stepper-input/macro.njk" import appStepperInput -%}
25+
{%- from "_components/environment/macro.njk" import appEnvironment -%}
2526

2627
{% block head %}
2728
<!-- Add your custom CSS or Sass in /app/assets/sass/main.scss -->

0 commit comments

Comments
 (0)