"use client" import { useMap } from "@vis.gl/react-google-maps" import { useCallback, useEffect, useMemo, useRef, useState } from "react" import { useIntl } from "react-intl" import { useMediaQuery } from "usehooks-ts" import { selectHotel } from "@/constants/routes/hotelReservation" import { useHotelFilterStore } from "@/stores/hotel-filters" import { useHotelsMapStore } from "@/stores/hotels-map" import { RoomCardSkeleton } from "@/components/HotelReservation/SelectRate/RoomSelection/RoomCard/RoomCardSkeleton" import { CloseIcon, CloseLargeIcon } from "@/components/Icons" import InteractiveMap from "@/components/Maps/InteractiveMap" import { BackToTopButton } from "@/components/TempDesignSystem/BackToTopButton" import Button from "@/components/TempDesignSystem/Button" import Link from "@/components/TempDesignSystem/Link" import useLang from "@/hooks/useLang" import { debounce } from "@/utils/debounce" import FilterAndSortModal from "../../FilterAndSortModal" import HotelListing from "../HotelListing" import { getVisibleHotels } from "./utils" import styles from "./selectHotelMapContent.module.css" import type { HotelData } from "@/types/components/hotelReservation/selectHotel/hotelCardListingProps" import type { SelectHotelMapProps } from "@/types/components/hotelReservation/selectHotel/map" const SKELETON_LOAD_DELAY = 750 export default function SelectHotelContent({ hotelPins, cityCoordinates, mapId, hotels, filterList, }: Omit) { const lang = useLang() const intl = useIntl() const map = useMap() const isAboveMobile = useMediaQuery("(min-width: 768px)") const [visibleHotels, setVisibleHotels] = useState([]) const [showBackToTop, setShowBackToTop] = useState(false) const [showSkeleton, setShowSkeleton] = useState(false) const listingContainerRef = useRef(null) const activeFilters = useHotelFilterStore((state) => state.activeFilters) const { activeHotelCard, activeHotelPin } = useHotelsMapStore() const coordinates = useMemo( () => isAboveMobile ? cityCoordinates : { ...cityCoordinates, lat: cityCoordinates.lat - 0.006 }, [isAboveMobile, cityCoordinates] ) useEffect(() => { if (listingContainerRef.current) { const activeElement = listingContainerRef.current.querySelector(`[data-active="true"]`) if (activeElement) { activeElement.scrollIntoView({ behavior: "smooth", block: "nearest" }) } } }, [activeHotelCard, activeHotelPin]) useEffect(() => { const hotelListingElement = document.querySelector( `.${styles.listingContainer}` ) if (!hotelListingElement) return const handleScroll = () => { const hasScrolledPast = hotelListingElement.scrollTop > 490 setShowBackToTop(hasScrolledPast) } hotelListingElement.addEventListener("scroll", handleScroll) return () => hotelListingElement.removeEventListener("scroll", handleScroll) }, []) function scrollToTop() { const hotelListingElement = document.querySelector( `.${styles.listingContainer}` ) hotelListingElement?.scrollTo({ top: 0, behavior: "smooth" }) } const filteredHotelPins = useMemo( () => hotelPins.filter((hotel) => activeFilters.every((filterId) => hotel.facilityIds.includes(Number(filterId)) ) ), [activeFilters, hotelPins] ) const getHotelCards = useCallback(() => { const visibleHotels = getVisibleHotels(hotels, filteredHotelPins, map) setVisibleHotels(visibleHotels) setTimeout(() => { setShowSkeleton(true) }, SKELETON_LOAD_DELAY) }, [hotels, filteredHotelPins, map]) /** * Updates visible hotels when map viewport changes (zoom/pan) * - Debounces updates to prevent excessive re-renders during map interaction * - Shows loading skeleton while map tiles load * - Triggers on: initial load, zoom, pan, and tile loading completion */ const debouncedUpdateHotelCards = useMemo( () => debounce(() => { if (!map) return setShowSkeleton(false) getHotelCards() }, 100), [map, getHotelCards] ) const closeButton = ( ) return (
{showSkeleton ? ( <> ) : ( )} {showBackToTop && ( )}
) }