import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { motion } from "framer-motion"; import { KeyboardEvent, useRef, useEffect, useState } from "react"; 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`; useEffect(() => { const img = new Image(); img.onload = () => setImageLoaded(true); img.src = imagePath; console.log("Attempting to load image:", imagePath); }, [imagePath]); // Focus input on initial render useEffect(() => { setTimeout(() => { inputRef.current?.focus(); }, 100); }, []); // Focus input after AI finishes thinking 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()) { // Create a synthetic form event to add the current word const syntheticEvent = { preventDefault: () => {}, } as React.FormEvent; onSubmitWord(syntheticEvent); } // Make the guess immediately without waiting for AI response onMakeGuess(); } }; return (

Build a Description

Take turns with AI to describe your word without using the word itself!

{imageLoaded && ( {currentWord} )}

{currentWord}

{sentence.length > 0 ? sentence.join(" ") : "Start your sentence..."}

onInputChange(e.target.value.replace(/\s/g, ''))} onKeyDown={handleKeyDown} placeholder="Enter your word..." className="mb-4" disabled={isAiThinking} />
); };