import React, { useState } from 'react'; import { ArrowLeft, ExternalLink, BookOpen, Clock, Share2, Bookmark, Eye, Sparkles, Wand2, Loader2 } from 'lucide-react'; import { WikimediaAPI } from '../utils/wikimedia-api'; interface ArticleViewerProps { title: string; project: string; content: string; onBack: () => void; onCreateStudyPlan?: (topic: string) => void; onTransformContent?: (title: string, content: string) => void; onViewArticle?: (title: string, project: string, content: string) => void; } const ArticleViewer: React.FC = ({ title, project, content, onBack, onCreateStudyPlan, onTransformContent, onViewArticle }) => { const [fontSize, setFontSize] = useState('text-base'); const [readingTime, setReadingTime] = useState(0); const [relatedArticles, setRelatedArticles] = useState([]); const [loadingRelated, setLoadingRelated] = useState(false); const [showRelated, setShowRelated] = useState(false); React.useEffect(() => { // Calculate reading time (average 200 words per minute) const wordCount = content.split(' ').length; const time = Math.ceil(wordCount / 200); setReadingTime(time); }, [content]); const formatContent = (text: string) => { // Split content into paragraphs and format const paragraphs = text.split('\n').filter(p => p.trim().length > 0); return paragraphs.map((paragraph, index) => { // Check if it's a heading (starts with ==) if (paragraph.startsWith('==') && paragraph.endsWith('==')) { const headingText = paragraph.replace(/=/g, '').trim(); return (

{headingText}

); } // Regular paragraph return (

{paragraph}

); }); }; const getProjectUrl = () => { const baseUrls: Record = { wikipedia: 'https://en.wikipedia.org/wiki/', 'en-wikipedia': 'https://en.wikipedia.org/wiki/', 'es-wikipedia': 'https://es.wikipedia.org/wiki/', 'fr-wikipedia': 'https://fr.wikipedia.org/wiki/', 'de-wikipedia': 'https://de.wikipedia.org/wiki/', wikibooks: 'https://en.wikibooks.org/wiki/', wikiquote: 'https://en.wikiquote.org/wiki/', wikiversity: 'https://en.wikiversity.org/wiki/', wiktionary: 'https://en.wiktionary.org/wiki/', wikisource: 'https://en.wikisource.org/wiki/', }; const baseUrl = baseUrls[project] || baseUrls.wikipedia; return baseUrl + encodeURIComponent(title.replace(/ /g, '_')); }; const handleShare = async () => { if (navigator.share) { try { await navigator.share({ title: title, text: `Check out this article about ${title}`, url: window.location.href, }); } catch (error) { console.log('Error sharing:', error); } } else { // Fallback: copy to clipboard navigator.clipboard.writeText(window.location.href); } }; const handleCreateStudyPlan = () => { if (onCreateStudyPlan) { onCreateStudyPlan(title); } }; const handleTransformContent = () => { if (onTransformContent) { onTransformContent(title, content); } }; const handleFindRelated = async () => { setLoadingRelated(true); setShowRelated(true); try { // Search for related articles using keywords from the title const searchTerms = title.split(' ').filter(term => term.length > 3); const relatedResults = []; // Search for each term and collect results for (const term of searchTerms.slice(0, 2)) { const results = await WikimediaAPI.search(term, 'wikipedia', 3); relatedResults.push(...results.filter(r => r.title !== title)); } // Remove duplicates and limit to 6 results const uniqueResults = relatedResults.filter((result, index, self) => index === self.findIndex(r => r.title === result.title) ).slice(0, 6); setRelatedArticles(uniqueResults); } catch (error) { console.error('Failed to find related articles:', error); setRelatedArticles([]); } finally { setLoadingRelated(false); } }; const handleViewRelated = async (article: any) => { 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 related article:', error); } }; return (
{/* Header */}
{project.replace('-wikipedia', '')}
{readingTime} min read

{title}

{/* Article Controls */}
Font size:
View Original
{/* Article Content */}
{formatContent(content)}
{content.length < 500 && (

This appears to be a short excerpt. For the complete article with images, references, and full content, please visit the original source.

)}
{/* Related Actions */}

What's Next?

{/* Related Articles Section */} {showRelated && (

Related Articles

{loadingRelated ? (
Finding related articles...
) : relatedArticles.length > 0 ? (
{relatedArticles.map((article, index) => (
handleViewRelated(article)} >

{article.title}

{article.snippet.replace(/<[^>]*>/g, '').substring(0, 100)}...

{article.project}
))}
) : (

No related articles found. Try searching for specific topics.

)}
)}
); }; export default ArticleViewer;