import React, { useState } from 'react'; import { supabase } from '../../lib/supabase'; import { Loader2 } from 'lucide-react'; interface SentenceCompletionProps { story: { id: string; content: { pages: Array<{ text: string; }>; }; }; } export function SentenceCompletion({ story }: SentenceCompletionProps) { const [currentSentence, setCurrentSentence] = useState(0); const [userAnswer, setUserAnswer] = useState(''); const [score, setScore] = useState(0); const [isSubmitting, setIsSubmitting] = useState(false); const [exerciseSentences, setExerciseSentences] = useState>([]); const [isLoading, setIsLoading] = useState(true); // Carregar palavras e preparar sentenças React.useEffect(() => { const loadExerciseWords = async () => { try { const { data: words, error } = await supabase .from('story_exercise_words') .select('*') .eq('story_id', story.id) .eq('exercise_type', 'completion'); if (error) throw error; // Extrair todas as sentenças do texto const allSentences = story.content.pages .map(page => page.text.split(/[.!?]+/)) .flat() .filter(Boolean) .map(sentence => sentence.trim()); // Preparar exercícios com as palavras do banco const exercises = allSentences .filter(sentence => words?.some(word => sentence.toLowerCase().includes(word.word.toLowerCase()) ) ) .map(sentence => { const word = words?.find(w => sentence.toLowerCase().includes(w.word.toLowerCase()) ); return { sentence: sentence.replace( new RegExp(word?.word || '', 'i'), '_____' ), answer: word?.word || '' }; }) .filter(exercise => exercise.answer); // Remover exercícios sem resposta setExerciseSentences(exercises); setIsLoading(false); } catch (error) { console.error('Erro ao carregar palavras:', error); setIsLoading(false); } }; loadExerciseWords(); }, [story.id, story.content.pages]); if (isLoading) { return (
); } if (!exerciseSentences.length) { return (

Não foi possível gerar exercícios para este texto.

); } const currentExercise = exerciseSentences[currentSentence]; const handleSubmit = async () => { setIsSubmitting(true); try { const isCorrect = userAnswer.toLowerCase() === currentExercise.answer.toLowerCase(); if (isCorrect) { setScore(prev => prev + 10); } // Avançar para próxima sentença if (currentSentence < exerciseSentences.length - 1) { setCurrentSentence(prev => prev + 1); setUserAnswer(''); } } catch (error) { console.error('Erro ao verificar resposta:', error); } finally { setIsSubmitting(false); } }; return (

Complete a Frase

{/* Progresso */}
Questão {currentSentence + 1} de {exerciseSentences.length} {score} pontos
{/* Sentença atual */}

{currentExercise.sentence}

{/* Campo de resposta */}
setUserAnswer(e.target.value)} placeholder="Digite a palavra que completa a frase..." className="w-full px-4 py-3 text-lg border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-purple-500" />
{/* Botão de verificação */}
); }