| 1 |
import { Spinner } from '@wordpress/components'; |
| 2 |
import { useState, useEffect } from '@wordpress/element'; |
| 3 |
import { __ } from '@wordpress/i18n'; |
| 4 |
import { useInView } from 'react-intersection-observer'; |
| 5 |
import Masonry from 'react-masonry-css'; |
| 6 |
import { BlockPreviewButton } from '@library/components/BlockPreviewButton'; |
| 7 |
import { usePatterns } from '@library/hooks/usePatterns'; |
| 8 |
|
| 9 |
export const ModalContent = ({ insertPattern, siteType, category }) => { |
| 10 |
const { data, isLoading, setSize } = usePatterns({ |
| 11 |
siteType: siteType?.slug, |
| 12 |
category, |
| 13 |
}); |
| 14 |
const [showLoading, setShowLoading] = useState(true); |
| 15 |
const [loadMoreRef, inView] = useInView(); |
| 16 |
const noMore = data?.at(-1)?.length < 9; // hard coded for now |
| 17 |
|
| 18 |
useEffect(() => { |
| 19 |
if (isLoading) { |
| 20 |
return setShowLoading(true); |
| 21 |
} |
| 22 |
const id = setTimeout(() => { |
| 23 |
setShowLoading(false); |
| 24 |
}, 750); |
| 25 |
return () => clearTimeout(id); |
| 26 |
}, [isLoading]); |
| 27 |
|
| 28 |
useEffect(() => { |
| 29 |
if (!inView || isLoading) return; |
| 30 |
setSize((size) => size + 1); |
| 31 |
}, [inView, isLoading, setSize]); |
| 32 |
|
| 33 |
if (isLoading || !data?.length) { |
| 34 |
return ( |
| 35 |
<div className="absolute inset-0 flex flex-col items-center justify-center text-center"> |
| 36 |
<Spinner /> |
| 37 |
<span className="sr-only"> |
| 38 |
{__('Loading Patterns...', 'extendify-local')} |
| 39 |
</span> |
| 40 |
</div> |
| 41 |
); |
| 42 |
} |
| 43 |
|
| 44 |
return ( |
| 45 |
<> |
| 46 |
<Masonry |
| 47 |
breakpointCols={{ |
| 48 |
default: 3, |
| 49 |
1600: 2, |
| 50 |
1000: 1, |
| 51 |
783: 2, |
| 52 |
600: 1, |
| 53 |
}} |
| 54 |
columnClassName="" |
| 55 |
className="relative flex w-full gap-6 p-8 pt-2"> |
| 56 |
{data.map((p) => |
| 57 |
p.map(({ id, code, patternReplacementCode }) => ( |
| 58 |
<BlockPreviewButton |
| 59 |
key={id} |
| 60 |
insertPattern={insertPattern} |
| 61 |
code={patternReplacementCode ?? code} |
| 62 |
/> |
| 63 |
)), |
| 64 |
)} |
| 65 |
</Masonry> |
| 66 |
{showLoading ? ( |
| 67 |
<div className="absolute inset-0 z-0 flex flex-col items-center justify-center text-center"> |
| 68 |
<Spinner /> |
| 69 |
<span className="sr-only"> |
| 70 |
{__('Loading Patterns...', 'extendify-local')} |
| 71 |
</span> |
| 72 |
</div> |
| 73 |
) : null} |
| 74 |
{showLoading || noMore ? null : ( |
| 75 |
<div |
| 76 |
ref={loadMoreRef} |
| 77 |
className="mb-6 mt-6 flex w-full justify-center md:mt-2"> |
| 78 |
<Spinner /> |
| 79 |
<span className="sr-only"> |
| 80 |
{__('Loading more patterns...', 'extendify-local')} |
| 81 |
</span> |
| 82 |
</div> |
| 83 |
)} |
| 84 |
</> |
| 85 |
); |
| 86 |
}; |
| 87 |
|