'use client' import { useState } from 'react' interface GrammarCorrection { hasErrors: boolean originalText: string correctedText: string | null corrections: Array<{ position: { start: number; end: number } errorType: string original: string corrected: string reason: string severity: 'high' | 'medium' | 'low' }> confidenceScore: number } interface GrammarCorrectionPanelProps { correction: GrammarCorrection onAcceptCorrection: () => void onRejectCorrection: () => void onManualEdit?: (text: string) => void } export function GrammarCorrectionPanel({ correction, onAcceptCorrection, onRejectCorrection, onManualEdit }: GrammarCorrectionPanelProps) { const [isExpanded, setIsExpanded] = useState(true) if (!correction.hasErrors) { return (
語法檢查:無錯誤
您的句子語法正確,可以直接進行學習分析!
) } const renderHighlightedText = (text: string, corrections: typeof correction.corrections) => { if (corrections.length === 0) return text let result: React.ReactNode[] = [] let lastIndex = 0 corrections.forEach((corr, index) => { // 添加錯誤前的正常文字 if (corr.position.start > lastIndex) { result.push( {text.slice(lastIndex, corr.position.start)} ) } // 添加錯誤文字(紅色標記) result.push( {corr.original} ) lastIndex = corr.position.end }) // 添加最後剩餘的正常文字 if (lastIndex < text.length) { result.push( {text.slice(lastIndex)} ) } return <>{result} } const renderCorrectedText = (text: string, corrections: typeof correction.corrections) => { if (corrections.length === 0 || !text) return text let result: React.ReactNode[] = [] let lastIndex = 0 let offset = 0 // 修正後文字長度變化的偏移量 corrections.forEach((corr, index) => { const adjustedStart = corr.position.start + offset const originalLength = corr.original.length const correctedLength = corr.corrected.length // 添加修正前的正常文字 if (adjustedStart > lastIndex) { result.push( {text.slice(lastIndex, adjustedStart)} ) } // 添加修正後的文字(綠色標記) result.push( {corr.corrected} ) lastIndex = adjustedStart + correctedLength offset += (correctedLength - originalLength) }) // 添加最後剩餘的正常文字 if (lastIndex < text.length) { result.push( {text.slice(lastIndex)} ) } return <>{result} } const getSeverityColor = (severity: string) => { switch (severity) { case 'high': return 'bg-red-100 text-red-700 border-red-300' case 'medium': return 'bg-yellow-100 text-yellow-700 border-yellow-300' case 'low': return 'bg-blue-100 text-blue-700 border-blue-300' default: return 'bg-gray-100 text-gray-700 border-gray-300' } } return (
{/* 標題區 */}

語法檢查:發現 {correction.corrections.length} 個錯誤

建議修正後再進行學習,以確保學習內容的正確性
{isExpanded && (
{/* 原始句子 */}

📝 用戶輸入:

{renderHighlightedText(correction.originalText, correction.corrections)}
{/* 修正建議 */}

🔧 建議修正:

{correction.correctedText && renderCorrectedText(correction.correctedText, correction.corrections)}
{/* 修正說明列表 */}
📋 修正說明:
{correction.corrections.map((corr, index) => (
{index + 1}
"{corr.original}" → "{corr.corrected}"
{corr.reason}
錯誤類型:{corr.errorType} | 嚴重程度:{corr.severity}
))}
{/* 信心度 */}
🎯 修正信心度:{(correction.confidenceScore * 100).toFixed(1)}%
{/* 操作按鈕 */}
{/* 學習提醒 */}
💡
學習建議: 建議使用修正版本進行學習,這樣可以確保您學到正確的英語表達方式。 所有後續的詞彙分析都將基於修正後的句子進行。
)}
) }