| 1 |
import { updateOption } from '@page-creator/api/WPApi'; |
| 2 |
import { VideoPlayer } from '@page-creator/components/content/VideoPlayer'; |
| 3 |
import { usePageCustomContent } from '@page-creator/hooks/usePageCustomContent'; |
| 4 |
import { processPatterns } from '@page-creator/lib/processPatterns'; |
| 5 |
import { useGlobalsStore } from '@page-creator/state/global'; |
| 6 |
import { installBlocks } from '@page-creator/util/installBlocks'; |
| 7 |
import { syncPageTitleTemplate } from '@page-creator/util/syncPageTitleTemplate'; |
| 8 |
import { rawHandler } from '@wordpress/blocks'; |
| 9 |
import { useDispatch, useSelect } from '@wordpress/data'; |
| 10 |
import { store as editorStore } from '@wordpress/editor'; |
| 11 |
import { useEffect, useRef, useState } from '@wordpress/element'; |
| 12 |
import { __ } from '@wordpress/i18n'; |
| 13 |
|
| 14 |
const { pageTitlePattern } = window.extPageCreator; |
| 15 |
|
| 16 |
export const GeneratingPage = ({ insertPage }) => { |
| 17 |
const { page, loading } = usePageCustomContent(); |
| 18 |
const { progress, setProgress } = useGlobalsStore(); |
| 19 |
const { editPost } = useDispatch(editorStore); |
| 20 |
const [patterns, setPatterns] = useState([]); |
| 21 |
const once = useRef(false); |
| 22 |
const templateSet = useRef(false); |
| 23 |
const { theme, templates } = useSelect((select) => { |
| 24 |
const core = select('core'); |
| 25 |
const current = core.getCurrentTheme(); |
| 26 |
|
| 27 |
return { |
| 28 |
theme: current, |
| 29 |
templates: core.getEntityRecords('postType', 'wp_template', { |
| 30 |
per_page: -1, |
| 31 |
context: 'edit', |
| 32 |
theme: current?.stylesheet, |
| 33 |
}), |
| 34 |
}; |
| 35 |
}, []); |
| 36 |
|
| 37 |
useEffect(() => { |
| 38 |
if (!page && loading) return; |
| 39 |
if (once.current) return; |
| 40 |
once.current = true; |
| 41 |
|
| 42 |
setProgress( |
| 43 |
__( |
| 44 |
'Processing patterns and installing required plugins...', |
| 45 |
'extendify-local', |
| 46 |
), |
| 47 |
); |
| 48 |
(async () => { |
| 49 |
// If page-with-title template isn’t customized and a page-title pattern is stashed, update the template with it. |
| 50 |
await syncPageTitleTemplate(pageTitlePattern); |
| 51 |
|
| 52 |
const patterns = await processPatterns(page?.patterns); |
| 53 |
await installBlocks({ patterns }); |
| 54 |
setPatterns(patterns); |
| 55 |
})(); |
| 56 |
}, [loading, page, setPatterns, setProgress]); |
| 57 |
|
| 58 |
useEffect(() => { |
| 59 |
if (!patterns?.length || !once.current) return; |
| 60 |
if (!theme || !Array.isArray(templates)) return; |
| 61 |
|
| 62 |
const isExtendable = theme.textdomain === 'extendable'; |
| 63 |
const hasPageWithTitle = |
| 64 |
isExtendable && templates.some((t) => t.slug === 'page-with-title'); |
| 65 |
|
| 66 |
const patternsToInsert = hasPageWithTitle |
| 67 |
? patterns.filter((p) => !p.patternTypes?.includes('page-title')) |
| 68 |
: patterns; |
| 69 |
|
| 70 |
const code = patternsToInsert.flatMap(({ code }) => { |
| 71 |
// find links with #extendify- like href="#extendify-hero-cta" |
| 72 |
const linksRegex = /href="#extendify-([^"]+)"/g; |
| 73 |
const c = code.replaceAll(linksRegex, 'href="#"'); |
| 74 |
|
| 75 |
return rawHandler({ HTML: c }); |
| 76 |
}); |
| 77 |
|
| 78 |
if (!templateSet.current && isExtendable) { |
| 79 |
const slug = hasPageWithTitle ? 'page-with-title' : 'no-title'; |
| 80 |
editPost({ template: slug }).catch(() => { |
| 81 |
/* silent */ |
| 82 |
}); |
| 83 |
templateSet.current = true; |
| 84 |
} |
| 85 |
|
| 86 |
// Signal to the importer to check for images |
| 87 |
updateOption('extendify_check_for_image_imports', true); |
| 88 |
|
| 89 |
const id = setTimeout(() => insertPage(code, page.title), 1000); |
| 90 |
|
| 91 |
return () => clearTimeout(id); |
| 92 |
}, [insertPage, patterns, editPost, page, theme, templates]); |
| 93 |
|
| 94 |
return ( |
| 95 |
<div className="mx-auto flex grow items-center justify-center"> |
| 96 |
<div className="mx-auto flex h-full flex-col justify-center"> |
| 97 |
<VideoPlayer |
| 98 |
poster={`${window.extSharedData.assetPath}/site-building.webp`} |
| 99 |
path="https://images.extendify-cdn.com/launch/site-building.webm" |
| 100 |
className="mx-auto h-auto w-50 md:w-100" |
| 101 |
/> |
| 102 |
{progress && ( |
| 103 |
<p className="text-center text-lg" aria-live="polite"> |
| 104 |
{progress} |
| 105 |
</p> |
| 106 |
)} |
| 107 |
</div> |
| 108 |
</div> |
| 109 |
); |
| 110 |
}; |
| 111 |
|