Migrate to a monorepo setup - step 1 * Move web to subfolder /apps/scandic-web * Yarn + transitive deps - Move to yarn - design-system package removed for now since yarn doesn't support the parameter for token (ie project currently broken) - Add missing transitive dependencies as Yarn otherwise prevents these imports - VS Code doesn't pick up TS path aliases unless you open /apps/scandic-web instead of root (will be fixed with monorepo) * Pin framer-motion to temporarily fix typing issue https://github.com/adobe/react-spectrum/issues/7494 * Pin zod to avoid typ error There seems to have been a breaking change in the types returned by zod where error is now returned as undefined instead of missing in the type. We should just handle this but to avoid merge conflicts just pin the dependency for now. * Pin react-intl version Pin version of react-intl to avoid tiny type issue where formatMessage does not accept a generic any more. This will be fixed in a future commit, but to avoid merge conflicts just pin for now. * Pin typescript version Temporarily pin version as newer versions as stricter and results in a type error. Will be fixed in future commit after merge. * Setup workspaces * Add design-system as a monorepo package * Remove unused env var DESIGN_SYSTEM_ACCESS_TOKEN * Fix husky for monorepo setup * Update netlify.toml * Add lint script to root package.json * Add stub readme * Fix react-intl formatMessage types * Test netlify.toml in root * Remove root toml * Update netlify.toml publish path * Remove package-lock.json * Update build for branch/preview builds Approved-by: Linus Flood
246 lines
7.2 KiB
TypeScript
246 lines
7.2 KiB
TypeScript
import { getFiltersFromHotels } from "@/stores/hotel-data/helper"
|
|
|
|
import { getIntl } from "@/i18n"
|
|
|
|
import {
|
|
getCityByCityIdentifier,
|
|
getHotelIdsByCityIdentifier,
|
|
getHotelsByHotelIds,
|
|
} from "../../hotels/utils"
|
|
|
|
import { RTETypeEnum } from "@/types/rte/enums"
|
|
import type {
|
|
MetadataInputSchema,
|
|
RawMetadataSchema,
|
|
} from "@/types/trpc/routers/contentstack/metadata"
|
|
import type { Lang } from "@/constants/languages"
|
|
|
|
export const affix = "metadata"
|
|
|
|
/**
|
|
* Truncates the given text "intelligently" based on the last period found near the max length.
|
|
*
|
|
* - If a period exists within the extended range (`maxLength` to `maxLength + maxExtension`),
|
|
* the function truncates after the closest period to `maxLength`.
|
|
* - If no period is found in the range, it truncates the text after the last period found in the max length of the text.
|
|
* - If no periods exist at all, it truncates at `maxLength` and appends ellipsis (`...`).
|
|
*
|
|
* @param {string} text - The input text to be truncated.
|
|
* @param {number} [maxLength=150] - The desired maximum length of the truncated text.
|
|
* @param {number} [minLength=120] - The minimum allowable length for the truncated text.
|
|
* @param {number} [maxExtension=10] - The maximum number of characters to extend beyond `maxLength` to find a period.
|
|
* @returns {string} - The truncated text.
|
|
*/
|
|
function truncateTextAfterLastPeriod(
|
|
text: string,
|
|
maxLength: number = 150,
|
|
minLength: number = 120,
|
|
maxExtension: number = 10
|
|
): string {
|
|
if (text.length <= maxLength) {
|
|
return text
|
|
}
|
|
|
|
// Define the extended range
|
|
const extendedEnd = Math.min(text.length, maxLength + maxExtension)
|
|
const extendedText = text.slice(0, extendedEnd)
|
|
|
|
// Find all periods within the extended range and filter after minLength to get valid periods
|
|
const periodsInRange = [...extendedText.matchAll(/\./g)].map(
|
|
({ index }) => index
|
|
)
|
|
const validPeriods = periodsInRange.filter((index) => index + 1 >= minLength)
|
|
|
|
if (validPeriods.length > 0) {
|
|
// Find the period closest to maxLength
|
|
const closestPeriod = validPeriods.reduce((closest, index) =>
|
|
Math.abs(index + 1 - maxLength) < Math.abs(closest + 1 - maxLength)
|
|
? index
|
|
: closest
|
|
)
|
|
return extendedText.slice(0, closestPeriod + 1)
|
|
}
|
|
|
|
// Fallback: If no period is found within the valid range, look for the last period in the truncated text
|
|
const maxLengthText = text.slice(0, maxLength)
|
|
const lastPeriodIndex = maxLengthText.lastIndexOf(".")
|
|
if (lastPeriodIndex !== -1) {
|
|
return text.slice(0, lastPeriodIndex + 1)
|
|
}
|
|
|
|
// Final fallback: Return maxLength text including ellipsis
|
|
return `${maxLengthText}...`
|
|
}
|
|
|
|
export async function getTitle(data: RawMetadataSchema) {
|
|
const intl = await getIntl()
|
|
const metadata = data.web?.seo_metadata
|
|
if (metadata?.title) {
|
|
return metadata.title
|
|
}
|
|
if (data.hotelData) {
|
|
return intl.formatMessage(
|
|
{ id: "Stay at {hotelName} | Hotel in {destination}" },
|
|
{
|
|
hotelName: data.hotelData.name,
|
|
destination: data.hotelData.address.city,
|
|
}
|
|
)
|
|
}
|
|
if (data.system.content_type_uid === "destination_city_page") {
|
|
if (data.cityName) {
|
|
if (data.cityFilter) {
|
|
if (data.cityFilterType === "facility") {
|
|
return intl.formatMessage(
|
|
{ id: "Hotels with {filter} in {cityName}" },
|
|
{ cityName: data.cityName, filter: data.cityFilter }
|
|
)
|
|
} else if (data.cityFilterType === "surroundings") {
|
|
return intl.formatMessage(
|
|
{ id: "Hotels near {filter} in {cityName}" },
|
|
{ cityName: data.cityName, filter: data.cityFilter }
|
|
)
|
|
}
|
|
}
|
|
return intl.formatMessage(
|
|
{ id: "Hotels in {city}" },
|
|
{ city: data.cityName }
|
|
)
|
|
}
|
|
}
|
|
if (data.web?.breadcrumbs?.title) {
|
|
return data.web.breadcrumbs.title
|
|
}
|
|
if (data.heading) {
|
|
return data.heading
|
|
}
|
|
if (data.header?.heading) {
|
|
return data.header.heading
|
|
}
|
|
return ""
|
|
}
|
|
|
|
export function getDescription(data: RawMetadataSchema) {
|
|
const metadata = data.web?.seo_metadata
|
|
if (metadata?.description) {
|
|
return metadata.description
|
|
}
|
|
if (data.hotelData) {
|
|
return data.hotelData.hotelContent.texts.descriptions?.short
|
|
}
|
|
if (data.preamble) {
|
|
return truncateTextAfterLastPeriod(data.preamble)
|
|
}
|
|
if (data.header?.preamble) {
|
|
return truncateTextAfterLastPeriod(data.header.preamble)
|
|
}
|
|
if (data.blocks?.length) {
|
|
const jsonData = data.blocks[0].content?.content?.json
|
|
// Finding the first paragraph with text
|
|
const firstParagraph = jsonData?.children?.find(
|
|
(child) => child.type === RTETypeEnum.p && child.children[0].text
|
|
)
|
|
|
|
if (firstParagraph?.children?.length) {
|
|
return firstParagraph.children[0].text
|
|
? truncateTextAfterLastPeriod(firstParagraph.children[0].text)
|
|
: ""
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
export function getImage(data: RawMetadataSchema) {
|
|
const metadataImage = data.web?.seo_metadata?.seo_image
|
|
const heroImage = data.hero_image
|
|
const hotelImage =
|
|
data.hotelData?.gallery?.heroImages?.[0] ||
|
|
data.hotelData?.gallery?.smallerImages?.[0]
|
|
|
|
// Currently we don't have the possibility to get smaller images from ImageVault (2024-11-15)
|
|
if (metadataImage) {
|
|
return {
|
|
url: metadataImage.url,
|
|
alt: metadataImage.meta.alt || undefined,
|
|
width: metadataImage.dimensions.width,
|
|
height: metadataImage.dimensions.height,
|
|
}
|
|
}
|
|
if (hotelImage) {
|
|
return {
|
|
url: hotelImage.imageSizes.small,
|
|
alt: hotelImage.metaData.altText || undefined,
|
|
}
|
|
}
|
|
if (heroImage) {
|
|
return {
|
|
url: heroImage.url,
|
|
alt: heroImage.meta.alt || undefined,
|
|
width: heroImage.dimensions.width,
|
|
height: heroImage.dimensions.height,
|
|
}
|
|
}
|
|
return undefined
|
|
}
|
|
|
|
export async function getCityData(
|
|
data: RawMetadataSchema,
|
|
input: MetadataInputSchema,
|
|
serviceToken: string,
|
|
lang: Lang
|
|
) {
|
|
const destinationSettings = data.destination_settings
|
|
const cityFilter = input.subpage
|
|
let cityIdentifier
|
|
let cityData
|
|
let filterType
|
|
|
|
if (destinationSettings) {
|
|
const {
|
|
city_sweden,
|
|
city_norway,
|
|
city_denmark,
|
|
city_finland,
|
|
city_germany,
|
|
city_poland,
|
|
} = destinationSettings
|
|
const cities = [
|
|
city_denmark,
|
|
city_finland,
|
|
city_germany,
|
|
city_poland,
|
|
city_norway,
|
|
city_sweden,
|
|
].filter((city): city is string => Boolean(city))
|
|
|
|
cityIdentifier = cities[0]
|
|
if (cityIdentifier) {
|
|
cityData = await getCityByCityIdentifier(cityIdentifier, serviceToken)
|
|
const hotelIds = await getHotelIdsByCityIdentifier(
|
|
cityIdentifier,
|
|
serviceToken
|
|
)
|
|
|
|
const hotels = await getHotelsByHotelIds(hotelIds, lang, serviceToken)
|
|
|
|
if (cityFilter) {
|
|
const allFilters = getFiltersFromHotels(hotels)
|
|
const facilityFilter = allFilters.facilityFilters.find(
|
|
(f) => f.slug === cityFilter
|
|
)
|
|
const surroudingsFilter = allFilters.surroundingsFilters.find(
|
|
(f) => f.slug === cityFilter
|
|
)
|
|
|
|
if (facilityFilter) {
|
|
filterType = "facility"
|
|
} else if (surroudingsFilter) {
|
|
filterType = "surroundings"
|
|
}
|
|
}
|
|
}
|
|
return { cityName: cityData?.name, cityFilter, cityFilterType: filterType }
|
|
}
|
|
return null
|
|
}
|