Consolidate autocomplete search SW-2253 SW-2338 * use fuse.js for fuzzy search * Handle weird behaviour when search field loses focus on destinationPage * Add error logging for JumpTo when no URL was provided * Switch to use <Typography /> over <Caption /> * fix: bookingWidget search label should always be red * fix: searchHistory can no longer add invalid items * fix: list more hits when searching * fix: issue when searchField value was undefined * fix: don't show searchHistory label if no searchHistory items * simplify skeleton for listitems in search Approved-by: Linus Flood
138 lines
4.2 KiB
TypeScript
138 lines
4.2 KiB
TypeScript
import { z } from "zod"
|
|
|
|
import { Lang } from "@/constants/languages"
|
|
import { safeProtectedServiceProcedure } from "@/server/trpc"
|
|
import { isDefined } from "@/server/utils"
|
|
|
|
import { getCacheClient } from "@/services/dataCache"
|
|
import { safeTry } from "@/utils/safeTry"
|
|
|
|
import { getCityPageUrls } from "../contentstack/destinationCityPage/utils"
|
|
import { getHotelPageUrls } from "../contentstack/hotelPage/utils"
|
|
import { getCitiesByCountry, getCountries, getLocations } from "../hotels/utils"
|
|
import { filterAndCategorizeAutoComplete } from "./util/filterAndCategorizeAutoComplete"
|
|
import { mapLocationToAutoCompleteLocation } from "./util/mapLocationToAutoCompleteLocation"
|
|
|
|
import type { AutoCompleteLocation } from "./schema"
|
|
|
|
const destinationsAutoCompleteInputSchema = z.object({
|
|
query: z.string(),
|
|
selectedHotelId: z.string().optional(),
|
|
selectedCity: z.string().optional(),
|
|
lang: z.nativeEnum(Lang),
|
|
})
|
|
|
|
type DestinationsAutoCompleteOutput = {
|
|
hits: {
|
|
hotels: AutoCompleteLocation[]
|
|
cities: AutoCompleteLocation[]
|
|
}
|
|
currentSelection: {
|
|
hotel: (AutoCompleteLocation & { type: "hotels" }) | null
|
|
city: (AutoCompleteLocation & { type: "cities" }) | null
|
|
}
|
|
}
|
|
|
|
export const getDestinationsAutoCompleteRoute = safeProtectedServiceProcedure
|
|
.input(destinationsAutoCompleteInputSchema)
|
|
.query(async ({ ctx, input }): Promise<DestinationsAutoCompleteOutput> => {
|
|
const cacheClient = await getCacheClient()
|
|
|
|
const lang = input.lang || ctx.lang
|
|
const locations: AutoCompleteLocation[] = await cacheClient.cacheOrGet(
|
|
`autocomplete:destinations:locations:${lang}`,
|
|
async () => {
|
|
const hotelUrlsPromise = safeTry(getHotelPageUrls(lang))
|
|
const cityUrlsPromise = safeTry(getCityPageUrls(lang))
|
|
|
|
const countries = await getCountries({
|
|
lang: lang,
|
|
serviceToken: ctx.serviceToken,
|
|
})
|
|
|
|
if (!countries) {
|
|
throw new Error("Unable to fetch countries")
|
|
}
|
|
const countryNames = countries.data.map((country) => country.name)
|
|
const citiesByCountry = await getCitiesByCountry({
|
|
countries: countryNames,
|
|
serviceToken: ctx.serviceToken,
|
|
lang,
|
|
})
|
|
|
|
const locations = await getLocations({
|
|
lang: lang,
|
|
serviceToken: ctx.serviceToken,
|
|
citiesByCountry: citiesByCountry,
|
|
})
|
|
|
|
const [hotelUrls, hotelUrlsError] = await hotelUrlsPromise
|
|
const [cityUrls, cityUrlsError] = await cityUrlsPromise
|
|
|
|
if (hotelUrlsError || cityUrlsError || !hotelUrls || !cityUrls) {
|
|
throw new Error("Unable to fetch location URLs")
|
|
}
|
|
|
|
return locations
|
|
.map((location) => {
|
|
let url: string | undefined
|
|
|
|
if (location.type === "cities") {
|
|
url = cityUrls.find(
|
|
(c) =>
|
|
c.city &&
|
|
location.cityIdentifier &&
|
|
c.city === location.cityIdentifier
|
|
)?.url
|
|
}
|
|
|
|
if (location.type === "hotels") {
|
|
url = hotelUrls.find(
|
|
(h) => h.hotelId && location.id && h.hotelId === location.id
|
|
)?.url
|
|
}
|
|
|
|
return { ...location, url }
|
|
})
|
|
.map(mapLocationToAutoCompleteLocation)
|
|
.filter(isDefined)
|
|
},
|
|
"1d"
|
|
)
|
|
|
|
const hits = filterAndCategorizeAutoComplete({
|
|
locations,
|
|
query: input.query,
|
|
})
|
|
|
|
const selectedHotel = locations.find(
|
|
(location) =>
|
|
location.type === "hotels" && location.id === input.selectedHotelId
|
|
)
|
|
|
|
const selectedCity = locations.find(
|
|
(location) =>
|
|
location.type === "cities" && location.name === input.selectedCity
|
|
)
|
|
|
|
return {
|
|
hits: hits,
|
|
currentSelection: {
|
|
city: isCity(selectedCity) ? selectedCity : null,
|
|
hotel: isHotel(selectedHotel) ? selectedHotel : null,
|
|
},
|
|
}
|
|
})
|
|
|
|
function isHotel(
|
|
location: AutoCompleteLocation | null | undefined
|
|
): location is AutoCompleteLocation & { type: "hotels" } {
|
|
return !!location && location.type === "hotels"
|
|
}
|
|
|
|
function isCity(
|
|
location: AutoCompleteLocation | null | undefined
|
|
): location is AutoCompleteLocation & { type: "cities" } {
|
|
return !!location && location.type === "cities"
|
|
}
|