import { useEffect, useMemo, useState } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { useUi } from '../runtime'; import { PhoneFrame, PhoneInbox } from './InboxPreview'; /** * A campaign's HTML as a sandboxed blob address, wrapped in a document when * it is only a fragment. * * @param {string} html The campaign's `content_html`. * @return {string} A blob URL, or '' when there is no content. */ function useEmailUrl( html ) { const url = useMemo( () => { if ( ! html ) { return ''; } const lower = html.trim().toLowerCase(); const doc = lower.startsWith( '
' + html + ''; return URL.createObjectURL( new Blob( [ doc ], { type: 'text/html' } ) ); }, [ html ] ); useEffect( () => () => url && URL.revokeObjectURL( url ), [ url ] ); return url; } /** * A small, still picture of the email for a summary row. * * @param {Object} props * @param {string} props.html The campaign's `content_html`. */ export function EmailThumb( { html } ) { const url = useEmailUrl( html ); if ( ! url ) { return null; } return ( ); } /** * The whole email, read-only, on the page at its own height: Review shows * what goes out. The frame runs no scripts; same-origin only lets it be * measured. * * @param {Object} props * @param {string} props.html The campaign's `content_html`. */ export function EmailFrame( { html } ) { const url = useEmailUrl( html ); const [ height, setHeight ] = useState( 480 ); if ( ! url ) { return null; } // A document is never shorter than its frame, so collapse the frame // before measuring; reading the height lays the email out again. const fit = ( e ) => { const frame = e.currentTarget; try { frame.style.height = '0px'; const h = frame.contentDocument?.documentElement?.scrollHeight || 0; setHeight( h > 0 ? h : 480 ); frame.style.height = `${ h > 0 ? h : 480 }px`; } catch { // Unreadable frame: keep the default height and let it scroll. frame.style.height = '480px'; } }; return ( ); } /** * The whole email, read-only, in a dialog: at desktop width, on a phone, and * as it arrives in a phone's inbox (sender, subject and preview text). * * @param {Object} props * @param {boolean} props.open Whether the dialog is open. * @param {Function} props.onOpenChange Receives the new open state. * @param {string} props.html The campaign's `content_html`. * @param {string} props.title The dialog's title. * @param {Object} props.campaign The campaign, for its subject, preview * text and sender. * @param {string} props.fromName The sender's name, when the caller * knows it better than the campaign. * @param {string} props.fromEmail The sender's address, likewise. */ export function EmailPreviewDialog( { open, onOpenChange, html, title, campaign = {}, fromName, fromEmail, } ) { const { ui } = useUi(); const { Dialog, SegmentedControl } = ui; const [ device, setDevice ] = useState( 'desktop' ); const url = useEmailUrl( open ? html : '' ); const site = window.mailpuffData?.delivery || {}; const frame = ( className ) => ( ); let view = url ? ( frame( 'crm-frame crm-frame--desktop' ) ) : ({ __( 'This email has no content.', 'mailpuff' ) }
); if ( 'mobile' === device && url ) { // The phone's screen is 375px wide, where the email's mobile layout // takes over. view =