import { useCallback, useEffect } from "react"; import { useForm } from "react-hook-form"; import SearchIcon from "./search-icon"; interface FormProps { search: string; source: string; } export default function SearchImageAndVideo({ isVideo, onImagesFetched, }: { isVideo?: boolean; onImagesFetched: (images: { thumb: string; full: string }[]) => void; }) { const { register, watch } = useForm({ mode: "onChange", }); const source = watch("source"); const onSubmit = useCallback(async () => { const search = watch("search"); if (!search) return; const images = await fetch("/api/photos", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ search, source, isVideo }), }).then((res) => res.json()); onImagesFetched(images); }, [source, onImagesFetched, watch, isVideo]); useEffect(() => { if (source) onSubmit(); }, [source, onSubmit]); return (
{ if (e.key === "Enter") { e.preventDefault(); onSubmit(); } }} placeholder="Or search the web ..." className="ctz:w-full ctz:p-3 ctz:border ctz:rounded-md ctz:focus:ring-0 ctz:focus:outline-none" />
); }