import React, { useState } from 'react' interface BluePlayButtonProps { text?: string lang?: string disabled?: boolean className?: string size?: 'sm' | 'md' | 'lg' title?: string onPlayStart?: () => void onPlayEnd?: () => void } export const BluePlayButton: React.FC = ({ text, lang = 'en-US', disabled = false, className = '', size = 'md', title, onPlayStart, onPlayEnd }) => { const [isPlaying, setIsPlaying] = useState(false) const sizeClasses = { sm: 'w-8 h-8', md: 'w-10 h-10', lg: 'w-12 h-12' } const iconSizes = { sm: 'w-4 h-4', md: 'w-5 h-5', lg: 'w-6 h-6' } // 內建 TTS 邏輯 const handleToggle = () => { // 停止播放邏輯 if (isPlaying) { speechSynthesis.cancel() setIsPlaying(false) if (onPlayEnd) onPlayEnd() return } // 開始播放邏輯 if (onPlayStart) { // 自定義播放場景(如錄音回放) setIsPlaying(true) onPlayStart() setTimeout(() => { setIsPlaying(false) if (onPlayEnd) onPlayEnd() }, 3000) } else if (text) { // 標準 TTS 播放 const utterance = new SpeechSynthesisUtterance(text) utterance.lang = lang utterance.rate = 0.8 utterance.onstart = () => { setIsPlaying(true) } utterance.onend = () => { setIsPlaying(false) if (onPlayEnd) onPlayEnd() } utterance.onerror = () => { setIsPlaying(false) if (onPlayEnd) onPlayEnd() } speechSynthesis.speak(utterance) } } return ( ) }