2024-03-18 07:44:32 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import classnames from 'classnames';
|
2024-03-28 09:19:35 +00:00
|
|
|
import { useState, useRef, useEffect } from '@wordpress/element';
|
|
|
|
import { Spinner } from '@woocommerce/components';
|
|
|
|
|
2024-03-18 07:44:32 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2024-03-28 09:19:35 +00:00
|
|
|
import { getAdminSetting } from '~/utils/admin-settings';
|
2024-03-18 07:44:32 +00:00
|
|
|
import type { MainContentComponentProps } from '../xstate';
|
2024-03-28 09:19:35 +00:00
|
|
|
import './site-preview.scss';
|
2024-03-18 07:44:32 +00:00
|
|
|
|
|
|
|
export const SitePreviewPage = ( props: MainContentComponentProps ) => {
|
2024-03-28 09:19:35 +00:00
|
|
|
const siteUrl = getAdminSetting( 'siteUrl' ) + '?site-preview=1';
|
|
|
|
const [ isLoading, setIsLoading ] = useState( true );
|
|
|
|
const iframeRef = useRef< HTMLIFrameElement >( null );
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
const iframeContentWindow = iframeRef.current?.contentWindow;
|
|
|
|
|
|
|
|
const beforeUnloadHandler = () => {
|
|
|
|
setIsLoading( true );
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( iframeContentWindow ) {
|
|
|
|
iframeContentWindow.addEventListener(
|
|
|
|
'beforeunload',
|
|
|
|
beforeUnloadHandler
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return () => {
|
|
|
|
if ( iframeContentWindow ) {
|
|
|
|
iframeContentWindow.removeEventListener(
|
|
|
|
'beforeunload',
|
|
|
|
beforeUnloadHandler
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// IsLoading is a dependency because we want to reset it when the iframe reloads.
|
|
|
|
}, [ iframeRef, setIsLoading, isLoading ] );
|
|
|
|
|
2024-03-18 07:44:32 +00:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={ classnames(
|
|
|
|
'launch-store-site-preview-page__container',
|
2024-03-28 09:19:35 +00:00
|
|
|
{ 'is-loading': isLoading },
|
2024-03-18 07:44:32 +00:00
|
|
|
props.className
|
|
|
|
) }
|
|
|
|
>
|
2024-03-28 09:19:35 +00:00
|
|
|
{ isLoading && (
|
|
|
|
<div className="launch-store-site-preview-site__loading-overlay">
|
|
|
|
<Spinner />
|
|
|
|
</div>
|
|
|
|
) }
|
|
|
|
<iframe
|
|
|
|
ref={ iframeRef }
|
|
|
|
className="launch-store-site__preview-site-iframe"
|
|
|
|
src={ siteUrl }
|
|
|
|
title="Preview"
|
|
|
|
onLoad={ () => setIsLoading( false ) }
|
|
|
|
/>
|
2024-03-18 07:44:32 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|