Files
web/apps/scandic-web/components/ChatbotScript/utils.ts
Erik Tiekstra 800df0ade9 feat(BOOK-67): Added functionality to show/hide the chatbot
Approved-by: Linus Flood
2025-10-16 10:59:47 +00:00

43 lines
1.1 KiB
TypeScript

import {
removeMultipleSlashes,
removeTrailingSlash,
} from "@scandic-hotels/common/utils/url"
import { CHATBOT_HIDE_CONDITIONS, CHATBOT_SHOW_ROUTES } from "./constants"
import type { Lang } from "@scandic-hotels/common/constants/language"
export function shouldShowChatbot(
pathname: string,
searchParams: URLSearchParams | null,
lang: Lang
): boolean {
const cleanPathname = removeTrailingSlash(removeMultipleSlashes(pathname))
const isOnShowRoute = CHATBOT_SHOW_ROUTES[lang].some((route) => {
const fullRoute = removeTrailingSlash(
removeMultipleSlashes(`/${lang}${route}`)
)
return cleanPathname.startsWith(fullRoute)
})
const isOnStartPage = cleanPathname === `/${lang}`
if (!isOnShowRoute && !isOnStartPage) {
return false
}
if (searchParams) {
const shouldHideOnSearchParams = CHATBOT_HIDE_CONDITIONS.searchParams.some(
({ key, value }) => {
const paramValue = searchParams.get(key)
return paramValue === value
}
)
if (shouldHideOnSearchParams) {
return false
}
}
return true
}