import React, { useState, useRef, useEffect, memo } from "react"; import { useGenerator } from "../../context/GeneratorContext"; const SearchIcon = ({ className = "w-5 h-5" }) => ( ); function SearchBar({ value, onChange, placeholder = "Search images...", onSubmit, showSubmitButton = true, submitLabel = "Search", disabled = false, providerId, isSearching = false, }) { const [inputValue, setInputValue] = useState(value); const [showHistory, setShowHistory] = useState(false); const [selectedHistoryIndex, setSelectedHistoryIndex] = useState(-1); const inputRef = useRef(null); const historyListRef = useRef(null); const generator = useGenerator(); const history = providerId ? generator.getSearchHistory(providerId) : []; const addToHistory = providerId ? generator.addSearchToHistory : () => {}; const clearHistory = providerId ? generator.clearSearchHistory : () => {}; // Sync input value when external value changes useEffect(() => { setInputValue(value); }, [value]); // Reset selected index when history visibility changes useEffect(() => { if (!showHistory) { setSelectedHistoryIndex(-1); } }, [showHistory]); useEffect(() => { const handleKeyDown = (e) => { if ((e.ctrlKey || e.metaKey) && e.key === "k") { e.preventDefault(); inputRef.current?.focus(); } }; window.addEventListener("keydown", handleKeyDown); return () => window.removeEventListener("keydown", handleKeyDown); }, []); const handleSubmit = (e) => { e.preventDefault(); const q = (inputValue || "").trim(); if (q) addToHistory(providerId, q); setShowHistory(false); onChange(q); // Only update search query on submit onSubmit?.(); }; const handleHistorySelect = (item) => { setInputValue(item); onChange(item); // Trigger search when selecting from history setShowHistory(false); inputRef.current?.focus(); }; const handleClear = () => { setInputValue(""); onChange(""); // Trigger search with empty query }; const hasText = (inputValue || "").trim().length > 0; return (
); } export default memo(SearchBar);