File size: 7,055 Bytes
8725cc4
 
 
0ce34cb
34f302f
831f7e7
81e6964
 
 
 
 
 
 
 
 
 
 
8725cc4
 
 
 
 
 
 
 
 
 
81e6964
8725cc4
 
 
 
 
 
 
 
 
 
 
81e6964
8725cc4
 
0ce34cb
81e6964
5835ecd
0ce34cb
34f302f
831f7e7
8725cc4
0ce34cb
 
 
 
 
 
 
8725cc4
 
 
 
 
 
0ce34cb
 
 
 
 
 
 
 
5835ecd
 
 
 
 
8725cc4
 
 
5835ecd
 
 
769c038
8725cc4
 
 
34f302f
 
 
 
 
5835ecd
 
 
 
 
 
 
 
 
ac04e03
34f302f
831f7e7
 
34f302f
 
 
 
 
 
 
831f7e7
 
34f302f
 
 
 
 
 
 
 
81e6964
 
 
 
 
 
 
 
8725cc4
 
 
 
81e6964
8725cc4
81e6964
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8725cc4
81e6964
8725cc4
81e6964
 
 
8725cc4
81e6964
 
 
 
 
 
 
 
 
 
 
 
8725cc4
34f302f
81e6964
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5835ecd
81e6964
 
5835ecd
81e6964
 
5835ecd
 
 
 
 
81e6964
8725cc4
 
 
 
5835ecd
8725cc4
81e6964
8725cc4
 
 
769c038
8725cc4
5835ecd
8725cc4
81e6964
8725cc4
 
 
81e6964
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8725cc4
 
5835ecd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
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";
import { House } from "lucide-react";
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
} from "@/components/ui/alert-dialog";

interface SentenceBuilderProps {
  currentWord: string;
  successfulRounds: number;
  sentence: string[];
  playerInput: string;
  isAiThinking: boolean;
  onInputChange: (value: string) => void;
  onSubmitWord: (e: React.FormEvent) => void;
  onMakeGuess: () => void;
  onBack?: () => void;
}

export const SentenceBuilder = ({
  currentWord,
  successfulRounds,
  sentence,
  playerInput,
  isAiThinking,
  onInputChange,
  onSubmitWord,
  onMakeGuess,
  onBack,
}: SentenceBuilderProps) => {
  const inputRef = useRef<HTMLInputElement>(null);
  const [imageLoaded, setImageLoaded] = useState(false);
  const [showConfirmDialog, setShowConfirmDialog] = useState(false);
  const [hasMultipleWords, setHasMultipleWords] = 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]);

  useEffect(() => {
    // Check if input contains multiple words
    setHasMultipleWords(playerInput.trim().split(/\s+/).length > 1);
  }, [playerInput]);

  const handleKeyDown = (e: KeyboardEvent<HTMLInputElement>) => {
    if (e.shiftKey && e.key === 'Enter') {
      e.preventDefault();
      // Only trigger if buttons are not disabled and either we have a sentence or valid input
      if (!hasMultipleWords && !isAiThinking && (sentence.length > 0 || playerInput.trim())) {
        onMakeGuess();
      }
    }
  };

  const handleSubmit = (e: React.FormEvent) => {
    e.preventDefault();
    const input = playerInput.trim().toLowerCase();
    const target = currentWord.toLowerCase();

    if (hasMultipleWords) {
      toast({
        title: t.game.invalidWord,
        description: t.game.singleWordOnly,
        variant: "destructive",
      });
      return;
    }

    if (!/^[\p{L}]+$/u.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);
  };

  const handleHomeClick = () => {
    if (successfulRounds > 0) {
      setShowConfirmDialog(true);
    } else {
      onBack?.();
    }
  };

  return (
    <motion.div
      initial={{ opacity: 0 }}
      animate={{ opacity: 1 }}
      className="text-center relative"
    >
      <div className="absolute right-0 top-0 bg-primary/10 px-3 py-1 rounded-lg">
        <span className="text-sm font-medium text-primary">
          {t.game.round} {successfulRounds + 1}
        </span>
      </div>

      <Button
        variant="ghost"
        size="icon"
        className="absolute left-0 top-0 text-gray-600 hover:text-primary"
        onClick={handleHomeClick}
      >
        <House className="h-5 w-5" />
      </Button>

      <h2 className="mb-4 text-2xl font-semibold text-gray-900">
        Think in Sync
      </h2>
      <div>
        <p className="mb-1 text-sm text-gray-600">
          {t.game.describeWord}
        </p>
        <div className="mb-6 overflow-hidden rounded-lg bg-secondary/10">
          {imageLoaded && (
            <img
              src={imagePath}
              alt={currentWord}
              className="mx-auto h-48 w-full object-cover"
            />
          )}
          <p className="p-4 text-2xl font-bold tracking-wider text-secondary">
            {currentWord}
          </p>
        </div>
      </div>
      <form onSubmit={handleSubmit} className="mb-4">
        {sentence.length > 0 && (
          <motion.div 
            initial={{ opacity: 0, y: -10 }}
            animate={{ opacity: 1, y: 0 }}
            className="mb-4 text-left p-3 rounded-lg bg-gray-50"
          >
            <p className="text-gray-700">
              {sentence.join(" ")}
            </p>
          </motion.div>
        )}
        <div className="relative mb-4">
          <Input
            ref={inputRef}
            type="text"
            value={playerInput}
            onChange={(e) => onInputChange(e.target.value)}
            onKeyDown={handleKeyDown}
            placeholder={t.game.inputPlaceholder}
            className={`w-full ${hasMultipleWords ? 'border-red-500 focus-visible:ring-red-500' : ''}`}
            disabled={isAiThinking}
          />
          {hasMultipleWords && (
            <p className="text-sm text-red-500 mt-1">
              {t.game.singleWordOnly}
            </p>
          )}
        </div>
        <div className="flex gap-4">
          <Button
            type="submit"
            className="flex-1 bg-primary text-lg hover:bg-primary/90"
            disabled={!playerInput.trim() || isAiThinking || hasMultipleWords}
          >
            {isAiThinking ? t.game.aiThinking : `${t.game.addWord}  ⏎`}
          </Button>
          <Button
            type="button"
            onClick={onMakeGuess}
            className="flex-1 bg-secondary text-lg hover:bg-secondary/90"
            disabled={(!sentence.length && !playerInput.trim()) || isAiThinking || hasMultipleWords}
          >
            {isAiThinking ? t.game.aiThinking : `${t.game.makeGuess} ⇧⏎`}
          </Button>
        </div>
      </form>

      <AlertDialog open={showConfirmDialog} onOpenChange={setShowConfirmDialog}>
        <AlertDialogContent>
          <AlertDialogHeader>
            <AlertDialogTitle>{t.game.leaveGameTitle}</AlertDialogTitle>
            <AlertDialogDescription>
              {t.game.leaveGameDescription}
            </AlertDialogDescription>
          </AlertDialogHeader>
          <AlertDialogFooter>
            <AlertDialogCancel>{t.game.cancel}</AlertDialogCancel>
            <AlertDialogAction onClick={() => onBack?.()}>
              {t.game.confirm}
            </AlertDialogAction>
          </AlertDialogFooter>
        </AlertDialogContent>
      </AlertDialog>
    </motion.div>
  );
};