'use client' import { useState } from 'react' import Link from 'next/link' import { ProtectedRoute } from '@/components/ProtectedRoute' import { Navigation } from '@/components/Navigation' function GenerateContent() { const [mode, setMode] = useState<'manual' | 'screenshot'>('manual') const [textInput, setTextInput] = useState('') const [extractionType, setExtractionType] = useState<'vocabulary' | 'smart'>('vocabulary') const [cardCount, setCardCount] = useState(10) const [isGenerating, setIsGenerating] = useState(false) const [generatedCards, setGeneratedCards] = useState([]) const [showPreview, setShowPreview] = useState(false) const [isPremium] = useState(false) // Mock premium status const [showImageForCard, setShowImageForCard] = useState<{ [key: number]: boolean }>({}) // Track which cards show images const [modalImage, setModalImage] = useState(null) // Track modal image const mockGeneratedCards = [ { id: 1, word: 'brought', partOfSpeech: 'verb', pronunciation: { us: '/brɔːt/', uk: '/brɔːt/' }, translation: '提出、帶來', definition: 'Past tense of bring; to mention or introduce a topic in conversation', synonyms: ['mentioned', 'raised', 'introduced'], antonyms: ['concealed', 'withheld'], originalExample: 'He brought this thing up during our meeting and no one agreed.', originalExampleTranslation: '他在我們的會議中提出了這件事,但沒有人同意。', generatedExample: { sentence: 'She brought up an interesting point about the budget.', translation: '她提出了一個關於預算的有趣觀點。', imageUrl: '/images/examples/bring_up.png', audioUrl: '#' }, difficulty: 'B1' }, { id: 2, word: 'instincts', partOfSpeech: 'noun', pronunciation: { us: '/ˈɪnstɪŋkts/', uk: '/ˈɪnstɪŋkts/' }, translation: '本能、直覺', definition: 'Natural abilities that help living things survive without learning', synonyms: ['intuition', 'impulse', 'tendency'], antonyms: ['logic', 'reasoning'], originalExample: 'Animals use their instincts to find food and stay safe.', originalExampleTranslation: '動物利用本能來尋找食物並保持安全。', generatedExample: { sentence: 'Trust your instincts when making important decisions.', translation: '在做重要決定時要相信你的直覺。', imageUrl: '/images/examples/instinct.png', audioUrl: '#' }, difficulty: 'B2' }, { id: 3, word: 'warrants', partOfSpeech: 'noun', pronunciation: { us: '/ˈwɔːrənts/', uk: '/ˈwɒrənts/' }, translation: '搜查令、授權令', definition: 'Official documents that give police permission to do something', synonyms: ['authorization', 'permit', 'license'], antonyms: ['prohibition', 'ban'], originalExample: 'The police obtained warrants to search the building.', originalExampleTranslation: '警方取得了搜查令來搜查這棟建築物。', generatedExample: { sentence: 'The judge issued arrest warrants for three suspects.', translation: '法官對三名嫌疑人發出了逮捕令。', imageUrl: '/images/examples/warrant.png', audioUrl: '#' }, difficulty: 'C1' } ] const handleGenerate = async () => { if (!textInput.trim()) return setIsGenerating(true) try { const response = await fetch('http://localhost:5000/api/ai/test/generate', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ inputText: textInput, extractionType: extractionType, cardCount: cardCount }) }) if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`) } const result = await response.json() if (result.success) { // 將後端格式轉換為前端格式 const convertedCards = result.data.generatedCards.map((card: any, index: number) => ({ id: index + 1, word: card.word, partOfSpeech: card.partOfSpeech, pronunciation: { us: card.pronunciation || '/unknown/', uk: card.pronunciation || '/unknown/' }, translation: card.translation, definition: card.definition, synonyms: card.synonyms || [], antonyms: [], // 後端暫不提供,使用空陣列 originalExample: card.example || '', originalExampleTranslation: card.exampleTranslation || '', generatedExample: { sentence: card.example || '', translation: card.exampleTranslation || '', imageUrl: '/images/examples/placeholder.png', // 佔位圖 audioUrl: '#' }, difficulty: card.difficultyLevel || 'B1' })) setGeneratedCards(convertedCards) setShowPreview(true) } else { throw new Error(result.error || 'Generation failed') } } catch (error) { console.error('Error generating cards:', error) alert(`生成詞卡時發生錯誤: ${error instanceof Error ? error.message : '未知錯誤'}`) // 錯誤時使用模擬資料作為備用 setGeneratedCards(mockGeneratedCards) setShowPreview(true) } finally { setIsGenerating(false) } } const handleSaveCards = async () => { if (generatedCards.length === 0) return try { // 將前端格式轉換為後端格式 const cardsToSave = generatedCards.map(card => ({ word: card.word, partOfSpeech: card.partOfSpeech, pronunciation: card.pronunciation.us, // 使用美式發音 translation: card.translation, definition: card.definition, synonyms: card.synonyms, example: card.originalExample || card.generatedExample.sentence, exampleTranslation: card.originalExampleTranslation || card.generatedExample.translation, difficultyLevel: card.difficulty })) const response = await fetch('http://localhost:5000/api/ai/test/save', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ selectedCards: cardsToSave }) }) if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`) } const result = await response.json() if (result.success) { alert(`🎉 成功保存 ${result.data.savedCount} 張詞卡到「${result.data.cardSetName}」!`) // 可選:清空生成的詞卡或跳轉到詞卡列表 // setGeneratedCards([]) // setShowPreview(false) } else { throw new Error(result.error || 'Save failed') } } catch (error) { console.error('Error saving cards:', error) alert(`❌ 保存詞卡時發生錯誤: ${error instanceof Error ? error.message : '未知錯誤'}`) } } const toggleImageForCard = (cardId: number) => { setShowImageForCard(prev => ({ ...prev, [cardId]: !prev[cardId] })) } return (
{/* Navigation */}
{!showPreview ? (

AI 智能生成詞卡

{/* Input Mode Selection */}

原始例句類型

{/* Extraction Type Selection */}

萃取方式

{/* Content Input */}
{mode === 'manual' ? (

輸入英文文本