From 1b6e62de9517065992172d22903e4edb7f3787f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=84=AD=E6=B2=9B=E8=BB=92?= Date: Wed, 8 Oct 2025 02:14:36 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E5=96=84AI=E7=94=9F=E6=88=90?= =?UTF-8?q?=E9=A0=81=E9=9D=A2=E5=8A=9F=E8=83=BD=E8=88=87=E9=AB=94=E9=A9=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修復詞彙顏色邏輯,實現基於用戶程度的相對難度顯示 - A2用戶看B2詞彙現在正確顯示橘色(挑戰等級),而非固定藍色 - 新增分析結果localStorage持久化,跳頁後保留分析內容 - 添加簡潔的用戶程度指示器,使用經典齒輪圖標 - 程度按鈕靠右對齊,固定灰色主題,清楚導航到設定頁面 提升個人化學習體驗和界面一致性 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- frontend/app/generate/page.tsx | 104 +++++++++++++++++++++++++-------- 1 file changed, 81 insertions(+), 23 deletions(-) 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 && (