import config from '@automattic/calypso-config'; import { Modal, Button, VisuallyHidden } from '@wordpress/components'; import { useState } from '@wordpress/element'; import { link } from '@wordpress/icons'; import clsx from 'clsx'; import { useTranslate } from 'i18n-calypso'; import React, { useEffect, useRef } from 'react'; import { trackStatsAnalyticsEvent } from '../utils'; import StatsUtmBuilderForm, { type UtmBuilderProps } from './stats-module-utm-builder-form'; interface Props { modalClassName: string; trigger?: React.ReactElement; initialData?: UtmBuilderProps[ 'initialData' ]; } const UTMBuilder: React.FC< Props > = ( { modalClassName, trigger, initialData } ) => { const [ isOpen, setOpen ] = useState< boolean | null >( null ); const scrollY = useRef( { y: 0, mobile: false } ); const openModal = () => { const isMobile = document.body.scrollTop > 0; scrollY.current.mobile = isMobile; scrollY.current.y = isMobile ? document.body.scrollTop : window.scrollY; setOpen( true ); }; // Prevent scroll to top when modal is opened useEffect( () => { // Do not scroll on initial render if ( isOpen === null ) { return; } if ( isOpen && ! scrollY.current.mobile ) { document.body.scrollTo( 0, scrollY.current.y ); } else if ( ! isOpen ) { const element = scrollY.current.mobile ? document.body : window; element.scrollTo( 0, scrollY.current.y ); } }, [ isOpen ] ); const closeModal = () => setOpen( false ); const translate = useTranslate(); const handleClick = () => { trackStatsAnalyticsEvent( 'utm_builder_opened' ); trackStatsAnalyticsEvent( 'advanced_feature_interaction', { feature: 'utm_builder' } ); openModal(); }; const triggerNode = trigger ? ( React.cloneElement( trigger, { onClick: handleClick } ) ) : (