| 1 |
import ElementorLoading from 'elementor-app/molecules/elementor-loading'; |
| 2 |
import ItemHeader from '../../components/item-header'; |
| 3 |
import Layout from '../../components/layout'; |
| 4 |
import PageLoader from '../../components/page-loader'; |
| 5 |
import PreviewResponsiveControls from './preview-responsive-controls'; |
| 6 |
import useKit from '../../hooks/use-kit'; |
| 7 |
import usePageTitle from 'elementor-app/hooks/use-page-title'; |
| 8 |
import { PreviewIframe } from './preview-iframe'; |
| 9 |
import { useLocation, useNavigate } from '@reach/router'; |
| 10 |
import { useState, useMemo } from 'react'; |
| 11 |
import { appsEventTrackingDispatch } from 'elementor-app/event-track/apps-event-tracking'; |
| 12 |
|
| 13 |
import './preview.scss'; |
| 14 |
|
| 15 |
export const breakpoints = [ |
| 16 |
{ |
| 17 |
value: 'desktop', |
| 18 |
label: __( 'Desktop', 'elementor' ), |
| 19 |
style: { |
| 20 |
width: '100%', |
| 21 |
height: '100%', |
| 22 |
}, |
| 23 |
}, |
| 24 |
{ |
| 25 |
value: 'tablet', |
| 26 |
label: __( 'Tablet', 'elementor' ), |
| 27 |
style: { |
| 28 |
marginTop: '30px', |
| 29 |
marginBottom: '30px', |
| 30 |
width: '768px', |
| 31 |
height: '1024px', |
| 32 |
}, |
| 33 |
}, |
| 34 |
{ |
| 35 |
value: 'mobile', |
| 36 |
label: __( 'Mobile', 'elementor' ), |
| 37 |
style: { |
| 38 |
marginTop: '30px', |
| 39 |
marginBottom: '30px', |
| 40 |
width: '375px', |
| 41 |
height: '667px', |
| 42 |
}, |
| 43 |
}, |
| 44 |
]; |
| 45 |
|
| 46 |
function useHeaderButtons( id, kitName ) { |
| 47 |
const navigate = useNavigate(); |
| 48 |
const eventTracking = ( command, viewTypeClicked, eventType = 'click' ) => { |
| 49 |
appsEventTrackingDispatch( |
| 50 |
command, |
| 51 |
{ |
| 52 |
kit_name: kitName, |
| 53 |
element_position: 'app_header', |
| 54 |
page_source: 'view demo', |
| 55 |
view_type_clicked: viewTypeClicked, |
| 56 |
event_type: eventType, |
| 57 |
}, |
| 58 |
); |
| 59 |
}; |
| 60 |
return useMemo( () => [ |
| 61 |
{ |
| 62 |
id: 'overview', |
| 63 |
text: __( 'Overview', 'elementor' ), |
| 64 |
hideText: false, |
| 65 |
variant: 'outlined', |
| 66 |
color: 'secondary', |
| 67 |
size: 'sm', |
| 68 |
onClick: () => { |
| 69 |
eventTracking( 'kit-library/view-overview-page', 'overview' ); |
| 70 |
navigate( `/kit-library/overview/${ id }` ); |
| 71 |
}, |
| 72 |
includeHeaderBtnClass: false, |
| 73 |
}, |
| 74 |
], [ id ] ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Get preview url. |
| 79 |
* |
| 80 |
* @param {*} data |
| 81 |
* @return {null|string} Preview URL |
| 82 |
*/ |
| 83 |
function usePreviewUrl( data ) { |
| 84 |
const location = useLocation(); |
| 85 |
|
| 86 |
return useMemo( () => { |
| 87 |
if ( ! data ) { |
| 88 |
return null; |
| 89 |
} |
| 90 |
|
| 91 |
const documentId = new URLSearchParams( location.pathname.split( '?' )?.[ 1 ] ).get( 'document_id' ), |
| 92 |
utm = '?utm_source=kit-library&utm_medium=wp-dash&utm_campaign=preview', |
| 93 |
previewUrl = data.previewUrl ? data.previewUrl + utm : data.previewUrl; |
| 94 |
|
| 95 |
if ( ! documentId ) { |
| 96 |
return previewUrl; |
| 97 |
} |
| 98 |
|
| 99 |
const documentPreviewUrl = data.documents.find( ( item ) => item.id === parseInt( documentId ) )?.previewUrl || previewUrl; |
| 100 |
return documentPreviewUrl ? documentPreviewUrl + utm : documentPreviewUrl; |
| 101 |
}, [ location, data ] ); |
| 102 |
} |
| 103 |
|
| 104 |
export default function Preview( props ) { |
| 105 |
const { data, isError, isLoading } = useKit( props.id ); |
| 106 |
const [ isIframeLoading, setIsIframeLoading ] = useState( true ); |
| 107 |
const headersButtons = useHeaderButtons( props.id, data && data.title ); |
| 108 |
const previewUrl = usePreviewUrl( data ); |
| 109 |
const [ activeDevice, setActiveDevice ] = useState( 'desktop' ); |
| 110 |
const iframeStyle = useMemo( |
| 111 |
() => breakpoints.find( ( { value } ) => value === activeDevice ).style, |
| 112 |
[ activeDevice ], |
| 113 |
); |
| 114 |
const eventTracking = ( command, layout, elementPosition = null, eventType = 'click' ) => { |
| 115 |
appsEventTrackingDispatch( |
| 116 |
command, |
| 117 |
{ |
| 118 |
kit_name: data.title, |
| 119 |
page_source: 'view demo', |
| 120 |
layout, |
| 121 |
element_position: elementPosition, |
| 122 |
event_type: eventType, |
| 123 |
}, |
| 124 |
); |
| 125 |
}; |
| 126 |
|
| 127 |
const onChange = ( device ) => { |
| 128 |
setActiveDevice( device ); |
| 129 |
eventTracking( 'kit-library/responsive-controls', device, 'app_header' ); |
| 130 |
}; |
| 131 |
|
| 132 |
usePageTitle( { |
| 133 |
title: data |
| 134 |
? `${ __( 'Kit Library', 'elementor' ) } | ${ data.title }` |
| 135 |
// eslint-disable-next-line @wordpress/i18n-ellipsis |
| 136 |
: __( 'Loading...', 'elementor' ), |
| 137 |
} ); |
| 138 |
|
| 139 |
if ( isError ) { |
| 140 |
// Will be caught by the App error boundary. |
| 141 |
throw new Error(); |
| 142 |
} |
| 143 |
|
| 144 |
if ( isLoading ) { |
| 145 |
return <ElementorLoading />; |
| 146 |
} |
| 147 |
|
| 148 |
return ( |
| 149 |
<Layout header={ |
| 150 |
<ItemHeader |
| 151 |
model={ data } |
| 152 |
buttons={ headersButtons } |
| 153 |
centerColumn={ <PreviewResponsiveControls active={ activeDevice } onChange={ ( device ) => onChange( device ) } kitName={ data.title } /> } |
| 154 |
pageId="demo" |
| 155 |
/> |
| 156 |
}> |
| 157 |
{ isIframeLoading && <PageLoader className="e-kit-library__preview-loader" /> } |
| 158 |
{ |
| 159 |
previewUrl && |
| 160 |
<PreviewIframe |
| 161 |
previewUrl={ previewUrl } |
| 162 |
style={ iframeStyle } |
| 163 |
onLoaded={ () => setIsIframeLoading( false ) } |
| 164 |
/> |
| 165 |
} |
| 166 |
</Layout> |
| 167 |
); |
| 168 |
} |
| 169 |
|
| 170 |
Preview.propTypes = { |
| 171 |
id: PropTypes.string, |
| 172 |
}; |
| 173 |
|
| 174 |
|