Merged in feature/SW-2320-languagebased-hide-for-next-release (pull request #1937)
Language based alternative to HIDE_FOR_NEXT_RELEASE Approved-by: Anton Gunnarsson
This commit is contained in:
29
apps/scandic-web/env/client.ts
vendored
29
apps/scandic-web/env/client.ts
vendored
@@ -1,18 +1,24 @@
|
||||
import { createEnv } from "@t3-oss/env-nextjs"
|
||||
import { z } from "zod"
|
||||
|
||||
export const env = createEnv({
|
||||
import { getLiveStatus } from "./getLiveStatus"
|
||||
import { isLangLive } from "./isLangLive"
|
||||
|
||||
import type { Lang } from "@/constants/languages"
|
||||
|
||||
const _env = createEnv({
|
||||
client: {
|
||||
NEXT_PUBLIC_NODE_ENV: z.enum(["development", "test", "production"]),
|
||||
NEXT_PUBLIC_PORT: z.string().default("3000"),
|
||||
NEXT_PUBLIC_SENTRY_ENVIRONMENT: z.string().default("development"),
|
||||
NEXT_PUBLIC_SENTRY_CLIENT_SAMPLERATE: z.coerce.number().default(0.001),
|
||||
NEXT_PUBLIC_HIDE_FOR_NEXT_RELEASE: z
|
||||
NEXT_PUBLIC_NEW_SITE_LIVE_FOR_LANGS: z
|
||||
.string()
|
||||
// only allow "true" or "false"
|
||||
.refine((s) => s === "true" || s === "false")
|
||||
// transform to boolean
|
||||
.transform((s) => s === "true"),
|
||||
.regex(/^([a-z]{2},)*([a-z]{2}){0,1}$/)
|
||||
.transform((val) => {
|
||||
return val.split(",")
|
||||
})
|
||||
.default(""),
|
||||
NEXT_PUBLIC_PUBLIC_URL: z.string().optional(),
|
||||
},
|
||||
emptyStringAsUndefined: true,
|
||||
@@ -22,8 +28,15 @@ export const env = createEnv({
|
||||
NEXT_PUBLIC_SENTRY_ENVIRONMENT: process.env.NEXT_PUBLIC_SENTRY_ENVIRONMENT,
|
||||
NEXT_PUBLIC_SENTRY_CLIENT_SAMPLERATE:
|
||||
process.env.NEXT_PUBLIC_SENTRY_CLIENT_SAMPLERATE,
|
||||
NEXT_PUBLIC_HIDE_FOR_NEXT_RELEASE:
|
||||
process.env.NEXT_PUBLIC_HIDE_FOR_NEXT_RELEASE,
|
||||
NEXT_PUBLIC_NEW_SITE_LIVE_FOR_LANGS:
|
||||
process.env.NEXT_PUBLIC_NEW_SITE_LIVE_FOR_LANGS,
|
||||
NEXT_PUBLIC_PUBLIC_URL: process.env.NEXT_PUBLIC_PUBLIC_URL,
|
||||
},
|
||||
})
|
||||
|
||||
export const env = {
|
||||
..._env,
|
||||
NEW_SITE_LIVE_STATUS: getLiveStatus(_env),
|
||||
isLangLive: (lang: Lang) =>
|
||||
isLangLive(lang, _env.NEXT_PUBLIC_NEW_SITE_LIVE_FOR_LANGS),
|
||||
}
|
||||
|
||||
54
apps/scandic-web/env/getLiveStatus.ts
vendored
Normal file
54
apps/scandic-web/env/getLiveStatus.ts
vendored
Normal file
@@ -0,0 +1,54 @@
|
||||
// Cannot use from "@/constants/languages" due to jiti
|
||||
import { Lang } from "../constants/languages"
|
||||
|
||||
export function getLiveStatus(
|
||||
internalEnv:
|
||||
| {
|
||||
NEW_SITE_LIVE_FOR_LANGS: string[]
|
||||
}
|
||||
| {
|
||||
NEXT_PUBLIC_NEW_SITE_LIVE_FOR_LANGS: string[]
|
||||
}
|
||||
): "ALL_LANGUAGES_LIVE" | "SOME_LANGUAGES_LIVE" | "NOT_LIVE" {
|
||||
const configuredLangs = getConfiguredLangs(internalEnv)
|
||||
if (!configuredLangs) {
|
||||
return "NOT_LIVE"
|
||||
}
|
||||
|
||||
const allLangs = Object.keys(Lang)
|
||||
const liveLangs = allLangs.filter((lang) => configuredLangs.includes(lang))
|
||||
if (liveLangs.length === 0) {
|
||||
return "NOT_LIVE"
|
||||
}
|
||||
|
||||
if (
|
||||
liveLangs.length === allLangs.length &&
|
||||
allLangs.every((lang) => liveLangs.includes(lang))
|
||||
) {
|
||||
return "ALL_LANGUAGES_LIVE"
|
||||
}
|
||||
|
||||
return "SOME_LANGUAGES_LIVE"
|
||||
}
|
||||
|
||||
function getConfiguredLangs<T extends {}>(env: T): string[] | undefined {
|
||||
if (
|
||||
!(
|
||||
"NEW_SITE_LIVE_FOR_LANGS" in env ||
|
||||
"NEXT_PUBLIC_NEW_SITE_LIVE_FOR_LANGS" in env
|
||||
)
|
||||
) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
const configuredLangs =
|
||||
"NEW_SITE_LIVE_FOR_LANGS" in env
|
||||
? env.NEW_SITE_LIVE_FOR_LANGS
|
||||
: env.NEXT_PUBLIC_NEW_SITE_LIVE_FOR_LANGS
|
||||
|
||||
if (!Array.isArray(configuredLangs)) {
|
||||
throw new Error("Misconfigured environment variable, expected array")
|
||||
}
|
||||
|
||||
return configuredLangs
|
||||
}
|
||||
18
apps/scandic-web/env/isLangLive.test.ts
vendored
Normal file
18
apps/scandic-web/env/isLangLive.test.ts
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
import { describe, expect, it } from "@jest/globals"
|
||||
|
||||
import { Lang } from "@/constants/languages"
|
||||
|
||||
import { isLangLive } from "./isLangLive"
|
||||
|
||||
describe("hideForNextRelease", () => {
|
||||
it("should return true if en is part of live languages", () => {
|
||||
expect(isLangLive(Lang.en, ["en", "sv"])).toBe(true)
|
||||
expect(isLangLive(Lang.en, ["en"])).toBe(true)
|
||||
})
|
||||
|
||||
it("should return false if en is not part of live languages", () => {
|
||||
expect(isLangLive(Lang.en, [])).toBe(false)
|
||||
expect(isLangLive(Lang.en, ["sv"])).toBe(false)
|
||||
expect(isLangLive(Lang.en, ["sv,fi"])).toBe(false)
|
||||
})
|
||||
})
|
||||
5
apps/scandic-web/env/isLangLive.ts
vendored
Normal file
5
apps/scandic-web/env/isLangLive.ts
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
import type { Lang } from "@/constants/languages"
|
||||
|
||||
export function isLangLive(lang: Lang, liveLangs: string[]): boolean {
|
||||
return liveLangs.includes(lang)
|
||||
}
|
||||
49
apps/scandic-web/env/server.ts
vendored
49
apps/scandic-web/env/server.ts
vendored
@@ -1,9 +1,14 @@
|
||||
import { createEnv } from "@t3-oss/env-nextjs"
|
||||
import { z } from "zod"
|
||||
|
||||
import { getLiveStatus } from "./getLiveStatus"
|
||||
import { isLangLive } from "./isLangLive"
|
||||
|
||||
import type { Lang } from "../constants/languages"
|
||||
|
||||
const TWENTYFOUR_HOURS = 24 * 60 * 60
|
||||
|
||||
export const env = createEnv({
|
||||
const _env = createEnv({
|
||||
/**
|
||||
* Due to t3-env only checking typeof window === "undefined"
|
||||
* and Netlify running Deno, window is never "undefined"
|
||||
@@ -107,19 +112,6 @@ export const env = createEnv({
|
||||
GOOGLE_STATIC_MAP_SIGNATURE_SECRET: z.string(),
|
||||
GOOGLE_DYNAMIC_MAP_ID: z.string(),
|
||||
GOOGLE_STATIC_MAP_ID: z.string(),
|
||||
HIDE_FOR_NEXT_RELEASE: z
|
||||
.string()
|
||||
// only allow "true" or "false"
|
||||
.refine((s) => s === "true" || s === "false")
|
||||
// transform to boolean
|
||||
.transform((s) => s === "true"),
|
||||
ENABLE_BOOKING_FLOW: z
|
||||
.string()
|
||||
// only allow "true" or "false"
|
||||
.refine((s) => s === "true" || s === "false")
|
||||
// transform to boolean
|
||||
.transform((s) => s === "true")
|
||||
.default("true"),
|
||||
ENABLE_BOOKING_WIDGET: z
|
||||
.string()
|
||||
// only allow "true" or "false"
|
||||
@@ -127,13 +119,6 @@ export const env = createEnv({
|
||||
// transform to boolean
|
||||
.transform((s) => s === "true")
|
||||
.default("false"),
|
||||
ENABLE_BOOKING_WIDGET_HOTELRESERVATION_PATH: z
|
||||
.string()
|
||||
// only allow "true" or "false"
|
||||
.refine((s) => s === "true" || s === "false")
|
||||
// transform to boolean
|
||||
.transform((s) => s === "true")
|
||||
.default("true"),
|
||||
ENABLE_SURPRISES: z
|
||||
.string()
|
||||
// only allow "true" or "false"
|
||||
@@ -200,6 +185,17 @@ export const env = createEnv({
|
||||
.transform((s) => s === "true")
|
||||
.default("true"),
|
||||
WARMUP_TOKEN: z.string().optional(),
|
||||
/**
|
||||
* Include the languages that should be hidden for the next release
|
||||
* Should be in the format of "en,da,de,fi,no,sv" or empty
|
||||
*/
|
||||
NEW_SITE_LIVE_FOR_LANGS: z
|
||||
.string()
|
||||
.regex(/^([a-z]{2},)*([a-z]{2}){0,1}$/)
|
||||
.transform((val) => {
|
||||
return val.split(",")
|
||||
})
|
||||
.default(""),
|
||||
},
|
||||
emptyStringAsUndefined: true,
|
||||
runtimeEnv: {
|
||||
@@ -268,12 +264,8 @@ export const env = createEnv({
|
||||
process.env.GOOGLE_STATIC_MAP_SIGNATURE_SECRET,
|
||||
GOOGLE_STATIC_MAP_ID: process.env.GOOGLE_STATIC_MAP_ID,
|
||||
GOOGLE_DYNAMIC_MAP_ID: process.env.GOOGLE_DYNAMIC_MAP_ID,
|
||||
HIDE_FOR_NEXT_RELEASE: process.env.NEXT_PUBLIC_HIDE_FOR_NEXT_RELEASE,
|
||||
USE_NEW_REWARD_MODEL: process.env.USE_NEW_REWARD_MODEL,
|
||||
ENABLE_BOOKING_FLOW: process.env.ENABLE_BOOKING_FLOW,
|
||||
ENABLE_BOOKING_WIDGET: process.env.ENABLE_BOOKING_WIDGET,
|
||||
ENABLE_BOOKING_WIDGET_HOTELRESERVATION_PATH:
|
||||
process.env.ENABLE_BOOKING_WIDGET_HOTELRESERVATION_PATH,
|
||||
ENABLE_SURPRISES: process.env.ENABLE_SURPRISES,
|
||||
SHOW_SITE_WIDE_ALERT: process.env.SHOW_SITE_WIDE_ALERT,
|
||||
SENTRY_ENVIRONMENT: process.env.NEXT_PUBLIC_SENTRY_ENVIRONMENT,
|
||||
@@ -295,9 +287,16 @@ export const env = createEnv({
|
||||
GIT_SHA: process.env.GIT_SHA,
|
||||
ENABLE_WARMUP_HOTEL: process.env.ENABLE_WARMUP_HOTEL,
|
||||
WARMUP_TOKEN: process.env.WARMUP_TOKEN,
|
||||
NEW_SITE_LIVE_FOR_LANGS: process.env.NEXT_PUBLIC_NEW_SITE_LIVE_FOR_LANGS,
|
||||
},
|
||||
})
|
||||
|
||||
export const env = {
|
||||
..._env,
|
||||
NEW_SITE_LIVE_STATUS: getLiveStatus(_env),
|
||||
isLangLive: (lang: Lang) => isLangLive(lang, _env.NEW_SITE_LIVE_FOR_LANGS),
|
||||
} as const
|
||||
|
||||
function replaceTopLevelDomain(url: string, domain: string) {
|
||||
return url.replaceAll("{topleveldomain}", domain)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user