import React, { memo, useState, useCallback } from 'react' import { BluePlayButton } from '@/components/shared/BluePlayButton' interface TestResultDisplayProps { isCorrect: boolean correctAnswer: string userAnswer?: string word: string pronunciation?: string example: string exampleTranslation: string showResult: boolean } export const TestResultDisplay = memo(({ isCorrect, correctAnswer, userAnswer, word, pronunciation, example, exampleTranslation, showResult }) => { const [isPlayingAnswer, setIsPlayingAnswer] = useState(false) const [isPlayingExample, setIsPlayingExample] = useState(false) // TTS 播放邏輯 const handleToggleTTS = useCallback((text: string, type: 'answer' | 'example', lang?: string) => { const isCurrentlyPlaying = type === 'answer' ? isPlayingAnswer : isPlayingExample const setPlaying = type === 'answer' ? setIsPlayingAnswer : setIsPlayingExample const stopOther = type === 'answer' ? setIsPlayingExample : setIsPlayingAnswer if (isCurrentlyPlaying) { speechSynthesis.cancel() setPlaying(false) return } // 停止另一個播放 stopOther(false) speechSynthesis.cancel() const utterance = new SpeechSynthesisUtterance(text) utterance.lang = lang || 'en-US' utterance.rate = 0.8 utterance.onstart = () => setPlaying(true) utterance.onend = () => setPlaying(false) utterance.onerror = () => setPlaying(false) speechSynthesis.speak(utterance) }, [isPlayingAnswer, isPlayingExample]) if (!showResult) return null return (

{isCorrect ? '正確!' : '錯誤!'}

{!isCorrect && userAnswer && (

正確答案是:{correctAnswer}

)}

{word && {word}} {pronunciation && {pronunciation}} handleToggleTTS(text, 'answer', lang)} size="sm" title="播放答案" />

{example} handleToggleTTS(text, 'example', lang)} size="sm" title="播放例句" />

{exampleTranslation}

) }) TestResultDisplay.displayName = 'TestResultDisplay'