File "index.js"

Full Path: /home/warrior1/public_html/languages/wp-content/plugins/astra-sites/inc/lib/onboarding/assets/src/components/site-preview/index.js
File size: 1.41 KB
MIME-type: text/plain
Charset: utf-8

import React, { memo, useEffect, useState } from 'react';
import { sendPostMessage } from '../../utils/functions';
import { useStateValue } from '../../store/store';
import { prependHTTPS } from '../../utils/prepend-https';
import { stripSlashes } from '../../utils/strip-slashes';
import { addTrailingSlash } from '../../utils/add-trailing-slash';
import SiteSkeleton from './site-skeleton';

const SitePreview = () => {
	const [ { templateResponse, siteLogo } ] = useStateValue();
	const [ previewUrl, setPreviewUrl ] = useState( '' );
	const [ loading, setLoading ] = useState( true );

	useEffect( () => {
		const url = templateResponse
			? templateResponse[ 'astra-site-url' ]
			: '';

		if ( url !== '' ) {
			setPreviewUrl(
				addTrailingSlash( prependHTTPS( stripSlashes( url ) ) )
			);
		}
	}, [ templateResponse ] );

	useEffect( () => {
		if ( loading !== false ) {
			return;
		}

		sendPostMessage( {
			param: 'cleanStorage',
			data: siteLogo,
		} );
	}, [ loading ] );

	const handleIframeLoading = () => {
		setLoading( false );
	};

	return (
		<>
			{ loading ? <SiteSkeleton /> : null }
			{ previewUrl !== '' && (
				<iframe
					id="astra-starter-templates-preview"
					title="Website Preview"
					height="100%"
					width="100%"
					src={ previewUrl }
					onLoad={ handleIframeLoading }
				/>
			) }
		</>
	);
};

export default memo( SitePreview );