import React, { useState, useEffect } from 'react'; import { Compass, Shuffle, BookOpen, Quote, GraduationCap, Languages, FileText, Sparkles, ExternalLink, ArrowLeft, Loader2 } from 'lucide-react'; import { WikimediaAPI, WIKIMEDIA_PROJECTS } from '../utils/wikimedia-api'; import { SearchResult } from '../types'; interface ExploreSectionProps { onViewArticle?: (title: string, project: string, content: string) => void; } const ExploreSection: React.FC = ({ onViewArticle }) => { const [randomArticles, setRandomArticles] = useState([]); const [loading, setLoading] = useState(false); const [selectedCategory, setSelectedCategory] = useState('featured'); const [selectedProject, setSelectedProject] = useState(null); const [projectContent, setProjectContent] = useState([]); const [loadingProject, setLoadingProject] = useState(false); const categories = [ { id: 'featured', name: 'Featured Content', icon: Sparkles }, { id: 'trending', name: 'Trending Topics', icon: Compass }, { id: 'random', name: 'Random Discovery', icon: Shuffle }, ]; const projectCards = WIKIMEDIA_PROJECTS.map(project => ({ ...project, stats: { articles: Math.floor(Math.random() * 1000000) + 100000, languages: Math.floor(Math.random() * 100) + 50, contributors: Math.floor(Math.random() * 50000) + 10000, } })); const getIconComponent = (iconName: string) => { const icons: { [key: string]: React.ComponentType } = { Book: BookOpen, BookOpen: BookOpen, Quote: Quote, GraduationCap: GraduationCap, Languages: Languages, FileText: FileText, }; return icons[iconName] || BookOpen; }; const loadRandomContent = async () => { setLoading(true); try { const allArticles: SearchResult[] = []; // Get random articles from multiple projects for (const project of WIKIMEDIA_PROJECTS.slice(0, 3)) { const articles = await WikimediaAPI.getRandomArticles(project.id, 2); allArticles.push(...articles); } // Shuffle the articles const shuffled = allArticles.sort(() => 0.5 - Math.random()); setRandomArticles(shuffled.slice(0, 6)); } catch (error) { console.error('Failed to load random content:', error); } finally { setLoading(false); } }; const handleProjectClick = async (projectId: string) => { setSelectedProject(projectId); setLoadingProject(true); try { // Get featured/popular content from the selected project const content = await WikimediaAPI.getRandomArticles(projectId, 12); setProjectContent(content); } catch (error) { console.error('Failed to load project content:', error); setProjectContent([]); } finally { setLoadingProject(false); } }; const handleViewInWikistro = async (article: SearchResult) => { try { const content = await WikimediaAPI.getPageContent(article.title, article.project); if (onViewArticle) { onViewArticle(article.title, article.project, content); } } catch (error) { console.error('Failed to load article content:', error); } }; useEffect(() => { if (selectedCategory === 'random') { loadRandomContent(); } }, [selectedCategory]); const featuredTopics = [ { title: 'Artificial Intelligence', description: 'Explore the fascinating world of AI, machine learning, and neural networks', image: 'https://images.pexels.com/photos/8386440/pexels-photo-8386440.jpeg?auto=compress&cs=tinysrgb&w=400', project: 'wikipedia', readTime: '15 min read' }, { title: 'Climate Change', description: 'Understanding global warming, its causes, effects, and potential solutions', image: 'https://images.pexels.com/photos/60013/desert-drought-dehydrated-clay-soil-60013.jpeg?auto=compress&cs=tinysrgb&w=400', project: 'wikipedia', readTime: '12 min read' }, { title: 'Quantum Physics', description: 'Dive into the mysterious world of quantum mechanics and particle physics', image: 'https://images.pexels.com/photos/998641/pexels-photo-998641.jpeg?auto=compress&cs=tinysrgb&w=400', project: 'wikipedia', readTime: '20 min read' }, { title: 'Ancient History', description: 'Journey through ancient civilizations and their remarkable achievements', image: 'https://images.pexels.com/photos/161799/egypt-hieroglyphics-text-wall-161799.jpeg?auto=compress&cs=tinysrgb&w=400', project: 'wikipedia', readTime: '18 min read' }, { title: 'Space Exploration', description: 'Discover the latest in space technology and cosmic discoveries', image: 'https://images.pexels.com/photos/586056/pexels-photo-586056.jpeg?auto=compress&cs=tinysrgb&w=400', project: 'wikipedia', readTime: '16 min read' }, { title: 'Renewable Energy', description: 'Learn about sustainable energy sources and green technologies', image: 'https://images.pexels.com/photos/9800029/pexels-photo-9800029.jpeg?auto=compress&cs=tinysrgb&w=400', project: 'wikipedia', readTime: '14 min read' } ]; // If a project is selected, show project content if (selectedProject) { const project = WIKIMEDIA_PROJECTS.find(p => p.id === selectedProject); const Icon = project ? getIconComponent(project.icon) : BookOpen; return (
{project && (

{project.name}

{project.description}

)}
{loadingProject ? (
Loading {project?.name} content...
) : (
{projectContent.map((article, index) => (
{article.project}

{article.title}

{article.snippet}

))}
)}
); } return (

Explore Knowledge

Discover amazing content across Wikimedia projects

{/* Category Tabs */}
{categories.map((category) => { const Icon = category.icon; return ( ); })}
{/* Wikimedia Projects Overview */}

Wikimedia Projects

{projectCards.map((project) => { const Icon = getIconComponent(project.icon); return (
handleProjectClick(project.id)} className="bg-white border border-gray-200 rounded-2xl p-6 hover:shadow-lg transition-all duration-200 hover:border-primary-200 cursor-pointer group" >

{project.name}

{project.description}

{(project.stats.articles / 1000).toFixed(0)}K
Articles
{project.stats.languages}
Languages
{(project.stats.contributors / 1000).toFixed(0)}K
Contributors
); })}
{/* Content based on selected category */} {selectedCategory === 'featured' && (

Featured Topics

{featuredTopics.map((topic, index) => (
handleViewInWikistro({ title: topic.title, pageid: index, size: 0, snippet: topic.description, timestamp: new Date().toISOString(), project: topic.project, url: `https://en.wikipedia.org/wiki/${encodeURIComponent(topic.title)}` })} >
{topic.title}
{topic.project} {topic.readTime}

{topic.title}

{topic.description}

))}
)} {selectedCategory === 'random' && (

Random Discovery

{loading && (
Finding random articles...
)} {!loading && randomArticles.length > 0 && (
{randomArticles.map((article, index) => (
{article.project}

{article.title}

{article.snippet}

))}
)}
)} {selectedCategory === 'trending' && (

Trending Topics

Coming Soon

We're working on bringing you the most trending topics across Wikimedia projects.

)}
); }; export default ExploreSection;