import { useState, useMemo } from 'react' import { FillTestProps, ReviewCardData } from '@/types/review' import { useReviewLogic } from '@/hooks/useReviewLogic' import { CardHeader, AudioSection, ErrorReportButton } from '@/components/review/shared' import { getCorrectAnswer } from '@/utils/answerExtractor' // 優化後的 SentenceFillTest 組件 export const SentenceFillTest: React.FC = ({ cardData, onAnswer, onReportError, disabled = false }) => { // 使用共用邏輯 Hook const { userAnswer, feedback, isSubmitted, setUserAnswer, submitAnswer } = useReviewLogic({ cardData, testType: 'SentenceFillTest' }) // 填空測試特定狀態 const [inputValue, setInputValue] = useState('') // 獲取正確答案 const correctAnswer = useMemo(() => { return getCorrectAnswer(cardData.filledQuestionText || cardData.example, cardData.word) }, [cardData.filledQuestionText, cardData.example, cardData.word]) // 處理答案提交 const handleSubmit = () => { if (isSubmitted || disabled || !inputValue.trim()) return const result = submitAnswer(inputValue.trim()) onAnswer(inputValue.trim()) } // 處理 Enter 鍵提交 const handleKeyPress = (e: React.KeyboardEvent) => { if (e.key === 'Enter') { handleSubmit() } } return (
{/* 詞卡標題 */}
{/* 標題區 */}

句子填空

{/* 填空句子顯示 */}

完成下列句子

{(cardData.filledQuestionText || cardData.example).split('____').map((part, index, array) => ( {part} {index < array.length - 1 && ( ____ )} ))}
{/* 答案輸入區 */}
setInputValue(e.target.value)} onKeyPress={handleKeyPress} disabled={disabled || isSubmitted} placeholder="請輸入答案..." className={` w-full px-4 py-3 rounded-lg border-2 text-lg ${isSubmitted ? feedback?.isCorrect ? 'border-green-500 bg-green-50' : 'border-red-500 bg-red-50' : 'border-gray-300 focus:border-blue-500 focus:outline-none' } ${disabled ? 'opacity-50 cursor-not-allowed' : ''} `} />
{/* 提交按鈕 */} {!isSubmitted && (
)} {/* 結果回饋 */} {feedback && (

{feedback.explanation}

)} {/* 翻譯和音頻 */}

中文翻譯

{cardData.exampleTranslation}

{/* 例句圖片 */} {cardData.exampleImage && (
{`Example
)} {/* 底部按鈕 */}
) } // 向後相容包裝器 interface LegacySentenceFillTestProps { word: string definition: string example: string filledQuestionText?: string exampleTranslation: string pronunciation?: string difficultyLevel: string exampleImage?: string onAnswer: (answer: string) => void onReportError: () => void onImageClick?: (image: string) => void disabled?: boolean } export const SentenceFillTestLegacy: React.FC = (props) => { const cardData: ReviewCardData = { id: `temp_${props.word}`, word: props.word, definition: props.definition, example: props.example, translation: props.exampleTranslation || '', pronunciation: props.pronunciation, synonyms: [], difficultyLevel: props.difficultyLevel, exampleTranslation: props.exampleTranslation, filledQuestionText: props.filledQuestionText, exampleImage: props.exampleImage } return ( ) }