import React, { useState, useCallback } from 'react'; import { BookOpen, Clock, Target, Plus, CheckCircle, Circle, Star, TrendingUp, Play, ExternalLink, Loader2, Eye, ArrowRight, AlertCircle } from 'lucide-react'; import { StudyPlan, StudyTopic } from '../types'; import { WikimediaAPI } from '../utils/wikimedia-api'; interface StudyPlansSectionProps { studyPlans: StudyPlan[]; onTopicComplete?: (planId: string, topicId: string) => void; onTopicStart?: (planId: string, topicId: string) => void; onPlanCreated?: (plan: StudyPlan) => void; onViewArticle?: (title: string, project: string, content: string) => void; } interface CreatePlanFormProps { onPlanCreated?: (plan: StudyPlan) => void; onCancel: () => void; } // Move the form component outside to prevent recreation const CreatePlanForm: React.FC = ({ onPlanCreated, onCancel }) => { const [title, setTitle] = useState(''); const [description, setDescription] = useState(''); const [difficulty, setDifficulty] = useState<'beginner' | 'intermediate' | 'advanced'>('beginner'); const [estimatedTime, setEstimatedTime] = useState(''); const [creatingPlan, setCreatingPlan] = useState(false); const [generationErrorMessage, setGenerationErrorMessage] = useState(''); const clearForm = () => { setTitle(''); setDescription(''); setDifficulty('beginner'); setEstimatedTime(''); setGenerationErrorMessage(''); }; const handleCreatePlan = async (e: React.FormEvent) => { e.preventDefault(); if (!title.trim()) return; setCreatingPlan(true); setGenerationErrorMessage(''); try { // Use AI to generate a real study plan const generatedPlan = await WikimediaAPI.generateStudyPlan(title, difficulty); // Override with user's custom details const customPlan: StudyPlan = { ...generatedPlan, title: title, description: description || generatedPlan.description, estimatedTime: estimatedTime || generatedPlan.estimatedTime }; if (onPlanCreated) { onPlanCreated(customPlan); } onCancel(); clearForm(); } catch (error) { console.error('Failed to create study plan:', error); // Check if it's the "No content found" error if (error instanceof Error && error.message === 'No content found for this topic') { setGenerationErrorMessage( 'We couldn\'t find enough content for this topic. Try using a more general topic like "Physics", "History", "Biology", or "Computer Science".' ); } else { setGenerationErrorMessage( 'Failed to generate AI study plan. We\'ll create a basic plan for you instead.' ); // Create a basic plan if API fails const basicPlan: StudyPlan = { id: `custom-${Date.now()}`, title: title, description: description || `Study plan for ${title}`, difficulty: difficulty, estimatedTime: estimatedTime || '4 weeks', created: new Date().toISOString(), topics: [ { id: `${Date.now()}-1`, title: `Introduction to ${title}`, description: 'Getting started with the basics', content: 'Introductory content will be loaded from Wikimedia sources.', completed: false, estimatedTime: '2 hours', resources: [] } ] }; if (onPlanCreated) { onPlanCreated(basicPlan); } onCancel(); clearForm(); } } finally { setCreatingPlan(false); } }; return (

Create New Study Plan

{generationErrorMessage && (

Unable to Generate Study Plan

{generationErrorMessage}

)}
{ setTitle(e.target.value); setGenerationErrorMessage(''); // Clear error when user types }} placeholder="e.g., Introduction to Quantum Physics" className="w-full px-4 py-3 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary-500 focus:border-transparent" required disabled={creatingPlan} />