import { useState, useEffect } from '@wordpress/element'; import apiFetch from '@wordpress/api-fetch'; import { TextControl } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; export default function LinkPicker(props) { const [activeKeyword, setActiveKeyword] = useState(''); const [links, setLinks] = useState< Array<{ id: number; url: string; title: string; post_type: string }> >([]); const [activeLink, setActiveLink] = useState({ url: '', title: '', }); useEffect(() => { if (activeKeyword === '') return; setLinks(null); apiFetch({ path: `/blockbite/v1/block-helpers/get-links/${activeKeyword}`, }).then( ( links: Array<{ id: number; url: string; title: string; post_type: string; }> | null ) => { if (links?.length) { setLinks([...links]); } else { setLinks([]); } } ); }, [activeKeyword]); useEffect(() => { if (activeLink.url !== '') { props.parentCallback(activeLink); } }, [activeLink]); return ( <>
Loading...
; } else if (links.length === 0) { returnNo Results
; } else { // iterate over the links and show img with icon based on icon_url and icon links.forEach((link) => { list.push( // add key