import { store as blockEditorStore } from '@wordpress/block-editor'; import { __experimentalHeading as Heading, __experimentalDivider as Divider, } from '@wordpress/components'; import { SearchControl } from '@wordpress/components'; import { useSelect, useDispatch } from '@wordpress/data'; import { useEffect, useState } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import classNames from 'classnames'; import { downloadImage, addImageToBlock } from '@draft/api/WPApi'; import { UnsplashImages } from '@draft/components/stock-images/UnsplashImages'; import { useRouter } from '@draft/hooks/useRouter'; import { useUnsplashImages } from '@draft/hooks/useUnsplashImages'; import { backArrow } from '@draft/svg/BackArrow'; export const Unsplash = () => { const { goBack } = useRouter(); const [search, setSearch] = useState(''); const [searchDebounced, setSearchDebounced] = useState(''); const [searching, setSearching] = useState(false); const { data: images, loading } = useUnsplashImages(searchDebounced); const [isInsertingImage, setIsInsertingImage] = useState(null); const selectedBlock = useSelect( (select) => select(blockEditorStore).getSelectedBlock(), [], ); const { updateBlockAttributes } = useDispatch(blockEditorStore); const handleClick = async (image) => { if (isInsertingImage) return; setIsInsertingImage(image); try { const downloadedImage = await downloadImage( image.requestMetadata?.id, image.urls?.regular, 'unsplash', image.id, ); addImageToBlock(selectedBlock, downloadedImage, updateBlockAttributes); } catch (error) { console.log(error); } finally { setIsInsertingImage(null); } }; useEffect(() => { setSearching(false); }, [searchDebounced]); useEffect(() => { if (!search) return setSearchDebounced(''); const id = setTimeout(() => setSearchDebounced(search), 750); return () => clearTimeout(id); }, [search]); return ( <>
{__('Photos from Unsplash', 'extendify-local')}
{ if (isInsertingImage) return; setSearch(value); }} value={search} />
); }; export const routes = [ { slug: 'unsplash', title: __('Unsplash', 'extendify-local'), component: Unsplash, }, ];