diff --git a/frontend/app/generate/page.tsx b/frontend/app/generate/page.tsx index ff2d92b..9dd8ea1 100644 --- a/frontend/app/generate/page.tsx +++ b/frontend/app/generate/page.tsx @@ -1,12 +1,12 @@ 'use client' -import { useState, useMemo, useCallback } from 'react' +import { useState, useMemo, useCallback, useEffect } from 'react' import { ProtectedRoute } from '@/components/shared/ProtectedRoute' import { Navigation } from '@/components/shared/Navigation' import { WordPopup } from '@/components/word/WordPopup' import { useToast } from '@/components/shared/Toast' import { flashcardsService } from '@/lib/services/flashcards' -import { getLevelIndex, getTargetLearningRange } from '@/lib/utils/cefrUtils' +import { getLevelIndex } from '@/lib/utils/cefrUtils' import { useWordAnalysis } from '@/hooks/word/useWordAnalysis' import { API_CONFIG } from '@/lib/config/api' import Link from 'next/link' @@ -49,8 +49,50 @@ function GenerateContent() { const [selectedIdiom, setSelectedIdiom] = useState(null) const [selectedWord, setSelectedWord] = useState(null) + // localStorage 快取函數 + const saveAnalysisToCache = useCallback((cacheData: any) => { + try { + localStorage.setItem('generate_analysis_cache', JSON.stringify(cacheData)) + } catch (error) { + console.warn('無法保存分析快取:', error) + } + }, []) + + const loadAnalysisFromCache = useCallback(() => { + try { + const cached = localStorage.getItem('generate_analysis_cache') + return cached ? JSON.parse(cached) : null + } catch (error) { + console.warn('無法載入分析快取:', error) + return null + } + }, []) + + const clearAnalysisCache = useCallback(() => { + try { + localStorage.removeItem('generate_analysis_cache') + } catch (error) { + console.warn('無法清除分析快取:', error) + } + }, []) + + // 組件載入時恢復快取的分析結果 + useEffect(() => { + const cached = loadAnalysisFromCache() + if (cached) { + setTextInput(cached.textInput || '') + setSentenceAnalysis(cached.sentenceAnalysis || null) + setSentenceMeaning(cached.sentenceMeaning || '') + setGrammarCorrection(cached.grammarCorrection || null) + setShowAnalysisView(cached.showAnalysisView || false) + console.log('✅ 已恢復快取的分析結果') + } + }, [loadAnalysisFromCache]) + // 處理句子分析 - 使用真實API const handleAnalyzeSentence = async () => { + // 清除舊的分析快取 + clearAnalysisCache() setIsAnalyzing(true) @@ -126,6 +168,29 @@ function GenerateContent() { } setShowAnalysisView(true) + + // 保存分析結果到快取 + const cacheData = { + textInput, + sentenceAnalysis: analysisData, + sentenceMeaning: apiData.sentenceMeaning || '', + grammarCorrection: apiData.grammarCorrection ? { + hasErrors: apiData.grammarCorrection.hasErrors, + originalText: textInput, + correctedText: apiData.grammarCorrection.correctedText || textInput, + corrections: apiData.grammarCorrection.corrections || [], + confidenceScore: apiData.grammarCorrection.confidenceScore || 1.0 + } : { + hasErrors: false, + originalText: textInput, + correctedText: textInput, + corrections: [], + confidenceScore: 1.0 + }, + showAnalysisView: true + } + saveAnalysisToCache(cacheData) + } catch (error) { console.error('Error in sentence analysis:', error) setGrammarCorrection({ @@ -299,32 +364,25 @@ function GenerateContent() { '🔍 分析句子' )} - - - {/* 個人化程度指示器 */} -
- {(() => { - const userLevel = localStorage.getItem('userEnglishLevel') || 'A2' - return ( -
- 🎯 您的程度: {userLevel} - | - 📈 重點學習範圍: {getTargetLearningRange(userLevel)} - - 調整 ⚙️ - -
- ) - })()} -
) : ( /* 重新設計的句子分析視圖 - 簡潔流暢 */ <> + {/* 用戶程度指示器 */} +
+ + 你的程度 {userLevel} + + + + + +
+ {/* 語法修正面板 - 如果需要的話 */} {grammarCorrection && grammarCorrection.hasErrors && (