PluginProbe
Extendify / 3.2.1
Extendify v3.2.1
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
extendify / src / Draft / pages / Unsplash.jsx

Unsplash.jsx in Extendify 3.2.1, at src/Draft/pages/Unsplash.jsx

109 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { addImageToBlock } from '@draft/api/WPApi';
2 import { UnsplashImages } from '@draft/components/stock-images/UnsplashImages';
3 import { useRouter } from '@draft/hooks/useRouter';
4 import { useUnsplashImages } from '@draft/hooks/useUnsplashImages';
5 import { backArrow } from '@draft/svg/BackArrow';
6 import { downloadImage } from '@shared/api/wp';
7 import { store as blockEditorStore } from '@wordpress/block-editor';
8 import {
9 __experimentalDivider as Divider,
10 __experimentalHeading as Heading,
11 SearchControl,
12 } from '@wordpress/components';
13 import { useDispatch, useSelect } from '@wordpress/data';
14 import { useEffect, useState } from '@wordpress/element';
15 import { __ } from '@wordpress/i18n';
16 import classNames from 'classnames';
17
18 export const Unsplash = () => {
19 const { goBack } = useRouter();
20 const [search, setSearch] = useState('');
21 const [searchDebounced, setSearchDebounced] = useState('');
22 const [searching, setSearching] = useState(false);
23 const { data: images, loading } = useUnsplashImages(searchDebounced, 'user');
24 const [isInsertingImage, setIsInsertingImage] = useState(null);
25
26 const selectedBlock = useSelect(
27 (select) => select(blockEditorStore).getSelectedBlock(),
28 [],
29 );
30 const { updateBlockAttributes } = useDispatch(blockEditorStore);
31
32 const handleClick = async (image) => {
33 if (isInsertingImage) return;
34 setIsInsertingImage(image);
35 try {
36 const downloadedImage = await downloadImage(
37 image.requestMetadata?.id,
38 image.urls?.regular,
39 'unsplash',
40 image.id,
41 );
42 addImageToBlock(selectedBlock, downloadedImage, updateBlockAttributes);
43 } catch (error) {
44 console.error(error);
45 } finally {
46 setIsInsertingImage(null);
47 }
48 };
49
50 useEffect(() => {
51 setSearching(false);
52 }, [searchDebounced]);
53
54 useEffect(() => {
55 if (!search) return setSearchDebounced('');
56 const id = setTimeout(() => setSearchDebounced(search), 750);
57 return () => clearTimeout(id);
58 }, [search]);
59
60 return (
61 <>
62 <div className="flex h-12 items-center gap-1 pl-1">
63 <button
64 className="h-9 w-9 border-0 bg-transparent"
65 onClick={goBack}
66 type="button"
67 aria-label={__('Go Back', 'extendify-local')}
68 >
69 {backArrow}
70 </button>
71 <Heading className="mb-0">
72 {__('Photos from Unsplash', 'extendify-local')}
73 </Heading>
74 </div>
75 <Divider className="my-0 border-gray-150" />
76 <div className="flex flex-col gap-2 p-4">
77 <SearchControl
78 autoFocus
79 // This wp component has no real disabled state
80 className={classNames({
81 'pointer-events-none bg-gray-150 opacity-50': isInsertingImage,
82 })}
83 disabled={isInsertingImage}
84 aria-disabled={isInsertingImage}
85 onChange={(value) => {
86 if (isInsertingImage) return;
87 setSearch(value);
88 }}
89 value={search}
90 />
91 <UnsplashImages
92 images={images}
93 isInsertingImage={isInsertingImage}
94 onClick={handleClick}
95 loading={loading || searching}
96 />
97 </div>
98 </>
99 );
100 };
101
102 export const routes = [
103 {
104 slug: 'unsplash',
105 title: __('Unsplash', 'extendify-local'),
106 component: Unsplash,
107 },
108 ];
109