46 lines
1.3 KiB
TypeScript
46 lines
1.3 KiB
TypeScript
import { AlertTypeEnum } from "@scandic-hotels/common/constants/alert"
|
|
import {
|
|
removeMultipleSlashes,
|
|
removeTrailingSlash,
|
|
} from "@scandic-hotels/common/utils/url"
|
|
|
|
import { CAMPAIGN_BANNER_HIDE_CONDITIONS } from "@/components/CampaignBanner/constants"
|
|
|
|
import type { Lang } from "@scandic-hotels/common/constants/language"
|
|
|
|
export function shouldShowCampaignBanner(
|
|
pathname: string,
|
|
lang: Lang,
|
|
closedPaths: Set<string>,
|
|
sitewideAlertType: AlertTypeEnum | null
|
|
): boolean {
|
|
const cleanPathname = removeTrailingSlash(removeMultipleSlashes(pathname))
|
|
|
|
// Check if the banner should not be visible on the current path based on hide conditions.
|
|
const isOnHideRoute = CAMPAIGN_BANNER_HIDE_CONDITIONS.routes[lang].some(
|
|
(route) => {
|
|
const fullRoute = removeTrailingSlash(
|
|
removeMultipleSlashes(`/${lang}${route}`)
|
|
)
|
|
return cleanPathname.startsWith(fullRoute)
|
|
}
|
|
)
|
|
|
|
if (isOnHideRoute) {
|
|
return false
|
|
}
|
|
|
|
// The campaign banner should not be visible if there is an alarming sitewide alert
|
|
if (sitewideAlertType === AlertTypeEnum.Alarm) {
|
|
return false
|
|
}
|
|
|
|
// Check if the user closed the banner and should therefore not be visible on the current path.
|
|
// This is saved in a local state and is reset on page reload.
|
|
if (closedPaths.has(pathname)) {
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|