diff --git a/components/ContentType/HotelPage/Map/MapWithCard/index.tsx b/components/ContentType/HotelPage/Map/MapWithCard/index.tsx index 3ec65c505..b544e39d3 100644 --- a/components/ContentType/HotelPage/Map/MapWithCard/index.tsx +++ b/components/ContentType/HotelPage/Map/MapWithCard/index.tsx @@ -8,7 +8,7 @@ import useStickyPosition from "@/hooks/useStickyPosition" import styles from "./mapWithCard.module.css" -export default function MapWithCardWrapper({ children }: PropsWithChildren) { +export default function MapWithCard({ children }: PropsWithChildren) { const mapWithCardRef = useRef(null) useStickyPosition({ ref: mapWithCardRef, diff --git a/components/SitewideAlert/Client.tsx b/components/SitewideAlert/Client.tsx new file mode 100644 index 000000000..613a1e18b --- /dev/null +++ b/components/SitewideAlert/Client.tsx @@ -0,0 +1,41 @@ +"use client" + +import { useRef } from "react" + +import { StickyElementNameEnum } from "@/stores/sticky-position" + +import Alert from "@/components/TempDesignSystem/Alert" +import useStickyPosition from "@/hooks/useStickyPosition" + +import { SitewideAlertProps } from "./sitewideAlert" + +import styles from "./sitewideAlert.module.css" + +import { AlertTypeEnum } from "@/types/enums/alert" + +export default function SiteWideAlertClient({ alert }: SitewideAlertProps) { + const alertRef = useRef(null) + useStickyPosition({ + ref: alertRef, + name: StickyElementNameEnum.SITEWIDE_ALERT, + }) + const isAlarm = alert.type === AlertTypeEnum.Alarm + + return ( +
+ +
+ ) +} diff --git a/components/SitewideAlert/index.tsx b/components/SitewideAlert/index.tsx index 2ce624119..cae0eb474 100644 --- a/components/SitewideAlert/index.tsx +++ b/components/SitewideAlert/index.tsx @@ -1,8 +1,6 @@ import { getSiteConfig } from "@/lib/trpc/memoizedRequests" -import Alert from "../TempDesignSystem/Alert" - -import styles from "./sitewideAlert.module.css" +import SiteWideAlertClient from "./Client" export function preload() { void getSiteConfig() @@ -15,19 +13,5 @@ export default async function SitewideAlert() { return null } - const { sitewideAlert } = siteConfig - return ( -
- -
- ) + return } diff --git a/components/SitewideAlert/sitewideAlert.ts b/components/SitewideAlert/sitewideAlert.ts new file mode 100644 index 000000000..8ba4b924f --- /dev/null +++ b/components/SitewideAlert/sitewideAlert.ts @@ -0,0 +1,5 @@ +import type { Alert } from "@/types/trpc/routers/contentstack/siteConfig" + +export interface SitewideAlertProps { + alert: Alert +} diff --git a/hooks/useStickyPosition.ts b/hooks/useStickyPosition.ts index ae402bc66..923f56c73 100644 --- a/hooks/useStickyPosition.ts +++ b/hooks/useStickyPosition.ts @@ -6,12 +6,17 @@ import useStickyPositionStore, { StickyElementNameEnum, } from "@/stores/sticky-position" +import { debounce } from "@/utils/debounce" + interface UseStickyPositionProps { ref?: React.RefObject name?: StickyElementNameEnum group?: string } +// Global singleton ResizeObserver to observe the body only once +let resizeObserver: ResizeObserver | null = null + /** * Custom hook to manage sticky positioning of elements within a page. * This hook registers an element as sticky, calculates its top offset based on @@ -83,6 +88,25 @@ export default function useStickyPosition({ } }, [stickyElements, ref]) + useEffect(() => { + if (!resizeObserver) { + const debouncedResizeHandler = debounce(() => { + updateHeights() + }, 100) + + resizeObserver = new ResizeObserver(debouncedResizeHandler) + console.log("Initialized ResizeObserver") + } + + resizeObserver.observe(document.body) + + return () => { + if (resizeObserver) { + resizeObserver.unobserve(document.body) + } + } + }, [updateHeights]) + return { currentHeight: ref?.current?.offsetHeight || null, allElements: getAllElements(), diff --git a/server/routers/contentstack/base/utils.ts b/server/routers/contentstack/base/utils.ts index df88e9cb7..2c9c13e2d 100644 --- a/server/routers/contentstack/base/utils.ts +++ b/server/routers/contentstack/base/utils.ts @@ -5,7 +5,7 @@ import type { System } from "@/types/requests/system" import type { Edges } from "@/types/requests/utils/edges" import type { NodeRefs } from "@/types/requests/utils/refs" import type { HeaderRefs } from "@/types/trpc/routers/contentstack/header" -import type { Alert } from "@/types/trpc/routers/contentstack/siteConfig" +import type { AlertOutput } from "@/types/trpc/routers/contentstack/siteConfig" import type { ContactConfig } from "./output" export function getConnections({ header }: HeaderRefs) { @@ -71,7 +71,7 @@ export function getFooterConnections(refs: FooterRefDataRaw) { } export function getAlertPhoneContactData( - alert: Alert, + alert: AlertOutput, contactConfig: ContactConfig ) { if (alert.phoneContact) { diff --git a/types/trpc/routers/contentstack/siteConfig.ts b/types/trpc/routers/contentstack/siteConfig.ts index 7c4160203..51ca2a6d8 100644 --- a/types/trpc/routers/contentstack/siteConfig.ts +++ b/types/trpc/routers/contentstack/siteConfig.ts @@ -11,5 +11,15 @@ export type GetSiteConfigRefData = z.infer export type GetSiteConfigData = z.input export type SiteConfig = z.output -export type Alert = z.output -export type SidepeekContent = Alert["sidepeekContent"] +export type AlertOutput = z.output +export type SidepeekContent = AlertOutput["sidepeekContent"] + +export type AlertPhoneContact = { + displayText: string + phoneNumber?: string + footnote?: string | null +} + +export type Alert = Omit & { + phoneContact: AlertPhoneContact | null +}