import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { motion } from "framer-motion"; import { KeyboardEvent, useRef, useEffect, useState } from "react"; import { useToast } from "@/hooks/use-toast"; import { useTranslation } from "@/hooks/useTranslation"; interface SentenceBuilderProps { currentWord: string; successfulRounds: number; sentence: string[]; playerInput: string; isAiThinking: boolean; onInputChange: (value: string) => void; onSubmitWord: (e: React.FormEvent) => void; onMakeGuess: () => void; } export const SentenceBuilder = ({ currentWord, successfulRounds, sentence, playerInput, isAiThinking, onInputChange, onSubmitWord, onMakeGuess, }: SentenceBuilderProps) => { const inputRef = useRef(null); const [imageLoaded, setImageLoaded] = useState(false); const imagePath = `/think_in_sync_assets/${currentWord.toLowerCase()}.jpg`; const { toast } = useToast(); const t = useTranslation(); useEffect(() => { const img = new Image(); img.onload = () => setImageLoaded(true); img.src = imagePath; console.log("Attempting to load image:", imagePath); }, [imagePath]); useEffect(() => { setTimeout(() => { inputRef.current?.focus(); }, 100); }, []); useEffect(() => { if (!isAiThinking && sentence.length > 0 && sentence.length % 2 === 0) { setTimeout(() => { inputRef.current?.focus(); }, 100); } }, [isAiThinking, sentence.length]); const handleKeyDown = (e: KeyboardEvent) => { if (e.shiftKey && e.key === 'Enter') { e.preventDefault(); if (playerInput.trim()) { handleSubmit(e as any); } onMakeGuess(); } }; const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); const input = playerInput.trim().toLowerCase(); const target = currentWord.toLowerCase(); if (!/^[a-zA-Z]+$/.test(input)) { toast({ title: t.game.invalidWord, description: t.game.lettersOnly, variant: "destructive", }); return; } if (input.includes(target)) { toast({ title: t.game.invalidWord, description: `${t.game.cantUseTargetWord} "${currentWord}"`, variant: "destructive", }); return; } onSubmitWord(e); }; return (

{t.game.buildDescription}

{t.game.buildSubtitle}

{imageLoaded && ( {currentWord} )}

{currentWord}

{sentence.length > 0 ? sentence.join(" ") : t.game.startSentence}

{ const value = e.target.value.replace(/[^a-zA-Z]/g, ''); onInputChange(value); }} onKeyDown={handleKeyDown} placeholder={t.game.inputPlaceholder} className="mb-4" disabled={isAiThinking} />
); };