import { useState, useEffect, useRef } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { motion } from "framer-motion"; import { useTranslation } from "@/hooks/useTranslation"; import { useContext } from "react"; import { LanguageContext } from "@/contexts/LanguageContext"; type Theme = "standard" | "technology" | "sports" | "food" | "custom"; interface ThemeSelectorProps { onThemeSelect: (theme: string) => void; } export const ThemeSelector = ({ onThemeSelect }: ThemeSelectorProps) => { const [selectedTheme, setSelectedTheme] = useState("standard"); const [customTheme, setCustomTheme] = useState(""); const [isGenerating, setIsGenerating] = useState(false); const inputRef = useRef(null); const t = useTranslation(); const { language } = useContext(LanguageContext); useEffect(() => { if (language !== 'en') { setSelectedTheme("technology"); } }, [language]); useEffect(() => { const handleKeyPress = (e: KeyboardEvent) => { if (e.target instanceof HTMLInputElement) return; switch(e.key.toLowerCase()) { case 'a': if (language === 'en') { setSelectedTheme("standard"); } else { setSelectedTheme("technology"); } break; case 'b': setSelectedTheme("sports"); break; case 'c': setSelectedTheme("food"); break; case 'd': e.preventDefault(); setSelectedTheme("custom"); break; case 'enter': if (selectedTheme !== "custom" || customTheme.trim()) { handleSubmit(); } break; } }; window.addEventListener('keydown', handleKeyPress); return () => window.removeEventListener('keydown', handleKeyPress); }, [selectedTheme, customTheme, language]); useEffect(() => { if (selectedTheme === "custom") { setTimeout(() => { inputRef.current?.focus(); }, 100); } }, [selectedTheme]); const handleSubmit = async () => { if (selectedTheme === "custom" && !customTheme.trim()) return; setIsGenerating(true); try { await onThemeSelect(selectedTheme === "custom" ? customTheme : selectedTheme); } finally { setIsGenerating(false); } }; const handleInputKeyPress = (e: React.KeyboardEvent) => { if (e.key === 'Enter' && customTheme.trim()) { handleSubmit(); } }; return (

{t.themes.title}

{t.themes.subtitle}

{language === 'en' ? ( ) : ( )} {selectedTheme === "custom" && ( setCustomTheme(e.target.value)} onKeyPress={handleInputKeyPress} className="w-full" /> )}
); };