import React, { useState, useCallback, useEffect, useRef } from 'react'; import { Transition } from '@headlessui/react'; import queryString from 'query-string'; import { WpInlineScript, Options } from '../interfaces'; import { Registration } from './steps/Registration'; import { PinCheck } from './steps/PinCheck'; import { Authenticated } from './steps/Authenticated'; import { Activate } from './steps/Activate'; import { Error } from './steps/Error'; import { Wordmark } from '../../../../src/components/Wordmark'; import { WebUiLogo } from '../../../../src/components/WebUiLogo'; declare const webEngineAccount: WpInlineScript; type Props = { setUserIsRegistered: ( visible: boolean | null ) => void; visible: boolean; setVisible: ( visible: boolean ) => void; options: Options; Logo: React.ReactNode; LogoBusy: React.ReactNode; ajax: WpInlineScript; callback?: Function; elevator?: string; isInline?: boolean; onMaybeLater?: () => void; }; export const Window: React.FC< Props > = ( { setUserIsRegistered, visible, setVisible, options, Logo, LogoBusy, ajax, callback, elevator, isInline = false, onMaybeLater, }: Props ) => { const [ registrationStep, setRegistrationStep ] = useState( 'init' ); const modalRef = useRef< HTMLDivElement >( null ); const [ registrationName, setRegistrationName ] = useState< string >( '' ); const [ registrationEmail, setRegistrationEmail ] = useState< string >( '' ); const [ commAdministrative, setCommAdministrative ] = useState< boolean >( false ); const [ commNews, setCommNews ] = useState< boolean >( false ); const [ commDeveloper, setCommDeveloper ] = useState< boolean >( false ); const [ loaded, setLoaded ] = useState< boolean >( true ); const [ error, setError ] = useState< string >( '' ); const [ pin, setPin ] = useState< string >( '' ); const onKeyDown = ( evt: KeyboardEvent ) => { if ( evt.key === 'Escape' ) { setVisible( false ); } }; const onStep = ( requestedStep: string ) => { switch ( requestedStep ) { case 'pin': setRegistrationStep( 'registering' ); const pdata = { action: 'we360cloud_account', token: webEngineAccount.token, route: 'hello', name: registrationName, email: registrationEmail, }; fetch( webEngineAccount.url, { method: 'POST', headers: { 'content-type': 'application/x-www-form-urlencoded', }, body: queryString.stringify( pdata ), } ) .then( ( response ) => response.json() ) .then( ( json ) => { // ::TODO:: look at the json - make sure the response was a send if ( true === json.success ) { transition( 'pin sent' ); } else { // message with timer countdown handled in registration // hold on registering state, allow to register after 10s setError( 'register-error' ); window.setTimeout( () => { setRegistrationStep( 'register' ); setError( '' ); }, 10000 ); } } ); break; case 'unregister-confirmed': const udata = { action: `we360cloud_${ options.product.toLowerCase() }`, token: ajax.token, [ `we360cloud_${ options.product.toLowerCase() }_route` ]: 'goodbye', }; fetch( ajax.url, { method: 'POST', headers: { 'content-type': 'application/x-www-form-urlencoded', }, body: queryString.stringify( udata ), } ) .then( ( response ) => response.json() ) .then( ( data ) => { if ( data.code === 200 ) { transition( 'activate' ); setUserIsRegistered( false ); //setRegistrationStep( 'activate' ); } else { onStep( 'error' ); } } ); break; case 'force-disconnect-confirmed': setRegistrationStep( 'disconnecting' ); //setLoaded( false ); // user really wants to disconnect from Web-Engine cloud completely const udata2 = { action: 'we360cloud_account', token: webEngineAccount.token, route: 'goodbye', }; fetch( webEngineAccount.url, { method: 'POST', headers: { 'content-type': 'application/x-www-form-urlencoded', }, body: queryString.stringify( udata2 ), } ) .then( ( response ) => response.json() ) .then( ( data ) => { if ( data.code === 200 ) { transition( 'register' ); } else { onStep( 'error' ); } } ); break; case 'pin check': setRegistrationStep( 'pin check' ); const pcdata = { action: 'we360cloud_account', token: webEngineAccount.token, route: 'hello', name: registrationName, email: registrationEmail, commAdministrative, commNews, commDeveloper, pin, }; fetch( webEngineAccount.url, { method: 'POST', headers: { 'content-type': 'application/x-www-form-urlencoded', //'token': webEngineAccount.token }, // headers: { 'Content-Type': 'application/json' }, body: queryString.stringify( pcdata ), } ) .then( ( response ) => response.json() ) .then( ( data ) => { if ( false === data.success || Number( data.code ) !== 200 ) { setRegistrationStep( 'error pin' ); } else if ( 'authenticated' === data.status ) { if ( false === data.success || Number( data.code ) !== 200 ) { setRegistrationStep( 'error pin' ); } else if ( 'authenticated' === data.status ) { onStep( 'setup' ); } else { setRegistrationStep( 'bad pin' ); } } } ); break; case 'setup': const setupdata = { action: `we360cloud_${ options.product.toLowerCase() }`, token: ajax.token, [ `we360cloud_${ options.product.toLowerCase() }_route` ]: `${ options.product.toLowerCase() }setup`, }; fetch( ajax.url, { method: 'POST', headers: { 'content-type': 'application/x-www-form-urlencoded', }, body: queryString.stringify( setupdata ), } ) .then( ( response ) => response.json() ) .then( ( json ) => { // email sent, set to 2 (success) if ( true === json.success ) { // Check if plugin has custom callback for authenticated users if ( undefined === callback ) { transition( 'authenticated' ); setUserIsRegistered( true ); } else { // if it does, hide this component and re-run the init function. // Resetting user registered to display loading screen //setVisible(false); //setLoaded(false); setUserIsRegistered( null ); init(); } } else { setError( `Error with ${ options.product } setup` ); window.setTimeout( () => { location.reload(); }, 10000 ); } } ); break; case 'error': setLoaded( true ); setRegistrationStep( 'error' ); window.setTimeout( () => { location.reload(); }, 10000 ); break; case 'activate': setRegistrationStep( 'activate' ); break; default: setRegistrationStep( 'register' ); break; } }; function transition( step: string ) { setLoaded( false ); window.setTimeout( () => { setLoaded( true ); setRegistrationStep( step ); }, 250 ); } const init = useCallback( () => { const idata = { action: 'we360cloud_account', token: webEngineAccount.token, route: 'hello', }; fetch( webEngineAccount.url, { method: 'POST', headers: { 'content-type': 'application/x-www-form-urlencoded', //'token': webEngineAccount.token }, // headers: { 'Content-Type': 'application/json' }, body: queryString.stringify( idata ), } ) .then( ( response ) => response.json() ) .then( ( data ) => { if ( false === data || ( data.code && Number( data.code ) !== 200 ) ) { onStep( 'error' ); setVisible( true ); } else { if ( data.email ) { setRegistrationEmail( data.email ); } if ( data.name ) { setRegistrationName( data.name ); } //setLogged( data.logged ); //setExpires( data.expires ); if ( ! data.products || ! data.products.includes( options.product.toLowerCase() ) ) { setUserIsRegistered( false ); if ( data.status === 'register' ) { //setLoaded( false ); setRegistrationStep( data.status ); } else if ( data.status === 'reset' ) { // malformed token. reset setRegistrationStep( 'register' ); } else { setRegistrationStep( 'activate' ); } } else { // custom callback for authenticated users // use this to override with plugin specifc screens. if ( undefined === callback ) { setUserIsRegistered( true ); setRegistrationStep( data.status ); } else { setVisible( false ); callback( data ); } } } } ); }, [] ); useEffect( () => { // Anything in here is fired on component mount. window.document.addEventListener( 'keydown', onKeyDown ); init(); return () => { // Anything in here is fired on component unmount. window.document.removeEventListener( 'keydown', onKeyDown ); }; }, [ init ] ); // Handle WordPress footer padding for proper modal spacing - only for modal mode useEffect( () => { if ( ! visible || ! loaded || isInline ) { return; } const timeoutId = setTimeout( () => { if ( ! modalRef.current ) { return; } const wpFooter = document.getElementById( 'wpfooter' ); const wpBodyContent = document.getElementById( 'wpbody-content' ); if ( ! wpFooter || ! wpBodyContent ) { return; } // Get modal and wpbody-content dimensions and positions const modalRect = modalRef.current.getBoundingClientRect(); const wpBodyRect = wpBodyContent.getBoundingClientRect(); // Calculate where the modal ends relative to wpbody-content const modalBottom = modalRect.bottom; const wpBodyBottom = wpBodyRect.bottom; // Calculate how much the modal extends beyond wpbody-content const modalOverhang = modalBottom - wpBodyBottom; // Add margin for spacing below the modal (e.g., 40px) const bottomMargin = 40; const requiredPadding = Math.max( 0, modalOverhang + bottomMargin ); console.info( 'Modal bottom:', modalBottom, 'WP Body bottom:', wpBodyBottom, 'Required padding:', requiredPadding ); // Store original styles const originalStyles = { paddingTop: wpFooter.style.paddingTop || '', position: wpFooter.style.position || '', }; // Store in a global for cleanup ( window as any )._modalFooterPadding = originalStyles; // Apply padding and position only if needed if ( requiredPadding > 0 ) { wpFooter.style.paddingTop = `${ requiredPadding }px`; wpFooter.style.position = 'relative'; } }, 100 ); return () => { clearTimeout( timeoutId ); // Restore original styles if ( ( window as any )._modalFooterPadding ) { const wpFooter = document.getElementById( 'wpfooter' ); if ( wpFooter ) { const originalStyles = ( window as any ) ._modalFooterPadding; wpFooter.style.paddingTop = originalStyles.paddingTop; wpFooter.style.position = originalStyles.position; } delete ( window as any )._modalFooterPadding; console.info( 'Restored WordPress footer styles' ); } }; }, [ visible, loaded ] ); return ( { isInline ? ( // Inline rendering
{ ( registrationStep === 'register' || registrationStep === 'registering' ) && (

{ elevator ? elevator : 'Connect to our services and enhance your experience' }

) } { registrationStep === 'unregister' && (

Are you sure?
This action will disconnect this site from the cloud service

) } { ( registrationStep === 'activate' || registrationStep === 'disconnecting' ) && (

The Web-Engine cloud service is connected, however { options.product } isn’t enabled.

) } { registrationStep === 'error' && (

An error occurred

) }
{ ( registrationStep === 'register' || registrationStep === 'registering' ) && ( ) } { ( registrationStep === 'pin sent' || registrationStep === 'bad pin' || registrationStep === 'error pin' || registrationStep === 'pin check' ) && ( ) } { registrationStep === 'authenticated' && ( ) } { ( registrationStep === 'activate' || registrationStep === 'disconnecting' ) && ( ) } { registrationStep === 'error' && ( ) }
) : ( // Modal rendering (original behavior)
setVisible( false ) } >
e.stopPropagation() } onKeyDown={ ( e ) => { if ( e.key === 'Enter' || e.key === ' ' ) { e.stopPropagation(); } } } >
{ ( registrationStep === 'register' || registrationStep === 'registering' ) && (

{ elevator ? elevator : 'Connect to our services and enhance your experience' }

) } { registrationStep === 'unregister' && (

Are you sure?
This action will disconnect this site from the cloud service

) } { ( registrationStep === 'activate' || registrationStep === 'disconnecting' ) && (

The Web-Engine cloud service is connected, however { options.product }{ ' ' } isn’t enabled.

) } { registrationStep === 'error' && (

An error occurred

) }
{ ( registrationStep === 'register' || registrationStep === 'registering' ) && ( ) } { ( registrationStep === 'pin sent' || registrationStep === 'bad pin' || registrationStep === 'error pin' || registrationStep === 'pin check' ) && ( ) } { registrationStep === 'authenticated' && ( ) } { ( registrationStep === 'activate' || registrationStep === 'disconnecting' ) && ( ) } { registrationStep === 'error' && ( ) }
) }
); };