import { lazy, Suspense, useMemo, useState } from 'react'; import { Box } from '@chakra-ui/react'; import { DEFERRED_LOAD_MARGIN } from '@/components/DeferredImage/DeferredImage'; import ContactFormGate from '@/components/ContactForm/ContactFormGate'; import type { EmailAddress, PricingPackage } from '@/content/types'; import useInView from '@/hooks/useInView'; import { mailSubject } from '@/lib/mailLink'; import { packageFromSearch } from '@/lib/packageFromSearch'; import { shootOptions } from '@/lib/shootOptions'; import { visiblePackages } from '@/lib/visiblePackages'; // The form (Chakra fields, validation, reducer) is its own chunk, so a page // only pays for it when the form is about to be used. On the homepage that // saved ±0.03 Lighthouse performance on mobile. const ContactForm = lazy(() => import('@/components/ContactForm/ContactForm')); // Keeps the form's space while it loads, so the notice sits where it will // stay and the page below does not jump (measured with the privacy line: // 649px on phones, where name and e-mail are stacked, 547px from sm) const FORM_PLACEHOLDER_HEIGHT = { base: '649px', sm: '547px' }; type ContactPanelProps = { // Prefix for the form's field ids, unique per page idPrefix: string; packages: PricingPackage[]; enabled: boolean; email: EmailAddress; replyTime: string; // Raw ?pakket= value from the page; checked against the shown packages pakket?: string; // lazy: load the form when it comes near the screen or a package is // chosen (homepage); eager: start loading it with the page (contact page) load: 'lazy' | 'eager'; }; // The contact form, ready to place on a page: the coming-soon gate, the // shoot options from the packages and a package chosen via ?pakket=, with // the mail subject that goes with it (BR-CPN-01..06). Heading, URL clean-up, // scrolling and focus stay with the page. export default function ContactPanel({ idPrefix, packages, enabled, email, replyTime, pakket, load, }: ContactPanelProps) { const shown = useMemo(() => visiblePackages(packages), [packages]); const options = useMemo(() => shootOptions(packages), [packages]); // Take over a new ?pakket= while rendering (React's pattern for a // changed prop); the last "Boek" click wins (BR-PARAM-03) const [seenPakket, setSeenPakket] = useState(); const [preselect, setPreselect] = useState<{ shoot: string; key: number; }>(); const [chosenName, setChosenName] = useState(); if (pakket !== seenPakket) { setSeenPakket(pakket); const id = pakket === undefined ? undefined : packageFromSearch(pakket, shown); if (id) { setPreselect({ shoot: id, key: (preselect?.key ?? 0) + 1 }); setChosenName(shown.find((pkg) => pkg.id === id)?.name); } } const subject = mailSubject(chosenName); const { ref, inView } = useInView({ rootMargin: DEFERRED_LOAD_MARGIN, }); // Once loaded the form stays, also after ?pakket= is removed from the // URL; otherwise it would be recreated and lose what was typed const [formLoaded, setFormLoaded] = useState(load === 'eager'); if (!formLoaded && (inView || pakket !== undefined)) setFormLoaded(true); const placeholder = ; return ( {formLoaded ? ( ) : ( placeholder )} ); }