feat(BOOK-67): Added functionality to show/hide the chatbot
Approved-by: Linus Flood
This commit is contained in:
42
apps/scandic-web/components/ChatbotScript/utils.ts
Normal file
42
apps/scandic-web/components/ChatbotScript/utils.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user