Files
web/apps/scandic-web/components/EnvironmentWatermark/index.tsx
Joakim Jäderberg 1ba94ff70a Merged in chore/fix-semver (pull request #3393)
Chore/fix semver

* fix semver versioning

* fix semver versioning

* Merge branch 'chore/fix-semver' of bitbucket.org:scandic-swap/web into chore/fix-semver

* Merge branch 'master' of bitbucket.org:scandic-swap/web into chore/fix-semver

* reset to master


Approved-by: Linus Flood
2026-01-07 10:49:14 +00:00

81 lines
1.8 KiB
TypeScript

"use client"
import { cva } from "class-variance-authority"
import { env } from "@/env/client"
import styles from "./index.module.css"
const variants = cva(styles.environmentWatermark, {
variants: {
environment: {
"pre-prod": styles.preprod,
stage: styles.stage,
test: styles.test,
development: styles.development,
},
},
})
const namedEnvironments = ["pre-prod", "stage", "test", "development"] as const
export function EnvironmentWatermark() {
if (
!env.NEXT_PUBLIC_SENTRY_ENVIRONMENT ||
env.NEXT_PUBLIC_SENTRY_ENVIRONMENT.localeCompare("production") === 0
) {
return null
}
const { environment, name } = getEnvironmentName()
const displayValues: string[] = [environment]
if (env.NEXT_PUBLIC_RELEASE_TAG) {
displayValues.push(`[${env.NEXT_PUBLIC_RELEASE_TAG}]`)
} else if (name !== environment) {
displayValues.push(`(${name})`)
}
return (
<div className={variants({ environment: environment })}>
<span>{displayValues.join(" ")}</span>
</div>
)
}
function getEnvironmentName(): {
name: string
environment: (typeof namedEnvironments)[number]
} {
const environment = getEnvironmentType()
const hostname =
typeof window !== "undefined" ? window.location.hostname : undefined
if (hostname === "localhost") {
return { name: "development", environment }
}
if (hostname?.endsWith("netlify.app")) {
const featureBranch = hostname.includes("--")
? hostname.split("--").at(0)
: null
if (featureBranch) {
return {
name: `${featureBranch.slice(0, 10)}`,
environment,
}
}
}
return { name: environment, environment }
}
function getEnvironmentType() {
const environment =
namedEnvironments.find(
(e) => e.localeCompare(env.NEXT_PUBLIC_SENTRY_ENVIRONMENT) === 0
) ?? "test"
return environment
}