| 1 |
import { getPageImages } from '@page-creator/api/DataApi'; |
| 2 |
import { usePageProfile } from '@page-creator/hooks/usePageProfile'; |
| 3 |
import { useGlobalsStore } from '@page-creator/state/global'; |
| 4 |
import { useEffect } from '@wordpress/element'; |
| 5 |
import { __ } from '@wordpress/i18n'; |
| 6 |
import useSWRImmutable from 'swr/immutable'; |
| 7 |
|
| 8 |
export const useSiteImages = () => { |
| 9 |
const { loading, pageProfile } = usePageProfile(); |
| 10 |
const { setProgress, regenerationCount } = useGlobalsStore(); |
| 11 |
|
| 12 |
const { data, error } = useSWRImmutable( |
| 13 |
loading ? null : { key: `page-images-${regenerationCount}`, pageProfile }, |
| 14 |
getPageImages, |
| 15 |
); |
| 16 |
|
| 17 |
useEffect(() => { |
| 18 |
if (data) return; |
| 19 |
setProgress(__('Finding images...', 'extendify-local')); |
| 20 |
}, [data, setProgress]); |
| 21 |
|
| 22 |
return { siteImages: data, error, loading: !data && !error }; |
| 23 |
}; |
| 24 |
|