| 1 |
import { |
| 2 |
getGeneratedPageTemplate, |
| 3 |
getImprintPageTemplate, |
| 4 |
} from '@page-creator/api/DataApi'; |
| 5 |
import { usePageProfile } from '@page-creator/hooks/usePageProfile'; |
| 6 |
import { useSiteImages } from '@page-creator/hooks/useSiteImages'; |
| 7 |
import { useSitePlugins } from '@page-creator/hooks/useSitePlugins'; |
| 8 |
import { useGlobalsStore } from '@page-creator/state/global'; |
| 9 |
import { useEffect } from '@wordpress/element'; |
| 10 |
import { __ } from '@wordpress/i18n'; |
| 11 |
import useSWRImmutable from 'swr/immutable'; |
| 12 |
|
| 13 |
export const usePageLayout = () => { |
| 14 |
const { pageProfile } = usePageProfile(); |
| 15 |
const { siteImages } = useSiteImages(); |
| 16 |
const { sitePlugins } = useSitePlugins(); |
| 17 |
const loading = !pageProfile || !siteImages || !sitePlugins; |
| 18 |
const { setProgress, regenerationCount } = useGlobalsStore(); |
| 19 |
|
| 20 |
const params = { |
| 21 |
key: `page-creator-page-layout-${regenerationCount}`, |
| 22 |
pageProfile, |
| 23 |
siteImages, |
| 24 |
sitePlugins, |
| 25 |
}; |
| 26 |
|
| 27 |
const fetcher = pageProfile?.isImprintPage |
| 28 |
? () => getImprintPageTemplate() |
| 29 |
: getGeneratedPageTemplate; |
| 30 |
|
| 31 |
const { data, error } = useSWRImmutable(loading ? null : params, fetcher); |
| 32 |
|
| 33 |
useEffect(() => { |
| 34 |
if (data) return; |
| 35 |
setProgress(__('Creating a custom layout...', 'extendify-local')); |
| 36 |
}, [data, setProgress]); |
| 37 |
|
| 38 |
return { template: data?.template ?? data, error, loading: !data && !error }; |
| 39 |
}; |
| 40 |
|