import { Icon } from "@wordpress/components"; import { useEffect, useRef, useState } from "@wordpress/element"; import { __ } from "@wordpress/i18n"; import classnames from "classnames"; import { blockIconThin } from "../icons"; import { useGlobalState } from "../state/global"; import { defaultTerms } from "../suggested-search"; import { PhpMaxFileSizeWarning } from "./Errors"; import { SearchSuggestions } from "./SearchSuggestions"; import { SettingsModal } from "./SettingsModal"; export const Sidebar = ({ initialFocus, }: { initialFocus: React.RefObject; }) => { const { page, setPage, searchTerm, setSearchTerm, importing, imageSize, currentTheme, recent, setRecent, deleteRecent, } = useGlobalState(); const [search, setSearch] = useState(""); const [showImportWarning, setShowImportWarning] = useState(false); const touched = useRef(false); const textShadow = { textShadow: currentTheme === "midnight" ? "0 0 4px white" : undefined, }; useEffect(() => { if (imageSize === "regular") { setShowImportWarning(false); return; } // @ts-expect-error-next-line if (Number(window?.unlimitedPhotosConfig?.maxUploadSize) < 3) { setShowImportWarning(true); } }, [imageSize]); useEffect(() => { if (!initialFocus?.current) return; initialFocus.current.focus(); }, [page, initialFocus]); useEffect(() => { if (!searchTerm) return; setSearch(searchTerm); setRecent(searchTerm); }, [setSearch, searchTerm, setRecent]); useEffect(() => { let timerId = 0; if (importing) return; if (searchTerm === search) return; if (!touched.current) return; if (!search) { setPage(1); setSearchTerm(""); return; } window.clearTimeout(timerId); timerId = window.setTimeout(() => { setPage(1); setSearchTerm(search); }, 500); return () => window.clearTimeout(timerId); }, [search, setSearchTerm, importing, setPage, searchTerm]); return (
blockIconThin(currentTheme === "midnight" ? "#ded8e6" : "#1e1e1e") } size={24} />

Unlimited Photos

e.preventDefault()}>
{ touched.current = true; setSearch(e.target.value); }} type="search" name="unlimited-photos-search" id="unlimited-photos-search" disabled={Boolean(importing)} className={classnames( "m-0 block rounded-none w-full sm:text-sm outline-none focus:outline-none ring-main-blue focus:shadow-none focus:ring-wp", { "bg-gray-400": importing, "text-main-grayish border-gray-900 focus:border-gray-900 bg-main-magenta": currentTheme === "midnight", "border-gray-700 focus:border-gray-700 bg-gray-100 text-gray-900": currentTheme !== "midnight", }, )} aria-describedby="search-description" />

{__( "Search millions of photos, textures, wallpapers, and more.", "unlimited-photos", )}

{showImportWarning && ( )}
{recent?.length > 0 && (

{__("Recent", "unlmiited-photos")}

{ touched.current = true; setPage(1); setSearchTerm(term.toLowerCase()); setSearch(term.toLowerCase()); }} handleDelete={deleteRecent} />
)}

{__("Suggestions", "unlmiited-photos")}

{ touched.current = true; setPage(1); setSearchTerm(term.toLowerCase()); setSearch(term.toLowerCase()); }} />
); };