| 1 |
import { getPageProfile } from '@page-creator/api/DataApi'; |
| 2 |
import { usePageDescriptionStore } from '@page-creator/state/cache'; |
| 3 |
import { useGlobalsStore } from '@page-creator/state/global'; |
| 4 |
import { useSiteProfileStore } from '@shared/state/site-profile'; |
| 5 |
import { useEffect } from '@wordpress/element'; |
| 6 |
import { __ } from '@wordpress/i18n'; |
| 7 |
import useSWRImmutable from 'swr/immutable'; |
| 8 |
|
| 9 |
// Returns the site profile and page profile |
| 10 |
export const usePageProfile = () => { |
| 11 |
const { description } = usePageDescriptionStore(); |
| 12 |
const { setProgress, regenerationCount } = useGlobalsStore(); |
| 13 |
const { siteProfile } = useSiteProfileStore(); |
| 14 |
|
| 15 |
const { data, error } = useSWRImmutable( |
| 16 |
{ key: `page-profile-${regenerationCount}`, description, siteProfile }, |
| 17 |
getPageProfile, |
| 18 |
); |
| 19 |
|
| 20 |
useEffect(() => { |
| 21 |
if (data) return; |
| 22 |
setProgress(__('Generating AI page profile...', 'extendify-local')); |
| 23 |
}, [data, setProgress]); |
| 24 |
|
| 25 |
return { |
| 26 |
pageProfile: data, |
| 27 |
error, |
| 28 |
loading: !data && !error, |
| 29 |
}; |
| 30 |
}; |
| 31 |
|