54 lines
1.3 KiB
TypeScript
54 lines
1.3 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
|
|
}
|
|
|
|
const isOnHideRoute = CHATBOT_HIDE_CONDITIONS.routes[lang].some((route) => {
|
|
const fullRoute = removeTrailingSlash(
|
|
removeMultipleSlashes(`/${lang}${route}`)
|
|
)
|
|
return cleanPathname === fullRoute
|
|
})
|
|
|
|
if (isOnHideRoute) {
|
|
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
|
|
}
|