Spaces:
Runtime error
Runtime error
Commit ·
fcb5a67
0
Parent(s):
Add codebase
Browse files- .bolt/config.json +3 -0
- .bolt/prompt +5 -0
- .gitattributes +35 -0
- .gitignore +25 -0
- Dockerfile +12 -0
- README.md +11 -0
- eslint.config.js +28 -0
- index.html +14 -0
- package-lock.json +0 -0
- package.json +34 -0
- postcss.config.js +6 -0
- src/App.tsx +250 -0
- src/components/AIConfigModal.tsx +342 -0
- src/components/AIStudyPlanGenerator.tsx +366 -0
- src/components/ArticleViewer.tsx +327 -0
- src/components/ContentTransformer.tsx +488 -0
- src/components/ExploreSection.tsx +419 -0
- src/components/Header.tsx +83 -0
- src/components/Hero.tsx +88 -0
- src/components/MultilingualExplorer.tsx +297 -0
- src/components/ProgressSection.tsx +304 -0
- src/components/SearchInterface.tsx +247 -0
- src/components/StudyPlansSection.tsx +604 -0
- src/index.css +3 -0
- src/main.tsx +10 -0
- src/types/index.ts +102 -0
- src/utils/ai-enhanced-wikimedia.ts +491 -0
- src/utils/ai-providers.ts +445 -0
- src/utils/wikimedia-api.ts +429 -0
- src/vite-env.d.ts +1 -0
- tailwind.config.js +109 -0
- tsconfig.app.json +24 -0
- tsconfig.json +7 -0
- tsconfig.node.json +22 -0
- vite.config.ts +11 -0
.bolt/config.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"template": "bolt-vite-react-ts"
|
| 3 |
+
}
|
.bolt/prompt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
For all designs I ask you to make, have them be beautiful, not cookie cutter. Make webpages that are fully featured and worthy for production.
|
| 2 |
+
|
| 3 |
+
By default, this template supports JSX syntax with Tailwind CSS classes, React hooks, and Lucide React for icons. Do not install other packages for UI themes, icons, etc unless absolutely necessary or I request them.
|
| 4 |
+
|
| 5 |
+
Use icons from lucide-react for logos.
|
.gitattributes
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Logs
|
| 2 |
+
logs
|
| 3 |
+
*.log
|
| 4 |
+
npm-debug.log*
|
| 5 |
+
yarn-debug.log*
|
| 6 |
+
yarn-error.log*
|
| 7 |
+
pnpm-debug.log*
|
| 8 |
+
lerna-debug.log*
|
| 9 |
+
|
| 10 |
+
node_modules
|
| 11 |
+
dist
|
| 12 |
+
dist-ssr
|
| 13 |
+
*.local
|
| 14 |
+
|
| 15 |
+
# Editor directories and files
|
| 16 |
+
.vscode/*
|
| 17 |
+
!.vscode/extensions.json
|
| 18 |
+
.idea
|
| 19 |
+
.DS_Store
|
| 20 |
+
*.suo
|
| 21 |
+
*.ntvs*
|
| 22 |
+
*.njsproj
|
| 23 |
+
*.sln
|
| 24 |
+
*.sw?
|
| 25 |
+
.env
|
Dockerfile
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Step 1: Build the React app
|
| 2 |
+
FROM node:18-alpine AS builder
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
COPY . .
|
| 5 |
+
RUN npm install
|
| 6 |
+
RUN npm run build
|
| 7 |
+
|
| 8 |
+
# Step 2: Serve the build with a static server
|
| 9 |
+
FROM nginx:alpine
|
| 10 |
+
COPY --from=builder /app/dist /usr/share/nginx/html
|
| 11 |
+
EXPOSE 80
|
| 12 |
+
CMD ["nginx", "-g", "daemon off;"]
|
README.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Wikistro
|
| 3 |
+
emoji: 🐨
|
| 4 |
+
colorFrom: red
|
| 5 |
+
colorTo: gray
|
| 6 |
+
sdk: docker
|
| 7 |
+
pinned: false
|
| 8 |
+
license: gpl-3.0
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
eslint.config.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import js from '@eslint/js';
|
| 2 |
+
import globals from 'globals';
|
| 3 |
+
import reactHooks from 'eslint-plugin-react-hooks';
|
| 4 |
+
import reactRefresh from 'eslint-plugin-react-refresh';
|
| 5 |
+
import tseslint from 'typescript-eslint';
|
| 6 |
+
|
| 7 |
+
export default tseslint.config(
|
| 8 |
+
{ ignores: ['dist'] },
|
| 9 |
+
{
|
| 10 |
+
extends: [js.configs.recommended, ...tseslint.configs.recommended],
|
| 11 |
+
files: ['**/*.{ts,tsx}'],
|
| 12 |
+
languageOptions: {
|
| 13 |
+
ecmaVersion: 2020,
|
| 14 |
+
globals: globals.browser,
|
| 15 |
+
},
|
| 16 |
+
plugins: {
|
| 17 |
+
'react-hooks': reactHooks,
|
| 18 |
+
'react-refresh': reactRefresh,
|
| 19 |
+
},
|
| 20 |
+
rules: {
|
| 21 |
+
...reactHooks.configs.recommended.rules,
|
| 22 |
+
'react-refresh/only-export-components': [
|
| 23 |
+
'warn',
|
| 24 |
+
{ allowConstantExport: true },
|
| 25 |
+
],
|
| 26 |
+
},
|
| 27 |
+
}
|
| 28 |
+
);
|
index.html
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!doctype html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8" />
|
| 5 |
+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
| 6 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
| 7 |
+
<title>Wikistro - Transform Knowledge into Learning</title>
|
| 8 |
+
<meta name="description" content="Leverage Wikimedia APIs to create interactive learning experiences from Wikipedia, Wikibooks, and other open knowledge sources.">
|
| 9 |
+
</head>
|
| 10 |
+
<body>
|
| 11 |
+
<div id="root"></div>
|
| 12 |
+
<script type="module" src="/src/main.tsx"></script>
|
| 13 |
+
</body>
|
| 14 |
+
</html>
|
package-lock.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "wikistro",
|
| 3 |
+
"private": true,
|
| 4 |
+
"version": "1.0.0",
|
| 5 |
+
"type": "module",
|
| 6 |
+
"scripts": {
|
| 7 |
+
"dev": "vite",
|
| 8 |
+
"build": "vite build",
|
| 9 |
+
"lint": "eslint .",
|
| 10 |
+
"preview": "vite preview"
|
| 11 |
+
},
|
| 12 |
+
"dependencies": {
|
| 13 |
+
"dotenv": "^17.0.1",
|
| 14 |
+
"lucide-react": "^0.344.0",
|
| 15 |
+
"react": "^18.3.1",
|
| 16 |
+
"react-dom": "^18.3.1"
|
| 17 |
+
},
|
| 18 |
+
"devDependencies": {
|
| 19 |
+
"@eslint/js": "^9.9.1",
|
| 20 |
+
"@types/react": "^18.3.5",
|
| 21 |
+
"@types/react-dom": "^18.3.0",
|
| 22 |
+
"@vitejs/plugin-react": "^4.3.1",
|
| 23 |
+
"autoprefixer": "^10.4.18",
|
| 24 |
+
"eslint": "^9.9.1",
|
| 25 |
+
"eslint-plugin-react-hooks": "^5.1.0-rc.0",
|
| 26 |
+
"eslint-plugin-react-refresh": "^0.4.11",
|
| 27 |
+
"globals": "^15.9.0",
|
| 28 |
+
"postcss": "^8.4.35",
|
| 29 |
+
"tailwindcss": "^3.4.1",
|
| 30 |
+
"typescript": "^5.5.3",
|
| 31 |
+
"typescript-eslint": "^8.3.0",
|
| 32 |
+
"vite": "^5.4.2"
|
| 33 |
+
}
|
| 34 |
+
}
|
postcss.config.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React, { useState, useEffect } from 'react';
|
| 2 |
+
import Header from './components/Header';
|
| 3 |
+
import Hero from './components/Hero';
|
| 4 |
+
import SearchInterface from './components/SearchInterface';
|
| 5 |
+
import ExploreSection from './components/ExploreSection';
|
| 6 |
+
import StudyPlansSection from './components/StudyPlansSection';
|
| 7 |
+
import AIStudyPlanGenerator from './components/AIStudyPlanGenerator';
|
| 8 |
+
import ContentTransformer from './components/ContentTransformer';
|
| 9 |
+
import MultilingualExplorer from './components/MultilingualExplorer';
|
| 10 |
+
import ArticleViewer from './components/ArticleViewer';
|
| 11 |
+
import { StudyPlan, StudyTopic, ProgressData } from './types';
|
| 12 |
+
import { WikimediaAPI } from './utils/wikimedia-api';
|
| 13 |
+
|
| 14 |
+
function App() {
|
| 15 |
+
const [activeTab, setActiveTab] = useState('hero');
|
| 16 |
+
const [studyPlans, setStudyPlans] = useState<StudyPlan[]>([]);
|
| 17 |
+
const [progressData, setProgressData] = useState<ProgressData>({
|
| 18 |
+
studyStreak: 0,
|
| 19 |
+
topicsCompleted: 0,
|
| 20 |
+
totalStudyTime: 0,
|
| 21 |
+
achievements: 0,
|
| 22 |
+
weeklyGoal: { current: 0, target: 12 },
|
| 23 |
+
recentActivity: [],
|
| 24 |
+
completedTopics: new Set()
|
| 25 |
+
});
|
| 26 |
+
|
| 27 |
+
// Article viewer state
|
| 28 |
+
const [viewingArticle, setViewingArticle] = useState<{
|
| 29 |
+
title: string;
|
| 30 |
+
project: string;
|
| 31 |
+
content: string;
|
| 32 |
+
} | null>(null);
|
| 33 |
+
|
| 34 |
+
// Load data from localStorage on component mount
|
| 35 |
+
useEffect(() => {
|
| 36 |
+
const storedPlans = WikimediaAPI.getStoredStudyPlans();
|
| 37 |
+
const storedProgress = WikimediaAPI.getStoredProgress();
|
| 38 |
+
|
| 39 |
+
setStudyPlans(storedPlans);
|
| 40 |
+
setProgressData({
|
| 41 |
+
...storedProgress,
|
| 42 |
+
completedTopics: new Set(storedProgress.completedTopics || [])
|
| 43 |
+
});
|
| 44 |
+
}, []);
|
| 45 |
+
|
| 46 |
+
// Save data to localStorage whenever it changes
|
| 47 |
+
useEffect(() => {
|
| 48 |
+
WikimediaAPI.saveStudyPlans(studyPlans);
|
| 49 |
+
}, [studyPlans]);
|
| 50 |
+
|
| 51 |
+
useEffect(() => {
|
| 52 |
+
WikimediaAPI.saveProgress({
|
| 53 |
+
...progressData,
|
| 54 |
+
completedTopics: Array.from(progressData.completedTopics)
|
| 55 |
+
});
|
| 56 |
+
}, [progressData]);
|
| 57 |
+
|
| 58 |
+
const handleGetStarted = () => {
|
| 59 |
+
setActiveTab('search');
|
| 60 |
+
};
|
| 61 |
+
|
| 62 |
+
const handlePlanGenerated = (plan: StudyPlan) => {
|
| 63 |
+
setStudyPlans(prev => [...prev, plan]);
|
| 64 |
+
setActiveTab('study');
|
| 65 |
+
};
|
| 66 |
+
|
| 67 |
+
const handlePlanCreated = (plan: StudyPlan) => {
|
| 68 |
+
setStudyPlans(prev => [...prev, plan]);
|
| 69 |
+
};
|
| 70 |
+
|
| 71 |
+
const handleTopicComplete = (planId: string, topicId: string) => {
|
| 72 |
+
// Update study plans
|
| 73 |
+
setStudyPlans(prev => prev.map(plan => {
|
| 74 |
+
if (plan.id === planId) {
|
| 75 |
+
return {
|
| 76 |
+
...plan,
|
| 77 |
+
topics: plan.topics.map(topic => {
|
| 78 |
+
if (topic.id === topicId) {
|
| 79 |
+
return { ...topic, completed: true };
|
| 80 |
+
}
|
| 81 |
+
return topic;
|
| 82 |
+
})
|
| 83 |
+
};
|
| 84 |
+
}
|
| 85 |
+
return plan;
|
| 86 |
+
}));
|
| 87 |
+
|
| 88 |
+
// Update progress data
|
| 89 |
+
setProgressData(prev => {
|
| 90 |
+
const newCompletedTopics = new Set(prev.completedTopics);
|
| 91 |
+
newCompletedTopics.add(topicId);
|
| 92 |
+
|
| 93 |
+
const plan = studyPlans.find(p => p.id === planId);
|
| 94 |
+
const topic = plan?.topics.find(t => t.id === topicId);
|
| 95 |
+
|
| 96 |
+
const newActivity = {
|
| 97 |
+
type: 'completed' as const,
|
| 98 |
+
title: topic?.title || 'Unknown Topic',
|
| 99 |
+
course: plan?.title || 'Unknown Plan',
|
| 100 |
+
time: 'Just now',
|
| 101 |
+
icon: 'CheckCircle' as const,
|
| 102 |
+
color: 'text-success-600'
|
| 103 |
+
};
|
| 104 |
+
|
| 105 |
+
// Calculate study streak
|
| 106 |
+
const today = new Date().toDateString();
|
| 107 |
+
const lastStudyDate = prev.lastStudyDate;
|
| 108 |
+
let newStreak = prev.studyStreak;
|
| 109 |
+
|
| 110 |
+
if (!lastStudyDate || lastStudyDate !== today) {
|
| 111 |
+
const yesterday = new Date();
|
| 112 |
+
yesterday.setDate(yesterday.getDate() - 1);
|
| 113 |
+
|
| 114 |
+
if (lastStudyDate === yesterday.toDateString()) {
|
| 115 |
+
newStreak += 1;
|
| 116 |
+
} else if (!lastStudyDate) {
|
| 117 |
+
newStreak = 1;
|
| 118 |
+
} else {
|
| 119 |
+
newStreak = 1; // Reset streak if gap
|
| 120 |
+
}
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
const newProgress = {
|
| 124 |
+
...prev,
|
| 125 |
+
studyStreak: newStreak,
|
| 126 |
+
topicsCompleted: prev.topicsCompleted + 1,
|
| 127 |
+
totalStudyTime: prev.totalStudyTime + 2,
|
| 128 |
+
weeklyGoal: {
|
| 129 |
+
...prev.weeklyGoal,
|
| 130 |
+
current: Math.min(prev.weeklyGoal.current + 2, prev.weeklyGoal.target)
|
| 131 |
+
},
|
| 132 |
+
completedTopics: newCompletedTopics,
|
| 133 |
+
recentActivity: [newActivity, ...prev.recentActivity.slice(0, 9)],
|
| 134 |
+
lastStudyDate: today
|
| 135 |
+
};
|
| 136 |
+
|
| 137 |
+
// Check for new achievements
|
| 138 |
+
const newAchievements = calculateAchievements(newProgress);
|
| 139 |
+
newProgress.achievements = newAchievements;
|
| 140 |
+
|
| 141 |
+
return newProgress;
|
| 142 |
+
});
|
| 143 |
+
};
|
| 144 |
+
|
| 145 |
+
const handleTopicStart = (planId: string, topicId: string) => {
|
| 146 |
+
const plan = studyPlans.find(p => p.id === planId);
|
| 147 |
+
const topic = plan?.topics.find(t => t.id === topicId);
|
| 148 |
+
|
| 149 |
+
const newActivity = {
|
| 150 |
+
type: 'started' as const,
|
| 151 |
+
title: topic?.title || 'Unknown Topic',
|
| 152 |
+
course: plan?.title || 'Unknown Plan',
|
| 153 |
+
time: 'Just now',
|
| 154 |
+
icon: 'BookOpen' as const,
|
| 155 |
+
color: 'text-primary-600'
|
| 156 |
+
};
|
| 157 |
+
|
| 158 |
+
setProgressData(prev => ({
|
| 159 |
+
...prev,
|
| 160 |
+
recentActivity: [newActivity, ...prev.recentActivity.slice(0, 9)]
|
| 161 |
+
}));
|
| 162 |
+
};
|
| 163 |
+
|
| 164 |
+
const calculateAchievements = (progress: ProgressData): number => {
|
| 165 |
+
let count = 0;
|
| 166 |
+
|
| 167 |
+
if (progress.topicsCompleted >= 1) count++;
|
| 168 |
+
if (progress.studyStreak >= 7) count++;
|
| 169 |
+
if (progress.topicsCompleted >= 10) count++;
|
| 170 |
+
if (progress.totalStudyTime >= 25) count++;
|
| 171 |
+
if (progress.topicsCompleted >= 50) count++;
|
| 172 |
+
if (progress.studyStreak >= 30) count++;
|
| 173 |
+
|
| 174 |
+
return count;
|
| 175 |
+
};
|
| 176 |
+
|
| 177 |
+
const handleViewArticle = (title: string, project: string, content: string) => {
|
| 178 |
+
setViewingArticle({ title, project, content });
|
| 179 |
+
};
|
| 180 |
+
|
| 181 |
+
const handleBackFromArticle = () => {
|
| 182 |
+
setViewingArticle(null);
|
| 183 |
+
};
|
| 184 |
+
|
| 185 |
+
const handleTabChange = (tab: string) => {
|
| 186 |
+
// If viewing an article, go back to the previous tab first
|
| 187 |
+
if (viewingArticle) {
|
| 188 |
+
setViewingArticle(null);
|
| 189 |
+
}
|
| 190 |
+
setActiveTab(tab);
|
| 191 |
+
};
|
| 192 |
+
|
| 193 |
+
const renderContent = () => {
|
| 194 |
+
// If viewing an article, show the article viewer
|
| 195 |
+
if (viewingArticle) {
|
| 196 |
+
return (
|
| 197 |
+
<ArticleViewer
|
| 198 |
+
title={viewingArticle.title}
|
| 199 |
+
project={viewingArticle.project}
|
| 200 |
+
content={viewingArticle.content}
|
| 201 |
+
onBack={handleBackFromArticle}
|
| 202 |
+
onCreateStudyPlan={(topic) => {
|
| 203 |
+
setActiveTab('ai-generator');
|
| 204 |
+
setViewingArticle(null);
|
| 205 |
+
}}
|
| 206 |
+
onTransformContent={(title, content) => {
|
| 207 |
+
setActiveTab('transformer');
|
| 208 |
+
setViewingArticle(null);
|
| 209 |
+
}}
|
| 210 |
+
/>
|
| 211 |
+
);
|
| 212 |
+
}
|
| 213 |
+
|
| 214 |
+
switch (activeTab) {
|
| 215 |
+
case 'search':
|
| 216 |
+
return <SearchInterface onViewArticle={handleViewArticle} />;
|
| 217 |
+
case 'explore':
|
| 218 |
+
return <ExploreSection onViewArticle={handleViewArticle} />;
|
| 219 |
+
case 'study':
|
| 220 |
+
return (
|
| 221 |
+
<StudyPlansSection
|
| 222 |
+
studyPlans={studyPlans}
|
| 223 |
+
onTopicComplete={handleTopicComplete}
|
| 224 |
+
onTopicStart={handleTopicStart}
|
| 225 |
+
onPlanCreated={handlePlanCreated}
|
| 226 |
+
onViewArticle={handleViewArticle}
|
| 227 |
+
/>
|
| 228 |
+
);
|
| 229 |
+
case 'ai-generator':
|
| 230 |
+
return <AIStudyPlanGenerator onPlanGenerated={handlePlanGenerated} />;
|
| 231 |
+
case 'transformer':
|
| 232 |
+
return <ContentTransformer />;
|
| 233 |
+
case 'multilingual':
|
| 234 |
+
return <MultilingualExplorer onViewArticle={handleViewArticle} />;
|
| 235 |
+
default:
|
| 236 |
+
return <Hero onGetStarted={handleGetStarted} />;
|
| 237 |
+
}
|
| 238 |
+
};
|
| 239 |
+
|
| 240 |
+
return (
|
| 241 |
+
<div className="min-h-screen bg-gray-50">
|
| 242 |
+
<Header activeTab={activeTab} onTabChange={handleTabChange} />
|
| 243 |
+
<main className="animate-fade-in">
|
| 244 |
+
{renderContent()}
|
| 245 |
+
</main>
|
| 246 |
+
</div>
|
| 247 |
+
);
|
| 248 |
+
}
|
| 249 |
+
|
| 250 |
+
export default App;
|
src/components/AIConfigModal.tsx
ADDED
|
@@ -0,0 +1,342 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React, { useState, useEffect } from 'react';
|
| 2 |
+
import { X, Settings, Check, AlertCircle, ExternalLink, Download, Key, Server, Zap } from 'lucide-react';
|
| 3 |
+
import { AI_PROVIDERS, AIProviderManager } from '../utils/ai-providers';
|
| 4 |
+
|
| 5 |
+
const token = import.meta.env.VITE_HF_TOKEN;
|
| 6 |
+
|
| 7 |
+
interface AIConfigModalProps {
|
| 8 |
+
isOpen: boolean;
|
| 9 |
+
onClose: () => void;
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
const AIConfigModal: React.FC<AIConfigModalProps> = ({ isOpen, onClose }) => {
|
| 13 |
+
const [config, setConfig] = useState(AIProviderManager.getConfig());
|
| 14 |
+
const [connectionStatus, setConnectionStatus] = useState<Record<string, boolean>>({});
|
| 15 |
+
const [testing, setTesting] = useState<string | null>(null);
|
| 16 |
+
const [showApiKey, setShowApiKey] = useState<Record<string, boolean>>({});
|
| 17 |
+
|
| 18 |
+
useEffect(() => {
|
| 19 |
+
if (isOpen) {
|
| 20 |
+
const currentConfig = AIProviderManager.getConfig();
|
| 21 |
+
setConfig(currentConfig);
|
| 22 |
+
|
| 23 |
+
// Auto-configure Hugging Face if not already set
|
| 24 |
+
if (!currentConfig.apiKeys?.huggingface) {
|
| 25 |
+
const newConfig = AIProviderManager.updateConfig({
|
| 26 |
+
selectedProvider: 'huggingface',
|
| 27 |
+
selectedModel: 'microsoft/DialoGPT-medium',
|
| 28 |
+
apiKeys: {
|
| 29 |
+
...currentConfig.apiKeys,
|
| 30 |
+
huggingface: token
|
| 31 |
+
}
|
| 32 |
+
});
|
| 33 |
+
setConfig(newConfig);
|
| 34 |
+
|
| 35 |
+
// Test connection automatically
|
| 36 |
+
setTimeout(() => {
|
| 37 |
+
testConnection('huggingface');
|
| 38 |
+
}, 500);
|
| 39 |
+
}
|
| 40 |
+
}
|
| 41 |
+
}, [isOpen]);
|
| 42 |
+
|
| 43 |
+
const handleProviderChange = (providerId: string) => {
|
| 44 |
+
const provider = AI_PROVIDERS.find(p => p.id === providerId);
|
| 45 |
+
const newConfig = AIProviderManager.updateConfig({
|
| 46 |
+
selectedProvider: providerId,
|
| 47 |
+
selectedModel: provider?.models[0]?.id || ''
|
| 48 |
+
});
|
| 49 |
+
setConfig(newConfig);
|
| 50 |
+
};
|
| 51 |
+
|
| 52 |
+
const handleModelChange = (modelId: string) => {
|
| 53 |
+
const newConfig = AIProviderManager.updateConfig({
|
| 54 |
+
selectedModel: modelId
|
| 55 |
+
});
|
| 56 |
+
setConfig(newConfig);
|
| 57 |
+
};
|
| 58 |
+
|
| 59 |
+
const handleApiKeyChange = (providerId: string, apiKey: string) => {
|
| 60 |
+
const newConfig = AIProviderManager.updateConfig({
|
| 61 |
+
apiKeys: { ...config.apiKeys, [providerId]: apiKey }
|
| 62 |
+
});
|
| 63 |
+
setConfig(newConfig);
|
| 64 |
+
};
|
| 65 |
+
|
| 66 |
+
const handleEndpointChange = (providerId: string, endpoint: string) => {
|
| 67 |
+
const newConfig = AIProviderManager.updateConfig({
|
| 68 |
+
customEndpoints: { ...config.customEndpoints, [providerId]: endpoint }
|
| 69 |
+
});
|
| 70 |
+
setConfig(newConfig);
|
| 71 |
+
};
|
| 72 |
+
|
| 73 |
+
const testConnection = async (providerId: string) => {
|
| 74 |
+
setTesting(providerId);
|
| 75 |
+
try {
|
| 76 |
+
const isConnected = await AIProviderManager.testConnection(providerId);
|
| 77 |
+
setConnectionStatus(prev => ({ ...prev, [providerId]: isConnected }));
|
| 78 |
+
|
| 79 |
+
if (isConnected && providerId === 'huggingface') {
|
| 80 |
+
// Show success message
|
| 81 |
+
console.log('✅ Hugging Face connected successfully!');
|
| 82 |
+
}
|
| 83 |
+
} catch (error) {
|
| 84 |
+
setConnectionStatus(prev => ({ ...prev, [providerId]: false }));
|
| 85 |
+
console.error(`Connection test failed for ${providerId}:`, error);
|
| 86 |
+
} finally {
|
| 87 |
+
setTesting(null);
|
| 88 |
+
}
|
| 89 |
+
};
|
| 90 |
+
|
| 91 |
+
const selectedProvider = AI_PROVIDERS.find(p => p.id === config.selectedProvider);
|
| 92 |
+
const selectedModel = selectedProvider?.models.find(m => m.id === config.selectedModel);
|
| 93 |
+
|
| 94 |
+
if (!isOpen) return null;
|
| 95 |
+
|
| 96 |
+
return (
|
| 97 |
+
<div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 p-4">
|
| 98 |
+
<div className="bg-white rounded-2xl max-w-4xl w-full max-h-[90vh] overflow-y-auto">
|
| 99 |
+
<div className="p-6 border-b border-gray-200">
|
| 100 |
+
<div className="flex items-center justify-between">
|
| 101 |
+
<div className="flex items-center space-x-3">
|
| 102 |
+
<div className="w-10 h-10 bg-gradient-to-r from-purple-500 to-pink-500 rounded-xl flex items-center justify-center">
|
| 103 |
+
<Settings className="w-5 h-5 text-white" />
|
| 104 |
+
</div>
|
| 105 |
+
<div>
|
| 106 |
+
<h2 className="text-xl font-bold text-gray-900">AI Configuration</h2>
|
| 107 |
+
<p className="text-gray-600">Configure your open-source AI providers</p>
|
| 108 |
+
</div>
|
| 109 |
+
</div>
|
| 110 |
+
<button
|
| 111 |
+
onClick={onClose}
|
| 112 |
+
className="p-2 hover:bg-gray-100 rounded-lg transition-colors"
|
| 113 |
+
>
|
| 114 |
+
<X className="w-5 h-5" />
|
| 115 |
+
</button>
|
| 116 |
+
</div>
|
| 117 |
+
</div>
|
| 118 |
+
|
| 119 |
+
<div className="p-6 space-y-8">
|
| 120 |
+
{/* Success Banner for Hugging Face */}
|
| 121 |
+
{config.apiKeys?.huggingface && connectionStatus.huggingface === true && (
|
| 122 |
+
<div className="bg-green-50 border border-green-200 rounded-xl p-4">
|
| 123 |
+
<div className="flex items-center space-x-3">
|
| 124 |
+
<Check className="w-5 h-5 text-green-600" />
|
| 125 |
+
<div>
|
| 126 |
+
<h4 className="font-medium text-green-900">🎉 Hugging Face Connected!</h4>
|
| 127 |
+
<p className="text-green-700 text-sm">Your API key is working perfectly. AI enhancement is now available!</p>
|
| 128 |
+
</div>
|
| 129 |
+
</div>
|
| 130 |
+
</div>
|
| 131 |
+
)}
|
| 132 |
+
|
| 133 |
+
{/* Provider Selection */}
|
| 134 |
+
<div>
|
| 135 |
+
<h3 className="text-lg font-semibold text-gray-900 mb-4">Select AI Provider</h3>
|
| 136 |
+
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
| 137 |
+
{AI_PROVIDERS.map((provider) => (
|
| 138 |
+
<div
|
| 139 |
+
key={provider.id}
|
| 140 |
+
onClick={() => handleProviderChange(provider.id)}
|
| 141 |
+
className={`p-4 border-2 rounded-xl cursor-pointer transition-all ${
|
| 142 |
+
config.selectedProvider === provider.id
|
| 143 |
+
? 'border-primary-500 bg-primary-50'
|
| 144 |
+
: 'border-gray-200 hover:border-gray-300'
|
| 145 |
+
}`}
|
| 146 |
+
>
|
| 147 |
+
<div className="flex items-start justify-between">
|
| 148 |
+
<div className="flex-1">
|
| 149 |
+
<div className="flex items-center space-x-2 mb-2">
|
| 150 |
+
<h4 className="font-semibold text-gray-900">{provider.name}</h4>
|
| 151 |
+
{provider.isLocal && (
|
| 152 |
+
<span className="px-2 py-1 bg-green-100 text-green-700 text-xs rounded-full">
|
| 153 |
+
Local
|
| 154 |
+
</span>
|
| 155 |
+
)}
|
| 156 |
+
{provider.requiresApiKey && (
|
| 157 |
+
<span className="px-2 py-1 bg-blue-100 text-blue-700 text-xs rounded-full">
|
| 158 |
+
API Key
|
| 159 |
+
</span>
|
| 160 |
+
)}
|
| 161 |
+
{provider.id === 'huggingface' && config.apiKeys?.huggingface && (
|
| 162 |
+
<span className="px-2 py-1 bg-green-100 text-green-700 text-xs rounded-full">
|
| 163 |
+
✓ Configured
|
| 164 |
+
</span>
|
| 165 |
+
)}
|
| 166 |
+
</div>
|
| 167 |
+
<p className="text-sm text-gray-600 mb-3">{provider.description}</p>
|
| 168 |
+
<div className="text-xs text-gray-500">
|
| 169 |
+
{provider.models.length} models available
|
| 170 |
+
</div>
|
| 171 |
+
</div>
|
| 172 |
+
<div className="ml-4">
|
| 173 |
+
{connectionStatus[provider.id] === true && (
|
| 174 |
+
<Check className="w-5 h-5 text-green-600" />
|
| 175 |
+
)}
|
| 176 |
+
{connectionStatus[provider.id] === false && (
|
| 177 |
+
<AlertCircle className="w-5 h-5 text-red-600" />
|
| 178 |
+
)}
|
| 179 |
+
</div>
|
| 180 |
+
</div>
|
| 181 |
+
|
| 182 |
+
<div className="mt-3 flex items-center space-x-2">
|
| 183 |
+
<button
|
| 184 |
+
onClick={(e) => {
|
| 185 |
+
e.stopPropagation();
|
| 186 |
+
testConnection(provider.id);
|
| 187 |
+
}}
|
| 188 |
+
disabled={testing === provider.id}
|
| 189 |
+
className="flex items-center space-x-1 px-3 py-1 bg-gray-100 text-gray-700 rounded-lg text-sm hover:bg-gray-200 transition-colors disabled:opacity-50"
|
| 190 |
+
>
|
| 191 |
+
<Zap className="w-3 h-3" />
|
| 192 |
+
<span>{testing === provider.id ? 'Testing...' : 'Test'}</span>
|
| 193 |
+
</button>
|
| 194 |
+
</div>
|
| 195 |
+
</div>
|
| 196 |
+
))}
|
| 197 |
+
</div>
|
| 198 |
+
</div>
|
| 199 |
+
|
| 200 |
+
{/* Provider Configuration */}
|
| 201 |
+
{selectedProvider && (
|
| 202 |
+
<div className="space-y-6">
|
| 203 |
+
{/* API Key Configuration */}
|
| 204 |
+
{selectedProvider.requiresApiKey && (
|
| 205 |
+
<div>
|
| 206 |
+
<label className="block text-sm font-medium text-gray-700 mb-2">
|
| 207 |
+
API Key for {selectedProvider.name}
|
| 208 |
+
</label>
|
| 209 |
+
<div className="relative">
|
| 210 |
+
<input
|
| 211 |
+
type={showApiKey[selectedProvider.id] ? 'text' : 'password'}
|
| 212 |
+
value={config.apiKeys[selectedProvider.id] || ''}
|
| 213 |
+
onChange={(e) => handleApiKeyChange(selectedProvider.id, e.target.value)}
|
| 214 |
+
placeholder="Enter your API key..."
|
| 215 |
+
className="w-full px-4 py-3 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary-500 focus:border-transparent pr-12"
|
| 216 |
+
/>
|
| 217 |
+
<button
|
| 218 |
+
onClick={() => setShowApiKey(prev => ({ ...prev, [selectedProvider.id]: !prev[selectedProvider.id] }))}
|
| 219 |
+
className="absolute right-3 top-1/2 transform -translate-y-1/2 p-1 hover:bg-gray-100 rounded"
|
| 220 |
+
>
|
| 221 |
+
<Key className="w-4 h-4 text-gray-400" />
|
| 222 |
+
</button>
|
| 223 |
+
</div>
|
| 224 |
+
<p className="text-xs text-gray-500 mt-1">
|
| 225 |
+
Your API key is stored locally and never sent to our servers
|
| 226 |
+
</p>
|
| 227 |
+
{selectedProvider.id === 'huggingface' && config.apiKeys?.huggingface && (
|
| 228 |
+
<p className="text-xs text-green-600 mt-1">
|
| 229 |
+
✓ API key configured and ready to use
|
| 230 |
+
</p>
|
| 231 |
+
)}
|
| 232 |
+
</div>
|
| 233 |
+
)}
|
| 234 |
+
|
| 235 |
+
{/* Custom Endpoint Configuration */}
|
| 236 |
+
{(selectedProvider.id === 'openai-compatible') && (
|
| 237 |
+
<div>
|
| 238 |
+
<label className="block text-sm font-medium text-gray-700 mb-2">
|
| 239 |
+
Custom Endpoint
|
| 240 |
+
</label>
|
| 241 |
+
<div className="relative">
|
| 242 |
+
<input
|
| 243 |
+
type="text"
|
| 244 |
+
value={config.customEndpoints[selectedProvider.id] || selectedProvider.apiUrl}
|
| 245 |
+
onChange={(e) => handleEndpointChange(selectedProvider.id, e.target.value)}
|
| 246 |
+
placeholder="http://localhost:8080"
|
| 247 |
+
className="w-full px-4 py-3 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary-500 focus:border-transparent pl-12"
|
| 248 |
+
/>
|
| 249 |
+
<Server className="absolute left-3 top-1/2 transform -translate-y-1/2 w-5 h-5 text-gray-400" />
|
| 250 |
+
</div>
|
| 251 |
+
</div>
|
| 252 |
+
)}
|
| 253 |
+
|
| 254 |
+
{/* Model Selection */}
|
| 255 |
+
<div>
|
| 256 |
+
<label className="block text-sm font-medium text-gray-700 mb-3">
|
| 257 |
+
Select Model
|
| 258 |
+
</label>
|
| 259 |
+
<div className="grid grid-cols-1 gap-3">
|
| 260 |
+
{selectedProvider.models.map((model) => (
|
| 261 |
+
<div
|
| 262 |
+
key={model.id}
|
| 263 |
+
onClick={() => handleModelChange(model.id)}
|
| 264 |
+
className={`p-4 border-2 rounded-xl cursor-pointer transition-all ${
|
| 265 |
+
config.selectedModel === model.id
|
| 266 |
+
? 'border-primary-500 bg-primary-50'
|
| 267 |
+
: 'border-gray-200 hover:border-gray-300'
|
| 268 |
+
}`}
|
| 269 |
+
>
|
| 270 |
+
<div className="flex items-start justify-between">
|
| 271 |
+
<div className="flex-1">
|
| 272 |
+
<h4 className="font-medium text-gray-900">{model.name}</h4>
|
| 273 |
+
<p className="text-sm text-gray-600 mt-1">{model.description}</p>
|
| 274 |
+
<div className="flex items-center space-x-4 mt-2 text-xs text-gray-500">
|
| 275 |
+
<span>Max tokens: {model.maxTokens.toLocaleString()}</span>
|
| 276 |
+
<span>Capabilities: {model.capabilities.join(', ')}</span>
|
| 277 |
+
</div>
|
| 278 |
+
</div>
|
| 279 |
+
</div>
|
| 280 |
+
</div>
|
| 281 |
+
))}
|
| 282 |
+
</div>
|
| 283 |
+
</div>
|
| 284 |
+
</div>
|
| 285 |
+
)}
|
| 286 |
+
|
| 287 |
+
{/* Installation Instructions */}
|
| 288 |
+
{selectedProvider?.isLocal && (
|
| 289 |
+
<div className="bg-blue-50 border border-blue-200 rounded-xl p-4">
|
| 290 |
+
<h4 className="font-medium text-blue-900 mb-2">Installation Instructions</h4>
|
| 291 |
+
{selectedProvider.id === 'ollama' && (
|
| 292 |
+
<div className="space-y-2 text-sm text-blue-800">
|
| 293 |
+
<p>1. Install Ollama from <a href="https://ollama.ai" target="_blank" rel="noopener noreferrer" className="underline">ollama.ai</a></p>
|
| 294 |
+
<p>2. Run: <code className="bg-blue-100 px-2 py-1 rounded">ollama pull llama3.2</code></p>
|
| 295 |
+
<p>3. Start Ollama service: <code className="bg-blue-100 px-2 py-1 rounded">ollama serve</code></p>
|
| 296 |
+
</div>
|
| 297 |
+
)}
|
| 298 |
+
{selectedProvider.id === 'openai-compatible' && (
|
| 299 |
+
<div className="space-y-2 text-sm text-blue-800">
|
| 300 |
+
<p>Compatible with LocalAI, vLLM, FastChat, and other OpenAI-compatible APIs</p>
|
| 301 |
+
<p>Set your custom endpoint URL above</p>
|
| 302 |
+
</div>
|
| 303 |
+
)}
|
| 304 |
+
</div>
|
| 305 |
+
)}
|
| 306 |
+
|
| 307 |
+
{/* Current Configuration Summary */}
|
| 308 |
+
{selectedProvider && selectedModel && (
|
| 309 |
+
<div className="bg-gray-50 border border-gray-200 rounded-xl p-4">
|
| 310 |
+
<h4 className="font-medium text-gray-900 mb-2">Current Configuration</h4>
|
| 311 |
+
<div className="space-y-1 text-sm text-gray-600">
|
| 312 |
+
<p><strong>Provider:</strong> {selectedProvider.name}</p>
|
| 313 |
+
<p><strong>Model:</strong> {selectedModel.name}</p>
|
| 314 |
+
<p><strong>Max Tokens:</strong> {selectedModel.maxTokens.toLocaleString()}</p>
|
| 315 |
+
<p><strong>Capabilities:</strong> {selectedModel.capabilities.join(', ')}</p>
|
| 316 |
+
{config.apiKeys?.huggingface && (
|
| 317 |
+
<p><strong>Status:</strong> <span className="text-green-600">✓ Ready to use</span></p>
|
| 318 |
+
)}
|
| 319 |
+
</div>
|
| 320 |
+
</div>
|
| 321 |
+
)}
|
| 322 |
+
</div>
|
| 323 |
+
|
| 324 |
+
<div className="p-6 border-t border-gray-200">
|
| 325 |
+
<div className="flex items-center justify-between">
|
| 326 |
+
<div className="text-sm text-gray-600">
|
| 327 |
+
All configurations are stored locally in your browser
|
| 328 |
+
</div>
|
| 329 |
+
<button
|
| 330 |
+
onClick={onClose}
|
| 331 |
+
className="px-6 py-2 bg-primary-600 text-white rounded-lg hover:bg-primary-700 transition-colors"
|
| 332 |
+
>
|
| 333 |
+
Done
|
| 334 |
+
</button>
|
| 335 |
+
</div>
|
| 336 |
+
</div>
|
| 337 |
+
</div>
|
| 338 |
+
</div>
|
| 339 |
+
);
|
| 340 |
+
};
|
| 341 |
+
|
| 342 |
+
export default AIConfigModal;
|
src/components/AIStudyPlanGenerator.tsx
ADDED
|
@@ -0,0 +1,366 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React, { useState } from 'react';
|
| 2 |
+
import { Sparkles, BookOpen, Clock, Target, Loader2, CheckCircle, ArrowRight, Settings, Zap, AlertCircle } from 'lucide-react';
|
| 3 |
+
import { AIEnhancedWikimedia } from '../utils/ai-enhanced-wikimedia';
|
| 4 |
+
import { AIProviderManager } from '../utils/ai-providers';
|
| 5 |
+
import { StudyPlan } from '../types';
|
| 6 |
+
|
| 7 |
+
interface AIStudyPlanGeneratorProps {
|
| 8 |
+
onPlanGenerated: (plan: StudyPlan) => void;
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
const AIStudyPlanGenerator: React.FC<AIStudyPlanGeneratorProps> = ({ onPlanGenerated }) => {
|
| 12 |
+
const [topic, setTopic] = useState('');
|
| 13 |
+
const [difficulty, setDifficulty] = useState<'beginner' | 'intermediate' | 'advanced'>('beginner');
|
| 14 |
+
const [loading, setLoading] = useState(false);
|
| 15 |
+
const [generatedPlan, setGeneratedPlan] = useState<StudyPlan | null>(null);
|
| 16 |
+
const [useAI, setUseAI] = useState(true);
|
| 17 |
+
const [aiStatus, setAiStatus] = useState<'checking' | 'available' | 'unavailable'>('checking');
|
| 18 |
+
const [showAIConfig, setShowAIConfig] = useState(false);
|
| 19 |
+
|
| 20 |
+
React.useEffect(() => {
|
| 21 |
+
checkAIAvailability();
|
| 22 |
+
}, []);
|
| 23 |
+
|
| 24 |
+
const checkAIAvailability = async () => {
|
| 25 |
+
setAiStatus('checking');
|
| 26 |
+
try {
|
| 27 |
+
const config = AIProviderManager.getConfig();
|
| 28 |
+
|
| 29 |
+
// Check if any provider is configured
|
| 30 |
+
if (!config.selectedProvider) {
|
| 31 |
+
setAiStatus('unavailable');
|
| 32 |
+
return;
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
// For providers that require API keys, check if key exists
|
| 36 |
+
const provider = AIProviderManager.getProviderById(config.selectedProvider);
|
| 37 |
+
if (provider?.requiresApiKey) {
|
| 38 |
+
const hasApiKey = config.apiKeys && config.apiKeys[config.selectedProvider];
|
| 39 |
+
if (!hasApiKey) {
|
| 40 |
+
setAiStatus('unavailable');
|
| 41 |
+
return;
|
| 42 |
+
}
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
// Test actual connection
|
| 46 |
+
const isAvailable = await AIProviderManager.testConnection(config.selectedProvider);
|
| 47 |
+
setAiStatus(isAvailable ? 'available' : 'unavailable');
|
| 48 |
+
} catch (error) {
|
| 49 |
+
console.error('AI availability check failed:', error);
|
| 50 |
+
setAiStatus('unavailable');
|
| 51 |
+
}
|
| 52 |
+
};
|
| 53 |
+
|
| 54 |
+
const handleGenerate = async () => {
|
| 55 |
+
if (!topic.trim()) return;
|
| 56 |
+
|
| 57 |
+
setLoading(true);
|
| 58 |
+
try {
|
| 59 |
+
let plan: StudyPlan;
|
| 60 |
+
|
| 61 |
+
if (useAI && aiStatus === 'available') {
|
| 62 |
+
// Use AI-enhanced generation
|
| 63 |
+
plan = await AIEnhancedWikimedia.generateEnhancedStudyPlan(topic, difficulty);
|
| 64 |
+
} else {
|
| 65 |
+
// Use standard Wikimedia-based generation
|
| 66 |
+
plan = await AIEnhancedWikimedia.generateStudyPlan(topic, difficulty);
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
setGeneratedPlan(plan);
|
| 70 |
+
} catch (error) {
|
| 71 |
+
console.error('Failed to generate study plan:', error);
|
| 72 |
+
} finally {
|
| 73 |
+
setLoading(false);
|
| 74 |
+
}
|
| 75 |
+
};
|
| 76 |
+
|
| 77 |
+
const handleAcceptPlan = () => {
|
| 78 |
+
if (generatedPlan) {
|
| 79 |
+
onPlanGenerated(generatedPlan);
|
| 80 |
+
setGeneratedPlan(null);
|
| 81 |
+
setTopic('');
|
| 82 |
+
}
|
| 83 |
+
};
|
| 84 |
+
|
| 85 |
+
const getDifficultyColor = (level: string) => {
|
| 86 |
+
switch (level) {
|
| 87 |
+
case 'beginner': return 'bg-success-100 text-success-800';
|
| 88 |
+
case 'intermediate': return 'bg-warning-100 text-warning-800';
|
| 89 |
+
case 'advanced': return 'bg-error-100 text-error-800';
|
| 90 |
+
default: return 'bg-gray-100 text-gray-800';
|
| 91 |
+
}
|
| 92 |
+
};
|
| 93 |
+
|
| 94 |
+
const getAIStatusMessage = () => {
|
| 95 |
+
const config = AIProviderManager.getConfig();
|
| 96 |
+
const provider = AIProviderManager.getProviderById(config.selectedProvider);
|
| 97 |
+
|
| 98 |
+
switch (aiStatus) {
|
| 99 |
+
case 'checking':
|
| 100 |
+
return 'Testing connection to configured AI provider...';
|
| 101 |
+
case 'available':
|
| 102 |
+
return `Using ${provider?.name || 'AI'} for enhanced study plan generation`;
|
| 103 |
+
case 'unavailable':
|
| 104 |
+
if (!config.selectedProvider) {
|
| 105 |
+
return 'No AI provider configured. Click "Configure AI" to set up open-source AI.';
|
| 106 |
+
}
|
| 107 |
+
if (provider?.requiresApiKey && !config.apiKeys?.[config.selectedProvider]) {
|
| 108 |
+
return `${provider.name} requires an API key. Click "Configure AI" to add it.`;
|
| 109 |
+
}
|
| 110 |
+
return `Cannot connect to ${provider?.name || 'AI provider'}. Using Wikimedia content analysis instead.`;
|
| 111 |
+
default:
|
| 112 |
+
return 'Checking AI availability...';
|
| 113 |
+
}
|
| 114 |
+
};
|
| 115 |
+
|
| 116 |
+
return (
|
| 117 |
+
<div className="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
|
| 118 |
+
<div className="bg-gradient-to-br from-primary-50 to-secondary-50 rounded-2xl p-8 border border-primary-100">
|
| 119 |
+
<div className="flex items-center space-x-3 mb-6">
|
| 120 |
+
<div className="w-12 h-12 bg-gradient-to-r from-primary-500 to-secondary-500 rounded-xl flex items-center justify-center">
|
| 121 |
+
<Sparkles className="w-6 h-6 text-white" />
|
| 122 |
+
</div>
|
| 123 |
+
<div>
|
| 124 |
+
<h2 className="text-2xl font-bold text-gray-900">AI Study Plan Generator</h2>
|
| 125 |
+
<p className="text-gray-600">Create personalized learning paths from Wikimedia content</p>
|
| 126 |
+
</div>
|
| 127 |
+
</div>
|
| 128 |
+
|
| 129 |
+
{/* AI Status Banner */}
|
| 130 |
+
<div className={`mb-6 p-4 rounded-xl border ${
|
| 131 |
+
aiStatus === 'available'
|
| 132 |
+
? 'bg-green-50 border-green-200'
|
| 133 |
+
: aiStatus === 'unavailable'
|
| 134 |
+
? 'bg-yellow-50 border-yellow-200'
|
| 135 |
+
: 'bg-gray-50 border-gray-200'
|
| 136 |
+
}`}>
|
| 137 |
+
<div className="flex items-start justify-between">
|
| 138 |
+
<div className="flex items-start space-x-3 flex-1">
|
| 139 |
+
{aiStatus === 'checking' && <Loader2 className="w-5 h-5 animate-spin text-gray-600 mt-0.5" />}
|
| 140 |
+
{aiStatus === 'available' && <Zap className="w-5 h-5 text-green-600 mt-0.5" />}
|
| 141 |
+
{aiStatus === 'unavailable' && <AlertCircle className="w-5 h-5 text-yellow-600 mt-0.5" />}
|
| 142 |
+
|
| 143 |
+
<div className="flex-1">
|
| 144 |
+
<div className="font-medium text-gray-900 mb-1">
|
| 145 |
+
{aiStatus === 'checking' && 'Checking AI Availability...'}
|
| 146 |
+
{aiStatus === 'available' && 'AI Enhancement Available'}
|
| 147 |
+
{aiStatus === 'unavailable' && 'AI Enhancement Unavailable'}
|
| 148 |
+
</div>
|
| 149 |
+
<div className="text-sm text-gray-600">
|
| 150 |
+
{getAIStatusMessage()}
|
| 151 |
+
</div>
|
| 152 |
+
|
| 153 |
+
{aiStatus === 'unavailable' && (
|
| 154 |
+
<button
|
| 155 |
+
onClick={() => setShowAIConfig(true)}
|
| 156 |
+
className="mt-2 text-sm text-primary-600 hover:text-primary-700 font-medium underline"
|
| 157 |
+
>
|
| 158 |
+
Configure AI Provider
|
| 159 |
+
</button>
|
| 160 |
+
)}
|
| 161 |
+
</div>
|
| 162 |
+
</div>
|
| 163 |
+
|
| 164 |
+
<div className="flex items-center space-x-3 ml-4">
|
| 165 |
+
{aiStatus === 'available' && (
|
| 166 |
+
<label className="flex items-center space-x-2">
|
| 167 |
+
<input
|
| 168 |
+
type="checkbox"
|
| 169 |
+
checked={useAI}
|
| 170 |
+
onChange={(e) => setUseAI(e.target.checked)}
|
| 171 |
+
className="rounded border-gray-300 text-primary-600 focus:ring-primary-500"
|
| 172 |
+
/>
|
| 173 |
+
<span className="text-sm font-medium text-gray-700">Use AI Enhancement</span>
|
| 174 |
+
</label>
|
| 175 |
+
)}
|
| 176 |
+
|
| 177 |
+
<button
|
| 178 |
+
onClick={() => setShowAIConfig(true)}
|
| 179 |
+
className="flex items-center space-x-1 px-3 py-1 bg-gray-100 text-gray-700 rounded-lg text-sm hover:bg-gray-200 transition-colors"
|
| 180 |
+
>
|
| 181 |
+
<Settings className="w-3 h-3" />
|
| 182 |
+
<span>Configure</span>
|
| 183 |
+
</button>
|
| 184 |
+
</div>
|
| 185 |
+
</div>
|
| 186 |
+
</div>
|
| 187 |
+
|
| 188 |
+
{!generatedPlan ? (
|
| 189 |
+
<div className="space-y-6">
|
| 190 |
+
<div>
|
| 191 |
+
<label className="block text-sm font-medium text-gray-700 mb-2">
|
| 192 |
+
What would you like to learn?
|
| 193 |
+
</label>
|
| 194 |
+
<input
|
| 195 |
+
type="text"
|
| 196 |
+
value={topic}
|
| 197 |
+
onChange={(e) => setTopic(e.target.value)}
|
| 198 |
+
placeholder="e.g., Machine Learning, Ancient History, Climate Science..."
|
| 199 |
+
className="w-full px-4 py-3 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary-500 focus:border-transparent text-lg"
|
| 200 |
+
onKeyDown={(e) => e.key === 'Enter' && handleGenerate()}
|
| 201 |
+
/>
|
| 202 |
+
</div>
|
| 203 |
+
|
| 204 |
+
<div>
|
| 205 |
+
<label className="block text-sm font-medium text-gray-700 mb-3">
|
| 206 |
+
Difficulty Level
|
| 207 |
+
</label>
|
| 208 |
+
<div className="grid grid-cols-3 gap-3">
|
| 209 |
+
{(['beginner', 'intermediate', 'advanced'] as const).map((level) => (
|
| 210 |
+
<button
|
| 211 |
+
key={level}
|
| 212 |
+
onClick={() => setDifficulty(level)}
|
| 213 |
+
className={`p-4 rounded-xl border-2 transition-all ${
|
| 214 |
+
difficulty === level
|
| 215 |
+
? 'border-primary-500 bg-primary-50 text-primary-700'
|
| 216 |
+
: 'border-gray-200 bg-white text-gray-700 hover:border-gray-300'
|
| 217 |
+
}`}
|
| 218 |
+
>
|
| 219 |
+
<div className="text-center">
|
| 220 |
+
<div className="font-medium capitalize">{level}</div>
|
| 221 |
+
<div className="text-sm text-gray-500 mt-1">
|
| 222 |
+
{level === 'beginner' && '3-5 topics'}
|
| 223 |
+
{level === 'intermediate' && '6-8 topics'}
|
| 224 |
+
{level === 'advanced' && '9-12 topics'}
|
| 225 |
+
</div>
|
| 226 |
+
</div>
|
| 227 |
+
</button>
|
| 228 |
+
))}
|
| 229 |
+
</div>
|
| 230 |
+
</div>
|
| 231 |
+
|
| 232 |
+
<button
|
| 233 |
+
onClick={handleGenerate}
|
| 234 |
+
disabled={loading || !topic.trim()}
|
| 235 |
+
className="w-full flex items-center justify-center space-x-2 px-6 py-4 bg-gradient-to-r from-primary-600 to-secondary-600 text-white rounded-xl hover:from-primary-700 hover:to-secondary-700 transition-all duration-200 disabled:opacity-50 disabled:cursor-not-allowed"
|
| 236 |
+
>
|
| 237 |
+
{loading ? (
|
| 238 |
+
<>
|
| 239 |
+
<Loader2 className="w-5 h-5 animate-spin" />
|
| 240 |
+
<span>
|
| 241 |
+
{useAI && aiStatus === 'available'
|
| 242 |
+
? 'AI is generating your study plan...'
|
| 243 |
+
: 'Generating Study Plan...'
|
| 244 |
+
}
|
| 245 |
+
</span>
|
| 246 |
+
</>
|
| 247 |
+
) : (
|
| 248 |
+
<>
|
| 249 |
+
<Sparkles className="w-5 h-5" />
|
| 250 |
+
<span>
|
| 251 |
+
{useAI && aiStatus === 'available'
|
| 252 |
+
? 'Generate AI-Enhanced Study Plan'
|
| 253 |
+
: 'Generate Study Plan'
|
| 254 |
+
}
|
| 255 |
+
</span>
|
| 256 |
+
</>
|
| 257 |
+
)}
|
| 258 |
+
</button>
|
| 259 |
+
|
| 260 |
+
{/* Quick Setup Guide */}
|
| 261 |
+
{aiStatus === 'unavailable' && (
|
| 262 |
+
<div className="bg-blue-50 border border-blue-200 rounded-xl p-4">
|
| 263 |
+
<h4 className="font-medium text-blue-900 mb-3">🚀 Quick AI Setup</h4>
|
| 264 |
+
<div className="space-y-3 text-sm text-blue-800">
|
| 265 |
+
<div className="flex items-start space-x-2">
|
| 266 |
+
<span className="font-medium">Option 1 (Privacy):</span>
|
| 267 |
+
<div>
|
| 268 |
+
<p>Install Ollama locally for complete privacy</p>
|
| 269 |
+
<p className="text-blue-600">• Download from ollama.ai</p>
|
| 270 |
+
<p className="text-blue-600">• Run: ollama pull llama3.2</p>
|
| 271 |
+
</div>
|
| 272 |
+
</div>
|
| 273 |
+
<div className="flex items-start space-x-2">
|
| 274 |
+
<span className="font-medium">Option 2 (Cloud):</span>
|
| 275 |
+
<div>
|
| 276 |
+
<p>Get free Hugging Face API key</p>
|
| 277 |
+
<p className="text-blue-600">• Sign up at huggingface.co</p>
|
| 278 |
+
<p className="text-blue-600">• Add key in AI configuration</p>
|
| 279 |
+
</div>
|
| 280 |
+
</div>
|
| 281 |
+
</div>
|
| 282 |
+
<button
|
| 283 |
+
onClick={checkAIAvailability}
|
| 284 |
+
className="mt-3 text-sm text-blue-600 hover:text-blue-700 font-medium"
|
| 285 |
+
>
|
| 286 |
+
Recheck AI Availability
|
| 287 |
+
</button>
|
| 288 |
+
</div>
|
| 289 |
+
)}
|
| 290 |
+
</div>
|
| 291 |
+
) : (
|
| 292 |
+
<div className="space-y-6">
|
| 293 |
+
<div className="bg-white rounded-xl p-6 border border-gray-200">
|
| 294 |
+
<div className="flex items-start justify-between mb-4">
|
| 295 |
+
<div className="flex-1">
|
| 296 |
+
<div className="flex items-center space-x-2 mb-2">
|
| 297 |
+
<h3 className="text-xl font-bold text-gray-900">{generatedPlan.title}</h3>
|
| 298 |
+
{useAI && aiStatus === 'available' && (
|
| 299 |
+
<span className="px-2 py-1 bg-purple-100 text-purple-700 text-xs rounded-full font-medium">
|
| 300 |
+
AI Enhanced
|
| 301 |
+
</span>
|
| 302 |
+
)}
|
| 303 |
+
</div>
|
| 304 |
+
<p className="text-gray-600 mb-4">{generatedPlan.description}</p>
|
| 305 |
+
|
| 306 |
+
<div className="flex items-center space-x-4">
|
| 307 |
+
<span className={`px-3 py-1 rounded-full text-sm font-medium ${getDifficultyColor(generatedPlan.difficulty)}`}>
|
| 308 |
+
{generatedPlan.difficulty}
|
| 309 |
+
</span>
|
| 310 |
+
<div className="flex items-center text-sm text-gray-600">
|
| 311 |
+
<Clock className="w-4 h-4 mr-1" />
|
| 312 |
+
{generatedPlan.estimatedTime}
|
| 313 |
+
</div>
|
| 314 |
+
<div className="flex items-center text-sm text-gray-600">
|
| 315 |
+
<BookOpen className="w-4 h-4 mr-1" />
|
| 316 |
+
{generatedPlan.topics.length} topics
|
| 317 |
+
</div>
|
| 318 |
+
</div>
|
| 319 |
+
</div>
|
| 320 |
+
</div>
|
| 321 |
+
|
| 322 |
+
<div className="space-y-3">
|
| 323 |
+
<h4 className="font-medium text-gray-900">Study Topics:</h4>
|
| 324 |
+
{generatedPlan.topics.slice(0, 5).map((topic, index) => (
|
| 325 |
+
<div key={topic.id} className="flex items-center space-x-3 p-3 bg-gray-50 rounded-lg">
|
| 326 |
+
<div className="w-6 h-6 bg-primary-100 rounded-full flex items-center justify-center text-primary-600 text-sm font-medium">
|
| 327 |
+
{index + 1}
|
| 328 |
+
</div>
|
| 329 |
+
<div className="flex-1">
|
| 330 |
+
<div className="font-medium text-gray-900">{topic.title}</div>
|
| 331 |
+
<div className="text-sm text-gray-600">{topic.estimatedTime}</div>
|
| 332 |
+
</div>
|
| 333 |
+
<CheckCircle className="w-5 h-5 text-gray-300" />
|
| 334 |
+
</div>
|
| 335 |
+
))}
|
| 336 |
+
{generatedPlan.topics.length > 5 && (
|
| 337 |
+
<div className="text-sm text-gray-500 text-center py-2">
|
| 338 |
+
+{generatedPlan.topics.length - 5} more topics...
|
| 339 |
+
</div>
|
| 340 |
+
)}
|
| 341 |
+
</div>
|
| 342 |
+
</div>
|
| 343 |
+
|
| 344 |
+
<div className="flex space-x-3">
|
| 345 |
+
<button
|
| 346 |
+
onClick={() => setGeneratedPlan(null)}
|
| 347 |
+
className="flex-1 px-4 py-3 border border-gray-300 rounded-xl text-gray-700 hover:bg-gray-50 transition-colors"
|
| 348 |
+
>
|
| 349 |
+
Generate New Plan
|
| 350 |
+
</button>
|
| 351 |
+
<button
|
| 352 |
+
onClick={handleAcceptPlan}
|
| 353 |
+
className="flex-1 flex items-center justify-center space-x-2 px-4 py-3 bg-primary-600 text-white rounded-xl hover:bg-primary-700 transition-colors"
|
| 354 |
+
>
|
| 355 |
+
<span>Accept Plan</span>
|
| 356 |
+
<ArrowRight className="w-4 h-4" />
|
| 357 |
+
</button>
|
| 358 |
+
</div>
|
| 359 |
+
</div>
|
| 360 |
+
)}
|
| 361 |
+
</div>
|
| 362 |
+
</div>
|
| 363 |
+
);
|
| 364 |
+
};
|
| 365 |
+
|
| 366 |
+
export default AIStudyPlanGenerator;
|
src/components/ArticleViewer.tsx
ADDED
|
@@ -0,0 +1,327 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React, { useState } from 'react';
|
| 2 |
+
import { ArrowLeft, ExternalLink, BookOpen, Clock, Share2, Bookmark, Eye, Sparkles, Wand2, Loader2 } from 'lucide-react';
|
| 3 |
+
import { WikimediaAPI } from '../utils/wikimedia-api';
|
| 4 |
+
|
| 5 |
+
interface ArticleViewerProps {
|
| 6 |
+
title: string;
|
| 7 |
+
project: string;
|
| 8 |
+
content: string;
|
| 9 |
+
onBack: () => void;
|
| 10 |
+
onCreateStudyPlan?: (topic: string) => void;
|
| 11 |
+
onTransformContent?: (title: string, content: string) => void;
|
| 12 |
+
onViewArticle?: (title: string, project: string, content: string) => void;
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
const ArticleViewer: React.FC<ArticleViewerProps> = ({
|
| 16 |
+
title,
|
| 17 |
+
project,
|
| 18 |
+
content,
|
| 19 |
+
onBack,
|
| 20 |
+
onCreateStudyPlan,
|
| 21 |
+
onTransformContent,
|
| 22 |
+
onViewArticle
|
| 23 |
+
}) => {
|
| 24 |
+
const [fontSize, setFontSize] = useState('text-base');
|
| 25 |
+
const [readingTime, setReadingTime] = useState(0);
|
| 26 |
+
const [relatedArticles, setRelatedArticles] = useState<any[]>([]);
|
| 27 |
+
const [loadingRelated, setLoadingRelated] = useState(false);
|
| 28 |
+
const [showRelated, setShowRelated] = useState(false);
|
| 29 |
+
|
| 30 |
+
React.useEffect(() => {
|
| 31 |
+
// Calculate reading time (average 200 words per minute)
|
| 32 |
+
const wordCount = content.split(' ').length;
|
| 33 |
+
const time = Math.ceil(wordCount / 200);
|
| 34 |
+
setReadingTime(time);
|
| 35 |
+
}, [content]);
|
| 36 |
+
|
| 37 |
+
const formatContent = (text: string) => {
|
| 38 |
+
// Split content into paragraphs and format
|
| 39 |
+
const paragraphs = text.split('\n').filter(p => p.trim().length > 0);
|
| 40 |
+
return paragraphs.map((paragraph, index) => {
|
| 41 |
+
// Check if it's a heading (starts with ==)
|
| 42 |
+
if (paragraph.startsWith('==') && paragraph.endsWith('==')) {
|
| 43 |
+
const headingText = paragraph.replace(/=/g, '').trim();
|
| 44 |
+
return (
|
| 45 |
+
<h2 key={index} className="text-2xl font-bold text-gray-900 mt-8 mb-4 first:mt-0">
|
| 46 |
+
{headingText}
|
| 47 |
+
</h2>
|
| 48 |
+
);
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
// Regular paragraph
|
| 52 |
+
return (
|
| 53 |
+
<p key={index} className={`${fontSize} text-gray-700 leading-relaxed mb-4`}>
|
| 54 |
+
{paragraph}
|
| 55 |
+
</p>
|
| 56 |
+
);
|
| 57 |
+
});
|
| 58 |
+
};
|
| 59 |
+
|
| 60 |
+
const getProjectUrl = () => {
|
| 61 |
+
const baseUrls: Record<string, string> = {
|
| 62 |
+
wikipedia: 'https://en.wikipedia.org/wiki/',
|
| 63 |
+
'en-wikipedia': 'https://en.wikipedia.org/wiki/',
|
| 64 |
+
'es-wikipedia': 'https://es.wikipedia.org/wiki/',
|
| 65 |
+
'fr-wikipedia': 'https://fr.wikipedia.org/wiki/',
|
| 66 |
+
'de-wikipedia': 'https://de.wikipedia.org/wiki/',
|
| 67 |
+
wikibooks: 'https://en.wikibooks.org/wiki/',
|
| 68 |
+
wikiquote: 'https://en.wikiquote.org/wiki/',
|
| 69 |
+
wikiversity: 'https://en.wikiversity.org/wiki/',
|
| 70 |
+
wiktionary: 'https://en.wiktionary.org/wiki/',
|
| 71 |
+
wikisource: 'https://en.wikisource.org/wiki/',
|
| 72 |
+
};
|
| 73 |
+
|
| 74 |
+
const baseUrl = baseUrls[project] || baseUrls.wikipedia;
|
| 75 |
+
return baseUrl + encodeURIComponent(title.replace(/ /g, '_'));
|
| 76 |
+
};
|
| 77 |
+
|
| 78 |
+
const handleShare = async () => {
|
| 79 |
+
if (navigator.share) {
|
| 80 |
+
try {
|
| 81 |
+
await navigator.share({
|
| 82 |
+
title: title,
|
| 83 |
+
text: `Check out this article about ${title}`,
|
| 84 |
+
url: window.location.href,
|
| 85 |
+
});
|
| 86 |
+
} catch (error) {
|
| 87 |
+
console.log('Error sharing:', error);
|
| 88 |
+
}
|
| 89 |
+
} else {
|
| 90 |
+
// Fallback: copy to clipboard
|
| 91 |
+
navigator.clipboard.writeText(window.location.href);
|
| 92 |
+
}
|
| 93 |
+
};
|
| 94 |
+
|
| 95 |
+
const handleCreateStudyPlan = () => {
|
| 96 |
+
if (onCreateStudyPlan) {
|
| 97 |
+
onCreateStudyPlan(title);
|
| 98 |
+
}
|
| 99 |
+
};
|
| 100 |
+
|
| 101 |
+
const handleTransformContent = () => {
|
| 102 |
+
if (onTransformContent) {
|
| 103 |
+
onTransformContent(title, content);
|
| 104 |
+
}
|
| 105 |
+
};
|
| 106 |
+
|
| 107 |
+
const handleFindRelated = async () => {
|
| 108 |
+
setLoadingRelated(true);
|
| 109 |
+
setShowRelated(true);
|
| 110 |
+
|
| 111 |
+
try {
|
| 112 |
+
// Search for related articles using keywords from the title
|
| 113 |
+
const searchTerms = title.split(' ').filter(term => term.length > 3);
|
| 114 |
+
const relatedResults = [];
|
| 115 |
+
|
| 116 |
+
// Search for each term and collect results
|
| 117 |
+
for (const term of searchTerms.slice(0, 2)) {
|
| 118 |
+
const results = await WikimediaAPI.search(term, 'wikipedia', 3);
|
| 119 |
+
relatedResults.push(...results.filter(r => r.title !== title));
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
// Remove duplicates and limit to 6 results
|
| 123 |
+
const uniqueResults = relatedResults.filter((result, index, self) =>
|
| 124 |
+
index === self.findIndex(r => r.title === result.title)
|
| 125 |
+
).slice(0, 6);
|
| 126 |
+
|
| 127 |
+
setRelatedArticles(uniqueResults);
|
| 128 |
+
} catch (error) {
|
| 129 |
+
console.error('Failed to find related articles:', error);
|
| 130 |
+
setRelatedArticles([]);
|
| 131 |
+
} finally {
|
| 132 |
+
setLoadingRelated(false);
|
| 133 |
+
}
|
| 134 |
+
};
|
| 135 |
+
|
| 136 |
+
const handleViewRelated = async (article: any) => {
|
| 137 |
+
try {
|
| 138 |
+
const content = await WikimediaAPI.getPageContent(article.title, article.project);
|
| 139 |
+
if (onViewArticle) {
|
| 140 |
+
onViewArticle(article.title, article.project, content);
|
| 141 |
+
}
|
| 142 |
+
} catch (error) {
|
| 143 |
+
console.error('Failed to load related article:', error);
|
| 144 |
+
}
|
| 145 |
+
};
|
| 146 |
+
|
| 147 |
+
return (
|
| 148 |
+
<div className="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
|
| 149 |
+
{/* Header */}
|
| 150 |
+
<div className="mb-8">
|
| 151 |
+
<button
|
| 152 |
+
onClick={onBack}
|
| 153 |
+
className="flex items-center space-x-2 text-primary-600 hover:text-primary-700 font-medium mb-6 transition-colors"
|
| 154 |
+
>
|
| 155 |
+
<ArrowLeft className="w-4 h-4" />
|
| 156 |
+
<span>Back</span>
|
| 157 |
+
</button>
|
| 158 |
+
|
| 159 |
+
<div className="bg-white rounded-2xl p-6 border border-gray-200 shadow-sm">
|
| 160 |
+
<div className="flex items-start justify-between mb-4">
|
| 161 |
+
<div className="flex-1">
|
| 162 |
+
<div className="flex items-center space-x-2 mb-2">
|
| 163 |
+
<span className="px-3 py-1 bg-primary-100 text-primary-700 text-sm rounded-full font-medium capitalize">
|
| 164 |
+
{project.replace('-wikipedia', '')}
|
| 165 |
+
</span>
|
| 166 |
+
<div className="flex items-center text-sm text-gray-600">
|
| 167 |
+
<Clock className="w-4 h-4 mr-1" />
|
| 168 |
+
<span>{readingTime} min read</span>
|
| 169 |
+
</div>
|
| 170 |
+
</div>
|
| 171 |
+
<h1 className="text-3xl font-bold text-gray-900 mb-2">{title}</h1>
|
| 172 |
+
</div>
|
| 173 |
+
</div>
|
| 174 |
+
|
| 175 |
+
{/* Article Controls */}
|
| 176 |
+
<div className="flex items-center justify-between pt-4 border-t border-gray-200">
|
| 177 |
+
<div className="flex items-center space-x-4">
|
| 178 |
+
<div className="flex items-center space-x-2">
|
| 179 |
+
<span className="text-sm text-gray-600">Font size:</span>
|
| 180 |
+
<select
|
| 181 |
+
value={fontSize}
|
| 182 |
+
onChange={(e) => setFontSize(e.target.value)}
|
| 183 |
+
className="text-sm border border-gray-300 rounded px-2 py-1"
|
| 184 |
+
>
|
| 185 |
+
<option value="text-sm">Small</option>
|
| 186 |
+
<option value="text-base">Medium</option>
|
| 187 |
+
<option value="text-lg">Large</option>
|
| 188 |
+
<option value="text-xl">Extra Large</option>
|
| 189 |
+
</select>
|
| 190 |
+
</div>
|
| 191 |
+
</div>
|
| 192 |
+
|
| 193 |
+
<div className="flex items-center space-x-2">
|
| 194 |
+
<button
|
| 195 |
+
onClick={handleShare}
|
| 196 |
+
className="flex items-center space-x-1 px-3 py-2 text-gray-600 hover:text-gray-900 transition-colors"
|
| 197 |
+
>
|
| 198 |
+
<Share2 className="w-4 h-4" />
|
| 199 |
+
<span className="text-sm">Share</span>
|
| 200 |
+
</button>
|
| 201 |
+
|
| 202 |
+
<button className="flex items-center space-x-1 px-3 py-2 text-gray-600 hover:text-gray-900 transition-colors">
|
| 203 |
+
<Bookmark className="w-4 h-4" />
|
| 204 |
+
<span className="text-sm">Save</span>
|
| 205 |
+
</button>
|
| 206 |
+
|
| 207 |
+
<a
|
| 208 |
+
href={getProjectUrl()}
|
| 209 |
+
target="_blank"
|
| 210 |
+
rel="noopener noreferrer"
|
| 211 |
+
className="flex items-center space-x-1 px-3 py-2 bg-primary-600 text-white rounded-lg hover:bg-primary-700 transition-colors"
|
| 212 |
+
>
|
| 213 |
+
<ExternalLink className="w-4 h-4" />
|
| 214 |
+
<span className="text-sm">View Original</span>
|
| 215 |
+
</a>
|
| 216 |
+
</div>
|
| 217 |
+
</div>
|
| 218 |
+
</div>
|
| 219 |
+
</div>
|
| 220 |
+
|
| 221 |
+
{/* Article Content */}
|
| 222 |
+
<div className="bg-white rounded-2xl p-8 border border-gray-200 shadow-sm">
|
| 223 |
+
<div className="prose max-w-none">
|
| 224 |
+
{formatContent(content)}
|
| 225 |
+
</div>
|
| 226 |
+
|
| 227 |
+
{content.length < 500 && (
|
| 228 |
+
<div className="mt-8 p-4 bg-yellow-50 border border-yellow-200 rounded-lg">
|
| 229 |
+
<p className="text-yellow-800 text-sm">
|
| 230 |
+
This appears to be a short excerpt. For the complete article with images, references, and full content,
|
| 231 |
+
please visit the original source.
|
| 232 |
+
</p>
|
| 233 |
+
</div>
|
| 234 |
+
)}
|
| 235 |
+
</div>
|
| 236 |
+
|
| 237 |
+
{/* Related Actions */}
|
| 238 |
+
<div className="mt-8 bg-white rounded-2xl p-6 border border-gray-200 shadow-sm">
|
| 239 |
+
<h3 className="text-lg font-semibold text-gray-900 mb-4">What's Next?</h3>
|
| 240 |
+
<div className="grid grid-cols-1 md:grid-cols-3 gap-4">
|
| 241 |
+
<button
|
| 242 |
+
onClick={handleCreateStudyPlan}
|
| 243 |
+
className="flex items-center space-x-3 p-4 border border-gray-200 rounded-lg hover:border-primary-200 hover:bg-primary-50 transition-colors group"
|
| 244 |
+
>
|
| 245 |
+
<Sparkles className="w-6 h-6 text-primary-600 group-hover:scale-110 transition-transform" />
|
| 246 |
+
<div className="text-left">
|
| 247 |
+
<div className="font-medium text-gray-900">Create Study Plan</div>
|
| 248 |
+
<div className="text-sm text-gray-600">Generate a learning path from this topic</div>
|
| 249 |
+
</div>
|
| 250 |
+
</button>
|
| 251 |
+
|
| 252 |
+
<button
|
| 253 |
+
onClick={handleFindRelated}
|
| 254 |
+
disabled={loadingRelated}
|
| 255 |
+
className="flex items-center space-x-3 p-4 border border-gray-200 rounded-lg hover:border-primary-200 hover:bg-primary-50 transition-colors group disabled:opacity-50"
|
| 256 |
+
>
|
| 257 |
+
{loadingRelated ? (
|
| 258 |
+
<Loader2 className="w-6 h-6 text-primary-600 animate-spin" />
|
| 259 |
+
) : (
|
| 260 |
+
<Eye className="w-6 h-6 text-primary-600 group-hover:scale-110 transition-transform" />
|
| 261 |
+
)}
|
| 262 |
+
<div className="text-left">
|
| 263 |
+
<div className="font-medium text-gray-900">Find Related</div>
|
| 264 |
+
<div className="text-sm text-gray-600">
|
| 265 |
+
{loadingRelated ? 'Searching...' : 'Discover similar articles'}
|
| 266 |
+
</div>
|
| 267 |
+
</div>
|
| 268 |
+
</button>
|
| 269 |
+
|
| 270 |
+
<button
|
| 271 |
+
onClick={handleTransformContent}
|
| 272 |
+
className="flex items-center space-x-3 p-4 border border-gray-200 rounded-lg hover:border-primary-200 hover:bg-primary-50 transition-colors group"
|
| 273 |
+
>
|
| 274 |
+
<Wand2 className="w-6 h-6 text-primary-600 group-hover:scale-110 transition-transform" />
|
| 275 |
+
<div className="text-left">
|
| 276 |
+
<div className="font-medium text-gray-900">Transform Content</div>
|
| 277 |
+
<div className="text-sm text-gray-600">Create quiz or summary</div>
|
| 278 |
+
</div>
|
| 279 |
+
</button>
|
| 280 |
+
</div>
|
| 281 |
+
</div>
|
| 282 |
+
|
| 283 |
+
{/* Related Articles Section */}
|
| 284 |
+
{showRelated && (
|
| 285 |
+
<div className="mt-8 bg-white rounded-2xl p-6 border border-gray-200 shadow-sm">
|
| 286 |
+
<h3 className="text-lg font-semibold text-gray-900 mb-4">Related Articles</h3>
|
| 287 |
+
|
| 288 |
+
{loadingRelated ? (
|
| 289 |
+
<div className="flex items-center justify-center py-8">
|
| 290 |
+
<Loader2 className="w-6 h-6 animate-spin text-primary-600" />
|
| 291 |
+
<span className="ml-2 text-gray-600">Finding related articles...</span>
|
| 292 |
+
</div>
|
| 293 |
+
) : relatedArticles.length > 0 ? (
|
| 294 |
+
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
| 295 |
+
{relatedArticles.map((article, index) => (
|
| 296 |
+
<div
|
| 297 |
+
key={index}
|
| 298 |
+
className="p-4 border border-gray-200 rounded-lg hover:border-primary-200 hover:bg-primary-50 transition-colors cursor-pointer"
|
| 299 |
+
onClick={() => handleViewRelated(article)}
|
| 300 |
+
>
|
| 301 |
+
<h4 className="font-medium text-gray-900 mb-2">{article.title}</h4>
|
| 302 |
+
<p className="text-sm text-gray-600 mb-2">
|
| 303 |
+
{article.snippet.replace(/<[^>]*>/g, '').substring(0, 100)}...
|
| 304 |
+
</p>
|
| 305 |
+
<div className="flex items-center justify-between">
|
| 306 |
+
<span className="text-xs text-primary-600 bg-primary-100 px-2 py-1 rounded-full">
|
| 307 |
+
{article.project}
|
| 308 |
+
</span>
|
| 309 |
+
<button className="text-sm text-primary-600 hover:text-primary-700">
|
| 310 |
+
Read more →
|
| 311 |
+
</button>
|
| 312 |
+
</div>
|
| 313 |
+
</div>
|
| 314 |
+
))}
|
| 315 |
+
</div>
|
| 316 |
+
) : (
|
| 317 |
+
<div className="text-center py-8">
|
| 318 |
+
<p className="text-gray-600">No related articles found. Try searching for specific topics.</p>
|
| 319 |
+
</div>
|
| 320 |
+
)}
|
| 321 |
+
</div>
|
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from 'react';
|
| 2 |
+
import { Wand2, FileText, List, BookOpen, MessageSquare, Loader2, Settings, Zap } from 'lucide-react';
|
| 3 |
+
import { AIEnhancedWikimedia } from '../utils/ai-enhanced-wikimedia';
|
| 4 |
+
import { AIProviderManager } from '../utils/ai-providers';
|
| 5 |
+
|
| 6 |
+
const ContentTransformer: React.FC = () => {
|
| 7 |
+
const [inputUrl, setInputUrl] = useState('');
|
| 8 |
+
const [transformationType, setTransformationType] = useState<'summary' | 'quiz' | 'outline' | 'flashcards'>('summary');
|
| 9 |
+
const [loading, setLoading] = useState(false);
|
| 10 |
+
const [transformedContent, setTransformedContent] = useState<any>(null);
|
| 11 |
+
const [selectedAnswers, setSelectedAnswers] = useState<Record<number, number>>({});
|
| 12 |
+
const [useAI, setUseAI] = useState(true);
|
| 13 |
+
const [aiStatus, setAiStatus] = useState<'checking' | 'available' | 'unavailable'>('checking');
|
| 14 |
+
|
| 15 |
+
React.useEffect(() => {
|
| 16 |
+
checkAIAvailability();
|
| 17 |
+
}, []);
|
| 18 |
+
|
| 19 |
+
const checkAIAvailability = async () => {
|
| 20 |
+
try {
|
| 21 |
+
const config = AIProviderManager.getConfig();
|
| 22 |
+
const isAvailable = await AIProviderManager.testConnection(config.selectedProvider);
|
| 23 |
+
setAiStatus(isAvailable ? 'available' : 'unavailable');
|
| 24 |
+
} catch (error) {
|
| 25 |
+
setAiStatus('unavailable');
|
| 26 |
+
}
|
| 27 |
+
};
|
| 28 |
+
|
| 29 |
+
const transformationTypes = [
|
| 30 |
+
{
|
| 31 |
+
id: 'summary' as const,
|
| 32 |
+
name: 'Summary',
|
| 33 |
+
description: 'Create concise summaries',
|
| 34 |
+
icon: FileText,
|
| 35 |
+
color: 'bg-primary-500'
|
| 36 |
+
},
|
| 37 |
+
{
|
| 38 |
+
id: 'quiz' as const,
|
| 39 |
+
name: 'Quiz',
|
| 40 |
+
description: 'Generate practice questions',
|
| 41 |
+
icon: MessageSquare,
|
| 42 |
+
color: 'bg-secondary-500'
|
| 43 |
+
},
|
| 44 |
+
{
|
| 45 |
+
id: 'outline' as const,
|
| 46 |
+
name: 'Study Outline',
|
| 47 |
+
description: 'Structured learning outline',
|
| 48 |
+
icon: List,
|
| 49 |
+
color: 'bg-accent-500'
|
| 50 |
+
},
|
| 51 |
+
{
|
| 52 |
+
id: 'flashcards' as const,
|
| 53 |
+
name: 'Flashcards',
|
| 54 |
+
description: 'Key concepts for memorization',
|
| 55 |
+
icon: BookOpen,
|
| 56 |
+
color: 'bg-success-500'
|
| 57 |
+
}
|
| 58 |
+
];
|
| 59 |
+
|
| 60 |
+
const extractTitleFromUrl = (url: string): string => {
|
| 61 |
+
try {
|
| 62 |
+
const urlObj = new URL(url);
|
| 63 |
+
const pathParts = urlObj.pathname.split('/');
|
| 64 |
+
const title = pathParts[pathParts.length - 1];
|
| 65 |
+
return decodeURIComponent(title.replace(/_/g, ' '));
|
| 66 |
+
} catch {
|
| 67 |
+
return '';
|
| 68 |
+
}
|
| 69 |
+
};
|
| 70 |
+
|
| 71 |
+
const detectProject = (url: string): string => {
|
| 72 |
+
if (url.includes('wikipedia.org')) return 'wikipedia';
|
| 73 |
+
if (url.includes('wikibooks.org')) return 'wikibooks';
|
| 74 |
+
if (url.includes('wikiversity.org')) return 'wikiversity';
|
| 75 |
+
if (url.includes('wikiquote.org')) return 'wikiquote';
|
| 76 |
+
if (url.includes('wiktionary.org')) return 'wiktionary';
|
| 77 |
+
if (url.includes('wikisource.org')) return 'wikisource';
|
| 78 |
+
return 'wikipedia';
|
| 79 |
+
};
|
| 80 |
+
|
| 81 |
+
const handleTransform = async () => {
|
| 82 |
+
if (!inputUrl.trim()) return;
|
| 83 |
+
|
| 84 |
+
setLoading(true);
|
| 85 |
+
setSelectedAnswers({});
|
| 86 |
+
try {
|
| 87 |
+
const title = extractTitleFromUrl(inputUrl);
|
| 88 |
+
const project = detectProject(inputUrl);
|
| 89 |
+
const content = await AIEnhancedWikimedia.getPageContent(title, project);
|
| 90 |
+
|
| 91 |
+
let transformed;
|
| 92 |
+
|
| 93 |
+
if (useAI && aiStatus === 'available') {
|
| 94 |
+
// Use AI-enhanced transformation
|
| 95 |
+
switch (transformationType) {
|
| 96 |
+
case 'summary':
|
| 97 |
+
transformed = await AIEnhancedWikimedia.generateSummaryFromContent(content, title);
|
| 98 |
+
break;
|
| 99 |
+
case 'quiz':
|
| 100 |
+
transformed = await AIEnhancedWikimedia.generateQuizFromContent(content, title);
|
| 101 |
+
break;
|
| 102 |
+
case 'outline':
|
| 103 |
+
transformed = await AIEnhancedWikimedia.generateStudyOutline(content, title);
|
| 104 |
+
break;
|
| 105 |
+
case 'flashcards':
|
| 106 |
+
transformed = await AIEnhancedWikimedia.generateFlashcards(content, title);
|
| 107 |
+
break;
|
| 108 |
+
}
|
| 109 |
+
} else {
|
| 110 |
+
// Use fallback rule-based transformation
|
| 111 |
+
switch (transformationType) {
|
| 112 |
+
case 'summary':
|
| 113 |
+
transformed = generateSummary(content, title);
|
| 114 |
+
break;
|
| 115 |
+
case 'quiz':
|
| 116 |
+
transformed = generateQuiz(content, title);
|
| 117 |
+
break;
|
| 118 |
+
case 'outline':
|
| 119 |
+
transformed = generateOutline(content, title);
|
| 120 |
+
break;
|
| 121 |
+
case 'flashcards':
|
| 122 |
+
transformed = generateFlashcards(content, title);
|
| 123 |
+
break;
|
| 124 |
+
}
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
setTransformedContent(transformed);
|
| 128 |
+
} catch (error) {
|
| 129 |
+
console.error('Content transformation failed:', error);
|
| 130 |
+
} finally {
|
| 131 |
+
setLoading(false);
|
| 132 |
+
}
|
| 133 |
+
};
|
| 134 |
+
|
| 135 |
+
// Fallback generation methods (existing code)
|
| 136 |
+
const generateSummary = (content: string, title: string) => {
|
| 137 |
+
const sentences = content.split('.').filter(s => s.trim().length > 20);
|
| 138 |
+
const keySentences = sentences.slice(0, 5);
|
| 139 |
+
|
| 140 |
+
return {
|
| 141 |
+
type: 'summary',
|
| 142 |
+
title: `Summary: ${title}`,
|
| 143 |
+
content: keySentences.join('. ') + '.',
|
| 144 |
+
keyPoints: sentences.slice(5, 10).map(s => s.trim()).filter(s => s.length > 0)
|
| 145 |
+
};
|
| 146 |
+
};
|
| 147 |
+
|
| 148 |
+
const generateQuiz = (content: string, title: string) => {
|
| 149 |
+
const questions = [
|
| 150 |
+
{
|
| 151 |
+
question: `What is the main topic discussed in the article about ${title}?`,
|
| 152 |
+
options: [
|
| 153 |
+
`The fundamental concepts and principles of ${title}`,
|
| 154 |
+
`The historical development of ${title}`,
|
| 155 |
+
`The practical applications of ${title}`,
|
| 156 |
+
`The criticism and controversies surrounding ${title}`
|
| 157 |
+
],
|
| 158 |
+
correct: 0
|
| 159 |
+
}
|
| 160 |
+
];
|
| 161 |
+
|
| 162 |
+
return {
|
| 163 |
+
type: 'quiz',
|
| 164 |
+
title: `Quiz: ${title}`,
|
| 165 |
+
questions
|
| 166 |
+
};
|
| 167 |
+
};
|
| 168 |
+
|
| 169 |
+
const generateOutline = (content: string, title: string) => {
|
| 170 |
+
return {
|
| 171 |
+
type: 'outline',
|
| 172 |
+
title: `Study Outline: ${title}`,
|
| 173 |
+
sections: [
|
| 174 |
+
{
|
| 175 |
+
title: 'Introduction',
|
| 176 |
+
points: [
|
| 177 |
+
`Overview and definition of ${title}`,
|
| 178 |
+
'Historical context and background',
|
| 179 |
+
'Key terminology and concepts'
|
| 180 |
+
]
|
| 181 |
+
},
|
| 182 |
+
{
|
| 183 |
+
title: 'Main Concepts',
|
| 184 |
+
points: [
|
| 185 |
+
'Core principles and theories',
|
| 186 |
+
'Important characteristics and features',
|
| 187 |
+
'Fundamental mechanisms and processes'
|
| 188 |
+
]
|
| 189 |
+
}
|
| 190 |
+
]
|
| 191 |
+
};
|
| 192 |
+
};
|
| 193 |
+
|
| 194 |
+
const generateFlashcards = (content: string, title: string) => {
|
| 195 |
+
const cards = [
|
| 196 |
+
{
|
| 197 |
+
front: `What is ${title}?`,
|
| 198 |
+
back: `${title} is a comprehensive topic that encompasses various concepts, principles, and applications within its field of study.`
|
| 199 |
+
},
|
| 200 |
+
{
|
| 201 |
+
front: 'Key characteristics',
|
| 202 |
+
back: `The main features include fundamental principles, practical applications, and significant impact on related areas.`
|
| 203 |
+
}
|
| 204 |
+
];
|
| 205 |
+
|
| 206 |
+
return {
|
| 207 |
+
type: 'flashcards',
|
| 208 |
+
title: `Flashcards: ${title}`,
|
| 209 |
+
cards
|
| 210 |
+
};
|
| 211 |
+
};
|
| 212 |
+
|
| 213 |
+
const handleAnswerSelect = (questionIndex: number, answerIndex: number) => {
|
| 214 |
+
setSelectedAnswers(prev => ({
|
| 215 |
+
...prev,
|
| 216 |
+
[questionIndex]: answerIndex
|
| 217 |
+
}));
|
| 218 |
+
};
|
| 219 |
+
|
| 220 |
+
const renderTransformedContent = () => {
|
| 221 |
+
if (!transformedContent) return null;
|
| 222 |
+
|
| 223 |
+
switch (transformedContent.type) {
|
| 224 |
+
case 'summary':
|
| 225 |
+
return (
|
| 226 |
+
<div className="space-y-4">
|
| 227 |
+
<div className="prose max-w-none">
|
| 228 |
+
<p className="text-gray-700 leading-relaxed">{transformedContent.content}</p>
|
| 229 |
+
</div>
|
| 230 |
+
{transformedContent.keyPoints && (
|
| 231 |
+
<div>
|
| 232 |
+
<h4 className="font-medium text-gray-900 mb-3">Key Points:</h4>
|
| 233 |
+
<ul className="space-y-2">
|
| 234 |
+
{transformedContent.keyPoints.map((point: string, index: number) => (
|
| 235 |
+
<li key={index} className="flex items-start space-x-2">
|
| 236 |
+
<div className="w-2 h-2 bg-primary-500 rounded-full mt-2 flex-shrink-0" />
|
| 237 |
+
<span className="text-gray-700">{point}</span>
|
| 238 |
+
</li>
|
| 239 |
+
))}
|
| 240 |
+
</ul>
|
| 241 |
+
</div>
|
| 242 |
+
)}
|
| 243 |
+
</div>
|
| 244 |
+
);
|
| 245 |
+
|
| 246 |
+
case 'quiz':
|
| 247 |
+
return (
|
| 248 |
+
<div className="space-y-6">
|
| 249 |
+
{transformedContent.questions.map((q: any, index: number) => (
|
| 250 |
+
<div key={index} className="p-6 bg-gray-50 rounded-xl">
|
| 251 |
+
<h4 className="font-medium text-gray-900 mb-4 text-lg">
|
| 252 |
+
{index + 1}. {q.question}
|
| 253 |
+
</h4>
|
| 254 |
+
<div className="space-y-3">
|
| 255 |
+
{q.options.map((option: string, optIndex: number) => (
|
| 256 |
+
<label
|
| 257 |
+
key={optIndex}
|
| 258 |
+
className={`flex items-start space-x-3 cursor-pointer p-3 rounded-lg border-2 transition-all ${
|
| 259 |
+
selectedAnswers[index] === optIndex
|
| 260 |
+
? selectedAnswers[index] === q.correct
|
| 261 |
+
? 'border-success-500 bg-success-50'
|
| 262 |
+
: 'border-error-500 bg-error-50'
|
| 263 |
+
: 'border-gray-200 hover:border-gray-300 bg-white'
|
| 264 |
+
}`}
|
| 265 |
+
>
|
| 266 |
+
<input
|
| 267 |
+
type="radio"
|
| 268 |
+
name={`q${index}`}
|
| 269 |
+
value={optIndex}
|
| 270 |
+
checked={selectedAnswers[index] === optIndex}
|
| 271 |
+
onChange={() => handleAnswerSelect(index, optIndex)}
|
| 272 |
+
className="mt-1 text-primary-600 focus:ring-primary-500"
|
| 273 |
+
/>
|
| 274 |
+
<span className="text-gray-700 flex-1">{option}</span>
|
| 275 |
+
{selectedAnswers[index] !== undefined && optIndex === q.correct && (
|
| 276 |
+
<span className="text-success-600 font-medium text-sm">✓ Correct</span>
|
| 277 |
+
)}
|
| 278 |
+
</label>
|
| 279 |
+
))}
|
| 280 |
+
</div>
|
| 281 |
+
{selectedAnswers[index] !== undefined && q.explanation && (
|
| 282 |
+
<div className="mt-4 p-3 bg-blue-50 rounded-lg">
|
| 283 |
+
<p className="text-sm text-blue-800">
|
| 284 |
+
<strong>Explanation:</strong> {q.explanation}
|
| 285 |
+
</p>
|
| 286 |
+
</div>
|
| 287 |
+
)}
|
| 288 |
+
</div>
|
| 289 |
+
))}
|
| 290 |
+
</div>
|
| 291 |
+
);
|
| 292 |
+
|
| 293 |
+
case 'outline':
|
| 294 |
+
return (
|
| 295 |
+
<div className="space-y-6">
|
| 296 |
+
{transformedContent.sections.map((section: any, index: number) => (
|
| 297 |
+
<div key={index} className="border-l-4 border-primary-500 pl-6">
|
| 298 |
+
<h4 className="font-semibold text-gray-900 mb-3 text-lg">{section.title}</h4>
|
| 299 |
+
<ul className="space-y-2">
|
| 300 |
+
{section.points.map((point: string, pointIndex: number) => (
|
| 301 |
+
<li key={pointIndex} className="flex items-start space-x-2">
|
| 302 |
+
<div className="w-1.5 h-1.5 bg-primary-500 rounded-full mt-2 flex-shrink-0" />
|
| 303 |
+
<span className="text-gray-700">{point}</span>
|
| 304 |
+
</li>
|
| 305 |
+
))}
|
| 306 |
+
</ul>
|
| 307 |
+
</div>
|
| 308 |
+
))}
|
| 309 |
+
</div>
|
| 310 |
+
);
|
| 311 |
+
|
| 312 |
+
case 'flashcards':
|
| 313 |
+
return (
|
| 314 |
+
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
|
| 315 |
+
{transformedContent.cards.map((card: any, index: number) => (
|
| 316 |
+
<div key={index} className="group perspective-1000">
|
| 317 |
+
<div className="relative w-full h-48 transition-transform duration-500 transform-style-preserve-3d group-hover:rotate-y-180">
|
| 318 |
+
<div className="absolute inset-0 w-full h-full backface-hidden bg-gradient-to-br from-primary-500 to-primary-600 rounded-xl p-6 flex items-center justify-center text-white">
|
| 319 |
+
<div className="text-center">
|
| 320 |
+
<h4 className="font-semibold text-lg mb-2">Question {index + 1}</h4>
|
| 321 |
+
<p className="text-primary-100">{card.front}</p>
|
| 322 |
+
</div>
|
| 323 |
+
</div>
|
| 324 |
+
|
| 325 |
+
<div className="absolute inset-0 w-full h-full backface-hidden rotate-y-180 bg-white border-2 border-primary-200 rounded-xl p-6 flex items-center justify-center">
|
| 326 |
+
<div className="text-center">
|
| 327 |
+
<p className="text-gray-700 leading-relaxed">{card.back}</p>
|
| 328 |
+
</div>
|
| 329 |
+
</div>
|
| 330 |
+
</div>
|
| 331 |
+
</div>
|
| 332 |
+
))}
|
| 333 |
+
</div>
|
| 334 |
+
);
|
| 335 |
+
|
| 336 |
+
default:
|
| 337 |
+
return null;
|
| 338 |
+
}
|
| 339 |
+
};
|
| 340 |
+
|
| 341 |
+
return (
|
| 342 |
+
<div className="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
|
| 343 |
+
<div className="mb-8">
|
| 344 |
+
<div className="flex items-center space-x-3 mb-4">
|
| 345 |
+
<div className="w-12 h-12 bg-gradient-to-r from-accent-500 to-warning-500 rounded-xl flex items-center justify-center">
|
| 346 |
+
<Wand2 className="w-6 h-6 text-white" />
|
| 347 |
+
</div>
|
| 348 |
+
<div>
|
| 349 |
+
<h1 className="text-3xl font-bold text-gray-900">Content Transformer</h1>
|
| 350 |
+
<p className="text-gray-600">Transform Wikimedia content into interactive learning materials</p>
|
| 351 |
+
</div>
|
| 352 |
+
</div>
|
| 353 |
+
</div>
|
| 354 |
+
|
| 355 |
+
<div className="bg-white rounded-2xl p-6 border border-gray-200 shadow-sm mb-8">
|
| 356 |
+
{/* AI Status Banner */}
|
| 357 |
+
<div className={`mb-6 p-4 rounded-xl border ${
|
| 358 |
+
aiStatus === 'available'
|
| 359 |
+
? 'bg-green-50 border-green-200'
|
| 360 |
+
: aiStatus === 'unavailable'
|
| 361 |
+
? 'bg-yellow-50 border-yellow-200'
|
| 362 |
+
: 'bg-gray-50 border-gray-200'
|
| 363 |
+
}`}>
|
| 364 |
+
<div className="flex items-center justify-between">
|
| 365 |
+
<div className="flex items-center space-x-3">
|
| 366 |
+
{aiStatus === 'checking' && <Loader2 className="w-5 h-5 animate-spin text-gray-600" />}
|
| 367 |
+
{aiStatus === 'available' && <Zap className="w-5 h-5 text-green-600" />}
|
| 368 |
+
{aiStatus === 'unavailable' && <Settings className="w-5 h-5 text-yellow-600" />}
|
| 369 |
+
|
| 370 |
+
<div>
|
| 371 |
+
<div className="font-medium text-gray-900">
|
| 372 |
+
{aiStatus === 'available' && 'AI Enhancement Available'}
|
| 373 |
+
{aiStatus === 'unavailable' && 'AI Enhancement Unavailable'}
|
| 374 |
+
{aiStatus === 'checking' && 'Checking AI availability...'}
|
| 375 |
+
</div>
|
| 376 |
+
<div className="text-sm text-gray-600">
|
| 377 |
+
{aiStatus === 'available' && 'Using open-source AI for intelligent content transformation'}
|
| 378 |
+
{aiStatus === 'unavailable' && 'Using rule-based transformation (still creates useful content!)'}
|
| 379 |
+
</div>
|
| 380 |
+
</div>
|
| 381 |
+
</div>
|
| 382 |
+
|
| 383 |
+
{aiStatus === 'available' && (
|
| 384 |
+
<label className="flex items-center space-x-2">
|
| 385 |
+
<input
|
| 386 |
+
type="checkbox"
|
| 387 |
+
checked={useAI}
|
| 388 |
+
onChange={(e) => setUseAI(e.target.checked)}
|
| 389 |
+
className="rounded border-gray-300 text-primary-600 focus:ring-primary-500"
|
| 390 |
+
/>
|
| 391 |
+
<span className="text-sm font-medium text-gray-700">Use AI Enhancement</span>
|
| 392 |
+
</label>
|
| 393 |
+
)}
|
| 394 |
+
</div>
|
| 395 |
+
</div>
|
| 396 |
+
|
| 397 |
+
<div className="space-y-6">
|
| 398 |
+
<div>
|
| 399 |
+
<label className="block text-sm font-medium text-gray-700 mb-2">
|
| 400 |
+
Wikimedia Article URL
|
| 401 |
+
</label>
|
| 402 |
+
<input
|
| 403 |
+
type="url"
|
| 404 |
+
value={inputUrl}
|
| 405 |
+
onChange={(e) => setInputUrl(e.target.value)}
|
| 406 |
+
placeholder="https://en.wikipedia.org/wiki/Machine_Learning"
|
| 407 |
+
className="w-full px-4 py-3 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary-500 focus:border-transparent"
|
| 408 |
+
/>
|
| 409 |
+
</div>
|
| 410 |
+
|
| 411 |
+
<div>
|
| 412 |
+
<label className="block text-sm font-medium text-gray-700 mb-3">
|
| 413 |
+
Transformation Type
|
| 414 |
+
</label>
|
| 415 |
+
<div className="grid grid-cols-2 md:grid-cols-4 gap-3">
|
| 416 |
+
{transformationTypes.map((type) => {
|
| 417 |
+
const Icon = type.icon;
|
| 418 |
+
return (
|
| 419 |
+
<button
|
| 420 |
+
key={type.id}
|
| 421 |
+
onClick={() => setTransformationType(type.id)}
|
| 422 |
+
className={`p-4 rounded-xl border-2 transition-all ${
|
| 423 |
+
transformationType === type.id
|
| 424 |
+
? 'border-primary-500 bg-primary-50'
|
| 425 |
+
: 'border-gray-200 bg-white hover:border-gray-300'
|
| 426 |
+
}`}
|
| 427 |
+
>
|
| 428 |
+
<div className="text-center">
|
| 429 |
+
<div className={`w-10 h-10 ${type.color} rounded-lg flex items-center justify-center mx-auto mb-2`}>
|
| 430 |
+
<Icon className="w-5 h-5 text-white" />
|
| 431 |
+
</div>
|
| 432 |
+
<div className="font-medium text-gray-900">{type.name}</div>
|
| 433 |
+
<div className="text-xs text-gray-500 mt-1">{type.description}</div>
|
| 434 |
+
</div>
|
| 435 |
+
</button>
|
| 436 |
+
);
|
| 437 |
+
})}
|
| 438 |
+
</div>
|
| 439 |
+
</div>
|
| 440 |
+
|
| 441 |
+
<button
|
| 442 |
+
onClick={handleTransform}
|
| 443 |
+
disabled={loading || !inputUrl.trim()}
|
| 444 |
+
className="w-full flex items-center justify-center space-x-2 px-6 py-4 bg-gradient-to-r from-accent-600 to-warning-600 text-white rounded-xl hover:from-accent-700 hover:to-warning-700 transition-all duration-200 disabled:opacity-50 disabled:cursor-not-allowed"
|
| 445 |
+
>
|
| 446 |
+
{loading ? (
|
| 447 |
+
<>
|
| 448 |
+
<Loader2 className="w-5 h-5 animate-spin" />
|
| 449 |
+
<span>
|
| 450 |
+
{useAI && aiStatus === 'available'
|
| 451 |
+
? 'AI is transforming content...'
|
| 452 |
+
: 'Transforming Content...'
|
| 453 |
+
}
|
| 454 |
+
</span>
|
| 455 |
+
</>
|
| 456 |
+
) : (
|
| 457 |
+
<>
|
| 458 |
+
<Wand2 className="w-5 h-5" />
|
| 459 |
+
<span>
|
| 460 |
+
{useAI && aiStatus === 'available'
|
| 461 |
+
? 'AI Transform Content'
|
| 462 |
+
: 'Transform Content'
|
| 463 |
+
}
|
| 464 |
+
</span>
|
| 465 |
+
</>
|
| 466 |
+
)}
|
| 467 |
+
</button>
|
| 468 |
+
</div>
|
| 469 |
+
</div>
|
| 470 |
+
|
| 471 |
+
{transformedContent && (
|
| 472 |
+
<div className="bg-white rounded-2xl p-6 border border-gray-200 shadow-sm">
|
| 473 |
+
<div className="flex items-center justify-between mb-6">
|
| 474 |
+
<h2 className="text-xl font-bold text-gray-900">{transformedContent.title}</h2>
|
| 475 |
+
{useAI && aiStatus === 'available' && (
|
| 476 |
+
<span className="px-3 py-1 bg-purple-100 text-purple-700 text-sm rounded-full font-medium">
|
| 477 |
+
AI Enhanced
|
| 478 |
+
</span>
|
| 479 |
+
)}
|
| 480 |
+
</div>
|
| 481 |
+
{renderTransformedContent()}
|
| 482 |
+
</div>
|
| 483 |
+
)}
|
| 484 |
+
</div>
|
| 485 |
+
);
|
| 486 |
+
};
|
| 487 |
+
|
| 488 |
+
export default ContentTransformer;
|
src/components/ExploreSection.tsx
ADDED
|
@@ -0,0 +1,419 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React, { useState, useEffect } from 'react';
|
| 2 |
+
import { Compass, Shuffle, BookOpen, Quote, GraduationCap, Languages, FileText, Sparkles, ExternalLink, ArrowLeft, Loader2 } from 'lucide-react';
|
| 3 |
+
import { WikimediaAPI, WIKIMEDIA_PROJECTS } from '../utils/wikimedia-api';
|
| 4 |
+
import { SearchResult } from '../types';
|
| 5 |
+
|
| 6 |
+
interface ExploreSectionProps {
|
| 7 |
+
onViewArticle?: (title: string, project: string, content: string) => void;
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
const ExploreSection: React.FC<ExploreSectionProps> = ({ onViewArticle }) => {
|
| 11 |
+
const [randomArticles, setRandomArticles] = useState<SearchResult[]>([]);
|
| 12 |
+
const [loading, setLoading] = useState(false);
|
| 13 |
+
const [selectedCategory, setSelectedCategory] = useState('featured');
|
| 14 |
+
const [selectedProject, setSelectedProject] = useState<string | null>(null);
|
| 15 |
+
const [projectContent, setProjectContent] = useState<SearchResult[]>([]);
|
| 16 |
+
const [loadingProject, setLoadingProject] = useState(false);
|
| 17 |
+
|
| 18 |
+
const categories = [
|
| 19 |
+
{ id: 'featured', name: 'Featured Content', icon: Sparkles },
|
| 20 |
+
{ id: 'trending', name: 'Trending Topics', icon: Compass },
|
| 21 |
+
{ id: 'random', name: 'Random Discovery', icon: Shuffle },
|
| 22 |
+
];
|
| 23 |
+
|
| 24 |
+
const projectCards = WIKIMEDIA_PROJECTS.map(project => ({
|
| 25 |
+
...project,
|
| 26 |
+
stats: {
|
| 27 |
+
articles: Math.floor(Math.random() * 1000000) + 100000,
|
| 28 |
+
languages: Math.floor(Math.random() * 100) + 50,
|
| 29 |
+
contributors: Math.floor(Math.random() * 50000) + 10000,
|
| 30 |
+
}
|
| 31 |
+
}));
|
| 32 |
+
|
| 33 |
+
const getIconComponent = (iconName: string) => {
|
| 34 |
+
const icons: { [key: string]: React.ComponentType<any> } = {
|
| 35 |
+
Book: BookOpen,
|
| 36 |
+
BookOpen: BookOpen,
|
| 37 |
+
Quote: Quote,
|
| 38 |
+
GraduationCap: GraduationCap,
|
| 39 |
+
Languages: Languages,
|
| 40 |
+
FileText: FileText,
|
| 41 |
+
};
|
| 42 |
+
return icons[iconName] || BookOpen;
|
| 43 |
+
};
|
| 44 |
+
|
| 45 |
+
const loadRandomContent = async () => {
|
| 46 |
+
setLoading(true);
|
| 47 |
+
try {
|
| 48 |
+
const allArticles: SearchResult[] = [];
|
| 49 |
+
|
| 50 |
+
// Get random articles from multiple projects
|
| 51 |
+
for (const project of WIKIMEDIA_PROJECTS.slice(0, 3)) {
|
| 52 |
+
const articles = await WikimediaAPI.getRandomArticles(project.id, 2);
|
| 53 |
+
allArticles.push(...articles);
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
// Shuffle the articles
|
| 57 |
+
const shuffled = allArticles.sort(() => 0.5 - Math.random());
|
| 58 |
+
setRandomArticles(shuffled.slice(0, 6));
|
| 59 |
+
} catch (error) {
|
| 60 |
+
console.error('Failed to load random content:', error);
|
| 61 |
+
} finally {
|
| 62 |
+
setLoading(false);
|
| 63 |
+
}
|
| 64 |
+
};
|
| 65 |
+
|
| 66 |
+
const handleProjectClick = async (projectId: string) => {
|
| 67 |
+
setSelectedProject(projectId);
|
| 68 |
+
setLoadingProject(true);
|
| 69 |
+
|
| 70 |
+
try {
|
| 71 |
+
// Get featured/popular content from the selected project
|
| 72 |
+
const content = await WikimediaAPI.getRandomArticles(projectId, 12);
|
| 73 |
+
setProjectContent(content);
|
| 74 |
+
} catch (error) {
|
| 75 |
+
console.error('Failed to load project content:', error);
|
| 76 |
+
setProjectContent([]);
|
| 77 |
+
} finally {
|
| 78 |
+
setLoadingProject(false);
|
| 79 |
+
}
|
| 80 |
+
};
|
| 81 |
+
|
| 82 |
+
const handleViewInWikistro = async (article: SearchResult) => {
|
| 83 |
+
try {
|
| 84 |
+
const content = await WikimediaAPI.getPageContent(article.title, article.project);
|
| 85 |
+
if (onViewArticle) {
|
| 86 |
+
onViewArticle(article.title, article.project, content);
|
| 87 |
+
}
|
| 88 |
+
} catch (error) {
|
| 89 |
+
console.error('Failed to load article content:', error);
|
| 90 |
+
}
|
| 91 |
+
};
|
| 92 |
+
|
| 93 |
+
useEffect(() => {
|
| 94 |
+
if (selectedCategory === 'random') {
|
| 95 |
+
loadRandomContent();
|
| 96 |
+
}
|
| 97 |
+
}, [selectedCategory]);
|
| 98 |
+
|
| 99 |
+
const featuredTopics = [
|
| 100 |
+
{
|
| 101 |
+
title: 'Artificial Intelligence',
|
| 102 |
+
description: 'Explore the fascinating world of AI, machine learning, and neural networks',
|
| 103 |
+
image: 'https://images.pexels.com/photos/8386440/pexels-photo-8386440.jpeg?auto=compress&cs=tinysrgb&w=400',
|
| 104 |
+
project: 'wikipedia',
|
| 105 |
+
readTime: '15 min read'
|
| 106 |
+
},
|
| 107 |
+
{
|
| 108 |
+
title: 'Climate Change',
|
| 109 |
+
description: 'Understanding global warming, its causes, effects, and potential solutions',
|
| 110 |
+
image: 'https://images.pexels.com/photos/60013/desert-drought-dehydrated-clay-soil-60013.jpeg?auto=compress&cs=tinysrgb&w=400',
|
| 111 |
+
project: 'wikipedia',
|
| 112 |
+
readTime: '12 min read'
|
| 113 |
+
},
|
| 114 |
+
{
|
| 115 |
+
title: 'Quantum Physics',
|
| 116 |
+
description: 'Dive into the mysterious world of quantum mechanics and particle physics',
|
| 117 |
+
image: 'https://images.pexels.com/photos/998641/pexels-photo-998641.jpeg?auto=compress&cs=tinysrgb&w=400',
|
| 118 |
+
project: 'wikipedia',
|
| 119 |
+
readTime: '20 min read'
|
| 120 |
+
},
|
| 121 |
+
{
|
| 122 |
+
title: 'Ancient History',
|
| 123 |
+
description: 'Journey through ancient civilizations and their remarkable achievements',
|
| 124 |
+
image: 'https://images.pexels.com/photos/161799/egypt-hieroglyphics-text-wall-161799.jpeg?auto=compress&cs=tinysrgb&w=400',
|
| 125 |
+
project: 'wikipedia',
|
| 126 |
+
readTime: '18 min read'
|
| 127 |
+
},
|
| 128 |
+
{
|
| 129 |
+
title: 'Space Exploration',
|
| 130 |
+
description: 'Discover the latest in space technology and cosmic discoveries',
|
| 131 |
+
image: 'https://images.pexels.com/photos/586056/pexels-photo-586056.jpeg?auto=compress&cs=tinysrgb&w=400',
|
| 132 |
+
project: 'wikipedia',
|
| 133 |
+
readTime: '16 min read'
|
| 134 |
+
},
|
| 135 |
+
{
|
| 136 |
+
title: 'Renewable Energy',
|
| 137 |
+
description: 'Learn about sustainable energy sources and green technologies',
|
| 138 |
+
image: 'https://images.pexels.com/photos/9800029/pexels-photo-9800029.jpeg?auto=compress&cs=tinysrgb&w=400',
|
| 139 |
+
project: 'wikipedia',
|
| 140 |
+
readTime: '14 min read'
|
| 141 |
+
}
|
| 142 |
+
];
|
| 143 |
+
|
| 144 |
+
// If a project is selected, show project content
|
| 145 |
+
if (selectedProject) {
|
| 146 |
+
const project = WIKIMEDIA_PROJECTS.find(p => p.id === selectedProject);
|
| 147 |
+
const Icon = project ? getIconComponent(project.icon) : BookOpen;
|
| 148 |
+
|
| 149 |
+
return (
|
| 150 |
+
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
|
| 151 |
+
<div className="mb-8">
|
| 152 |
+
<button
|
| 153 |
+
onClick={() => setSelectedProject(null)}
|
| 154 |
+
className="flex items-center space-x-2 text-primary-600 hover:text-primary-700 font-medium mb-4"
|
| 155 |
+
>
|
| 156 |
+
<ArrowLeft className="w-4 h-4" />
|
| 157 |
+
<span>Back to Explore</span>
|
| 158 |
+
</button>
|
| 159 |
+
|
| 160 |
+
{project && (
|
| 161 |
+
<div className="flex items-center space-x-4 mb-6">
|
| 162 |
+
<div className={`w-16 h-16 ${project.color} rounded-2xl flex items-center justify-center`}>
|
| 163 |
+
<Icon className="w-8 h-8 text-white" />
|
| 164 |
+
</div>
|
| 165 |
+
<div>
|
| 166 |
+
<h1 className="text-3xl font-bold text-gray-900">{project.name}</h1>
|
| 167 |
+
<p className="text-gray-600">{project.description}</p>
|
| 168 |
+
</div>
|
| 169 |
+
</div>
|
| 170 |
+
)}
|
| 171 |
+
</div>
|
| 172 |
+
|
| 173 |
+
{loadingProject ? (
|
| 174 |
+
<div className="flex items-center justify-center py-12">
|
| 175 |
+
<Loader2 className="w-8 h-8 animate-spin text-primary-600" />
|
| 176 |
+
<span className="ml-3 text-gray-600">Loading {project?.name} content...</span>
|
| 177 |
+
</div>
|
| 178 |
+
) : (
|
| 179 |
+
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
|
| 180 |
+
{projectContent.map((article, index) => (
|
| 181 |
+
<div
|
| 182 |
+
key={index}
|
| 183 |
+
className="bg-white border border-gray-200 rounded-2xl p-6 hover:shadow-md transition-all duration-200 hover:border-primary-200"
|
| 184 |
+
>
|
| 185 |
+
<div className="flex items-center space-x-2 mb-3">
|
| 186 |
+
<span className="text-xs font-medium text-secondary-600 bg-secondary-50 px-2 py-1 rounded-full">
|
| 187 |
+
{article.project}
|
| 188 |
+
</span>
|
| 189 |
+
</div>
|
| 190 |
+
<h3 className="text-lg font-semibold text-gray-900 mb-2">
|
| 191 |
+
{article.title}
|
| 192 |
+
</h3>
|
| 193 |
+
<p className="text-gray-600 text-sm mb-4">{article.snippet}</p>
|
| 194 |
+
|
| 195 |
+
<div className="flex space-x-2">
|
| 196 |
+
<button
|
| 197 |
+
onClick={() => handleViewInWikistro(article)}
|
| 198 |
+
className="flex-1 px-3 py-2 bg-primary-600 text-white rounded-lg hover:bg-primary-700 transition-colors text-sm"
|
| 199 |
+
>
|
| 200 |
+
View in Wikistro
|
| 201 |
+
</button>
|
| 202 |
+
<a
|
| 203 |
+
href={article.url}
|
| 204 |
+
target="_blank"
|
| 205 |
+
rel="noopener noreferrer"
|
| 206 |
+
className="px-3 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 transition-colors"
|
| 207 |
+
>
|
| 208 |
+
<ExternalLink className="w-4 h-4" />
|
| 209 |
+
</a>
|
| 210 |
+
</div>
|
| 211 |
+
</div>
|
| 212 |
+
))}
|
| 213 |
+
</div>
|
| 214 |
+
)}
|
| 215 |
+
</div>
|
| 216 |
+
);
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
+
return (
|
| 220 |
+
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
|
| 221 |
+
<div className="mb-8">
|
| 222 |
+
<h1 className="text-3xl font-bold text-gray-900 mb-2">Explore Knowledge</h1>
|
| 223 |
+
<p className="text-gray-600">Discover amazing content across Wikimedia projects</p>
|
| 224 |
+
</div>
|
| 225 |
+
|
| 226 |
+
{/* Category Tabs */}
|
| 227 |
+
<div className="mb-8">
|
| 228 |
+
<div className="flex space-x-1 bg-gray-100 p-1 rounded-xl">
|
| 229 |
+
{categories.map((category) => {
|
| 230 |
+
const Icon = category.icon;
|
| 231 |
+
return (
|
| 232 |
+
<button
|
| 233 |
+
key={category.id}
|
| 234 |
+
onClick={() => setSelectedCategory(category.id)}
|
| 235 |
+
className={`flex items-center space-x-2 px-4 py-3 rounded-lg text-sm font-medium transition-all ${
|
| 236 |
+
selectedCategory === category.id
|
| 237 |
+
? 'bg-white text-primary-700 shadow-sm'
|
| 238 |
+
: 'text-gray-600 hover:text-gray-900'
|
| 239 |
+
}`}
|
| 240 |
+
>
|
| 241 |
+
<Icon className="w-4 h-4" />
|
| 242 |
+
<span>{category.name}</span>
|
| 243 |
+
</button>
|
| 244 |
+
);
|
| 245 |
+
})}
|
| 246 |
+
</div>
|
| 247 |
+
</div>
|
| 248 |
+
|
| 249 |
+
{/* Wikimedia Projects Overview */}
|
| 250 |
+
<div className="mb-12">
|
| 251 |
+
<h2 className="text-2xl font-semibold text-gray-900 mb-6">Wikimedia Projects</h2>
|
| 252 |
+
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
|
| 253 |
+
{projectCards.map((project) => {
|
| 254 |
+
const Icon = getIconComponent(project.icon);
|
| 255 |
+
return (
|
| 256 |
+
<div
|
| 257 |
+
key={project.id}
|
| 258 |
+
onClick={() => handleProjectClick(project.id)}
|
| 259 |
+
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"
|
| 260 |
+
>
|
| 261 |
+
<div className="flex items-center space-x-3 mb-4">
|
| 262 |
+
<div className={`w-12 h-12 ${project.color} rounded-xl flex items-center justify-center group-hover:scale-110 transition-transform`}>
|
| 263 |
+
<Icon className="w-6 h-6 text-white" />
|
| 264 |
+
</div>
|
| 265 |
+
<div className="flex-1">
|
| 266 |
+
<h3 className="font-semibold text-gray-900 group-hover:text-primary-600 transition-colors">{project.name}</h3>
|
| 267 |
+
<p className="text-sm text-gray-600">{project.description}</p>
|
| 268 |
+
</div>
|
| 269 |
+
<ExternalLink className="w-5 h-5 text-gray-400 group-hover:text-primary-600 transition-colors" />
|
| 270 |
+
</div>
|
| 271 |
+
|
| 272 |
+
<div className="grid grid-cols-3 gap-4 text-center">
|
| 273 |
+
<div>
|
| 274 |
+
<div className="text-lg font-semibold text-gray-900">
|
| 275 |
+
{(project.stats.articles / 1000).toFixed(0)}K
|
| 276 |
+
</div>
|
| 277 |
+
<div className="text-xs text-gray-600">Articles</div>
|
| 278 |
+
</div>
|
| 279 |
+
<div>
|
| 280 |
+
<div className="text-lg font-semibold text-gray-900">
|
| 281 |
+
{project.stats.languages}
|
| 282 |
+
</div>
|
| 283 |
+
<div className="text-xs text-gray-600">Languages</div>
|
| 284 |
+
</div>
|
| 285 |
+
<div>
|
| 286 |
+
<div className="text-lg font-semibold text-gray-900">
|
| 287 |
+
{(project.stats.contributors / 1000).toFixed(0)}K
|
| 288 |
+
</div>
|
| 289 |
+
<div className="text-xs text-gray-600">Contributors</div>
|
| 290 |
+
</div>
|
| 291 |
+
</div>
|
| 292 |
+
</div>
|
| 293 |
+
);
|
| 294 |
+
})}
|
| 295 |
+
</div>
|
| 296 |
+
</div>
|
| 297 |
+
|
| 298 |
+
{/* Content based on selected category */}
|
| 299 |
+
{selectedCategory === 'featured' && (
|
| 300 |
+
<div>
|
| 301 |
+
<h2 className="text-2xl font-semibold text-gray-900 mb-6">Featured Topics</h2>
|
| 302 |
+
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
|
| 303 |
+
{featuredTopics.map((topic, index) => (
|
| 304 |
+
<div
|
| 305 |
+
key={index}
|
| 306 |
+
className="bg-white rounded-2xl overflow-hidden shadow-sm hover:shadow-lg transition-all duration-300 cursor-pointer group"
|
| 307 |
+
onClick={() => handleViewInWikistro({
|
| 308 |
+
title: topic.title,
|
| 309 |
+
pageid: index,
|
| 310 |
+
size: 0,
|
| 311 |
+
snippet: topic.description,
|
| 312 |
+
timestamp: new Date().toISOString(),
|
| 313 |
+
project: topic.project,
|
| 314 |
+
url: `https://en.wikipedia.org/wiki/${encodeURIComponent(topic.title)}`
|
| 315 |
+
})}
|
| 316 |
+
>
|
| 317 |
+
<div className="aspect-w-16 aspect-h-9 overflow-hidden">
|
| 318 |
+
<img
|
| 319 |
+
src={topic.image}
|
| 320 |
+
alt={topic.title}
|
| 321 |
+
className="w-full h-48 object-cover group-hover:scale-105 transition-transform duration-300"
|
| 322 |
+
/>
|
| 323 |
+
</div>
|
| 324 |
+
<div className="p-6">
|
| 325 |
+
<div className="flex items-center justify-between mb-3">
|
| 326 |
+
<span className="text-xs font-medium text-primary-600 bg-primary-50 px-2 py-1 rounded-full">
|
| 327 |
+
{topic.project}
|
| 328 |
+
</span>
|
| 329 |
+
<span className="text-xs text-gray-500">{topic.readTime}</span>
|
| 330 |
+
</div>
|
| 331 |
+
<h3 className="text-lg font-semibold text-gray-900 mb-2 group-hover:text-primary-600 transition-colors">
|
| 332 |
+
{topic.title}
|
| 333 |
+
</h3>
|
| 334 |
+
<p className="text-gray-600 text-sm leading-relaxed">{topic.description}</p>
|
| 335 |
+
</div>
|
| 336 |
+
</div>
|
| 337 |
+
))}
|
| 338 |
+
</div>
|
| 339 |
+
</div>
|
| 340 |
+
)}
|
| 341 |
+
|
| 342 |
+
{selectedCategory === 'random' && (
|
| 343 |
+
<div>
|
| 344 |
+
<div className="flex items-center justify-between mb-6">
|
| 345 |
+
<h2 className="text-2xl font-semibold text-gray-900">Random Discovery</h2>
|
| 346 |
+
<button
|
| 347 |
+
onClick={loadRandomContent}
|
| 348 |
+
disabled={loading}
|
| 349 |
+
className="flex items-center space-x-2 px-4 py-2 bg-primary-600 text-white rounded-lg hover:bg-primary-700 transition-colors disabled:opacity-50"
|
| 350 |
+
>
|
| 351 |
+
<Shuffle className="w-4 h-4" />
|
| 352 |
+
<span>{loading ? 'Loading...' : 'Shuffle'}</span>
|
| 353 |
+
</button>
|
| 354 |
+
</div>
|
| 355 |
+
|
| 356 |
+
{loading && (
|
| 357 |
+
<div className="flex items-center justify-center py-12">
|
| 358 |
+
<Loader2 className="w-8 h-8 animate-spin text-primary-600" />
|
| 359 |
+
<span className="ml-3 text-gray-600">Finding random articles...</span>
|
| 360 |
+
</div>
|
| 361 |
+
)}
|
| 362 |
+
|
| 363 |
+
{!loading && randomArticles.length > 0 && (
|
| 364 |
+
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
|
| 365 |
+
{randomArticles.map((article, index) => (
|
| 366 |
+
<div
|
| 367 |
+
key={index}
|
| 368 |
+
className="bg-white border border-gray-200 rounded-2xl p-6 hover:shadow-md transition-all duration-200 hover:border-primary-200"
|
| 369 |
+
>
|
| 370 |
+
<div className="flex items-center space-x-2 mb-3">
|
| 371 |
+
<span className="text-xs font-medium text-secondary-600 bg-secondary-50 px-2 py-1 rounded-full">
|
| 372 |
+
{article.project}
|
| 373 |
+
</span>
|
| 374 |
+
</div>
|
| 375 |
+
<h3 className="text-lg font-semibold text-gray-900 mb-2">
|
| 376 |
+
{article.title}
|
| 377 |
+
</h3>
|
| 378 |
+
<p className="text-gray-600 text-sm mb-4">{article.snippet}</p>
|
| 379 |
+
|
| 380 |
+
<div className="flex space-x-2">
|
| 381 |
+
<button
|
| 382 |
+
onClick={() => handleViewInWikistro(article)}
|
| 383 |
+
className="flex-1 px-3 py-2 bg-primary-600 text-white rounded-lg hover:bg-primary-700 transition-colors text-sm"
|
| 384 |
+
>
|
| 385 |
+
View in Wikistro
|
| 386 |
+
</button>
|
| 387 |
+
<a
|
| 388 |
+
href={article.url}
|
| 389 |
+
target="_blank"
|
| 390 |
+
rel="noopener noreferrer"
|
| 391 |
+
className="px-3 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 transition-colors"
|
| 392 |
+
>
|
| 393 |
+
<ExternalLink className="w-4 h-4" />
|
| 394 |
+
</a>
|
| 395 |
+
</div>
|
| 396 |
+
</div>
|
| 397 |
+
))}
|
| 398 |
+
</div>
|
| 399 |
+
)}
|
| 400 |
+
</div>
|
| 401 |
+
)}
|
| 402 |
+
|
| 403 |
+
{selectedCategory === 'trending' && (
|
| 404 |
+
<div>
|
| 405 |
+
<h2 className="text-2xl font-semibold text-gray-900 mb-6">Trending Topics</h2>
|
| 406 |
+
<div className="bg-white rounded-2xl p-8 text-center border border-gray-200">
|
| 407 |
+
<Compass className="w-16 h-16 text-gray-300 mx-auto mb-4" />
|
| 408 |
+
<h3 className="text-lg font-medium text-gray-900 mb-2">Coming Soon</h3>
|
| 409 |
+
<p className="text-gray-600">
|
| 410 |
+
We're working on bringing you the most trending topics across Wikimedia projects.
|
| 411 |
+
</p>
|
| 412 |
+
</div>
|
| 413 |
+
</div>
|
| 414 |
+
)}
|
| 415 |
+
</div>
|
| 416 |
+
);
|
| 417 |
+
};
|
| 418 |
+
|
| 419 |
+
export default ExploreSection;
|
src/components/Header.tsx
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React, { useState } from 'react';
|
| 2 |
+
import { Search, BookOpen, Compass, Sparkles, Wand2, Globe, Settings } from 'lucide-react';
|
| 3 |
+
import AIConfigModal from './AIConfigModal';
|
| 4 |
+
|
| 5 |
+
interface HeaderProps {
|
| 6 |
+
activeTab: string;
|
| 7 |
+
onTabChange: (tab: string) => void;
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
const Header: React.FC<HeaderProps> = ({ activeTab, onTabChange }) => {
|
| 11 |
+
const [showAIConfig, setShowAIConfig] = useState(false);
|
| 12 |
+
|
| 13 |
+
const tabs = [
|
| 14 |
+
{ id: 'search', label: 'Search', icon: Search },
|
| 15 |
+
{ id: 'explore', label: 'Explore', icon: Compass },
|
| 16 |
+
{ id: 'study', label: 'Study Plans', icon: BookOpen },
|
| 17 |
+
{ id: 'ai-generator', label: 'AI Generator', icon: Sparkles },
|
| 18 |
+
{ id: 'transformer', label: 'Transform', icon: Wand2 },
|
| 19 |
+
{ id: 'multilingual', label: 'Multilingual', icon: Globe },
|
| 20 |
+
];
|
| 21 |
+
|
| 22 |
+
return (
|
| 23 |
+
<>
|
| 24 |
+
<header className="bg-white shadow-sm border-b border-gray-200 sticky top-0 z-50">
|
| 25 |
+
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
|
| 26 |
+
<div className="flex justify-between items-center h-16">
|
| 27 |
+
<div className="flex items-center space-x-4">
|
| 28 |
+
<div className="flex items-center space-x-2">
|
| 29 |
+
<div className="w-8 h-8 bg-gradient-to-r from-primary-500 to-secondary-500 rounded-lg flex items-center justify-center">
|
| 30 |
+
<BookOpen className="w-5 h-5 text-white" />
|
| 31 |
+
</div>
|
| 32 |
+
<h1 className="text-xl font-bold text-gray-900">Wikistro</h1>
|
| 33 |
+
</div>
|
| 34 |
+
<div className="hidden sm:block">
|
| 35 |
+
<p className="text-sm text-gray-600">Transform Knowledge into Learning</p>
|
| 36 |
+
</div>
|
| 37 |
+
</div>
|
| 38 |
+
|
| 39 |
+
<nav className="flex items-center space-x-1">
|
| 40 |
+
<div className="flex space-x-1 overflow-x-auto">
|
| 41 |
+
{tabs.map((tab) => {
|
| 42 |
+
const Icon = tab.icon;
|
| 43 |
+
return (
|
| 44 |
+
<button
|
| 45 |
+
key={tab.id}
|
| 46 |
+
onClick={() => onTabChange(tab.id)}
|
| 47 |
+
className={`flex items-center space-x-2 px-3 py-2 rounded-lg text-sm font-medium transition-all duration-200 whitespace-nowrap ${
|
| 48 |
+
activeTab === tab.id
|
| 49 |
+
? 'bg-primary-50 text-primary-700 shadow-sm'
|
| 50 |
+
: 'text-gray-600 hover:text-gray-900 hover:bg-gray-50'
|
| 51 |
+
}`}
|
| 52 |
+
>
|
| 53 |
+
<Icon className="w-4 h-4" />
|
| 54 |
+
<span className="hidden sm:inline">{tab.label}</span>
|
| 55 |
+
</button>
|
| 56 |
+
);
|
| 57 |
+
})}
|
| 58 |
+
</div>
|
| 59 |
+
|
| 60 |
+
<div className="ml-2 pl-2 border-l border-gray-200">
|
| 61 |
+
<button
|
| 62 |
+
onClick={() => setShowAIConfig(true)}
|
| 63 |
+
className="flex items-center space-x-2 px-3 py-2 text-gray-600 hover:text-gray-900 hover:bg-gray-50 rounded-lg transition-all duration-200"
|
| 64 |
+
title="AI Configuration"
|
| 65 |
+
>
|
| 66 |
+
<Settings className="w-4 h-4" />
|
| 67 |
+
<span className="hidden sm:inline text-sm font-medium">AI Config</span>
|
| 68 |
+
</button>
|
| 69 |
+
</div>
|
| 70 |
+
</nav>
|
| 71 |
+
</div>
|
| 72 |
+
</div>
|
| 73 |
+
</header>
|
| 74 |
+
|
| 75 |
+
<AIConfigModal
|
| 76 |
+
isOpen={showAIConfig}
|
| 77 |
+
onClose={() => setShowAIConfig(false)}
|
| 78 |
+
/>
|
| 79 |
+
</>
|
| 80 |
+
);
|
| 81 |
+
};
|
| 82 |
+
|
| 83 |
+
export default Header;
|
src/components/Hero.tsx
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React from 'react';
|
| 2 |
+
import { ArrowRight, BookOpen, Users, Globe, Sparkles } from 'lucide-react';
|
| 3 |
+
|
| 4 |
+
interface HeroProps {
|
| 5 |
+
onGetStarted: () => void;
|
| 6 |
+
}
|
| 7 |
+
|
| 8 |
+
const Hero: React.FC<HeroProps> = ({ onGetStarted }) => {
|
| 9 |
+
const features = [
|
| 10 |
+
{
|
| 11 |
+
icon: BookOpen,
|
| 12 |
+
title: 'Interactive Learning',
|
| 13 |
+
description: 'Transform static content into engaging study experiences'
|
| 14 |
+
},
|
| 15 |
+
{
|
| 16 |
+
icon: Globe,
|
| 17 |
+
title: 'Multi-language Support',
|
| 18 |
+
description: 'Access knowledge in hundreds of languages'
|
| 19 |
+
},
|
| 20 |
+
{
|
| 21 |
+
icon: Users,
|
| 22 |
+
title: 'Community Driven',
|
| 23 |
+
description: 'Built on the foundation of collaborative knowledge'
|
| 24 |
+
},
|
| 25 |
+
{
|
| 26 |
+
icon: Sparkles,
|
| 27 |
+
title: 'Smart Recommendations',
|
| 28 |
+
description: 'Discover related topics and build comprehensive understanding'
|
| 29 |
+
}
|
| 30 |
+
];
|
| 31 |
+
|
| 32 |
+
return (
|
| 33 |
+
<div className="relative bg-gradient-to-br from-primary-50 via-white to-secondary-50 py-20">
|
| 34 |
+
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
|
| 35 |
+
<div className="text-center">
|
| 36 |
+
<h1 className="text-4xl md:text-6xl font-bold text-gray-900 mb-6 animate-fade-in">
|
| 37 |
+
Transform{' '}
|
| 38 |
+
<span className="bg-gradient-to-r from-primary-600 to-secondary-600 bg-clip-text text-transparent">
|
| 39 |
+
Knowledge
|
| 40 |
+
</span>{' '}
|
| 41 |
+
into Learning
|
| 42 |
+
</h1>
|
| 43 |
+
|
| 44 |
+
<p className="text-xl text-gray-600 mb-8 max-w-3xl mx-auto leading-relaxed animate-slide-up">
|
| 45 |
+
Leverage the power of Wikimedia APIs to create interactive learning experiences from Wikipedia,
|
| 46 |
+
Wikibooks, and other open knowledge sources. Build study plans, explore topics, and enhance
|
| 47 |
+
your understanding with AI-powered tools.
|
| 48 |
+
</p>
|
| 49 |
+
|
| 50 |
+
<div className="flex flex-col sm:flex-row gap-4 justify-center mb-16">
|
| 51 |
+
<button
|
| 52 |
+
onClick={onGetStarted}
|
| 53 |
+
className="inline-flex items-center px-8 py-4 border border-transparent text-lg font-medium rounded-xl text-white bg-gradient-to-r from-primary-600 to-secondary-600 hover:from-primary-700 hover:to-secondary-700 transition-all duration-200 shadow-lg hover:shadow-xl transform hover:-translate-y-1"
|
| 54 |
+
>
|
| 55 |
+
Get Started
|
| 56 |
+
<ArrowRight className="ml-2 w-5 h-5" />
|
| 57 |
+
</button>
|
| 58 |
+
|
| 59 |
+
<button className="inline-flex items-center px-8 py-4 border-2 border-primary-200 text-lg font-medium rounded-xl text-primary-700 bg-white hover:bg-primary-50 transition-all duration-200 shadow-sm hover:shadow-md">
|
| 60 |
+
View Documentation
|
| 61 |
+
</button>
|
| 62 |
+
</div>
|
| 63 |
+
|
| 64 |
+
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-8">
|
| 65 |
+
{features.map((feature, index) => {
|
| 66 |
+
const Icon = feature.icon;
|
| 67 |
+
return (
|
| 68 |
+
<div
|
| 69 |
+
key={index}
|
| 70 |
+
className="bg-white p-6 rounded-2xl shadow-sm hover:shadow-md transition-all duration-200 transform hover:-translate-y-1 animate-fade-in"
|
| 71 |
+
style={{ animationDelay: `${index * 0.1}s` }}
|
| 72 |
+
>
|
| 73 |
+
<div className="w-12 h-12 bg-gradient-to-r from-primary-500 to-secondary-500 rounded-xl flex items-center justify-center mb-4">
|
| 74 |
+
<Icon className="w-6 h-6 text-white" />
|
| 75 |
+
</div>
|
| 76 |
+
<h3 className="text-lg font-semibold text-gray-900 mb-2">{feature.title}</h3>
|
| 77 |
+
<p className="text-gray-600 text-sm leading-relaxed">{feature.description}</p>
|
| 78 |
+
</div>
|
| 79 |
+
);
|
| 80 |
+
})}
|
| 81 |
+
</div>
|
| 82 |
+
</div>
|
| 83 |
+
</div>
|
| 84 |
+
</div>
|
| 85 |
+
);
|
| 86 |
+
};
|
| 87 |
+
|
| 88 |
+
export default Hero;
|
src/components/MultilingualExplorer.tsx
ADD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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React, { useState } from 'react';
|
| 2 |
+
import { Globe, Languages, Search, ExternalLink, Loader2, Eye } from 'lucide-react';
|
| 3 |
+
import { WikimediaAPI } from '../utils/wikimedia-api';
|
| 4 |
+
|
| 5 |
+
interface MultilingualExplorerProps {
|
| 6 |
+
onViewArticle?: (title: string, project: string, content: string) => void;
|
| 7 |
+
}
|
| 8 |
+
|
| 9 |
+
const MultilingualExplorer: React.FC<MultilingualExplorerProps> = ({ onViewArticle }) => {
|
| 10 |
+
const [query, setQuery] = useState('');
|
| 11 |
+
const [selectedLanguages, setSelectedLanguages] = useState<string[]>(['en', 'es', 'fr']);
|
| 12 |
+
const [loading, setLoading] = useState(false);
|
| 13 |
+
const [results, setResults] = useState<any[]>([]);
|
| 14 |
+
const [loadingContent, setLoadingContent] = useState<string | null>(null);
|
| 15 |
+
|
| 16 |
+
const languages = [
|
| 17 |
+
{ code: 'en', name: 'English', flag: '🇺🇸' },
|
| 18 |
+
{ code: 'es', name: 'Spanish', flag: '🇪🇸' },
|
| 19 |
+
{ code: 'fr', name: 'French', flag: '🇫🇷' },
|
| 20 |
+
{ code: 'de', name: 'German', flag: '🇩🇪' },
|
| 21 |
+
{ code: 'it', name: 'Italian', flag: '🇮🇹' },
|
| 22 |
+
{ code: 'pt', name: 'Portuguese', flag: '🇵🇹' },
|
| 23 |
+
{ code: 'ru', name: 'Russian', flag: '🇷🇺' },
|
| 24 |
+
{ code: 'ja', name: 'Japanese', flag: '🇯🇵' },
|
| 25 |
+
{ code: 'zh', name: 'Chinese', flag: '🇨🇳' },
|
| 26 |
+
{ code: 'ar', name: 'Arabic', flag: '🇸🇦' },
|
| 27 |
+
{ code: 'hi', name: 'Hindi', flag: '🇮🇳' },
|
| 28 |
+
{ code: 'ko', name: 'Korean', flag: '🇰🇷' }
|
| 29 |
+
];
|
| 30 |
+
|
| 31 |
+
const toggleLanguage = (langCode: string) => {
|
| 32 |
+
setSelectedLanguages(prev =>
|
| 33 |
+
prev.includes(langCode)
|
| 34 |
+
? prev.filter(l => l !== langCode)
|
| 35 |
+
: [...prev, langCode]
|
| 36 |
+
);
|
| 37 |
+
};
|
| 38 |
+
|
| 39 |
+
const handleSearch = async () => {
|
| 40 |
+
if (!query.trim() || selectedLanguages.length === 0) return;
|
| 41 |
+
|
| 42 |
+
setLoading(true);
|
| 43 |
+
try {
|
| 44 |
+
const searchResults = [];
|
| 45 |
+
|
| 46 |
+
for (const langCode of selectedLanguages) {
|
| 47 |
+
try {
|
| 48 |
+
const language = languages.find(l => l.code === langCode);
|
| 49 |
+
const apiUrl = `https://${langCode}.wikipedia.org/w/api.php`;
|
| 50 |
+
|
| 51 |
+
const params = {
|
| 52 |
+
action: 'query',
|
| 53 |
+
format: 'json',
|
| 54 |
+
list: 'search',
|
| 55 |
+
srsearch: query,
|
| 56 |
+
srlimit: '3',
|
| 57 |
+
srprop: 'snippet|size|timestamp',
|
| 58 |
+
origin: '*'
|
| 59 |
+
};
|
| 60 |
+
|
| 61 |
+
const url = new URL(apiUrl);
|
| 62 |
+
Object.entries(params).forEach(([key, value]) => {
|
| 63 |
+
url.searchParams.append(key, value);
|
| 64 |
+
});
|
| 65 |
+
|
| 66 |
+
const response = await fetch(url.toString());
|
| 67 |
+
const data = await response.json();
|
| 68 |
+
|
| 69 |
+
if (data.query?.search?.length > 0) {
|
| 70 |
+
const firstResult = data.query.search[0];
|
| 71 |
+
searchResults.push({
|
| 72 |
+
language: language?.name || langCode,
|
| 73 |
+
flag: language?.flag || '🌐',
|
| 74 |
+
code: langCode,
|
| 75 |
+
title: firstResult.title,
|
| 76 |
+
snippet: firstResult.snippet?.replace(/<[^>]*>/g, '') || `Article about ${query} in ${language?.name}`,
|
| 77 |
+
url: `https://${langCode}.wikipedia.org/wiki/${encodeURIComponent(firstResult.title)}`,
|
| 78 |
+
wordCount: Math.floor(firstResult.size / 5) || Math.floor(Math.random() * 3000) + 1000,
|
| 79 |
+
lastModified: new Date(firstResult.timestamp || Date.now()).toLocaleDateString(),
|
| 80 |
+
pageid: firstResult.pageid
|
| 81 |
+
});
|
| 82 |
+
}
|
| 83 |
+
} catch (error) {
|
| 84 |
+
console.error(`Failed to search in ${langCode}:`, error);
|
| 85 |
+
// Add fallback result
|
| 86 |
+
const language = languages.find(l => l.code === langCode);
|
| 87 |
+
searchResults.push({
|
| 88 |
+
language: language?.name || langCode,
|
| 89 |
+
flag: language?.flag || '🌐',
|
| 90 |
+
code: langCode,
|
| 91 |
+
title: `${query} (${language?.name})`,
|
| 92 |
+
snippet: `Information about ${query} in ${language?.name}. This content would be available in the ${language?.name} Wikipedia.`,
|
| 93 |
+
url: `https://${langCode}.wikipedia.org/wiki/${encodeURIComponent(query)}`,
|
| 94 |
+
wordCount: Math.floor(Math.random() * 3000) + 1000,
|
| 95 |
+
lastModified: new Date().toLocaleDateString(),
|
| 96 |
+
pageid: Math.floor(Math.random() * 1000000)
|
| 97 |
+
});
|
| 98 |
+
}
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
setResults(searchResults);
|
| 102 |
+
} catch (error) {
|
| 103 |
+
console.error('Multilingual search failed:', error);
|
| 104 |
+
} finally {
|
| 105 |
+
setLoading(false);
|
| 106 |
+
}
|
| 107 |
+
};
|
| 108 |
+
|
| 109 |
+
const handleViewInWikistro = async (result: any) => {
|
| 110 |
+
setLoadingContent(result.code);
|
| 111 |
+
try {
|
| 112 |
+
// Try to get content from the specific language Wikipedia
|
| 113 |
+
const apiUrl = `https://${result.code}.wikipedia.org/w/api.php`;
|
| 114 |
+
const params = {
|
| 115 |
+
action: 'query',
|
| 116 |
+
format: 'json',
|
| 117 |
+
titles: result.title,
|
| 118 |
+
prop: 'extracts',
|
| 119 |
+
exintro: 'false',
|
| 120 |
+
explaintext: 'true',
|
| 121 |
+
exsectionformat: 'plain',
|
| 122 |
+
origin: '*'
|
| 123 |
+
};
|
| 124 |
+
|
| 125 |
+
const url = new URL(apiUrl);
|
| 126 |
+
Object.entries(params).forEach(([key, value]) => {
|
| 127 |
+
url.searchParams.append(key, value);
|
| 128 |
+
});
|
| 129 |
+
|
| 130 |
+
const response = await fetch(url.toString());
|
| 131 |
+
const data = await response.json();
|
| 132 |
+
const pages = data.query?.pages || {};
|
| 133 |
+
const pageId = Object.keys(pages)[0];
|
| 134 |
+
|
| 135 |
+
let content = 'Content could not be loaded from this language version.';
|
| 136 |
+
if (pageId !== '-1' && pages[pageId]?.extract) {
|
| 137 |
+
content = pages[pageId].extract;
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
if (onViewArticle) {
|
| 141 |
+
onViewArticle(result.title, `${result.code}-wikipedia`, content);
|
| 142 |
+
}
|
| 143 |
+
} catch (error) {
|
| 144 |
+
console.error('Failed to load article content:', error);
|
| 145 |
+
if (onViewArticle) {
|
| 146 |
+
onViewArticle(result.title, `${result.code}-wikipedia`, 'Failed to load content. Please try viewing the original article.');
|
| 147 |
+
}
|
| 148 |
+
} finally {
|
| 149 |
+
setLoadingContent(null);
|
| 150 |
+
}
|
| 151 |
+
};
|
| 152 |
+
|
| 153 |
+
return (
|
| 154 |
+
<div className="max-w-6xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
|
| 155 |
+
<div className="mb-8">
|
| 156 |
+
<div className="flex items-center space-x-3 mb-4">
|
| 157 |
+
<div className="w-12 h-12 bg-gradient-to-r from-secondary-500 to-success-500 rounded-xl flex items-center justify-center">
|
| 158 |
+
<Globe className="w-6 h-6 text-white" />
|
| 159 |
+
</div>
|
| 160 |
+
<div>
|
| 161 |
+
<h1 className="text-3xl font-bold text-gray-900">Multilingual Explorer</h1>
|
| 162 |
+
<p className="text-gray-600">Discover knowledge across languages and cultures</p>
|
| 163 |
+
</div>
|
| 164 |
+
</div>
|
| 165 |
+
</div>
|
| 166 |
+
|
| 167 |
+
<div className="bg-white rounded-2xl p-6 border border-gray-200 shadow-sm mb-8">
|
| 168 |
+
<div className="space-y-6">
|
| 169 |
+
<div>
|
| 170 |
+
<label className="block text-sm font-medium text-gray-700 mb-2">
|
| 171 |
+
Search Topic
|
| 172 |
+
</label>
|
| 173 |
+
<div className="flex gap-3">
|
| 174 |
+
<input
|
| 175 |
+
type="text"
|
| 176 |
+
value={query}
|
| 177 |
+
onChange={(e) => setQuery(e.target.value)}
|
| 178 |
+
placeholder="e.g., Artificial Intelligence, Climate Change..."
|
| 179 |
+
className="flex-1 px-4 py-3 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary-500 focus:border-transparent"
|
| 180 |
+
onKeyDown={(e) => e.key === 'Enter' && handleSearch()}
|
| 181 |
+
/>
|
| 182 |
+
<button
|
| 183 |
+
onClick={handleSearch}
|
| 184 |
+
disabled={loading || !query.trim() || selectedLanguages.length === 0}
|
| 185 |
+
className="flex items-center space-x-2 px-6 py-3 bg-primary-600 text-white rounded-xl hover:bg-primary-700 transition-colors disabled:opacity-50"
|
| 186 |
+
>
|
| 187 |
+
{loading ? (
|
| 188 |
+
<Loader2 className="w-5 h-5 animate-spin" />
|
| 189 |
+
) : (
|
| 190 |
+
<Search className="w-5 h-5" />
|
| 191 |
+
)}
|
| 192 |
+
</button>
|
| 193 |
+
</div>
|
| 194 |
+
</div>
|
| 195 |
+
|
| 196 |
+
<div>
|
| 197 |
+
<label className="block text-sm font-medium text-gray-700 mb-3">
|
| 198 |
+
Select Languages ({selectedLanguages.length} selected)
|
| 199 |
+
</label>
|
| 200 |
+
<div className="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-3">
|
| 201 |
+
{languages.map((lang) => (
|
| 202 |
+
<button
|
| 203 |
+
key={lang.code}
|
| 204 |
+
onClick={() => toggleLanguage(lang.code)}
|
| 205 |
+
className={`flex items-center space-x-2 p-3 rounded-lg border-2 transition-all ${
|
| 206 |
+
selectedLanguages.includes(lang.code)
|
| 207 |
+
? 'border-primary-500 bg-primary-50 text-primary-700'
|
| 208 |
+
: 'border-gray-200 bg-white text-gray-700 hover:border-gray-300'
|
| 209 |
+
}`}
|
| 210 |
+
>
|
| 211 |
+
<span className="text-lg">{lang.flag}</span>
|
| 212 |
+
<span className="font-medium">{lang.name}</span>
|
| 213 |
+
</button>
|
| 214 |
+
))}
|
| 215 |
+
</div>
|
| 216 |
+
</div>
|
| 217 |
+
</div>
|
| 218 |
+
</div>
|
| 219 |
+
|
| 220 |
+
{results.length > 0 && (
|
| 221 |
+
<div className="space-y-4">
|
| 222 |
+
<h2 className="text-xl font-semibold text-gray-900">
|
| 223 |
+
Results for "{query}" in {selectedLanguages.length} languages
|
| 224 |
+
</h2>
|
| 225 |
+
|
| 226 |
+
<div className="grid gap-6">
|
| 227 |
+
{results.map((result, index) => (
|
| 228 |
+
<div key={index} className="bg-white rounded-2xl p-6 border border-gray-200 shadow-sm hover:shadow-md transition-shadow">
|
| 229 |
+
<div className="flex items-start justify-between">
|
| 230 |
+
<div className="flex-1">
|
| 231 |
+
<div className="flex items-center space-x-3 mb-3">
|
| 232 |
+
<span className="text-2xl">{result.flag}</span>
|
| 233 |
+
<div>
|
| 234 |
+
<h3 className="text-lg font-semibold text-gray-900">{result.title}</h3>
|
| 235 |
+
<p className="text-sm text-gray-600">{result.language} Wikipedia</p>
|
| 236 |
+
</div>
|
| 237 |
+
</div>
|
| 238 |
+
|
| 239 |
+
<p className="text-gray-700 mb-4 leading-relaxed">{result.snippet}</p>
|
| 240 |
+
|
| 241 |
+
<div className="flex items-center space-x-6 text-sm text-gray-500 mb-4">
|
| 242 |
+
<div className="flex items-center space-x-1">
|
| 243 |
+
<Languages className="w-4 h-4" />
|
| 244 |
+
<span>{result.language}</span>
|
| 245 |
+
</div>
|
| 246 |
+
<div>~{result.wordCount.toLocaleString()} words</div>
|
| 247 |
+
<div>Updated {result.lastModified}</div>
|
| 248 |
+
</div>
|
| 249 |
+
|
| 250 |
+
<div className="flex items-center space-x-3">
|
| 251 |
+
<button
|
| 252 |
+
onClick={() => handleViewInWikistro(result)}
|
| 253 |
+
disabled={loadingContent === result.code}
|
| 254 |
+
className="flex items-center space-x-2 px-4 py-2 bg-primary-600 text-white rounded-lg hover:bg-primary-700 transition-colors disabled:opacity-50"
|
| 255 |
+
>
|
| 256 |
+
{loadingContent === result.code ? (
|
| 257 |
+
<>
|
| 258 |
+
<Loader2 className="w-4 h-4 animate-spin" />
|
| 259 |
+
<span>Loading...</span>
|
| 260 |
+
</>
|
| 261 |
+
) : (
|
| 262 |
+
<>
|
| 263 |
+
<Eye className="w-4 h-4" />
|
| 264 |
+
<span>View in Wikistro</span>
|
| 265 |
+
</>
|
| 266 |
+
)}
|
| 267 |
+
</button>
|
| 268 |
+
|
| 269 |
+
<a
|
| 270 |
+
href={result.url}
|
| 271 |
+
target="_blank"
|
| 272 |
+
rel="noopener noreferrer"
|
| 273 |
+
className="flex items-center space-x-2 px-4 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 transition-colors"
|
| 274 |
+
>
|
| 275 |
+
<ExternalLink className="w-4 h-4" />
|
| 276 |
+
<span>Open Original</span>
|
| 277 |
+
</a>
|
| 278 |
+
</div>
|
| 279 |
+
</div>
|
| 280 |
+
</div>
|
| 281 |
+
</div>
|
| 282 |
+
))}
|
| 283 |
+
</div>
|
| 284 |
+
</div>
|
| 285 |
+
)}
|
| 286 |
+
|
| 287 |
+
{loading && (
|
| 288 |
+
<div className="flex items-center justify-center py-12">
|
| 289 |
+
<Loader2 className="w-8 h-8 animate-spin text-primary-600" />
|
| 290 |
+
<span className="ml-3 text-gray-600">Searching across {selectedLanguages.length} languages...</span>
|
| 291 |
+
</div>
|
| 292 |
+
)}
|
| 293 |
+
</div>
|
| 294 |
+
);
|
| 295 |
+
};
|
| 296 |
+
|
| 297 |
+
export default MultilingualExplorer;
|
src/components/ProgressSection.tsx
ADDED
|
@@ -0,0 +1,304 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React from 'react';
|
| 2 |
+
import { TrendingUp, Award, Target, Calendar, BookOpen, Clock, Star, CheckCircle } from 'lucide-react';
|
| 3 |
+
import { ProgressData } from '../types';
|
| 4 |
+
|
| 5 |
+
interface ProgressSectionProps {
|
| 6 |
+
progressData: ProgressData;
|
| 7 |
+
}
|
| 8 |
+
|
| 9 |
+
const ProgressSection: React.FC<ProgressSectionProps> = ({ progressData }) => {
|
| 10 |
+
const stats = [
|
| 11 |
+
{
|
| 12 |
+
label: 'Study Streak',
|
| 13 |
+
value: `${progressData.studyStreak} days`,
|
| 14 |
+
icon: Calendar,
|
| 15 |
+
color: 'text-primary-600',
|
| 16 |
+
bgColor: 'bg-primary-50'
|
| 17 |
+
},
|
| 18 |
+
{
|
| 19 |
+
label: 'Topics Completed',
|
| 20 |
+
value: progressData.topicsCompleted.toString(),
|
| 21 |
+
icon: CheckCircle,
|
| 22 |
+
color: 'text-success-600',
|
| 23 |
+
bgColor: 'bg-success-50'
|
| 24 |
+
},
|
| 25 |
+
{
|
| 26 |
+
label: 'Study Time',
|
| 27 |
+
value: `${progressData.totalStudyTime} hours`,
|
| 28 |
+
icon: Clock,
|
| 29 |
+
color: 'text-secondary-600',
|
| 30 |
+
bgColor: 'bg-secondary-50'
|
| 31 |
+
},
|
| 32 |
+
{
|
| 33 |
+
label: 'Achievements',
|
| 34 |
+
value: progressData.achievements.toString(),
|
| 35 |
+
icon: Award,
|
| 36 |
+
color: 'text-accent-600',
|
| 37 |
+
bgColor: 'bg-accent-50'
|
| 38 |
+
}
|
| 39 |
+
];
|
| 40 |
+
|
| 41 |
+
const getIconComponent = (iconName: string) => {
|
| 42 |
+
const icons: { [key: string]: React.ComponentType<any> } = {
|
| 43 |
+
CheckCircle,
|
| 44 |
+
BookOpen,
|
| 45 |
+
Award,
|
| 46 |
+
Calendar,
|
| 47 |
+
Clock
|
| 48 |
+
};
|
| 49 |
+
return icons[iconName] || BookOpen;
|
| 50 |
+
};
|
| 51 |
+
|
| 52 |
+
const achievements = [
|
| 53 |
+
{
|
| 54 |
+
title: 'First Steps',
|
| 55 |
+
description: 'Complete your first study topic',
|
| 56 |
+
earned: progressData.topicsCompleted >= 1,
|
| 57 |
+
icon: '🎯'
|
| 58 |
+
},
|
| 59 |
+
{
|
| 60 |
+
title: 'Week Warrior',
|
| 61 |
+
description: 'Study for 7 consecutive days',
|
| 62 |
+
earned: progressData.studyStreak >= 7,
|
| 63 |
+
icon: '🔥'
|
| 64 |
+
},
|
| 65 |
+
{
|
| 66 |
+
title: 'Knowledge Seeker',
|
| 67 |
+
description: 'Complete 10 study topics',
|
| 68 |
+
earned: progressData.topicsCompleted >= 10,
|
| 69 |
+
icon: '📚'
|
| 70 |
+
},
|
| 71 |
+
{
|
| 72 |
+
title: 'Time Master',
|
| 73 |
+
description: 'Study for 25 hours total',
|
| 74 |
+
earned: progressData.totalStudyTime >= 25,
|
| 75 |
+
icon: '⏰'
|
| 76 |
+
},
|
| 77 |
+
{
|
| 78 |
+
title: 'Dedicated Learner',
|
| 79 |
+
description: 'Complete 50 study topics',
|
| 80 |
+
earned: progressData.topicsCompleted >= 50,
|
| 81 |
+
icon: '🎓'
|
| 82 |
+
},
|
| 83 |
+
{
|
| 84 |
+
title: 'Explorer',
|
| 85 |
+
description: 'Study for 30 consecutive days',
|
| 86 |
+
earned: progressData.studyStreak >= 30,
|
| 87 |
+
icon: '🌍'
|
| 88 |
+
}
|
| 89 |
+
];
|
| 90 |
+
|
| 91 |
+
const generateCalendarData = () => {
|
| 92 |
+
const today = new Date();
|
| 93 |
+
const startDate = new Date(today);
|
| 94 |
+
startDate.setDate(today.getDate() - 34); // Show last 35 days
|
| 95 |
+
|
| 96 |
+
const calendarData = [];
|
| 97 |
+
for (let i = 0; i < 35; i++) {
|
| 98 |
+
const date = new Date(startDate);
|
| 99 |
+
date.setDate(startDate.getDate() + i);
|
| 100 |
+
|
| 101 |
+
// Simulate activity based on study streak
|
| 102 |
+
const daysSinceToday = Math.floor((today.getTime() - date.getTime()) / (1000 * 60 * 60 * 24));
|
| 103 |
+
let intensity = 0;
|
| 104 |
+
|
| 105 |
+
if (daysSinceToday <= progressData.studyStreak) {
|
| 106 |
+
intensity = Math.random() * 0.8 + 0.2; // Active days
|
| 107 |
+
} else {
|
| 108 |
+
intensity = Math.random() * 0.3; // Less active days
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
calendarData.push({
|
| 112 |
+
date: date.toISOString().split('T')[0],
|
| 113 |
+
intensity,
|
| 114 |
+
isToday: daysSinceToday === 0
|
| 115 |
+
});
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
return calendarData;
|
| 119 |
+
};
|
| 120 |
+
|
| 121 |
+
const calendarData = generateCalendarData();
|
| 122 |
+
|
| 123 |
+
return (
|
| 124 |
+
<div className="max-w-6xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
|
| 125 |
+
<div className="mb-8">
|
| 126 |
+
<h1 className="text-3xl font-bold text-gray-900 mb-2">Your Progress</h1>
|
| 127 |
+
<p className="text-gray-600">Track your learning journey and achievements</p>
|
| 128 |
+
</div>
|
| 129 |
+
|
| 130 |
+
{/* Stats Overview */}
|
| 131 |
+
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">
|
| 132 |
+
{stats.map((stat) => {
|
| 133 |
+
const Icon = stat.icon;
|
| 134 |
+
return (
|
| 135 |
+
<div key={stat.label} className="bg-white rounded-2xl p-6 border border-gray-200 shadow-sm">
|
| 136 |
+
<div className="flex items-center justify-between">
|
| 137 |
+
<div>
|
| 138 |
+
<p className="text-sm font-medium text-gray-600">{stat.label}</p>
|
| 139 |
+
<p className="text-2xl font-bold text-gray-900 mt-1">{stat.value}</p>
|
| 140 |
+
</div>
|
| 141 |
+
<div className={`w-12 h-12 ${stat.bgColor} rounded-xl flex items-center justify-center`}>
|
| 142 |
+
<Icon className={`w-6 h-6 ${stat.color}`} />
|
| 143 |
+
</div>
|
| 144 |
+
</div>
|
| 145 |
+
</div>
|
| 146 |
+
);
|
| 147 |
+
})}
|
| 148 |
+
</div>
|
| 149 |
+
|
| 150 |
+
<div className="grid grid-cols-1 lg:grid-cols-2 gap-8">
|
| 151 |
+
{/* Weekly Goal */}
|
| 152 |
+
<div className="bg-white rounded-2xl p-6 border border-gray-200 shadow-sm">
|
| 153 |
+
<div className="flex items-center justify-between mb-4">
|
| 154 |
+
<h2 className="text-xl font-semibold text-gray-900">Weekly Goal</h2>
|
| 155 |
+
<Target className="w-6 h-6 text-primary-600" />
|
| 156 |
+
</div>
|
| 157 |
+
|
| 158 |
+
<div className="mb-4">
|
| 159 |
+
<div className="flex items-center justify-between text-sm text-gray-600 mb-2">
|
| 160 |
+
<span>Study Time</span>
|
| 161 |
+
<span>{progressData.weeklyGoal.current} / {progressData.weeklyGoal.target} hours</span>
|
| 162 |
+
</div>
|
| 163 |
+
<div className="w-full bg-gray-200 rounded-full h-3">
|
| 164 |
+
<div
|
| 165 |
+
className="bg-gradient-to-r from-primary-500 to-secondary-500 h-3 rounded-full transition-all duration-500"
|
| 166 |
+
style={{ width: `${Math.min((progressData.weeklyGoal.current / progressData.weeklyGoal.target) * 100, 100)}%` }}
|
| 167 |
+
/>
|
| 168 |
+
</div>
|
| 169 |
+
</div>
|
| 170 |
+
|
| 171 |
+
<p className="text-sm text-gray-600">
|
| 172 |
+
You're {Math.round(Math.min((progressData.weeklyGoal.current / progressData.weeklyGoal.target) * 100, 100))}% of the way to your weekly goal!
|
| 173 |
+
</p>
|
| 174 |
+
</div>
|
| 175 |
+
|
| 176 |
+
{/* Recent Activity */}
|
| 177 |
+
<div className="bg-white rounded-2xl p-6 border border-gray-200 shadow-sm">
|
| 178 |
+
<div className="flex items-center justify-between mb-4">
|
| 179 |
+
<h2 className="text-xl font-semibold text-gray-900">Recent Activity</h2>
|
| 180 |
+
<TrendingUp className="w-6 h-6 text-primary-600" />
|
| 181 |
+
</div>
|
| 182 |
+
|
| 183 |
+
<div className="space-y-4">
|
| 184 |
+
{progressData.recentActivity.length > 0 ? (
|
| 185 |
+
progressData.recentActivity.slice(0, 5).map((activity, index) => {
|
| 186 |
+
const Icon = getIconComponent(activity.icon);
|
| 187 |
+
return (
|
| 188 |
+
<div key={index} className="flex items-start space-x-3">
|
| 189 |
+
<div className="flex-shrink-0">
|
| 190 |
+
<Icon className={`w-5 h-5 ${activity.color}`} />
|
| 191 |
+
</div>
|
| 192 |
+
<div className="flex-1 min-w-0">
|
| 193 |
+
<p className="text-sm font-medium text-gray-900">{activity.title}</p>
|
| 194 |
+
<p className="text-sm text-gray-600">{activity.course}</p>
|
| 195 |
+
<p className="text-xs text-gray-500 mt-1">{activity.time}</p>
|
| 196 |
+
</div>
|
| 197 |
+
</div>
|
| 198 |
+
);
|
| 199 |
+
})
|
| 200 |
+
) : (
|
| 201 |
+
<div className="text-center py-8">
|
| 202 |
+
<BookOpen className="w-12 h-12 text-gray-300 mx-auto mb-3" />
|
| 203 |
+
<p className="text-gray-500">No recent activity</p>
|
| 204 |
+
<p className="text-sm text-gray-400">Start studying to see your progress here!</p>
|
| 205 |
+
</div>
|
| 206 |
+
)}
|
| 207 |
+
</div>
|
| 208 |
+
</div>
|
| 209 |
+
</div>
|
| 210 |
+
|
| 211 |
+
{/* Achievements */}
|
| 212 |
+
<div className="mt-8">
|
| 213 |
+
<div className="bg-white rounded-2xl p-6 border border-gray-200 shadow-sm">
|
| 214 |
+
<div className="flex items-center justify-between mb-6">
|
| 215 |
+
<h2 className="text-xl font-semibold text-gray-900">Achievements</h2>
|
| 216 |
+
<Award className="w-6 h-6 text-accent-600" />
|
| 217 |
+
</div>
|
| 218 |
+
|
| 219 |
+
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
|
| 220 |
+
{achievements.map((achievement, index) => (
|
| 221 |
+
<div
|
| 222 |
+
key={index}
|
| 223 |
+
className={`p-4 rounded-xl border-2 transition-all duration-200 ${
|
| 224 |
+
achievement.earned
|
| 225 |
+
? 'border-success-200 bg-success-50'
|
| 226 |
+
: 'border-gray-200 bg-gray-50 opacity-60'
|
| 227 |
+
}`}
|
| 228 |
+
>
|
| 229 |
+
<div className="flex items-start space-x-3">
|
| 230 |
+
<div className="text-2xl">{achievement.icon}</div>
|
| 231 |
+
<div className="flex-1">
|
| 232 |
+
<h3 className={`font-medium ${achievement.earned ? 'text-success-900' : 'text-gray-700'}`}>
|
| 233 |
+
{achievement.title}
|
| 234 |
+
</h3>
|
| 235 |
+
<p className={`text-sm ${achievement.earned ? 'text-success-700' : 'text-gray-600'}`}>
|
| 236 |
+
{achievement.description}
|
| 237 |
+
</p>
|
| 238 |
+
</div>
|
| 239 |
+
{achievement.earned && (
|
| 240 |
+
<CheckCircle className="w-5 h-5 text-success-600 flex-shrink-0" />
|
| 241 |
+
)}
|
| 242 |
+
</div>
|
| 243 |
+
</div>
|
| 244 |
+
))}
|
| 245 |
+
</div>
|
| 246 |
+
</div>
|
| 247 |
+
</div>
|
| 248 |
+
|
| 249 |
+
{/* Study Calendar Heatmap */}
|
| 250 |
+
<div className="mt-8">
|
| 251 |
+
<div className="bg-white rounded-2xl p-6 border border-gray-200 shadow-sm">
|
| 252 |
+
<h2 className="text-xl font-semibold text-gray-900 mb-6">Study Activity</h2>
|
| 253 |
+
|
| 254 |
+
<div className="mb-4">
|
| 255 |
+
<div className="grid grid-cols-7 gap-1 text-xs text-gray-500 mb-2">
|
| 256 |
+
<div>Sun</div>
|
| 257 |
+
<div>Mon</div>
|
| 258 |
+
<div>Tue</div>
|
| 259 |
+
<div>Wed</div>
|
| 260 |
+
<div>Thu</div>
|
| 261 |
+
<div>Fri</div>
|
| 262 |
+
<div>Sat</div>
|
| 263 |
+
</div>
|
| 264 |
+
|
| 265 |
+
<div className="grid grid-cols-7 gap-1">
|
| 266 |
+
{calendarData.map((day, i) => (
|
| 267 |
+
<div
|
| 268 |
+
key={i}
|
| 269 |
+
className={`w-8 h-8 rounded-sm transition-colors ${
|
| 270 |
+
day.isToday
|
| 271 |
+
? 'ring-2 ring-primary-500'
|
| 272 |
+
: ''
|
| 273 |
+
} ${
|
| 274 |
+
day.intensity > 0.7
|
| 275 |
+
? 'bg-primary-500'
|
| 276 |
+
: day.intensity > 0.4
|
| 277 |
+
? 'bg-primary-300'
|
| 278 |
+
: day.intensity > 0.2
|
| 279 |
+
? 'bg-primary-100'
|
| 280 |
+
: 'bg-gray-100'
|
| 281 |
+
}`}
|
| 282 |
+
title={`${day.date}: Activity level ${Math.round(day.intensity * 100)}%`}
|
| 283 |
+
/>
|
| 284 |
+
))}
|
| 285 |
+
</div>
|
| 286 |
+
</div>
|
| 287 |
+
|
| 288 |
+
<div className="flex items-center justify-between text-sm text-gray-600">
|
| 289 |
+
<span>Less active</span>
|
| 290 |
+
<div className="flex items-center space-x-1">
|
| 291 |
+
<div className="w-3 h-3 bg-gray-100 rounded-sm" />
|
| 292 |
+
<div className="w-3 h-3 bg-primary-100 rounded-sm" />
|
| 293 |
+
<div className="w-3 h-3 bg-primary-300 rounded-sm" />
|
| 294 |
+
<div className="w-3 h-3 bg-primary-500 rounded-sm" />
|
| 295 |
+
</div>
|
| 296 |
+
<span>More active</span>
|
| 297 |
+
</div>
|
| 298 |
+
</div>
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React, { useState, useEffect } from 'react';
|
| 2 |
+
import { Search, Filter, ExternalLink, Clock, FileText, Loader2, BookOpen, Eye } from 'lucide-react';
|
| 3 |
+
import { WikimediaAPI, WIKIMEDIA_PROJECTS } from '../utils/wikimedia-api';
|
| 4 |
+
import { SearchResult } from '../types';
|
| 5 |
+
|
| 6 |
+
interface SearchInterfaceProps {
|
| 7 |
+
onViewArticle?: (title: string, project: string, content: string) => void;
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
const SearchInterface: React.FC<SearchInterfaceProps> = ({ onViewArticle }) => {
|
| 11 |
+
const [query, setQuery] = useState('');
|
| 12 |
+
const [selectedProject, setSelectedProject] = useState('wikipedia');
|
| 13 |
+
const [results, setResults] = useState<SearchResult[]>([]);
|
| 14 |
+
const [loading, setLoading] = useState(false);
|
| 15 |
+
const [showFilters, setShowFilters] = useState(false);
|
| 16 |
+
const [loadingContent, setLoadingContent] = useState<string | null>(null);
|
| 17 |
+
|
| 18 |
+
const handleSearch = async (searchQuery: string = query) => {
|
| 19 |
+
if (!searchQuery.trim()) return;
|
| 20 |
+
|
| 21 |
+
setLoading(true);
|
| 22 |
+
try {
|
| 23 |
+
const searchResults = await WikimediaAPI.search(searchQuery, selectedProject, 20);
|
| 24 |
+
setResults(searchResults);
|
| 25 |
+
} catch (error) {
|
| 26 |
+
console.error('Search failed:', error);
|
| 27 |
+
setResults([]);
|
| 28 |
+
} finally {
|
| 29 |
+
setLoading(false);
|
| 30 |
+
}
|
| 31 |
+
};
|
| 32 |
+
|
| 33 |
+
const handleViewInWikistro = async (result: SearchResult) => {
|
| 34 |
+
setLoadingContent(result.pageid.toString());
|
| 35 |
+
try {
|
| 36 |
+
const content = await WikimediaAPI.getPageContent(result.title, result.project);
|
| 37 |
+
if (onViewArticle) {
|
| 38 |
+
onViewArticle(result.title, result.project, content);
|
| 39 |
+
}
|
| 40 |
+
} catch (error) {
|
| 41 |
+
console.error('Failed to load article content:', error);
|
| 42 |
+
} finally {
|
| 43 |
+
setLoadingContent(null);
|
| 44 |
+
}
|
| 45 |
+
};
|
| 46 |
+
|
| 47 |
+
const formatDate = (timestamp: string) => {
|
| 48 |
+
return new Date(timestamp).toLocaleDateString('en-US', {
|
| 49 |
+
year: 'numeric',
|
| 50 |
+
month: 'short',
|
| 51 |
+
day: 'numeric'
|
| 52 |
+
});
|
| 53 |
+
};
|
| 54 |
+
|
| 55 |
+
const truncateSnippet = (snippet: string, maxLength: number = 200) => {
|
| 56 |
+
const cleanSnippet = snippet.replace(/<[^>]*>/g, '');
|
| 57 |
+
return cleanSnippet.length > maxLength
|
| 58 |
+
? cleanSnippet.substring(0, maxLength) + '...'
|
| 59 |
+
: cleanSnippet;
|
| 60 |
+
};
|
| 61 |
+
|
| 62 |
+
useEffect(() => {
|
| 63 |
+
const delayedSearch = setTimeout(() => {
|
| 64 |
+
if (query.trim()) {
|
| 65 |
+
handleSearch(query);
|
| 66 |
+
}
|
| 67 |
+
}, 500);
|
| 68 |
+
|
| 69 |
+
return () => clearTimeout(delayedSearch);
|
| 70 |
+
}, [query, selectedProject]);
|
| 71 |
+
|
| 72 |
+
const currentProject = WIKIMEDIA_PROJECTS.find(p => p.id === selectedProject);
|
| 73 |
+
|
| 74 |
+
return (
|
| 75 |
+
<div className="max-w-6xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
|
| 76 |
+
<div className="mb-8">
|
| 77 |
+
<div className="flex flex-col lg:flex-row gap-4">
|
| 78 |
+
<div className="flex-1 relative">
|
| 79 |
+
<Search className="absolute left-4 top-1/2 transform -translate-y-1/2 text-gray-400 w-5 h-5" />
|
| 80 |
+
<input
|
| 81 |
+
type="text"
|
| 82 |
+
value={query}
|
| 83 |
+
onChange={(e) => setQuery(e.target.value)}
|
| 84 |
+
placeholder="Search across Wikimedia projects..."
|
| 85 |
+
className="w-full pl-12 pr-4 py-4 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary-500 focus:border-transparent text-lg"
|
| 86 |
+
onKeyDown={(e) => e.key === 'Enter' && handleSearch()}
|
| 87 |
+
/>
|
| 88 |
+
</div>
|
| 89 |
+
|
| 90 |
+
<div className="flex gap-2">
|
| 91 |
+
<button
|
| 92 |
+
onClick={() => setShowFilters(!showFilters)}
|
| 93 |
+
className="flex items-center px-4 py-4 border border-gray-300 rounded-xl hover:bg-gray-50 transition-colors"
|
| 94 |
+
>
|
| 95 |
+
<Filter className="w-5 h-5 mr-2" />
|
| 96 |
+
Filters
|
| 97 |
+
</button>
|
| 98 |
+
|
| 99 |
+
<button
|
| 100 |
+
onClick={() => handleSearch()}
|
| 101 |
+
disabled={loading}
|
| 102 |
+
className="flex items-center px-6 py-4 bg-primary-600 text-white rounded-xl hover:bg-primary-700 transition-colors disabled:opacity-50"
|
| 103 |
+
>
|
| 104 |
+
{loading ? (
|
| 105 |
+
<Loader2 className="w-5 h-5 animate-spin" />
|
| 106 |
+
) : (
|
| 107 |
+
<Search className="w-5 h-5" />
|
| 108 |
+
)}
|
| 109 |
+
</button>
|
| 110 |
+
</div>
|
| 111 |
+
</div>
|
| 112 |
+
|
| 113 |
+
{showFilters && (
|
| 114 |
+
<div className="mt-4 p-4 bg-gray-50 rounded-xl">
|
| 115 |
+
<div className="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-6 gap-3">
|
| 116 |
+
{WIKIMEDIA_PROJECTS.map((project) => (
|
| 117 |
+
<button
|
| 118 |
+
key={project.id}
|
| 119 |
+
onClick={() => setSelectedProject(project.id)}
|
| 120 |
+
className={`p-3 rounded-lg text-sm font-medium transition-all ${
|
| 121 |
+
selectedProject === project.id
|
| 122 |
+
? 'bg-primary-600 text-white shadow-md'
|
| 123 |
+
: 'bg-white text-gray-700 hover:bg-gray-100 border border-gray-200'
|
| 124 |
+
}`}
|
| 125 |
+
>
|
| 126 |
+
{project.name}
|
| 127 |
+
</button>
|
| 128 |
+
))}
|
| 129 |
+
</div>
|
| 130 |
+
</div>
|
| 131 |
+
)}
|
| 132 |
+
</div>
|
| 133 |
+
|
| 134 |
+
{currentProject && (
|
| 135 |
+
<div className="mb-6">
|
| 136 |
+
<div className="flex items-center space-x-3 p-4 bg-white rounded-xl border border-gray-200">
|
| 137 |
+
<div className={`w-3 h-3 rounded-full ${currentProject.color}`} />
|
| 138 |
+
<div>
|
| 139 |
+
<h3 className="font-semibold text-gray-900">{currentProject.name}</h3>
|
| 140 |
+
<p className="text-sm text-gray-600">{currentProject.description}</p>
|
| 141 |
+
</div>
|
| 142 |
+
</div>
|
| 143 |
+
</div>
|
| 144 |
+
)}
|
| 145 |
+
|
| 146 |
+
{loading && (
|
| 147 |
+
<div className="flex items-center justify-center py-12">
|
| 148 |
+
<Loader2 className="w-8 h-8 animate-spin text-primary-600" />
|
| 149 |
+
<span className="ml-3 text-gray-600">Searching {currentProject?.name}...</span>
|
| 150 |
+
</div>
|
| 151 |
+
)}
|
| 152 |
+
|
| 153 |
+
{!loading && results.length === 0 && query && (
|
| 154 |
+
<div className="text-center py-12">
|
| 155 |
+
<FileText className="w-16 h-16 text-gray-300 mx-auto mb-4" />
|
| 156 |
+
<h3 className="text-lg font-medium text-gray-900 mb-2">No results found</h3>
|
| 157 |
+
<p className="text-gray-600">Try adjusting your search terms or selecting a different project.</p>
|
| 158 |
+
</div>
|
| 159 |
+
)}
|
| 160 |
+
|
| 161 |
+
{!loading && results.length > 0 && (
|
| 162 |
+
<div className="space-y-4">
|
| 163 |
+
<div className="flex items-center justify-between">
|
| 164 |
+
<h2 className="text-xl font-semibold text-gray-900">
|
| 165 |
+
Search Results ({results.length})
|
| 166 |
+
</h2>
|
| 167 |
+
</div>
|
| 168 |
+
|
| 169 |
+
<div className="grid gap-4">
|
| 170 |
+
{results.map((result) => (
|
| 171 |
+
<div
|
| 172 |
+
key={result.pageid}
|
| 173 |
+
className="bg-white border border-gray-200 rounded-xl p-6 hover:shadow-md transition-all duration-200 hover:border-primary-200"
|
| 174 |
+
>
|
| 175 |
+
<div className="flex items-start justify-between">
|
| 176 |
+
<div className="flex-1">
|
| 177 |
+
<h3 className="text-lg font-semibold text-gray-900 mb-2 hover:text-primary-600">
|
| 178 |
+
<a
|
| 179 |
+
href={result.url}
|
| 180 |
+
target="_blank"
|
| 181 |
+
rel="noopener noreferrer"
|
| 182 |
+
className="hover:underline"
|
| 183 |
+
>
|
| 184 |
+
{result.title}
|
| 185 |
+
</a>
|
| 186 |
+
</h3>
|
| 187 |
+
|
| 188 |
+
<p className="text-gray-600 mb-3 leading-relaxed">
|
| 189 |
+
{truncateSnippet(result.snippet)}
|
| 190 |
+
</p>
|
| 191 |
+
|
| 192 |
+
<div className="flex items-center space-x-6 text-sm text-gray-500 mb-4">
|
| 193 |
+
<div className="flex items-center space-x-1">
|
| 194 |
+
<Clock className="w-4 h-4" />
|
| 195 |
+
<span>{formatDate(result.timestamp)}</span>
|
| 196 |
+
</div>
|
| 197 |
+
<div className="flex items-center space-x-1">
|
| 198 |
+
<FileText className="w-4 h-4" />
|
| 199 |
+
<span>{Math.round(result.size / 1024)}KB</span>
|
| 200 |
+
</div>
|
| 201 |
+
<div className="flex items-center space-x-1">
|
| 202 |
+
<BookOpen className="w-4 h-4" />
|
| 203 |
+
<span className="capitalize">{result.project}</span>
|
| 204 |
+
</div>
|
| 205 |
+
</div>
|
| 206 |
+
|
| 207 |
+
<div className="flex items-center space-x-3">
|
| 208 |
+
<button
|
| 209 |
+
onClick={() => handleViewInWikistro(result)}
|
| 210 |
+
disabled={loadingContent === result.pageid.toString()}
|
| 211 |
+
className="flex items-center space-x-2 px-4 py-2 bg-primary-600 text-white rounded-lg hover:bg-primary-700 transition-colors disabled:opacity-50"
|
| 212 |
+
>
|
| 213 |
+
{loadingContent === result.pageid.toString() ? (
|
| 214 |
+
<>
|
| 215 |
+
<Loader2 className="w-4 h-4 animate-spin" />
|
| 216 |
+
<span>Loading...</span>
|
| 217 |
+
</>
|
| 218 |
+
) : (
|
| 219 |
+
<>
|
| 220 |
+
<Eye className="w-4 h-4" />
|
| 221 |
+
<span>View in Wikistro</span>
|
| 222 |
+
</>
|
| 223 |
+
)}
|
| 224 |
+
</button>
|
| 225 |
+
|
| 226 |
+
<a
|
| 227 |
+
href={result.url}
|
| 228 |
+
target="_blank"
|
| 229 |
+
rel="noopener noreferrer"
|
| 230 |
+
className="flex items-center space-x-2 px-4 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 transition-colors"
|
| 231 |
+
>
|
| 232 |
+
<ExternalLink className="w-4 h-4" />
|
| 233 |
+
<span>Open Original</span>
|
| 234 |
+
</a>
|
| 235 |
+
</div>
|
| 236 |
+
</div>
|
| 237 |
+
</div>
|
| 238 |
+
</div>
|
| 239 |
+
))}
|
| 240 |
+
</div>
|
| 241 |
+
</div>
|
| 242 |
+
)}
|
| 243 |
+
</div>
|
| 244 |
+
);
|
| 245 |
+
};
|
| 246 |
+
|
| 247 |
+
export default SearchInter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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React, { useState, useCallback } from 'react';
|
| 2 |
+
import { BookOpen, Clock, Target, Plus, CheckCircle, Circle, Star, TrendingUp, Play, ExternalLink, Loader2, Eye, ArrowRight, AlertCircle } from 'lucide-react';
|
| 3 |
+
import { StudyPlan, StudyTopic } from '../types';
|
| 4 |
+
import { WikimediaAPI } from '../utils/wikimedia-api';
|
| 5 |
+
|
| 6 |
+
interface StudyPlansSectionProps {
|
| 7 |
+
studyPlans: StudyPlan[];
|
| 8 |
+
onTopicComplete?: (planId: string, topicId: string) => void;
|
| 9 |
+
onTopicStart?: (planId: string, topicId: string) => void;
|
| 10 |
+
onPlanCreated?: (plan: StudyPlan) => void;
|
| 11 |
+
onViewArticle?: (title: string, project: string, content: string) => void;
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
interface CreatePlanFormProps {
|
| 15 |
+
onPlanCreated?: (plan: StudyPlan) => void;
|
| 16 |
+
onCancel: () => void;
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
// Move the form component outside to prevent recreation
|
| 20 |
+
const CreatePlanForm: React.FC<CreatePlanFormProps> = ({ onPlanCreated, onCancel }) => {
|
| 21 |
+
const [title, setTitle] = useState('');
|
| 22 |
+
const [description, setDescription] = useState('');
|
| 23 |
+
const [difficulty, setDifficulty] = useState<'beginner' | 'intermediate' | 'advanced'>('beginner');
|
| 24 |
+
const [estimatedTime, setEstimatedTime] = useState('');
|
| 25 |
+
const [creatingPlan, setCreatingPlan] = useState(false);
|
| 26 |
+
const [generationErrorMessage, setGenerationErrorMessage] = useState<string>('');
|
| 27 |
+
|
| 28 |
+
const clearForm = () => {
|
| 29 |
+
setTitle('');
|
| 30 |
+
setDescription('');
|
| 31 |
+
setDifficulty('beginner');
|
| 32 |
+
setEstimatedTime('');
|
| 33 |
+
setGenerationErrorMessage('');
|
| 34 |
+
};
|
| 35 |
+
|
| 36 |
+
const handleCreatePlan = async (e: React.FormEvent) => {
|
| 37 |
+
e.preventDefault();
|
| 38 |
+
if (!title.trim()) return;
|
| 39 |
+
|
| 40 |
+
setCreatingPlan(true);
|
| 41 |
+
setGenerationErrorMessage('');
|
| 42 |
+
|
| 43 |
+
try {
|
| 44 |
+
// Use AI to generate a real study plan
|
| 45 |
+
const generatedPlan = await WikimediaAPI.generateStudyPlan(title, difficulty);
|
| 46 |
+
|
| 47 |
+
// Override with user's custom details
|
| 48 |
+
const customPlan: StudyPlan = {
|
| 49 |
+
...generatedPlan,
|
| 50 |
+
title: title,
|
| 51 |
+
description: description || generatedPlan.description,
|
| 52 |
+
estimatedTime: estimatedTime || generatedPlan.estimatedTime
|
| 53 |
+
};
|
| 54 |
+
|
| 55 |
+
if (onPlanCreated) {
|
| 56 |
+
onPlanCreated(customPlan);
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
onCancel();
|
| 60 |
+
clearForm();
|
| 61 |
+
} catch (error) {
|
| 62 |
+
console.error('Failed to create study plan:', error);
|
| 63 |
+
|
| 64 |
+
// Check if it's the "No content found" error
|
| 65 |
+
if (error instanceof Error && error.message === 'No content found for this topic') {
|
| 66 |
+
setGenerationErrorMessage(
|
| 67 |
+
'We couldn\'t find enough content for this topic. Try using a more general topic like "Physics", "History", "Biology", or "Computer Science".'
|
| 68 |
+
);
|
| 69 |
+
} else {
|
| 70 |
+
setGenerationErrorMessage(
|
| 71 |
+
'Failed to generate AI study plan. We\'ll create a basic plan for you instead.'
|
| 72 |
+
);
|
| 73 |
+
|
| 74 |
+
// Create a basic plan if API fails
|
| 75 |
+
const basicPlan: StudyPlan = {
|
| 76 |
+
id: `custom-${Date.now()}`,
|
| 77 |
+
title: title,
|
| 78 |
+
description: description || `Study plan for ${title}`,
|
| 79 |
+
difficulty: difficulty,
|
| 80 |
+
estimatedTime: estimatedTime || '4 weeks',
|
| 81 |
+
created: new Date().toISOString(),
|
| 82 |
+
topics: [
|
| 83 |
+
{
|
| 84 |
+
id: `${Date.now()}-1`,
|
| 85 |
+
title: `Introduction to ${title}`,
|
| 86 |
+
description: 'Getting started with the basics',
|
| 87 |
+
content: 'Introductory content will be loaded from Wikimedia sources.',
|
| 88 |
+
completed: false,
|
| 89 |
+
estimatedTime: '2 hours',
|
| 90 |
+
resources: []
|
| 91 |
+
}
|
| 92 |
+
]
|
| 93 |
+
};
|
| 94 |
+
|
| 95 |
+
if (onPlanCreated) {
|
| 96 |
+
onPlanCreated(basicPlan);
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
onCancel();
|
| 100 |
+
clearForm();
|
| 101 |
+
}
|
| 102 |
+
} finally {
|
| 103 |
+
setCreatingPlan(false);
|
| 104 |
+
}
|
| 105 |
+
};
|
| 106 |
+
|
| 107 |
+
return (
|
| 108 |
+
<div className="bg-white rounded-2xl p-6 border border-gray-200 shadow-sm">
|
| 109 |
+
<h3 className="text-lg font-semibold text-gray-900 mb-4">Create New Study Plan</h3>
|
| 110 |
+
|
| 111 |
+
{generationErrorMessage && (
|
| 112 |
+
<div className="mb-4 p-4 bg-red-50 border border-red-200 rounded-xl flex items-start space-x-3">
|
| 113 |
+
<AlertCircle className="w-5 h-5 text-red-600 flex-shrink-0 mt-0.5" />
|
| 114 |
+
<div>
|
| 115 |
+
<p className="text-red-800 font-medium">Unable to Generate Study Plan</p>
|
| 116 |
+
<p className="text-red-700 text-sm mt-1">{generationErrorMessage}</p>
|
| 117 |
+
</div>
|
| 118 |
+
</div>
|
| 119 |
+
)}
|
| 120 |
+
|
| 121 |
+
<form onSubmit={handleCreatePlan} className="space-y-4">
|
| 122 |
+
<div>
|
| 123 |
+
<label className="block text-sm font-medium text-gray-700 mb-2">Plan Title</label>
|
| 124 |
+
<input
|
| 125 |
+
type="text"
|
| 126 |
+
value={title}
|
| 127 |
+
onChange={(e) => {
|
| 128 |
+
setTitle(e.target.value);
|
| 129 |
+
setGenerationErrorMessage(''); // Clear error when user types
|
| 130 |
+
}}
|
| 131 |
+
placeholder="e.g., Introduction to Quantum Physics"
|
| 132 |
+
className="w-full px-4 py-3 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary-500 focus:border-transparent"
|
| 133 |
+
required
|
| 134 |
+
disabled={creatingPlan}
|
| 135 |
+
/>
|
| 136 |
+
</div>
|
| 137 |
+
|
| 138 |
+
<div>
|
| 139 |
+
<label className="block text-sm font-medium text-gray-700 mb-2">Description</label>
|
| 140 |
+
<textarea
|
| 141 |
+
value={description}
|
| 142 |
+
onChange={(e) => {
|
| 143 |
+
setDescription(e.target.value);
|
| 144 |
+
setGenerationErrorMessage(''); // Clear error when user types
|
| 145 |
+
}}
|
| 146 |
+
placeholder="Brief description of what this study plan covers..."
|
| 147 |
+
rows={3}
|
| 148 |
+
className="w-full px-4 py-3 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary-500 focus:border-transparent"
|
| 149 |
+
disabled={creatingPlan}
|
| 150 |
+
/>
|
| 151 |
+
</div>
|
| 152 |
+
|
| 153 |
+
<div className="grid grid-cols-2 gap-4">
|
| 154 |
+
<div>
|
| 155 |
+
<label className="block text-sm font-medium text-gray-700 mb-2">Difficulty</label>
|
| 156 |
+
<select
|
| 157 |
+
value={difficulty}
|
| 158 |
+
onChange={(e) => setDifficulty(e.target.value as 'beginner' | 'intermediate' | 'advanced')}
|
| 159 |
+
className="w-full px-4 py-3 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary-500 focus:border-transparent"
|
| 160 |
+
disabled={creatingPlan}
|
| 161 |
+
>
|
| 162 |
+
<option value="beginner">Beginner</option>
|
| 163 |
+
<option value="intermediate">Intermediate</option>
|
| 164 |
+
<option value="advanced">Advanced</option>
|
| 165 |
+
</select>
|
| 166 |
+
</div>
|
| 167 |
+
|
| 168 |
+
<div>
|
| 169 |
+
<label className="block text-sm font-medium text-gray-700 mb-2">Estimated Time</label>
|
| 170 |
+
<input
|
| 171 |
+
type="text"
|
| 172 |
+
value={estimatedTime}
|
| 173 |
+
onChange={(e) => setEstimatedTime(e.target.value)}
|
| 174 |
+
placeholder="e.g., 4 weeks"
|
| 175 |
+
className="w-full px-4 py-3 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary-500 focus:border-transparent"
|
| 176 |
+
disabled={creatingPlan}
|
| 177 |
+
/>
|
| 178 |
+
</div>
|
| 179 |
+
</div>
|
| 180 |
+
|
| 181 |
+
<div className="flex space-x-3 pt-4">
|
| 182 |
+
<button
|
| 183 |
+
type="button"
|
| 184 |
+
onClick={() => {
|
| 185 |
+
onCancel();
|
| 186 |
+
clearForm();
|
| 187 |
+
}}
|
| 188 |
+
className="flex-1 px-4 py-3 border border-gray-300 rounded-xl text-gray-700 hover:bg-gray-50 transition-colors"
|
| 189 |
+
disabled={creatingPlan}
|
| 190 |
+
>
|
| 191 |
+
Cancel
|
| 192 |
+
</button>
|
| 193 |
+
<button
|
| 194 |
+
type="submit"
|
| 195 |
+
disabled={creatingPlan || !title.trim()}
|
| 196 |
+
className="flex-1 flex items-center justify-center space-x-2 px-4 py-3 bg-primary-600 text-white rounded-xl hover:bg-primary-700 transition-colors disabled:opacity-50"
|
| 197 |
+
>
|
| 198 |
+
{creatingPlan ? (
|
| 199 |
+
<>
|
| 200 |
+
<Loader2 className="w-4 h-4 animate-spin" />
|
| 201 |
+
<span>Creating Plan...</span>
|
| 202 |
+
</>
|
| 203 |
+
) : (
|
| 204 |
+
<span>Create Plan</span>
|
| 205 |
+
)}
|
| 206 |
+
</button>
|
| 207 |
+
</div>
|
| 208 |
+
</form>
|
| 209 |
+
</div>
|
| 210 |
+
);
|
| 211 |
+
};
|
| 212 |
+
|
| 213 |
+
const StudyPlansSection: React.FC<StudyPlansSectionProps> = ({
|
| 214 |
+
studyPlans,
|
| 215 |
+
onTopicComplete,
|
| 216 |
+
onTopicStart,
|
| 217 |
+
onPlanCreated,
|
| 218 |
+
onViewArticle
|
| 219 |
+
}) => {
|
| 220 |
+
const [selectedPlan, setSelectedPlan] = useState<StudyPlan | null>(null);
|
| 221 |
+
const [showCreateForm, setShowCreateForm] = useState(false);
|
| 222 |
+
const [loadingResource, setLoadingResource] = useState<string | null>(null);
|
| 223 |
+
const [startingTopic, setStartingTopic] = useState<string | null>(null);
|
| 224 |
+
|
| 225 |
+
const getDifficultyColor = (difficulty: string) => {
|
| 226 |
+
switch (difficulty) {
|
| 227 |
+
case 'beginner': return 'bg-emerald-100 text-emerald-800 border-emerald-200';
|
| 228 |
+
case 'intermediate': return 'bg-amber-100 text-amber-800 border-amber-200';
|
| 229 |
+
case 'advanced': return 'bg-red-100 text-red-800 border-red-200';
|
| 230 |
+
default: return 'bg-gray-100 text-gray-800 border-gray-200';
|
| 231 |
+
}
|
| 232 |
+
};
|
| 233 |
+
|
| 234 |
+
const getCompletionPercentage = (topics: StudyTopic[]) => {
|
| 235 |
+
if (topics.length === 0) return 0;
|
| 236 |
+
const completed = topics.filter(t => t.completed).length;
|
| 237 |
+
return Math.round((completed / topics.length) * 100);
|
| 238 |
+
};
|
| 239 |
+
|
| 240 |
+
const handleTopicAction = async (planId: string, topicId: string, action: 'start' | 'complete') => {
|
| 241 |
+
if (action === 'start') {
|
| 242 |
+
setStartingTopic(topicId);
|
| 243 |
+
|
| 244 |
+
// Find the topic and load its content
|
| 245 |
+
const plan = studyPlans.find(p => p.id === planId);
|
| 246 |
+
const topic = plan?.topics.find(t => t.id === topicId);
|
| 247 |
+
|
| 248 |
+
if (topic && topic.resources.length > 0 && onViewArticle) {
|
| 249 |
+
try {
|
| 250 |
+
const resource = topic.resources[0];
|
| 251 |
+
const content = await WikimediaAPI.getPageContent(resource.title, resource.project);
|
| 252 |
+
onViewArticle(resource.title, resource.project, content);
|
| 253 |
+
} catch (error) {
|
| 254 |
+
console.error('Failed to load topic content:', error);
|
| 255 |
+
}
|
| 256 |
+
}
|
| 257 |
+
|
| 258 |
+
if (onTopicStart) {
|
| 259 |
+
onTopicStart(planId, topicId);
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
setStartingTopic(null);
|
| 263 |
+
} else if (action === 'complete' && onTopicComplete) {
|
| 264 |
+
// Immediately update the completion status
|
| 265 |
+
onTopicComplete(planId, topicId);
|
| 266 |
+
}
|
| 267 |
+
};
|
| 268 |
+
|
| 269 |
+
const handleViewResource = async (resource: any) => {
|
| 270 |
+
setLoadingResource(resource.url);
|
| 271 |
+
try {
|
| 272 |
+
const content = await WikimediaAPI.getPageContent(resource.title, resource.project);
|
| 273 |
+
if (onViewArticle) {
|
| 274 |
+
onViewArticle(resource.title, resource.project, content);
|
| 275 |
+
}
|
| 276 |
+
} catch (error) {
|
| 277 |
+
console.error('Failed to load resource content:', error);
|
| 278 |
+
} finally {
|
| 279 |
+
setLoadingResource(null);
|
| 280 |
+
}
|
| 281 |
+
};
|
| 282 |
+
|
| 283 |
+
const getNextTopic = (plan: StudyPlan) => {
|
| 284 |
+
const nextTopic = plan.topics.find(topic => !topic.completed);
|
| 285 |
+
return nextTopic;
|
| 286 |
+
};
|
| 287 |
+
|
| 288 |
+
if (selectedPlan) {
|
| 289 |
+
const nextTopic = getNextTopic(selectedPlan);
|
| 290 |
+
|
| 291 |
+
return (
|
| 292 |
+
<div className="max-w-5xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
|
| 293 |
+
<div className="mb-8">
|
| 294 |
+
<button
|
| 295 |
+
onClick={() => setSelectedPlan(null)}
|
| 296 |
+
className="flex items-center space-x-2 text-primary-600 hover:text-primary-700 font-medium mb-6 transition-colors"
|
| 297 |
+
>
|
| 298 |
+
<ArrowRight className="w-4 h-4 rotate-180" />
|
| 299 |
+
<span>Back to Study Plans</span>
|
| 300 |
+
</button>
|
| 301 |
+
|
| 302 |
+
<div className="bg-white rounded-2xl p-8 border border-gray-200 shadow-sm">
|
| 303 |
+
<div className="flex items-start justify-between mb-6">
|
| 304 |
+
<div className="flex-1">
|
| 305 |
+
<div className="flex items-center space-x-3 mb-3">
|
| 306 |
+
<h1 className="text-3xl font-bold text-gray-900">{selectedPlan.title}</h1>
|
| 307 |
+
<span className="px-3 py-1 bg-gradient-to-r from-primary-100 to-secondary-100 text-primary-700 text-sm rounded-full font-medium border border-primary-200">
|
| 308 |
+
AI Generated
|
| 309 |
+
</span>
|
| 310 |
+
</div>
|
| 311 |
+
<p className="text-gray-600 mb-6 text-lg leading-relaxed">{selectedPlan.description}</p>
|
| 312 |
+
|
| 313 |
+
<div className="flex items-center space-x-6">
|
| 314 |
+
<span className={`px-4 py-2 rounded-full text-sm font-medium border ${getDifficultyColor(selectedPlan.difficulty)}`}>
|
| 315 |
+
{selectedPlan.difficulty.charAt(0).toUpperCase() + selectedPlan.difficulty.slice(1)}
|
| 316 |
+
</span>
|
| 317 |
+
<div className="flex items-center text-gray-600">
|
| 318 |
+
<Clock className="w-5 h-5 mr-2" />
|
| 319 |
+
<span className="font-medium">{selectedPlan.estimatedTime}</span>
|
| 320 |
+
</div>
|
| 321 |
+
<div className="flex items-center text-gray-600">
|
| 322 |
+
<Target className="w-5 h-5 mr-2" />
|
| 323 |
+
<span className="font-medium">{getCompletionPercentage(selectedPlan.topics)}% Complete</span>
|
| 324 |
+
</div>
|
| 325 |
+
</div>
|
| 326 |
+
</div>
|
| 327 |
+
|
| 328 |
+
<div className="ml-6">
|
| 329 |
+
<div className="w-20 h-20 bg-gradient-to-br from-primary-100 to-secondary-100 rounded-2xl flex items-center justify-center border border-primary-200">
|
| 330 |
+
<TrendingUp className="w-10 h-10 text-primary-600" />
|
| 331 |
+
</div>
|
| 332 |
+
</div>
|
| 333 |
+
</div>
|
| 334 |
+
|
| 335 |
+
<div className="w-full bg-gray-200 rounded-full h-3 mb-6">
|
| 336 |
+
<div
|
| 337 |
+
className="bg-gradient-to-r from-primary-500 to-secondary-500 h-3 rounded-full transition-all duration-500"
|
| 338 |
+
style={{ width: `${getCompletionPercentage(selectedPlan.topics)}%` }}
|
| 339 |
+
/>
|
| 340 |
+
</div>
|
| 341 |
+
|
| 342 |
+
{/* What's Next Section */}
|
| 343 |
+
{nextTopic && (
|
| 344 |
+
<div className="bg-gradient-to-r from-primary-50 to-secondary-50 rounded-2xl p-6 border border-primary-200">
|
| 345 |
+
<h3 className="font-semibold text-primary-900 mb-3 text-lg">🎯 What's Next?</h3>
|
| 346 |
+
<div className="flex items-center justify-between">
|
| 347 |
+
<div className="flex-1">
|
| 348 |
+
<p className="text-primary-800 font-semibold text-lg">{nextTopic.title}</p>
|
| 349 |
+
<p className="text-primary-600 mt-1">{nextTopic.estimatedTime} • {nextTopic.resources.length} resources available</p>
|
| 350 |
+
</div>
|
| 351 |
+
<button
|
| 352 |
+
onClick={() => handleTopicAction(selectedPlan.id, nextTopic.id, 'start')}
|
| 353 |
+
disabled={startingTopic === nextTopic.id}
|
| 354 |
+
className="flex items-center space-x-2 px-6 py-3 bg-primary-600 text-white rounded-xl hover:bg-primary-700 transition-colors shadow-md hover:shadow-lg disabled:opacity-50"
|
| 355 |
+
>
|
| 356 |
+
{startingTopic === nextTopic.id ? (
|
| 357 |
+
<>
|
| 358 |
+
<Loader2 className="w-5 h-5 animate-spin" />
|
| 359 |
+
<span className="font-medium">Starting...</span>
|
| 360 |
+
</>
|
| 361 |
+
) : (
|
| 362 |
+
<>
|
| 363 |
+
<Play className="w-5 h-5" />
|
| 364 |
+
<span className="font-medium">Start Now</span>
|
| 365 |
+
</>
|
| 366 |
+
)}
|
| 367 |
+
</button>
|
| 368 |
+
</div>
|
| 369 |
+
</div>
|
| 370 |
+
)}
|
| 371 |
+
</div>
|
| 372 |
+
</div>
|
| 373 |
+
|
| 374 |
+
<div className="space-y-6">
|
| 375 |
+
{selectedPlan.topics.map((topic, index) => (
|
| 376 |
+
<div
|
| 377 |
+
key={topic.id}
|
| 378 |
+
className="bg-white rounded-2xl p-6 border border-gray-200 shadow-sm hover:shadow-md transition-all duration-200"
|
| 379 |
+
>
|
| 380 |
+
<div className="flex items-start space-x-4">
|
| 381 |
+
<div className="flex-shrink-0 mt-1">
|
| 382 |
+
{topic.completed ? (
|
| 383 |
+
<CheckCircle className="w-7 h-7 text-emerald-600" />
|
| 384 |
+
) : (
|
| 385 |
+
<Circle className="w-7 h-7 text-gray-400" />
|
| 386 |
+
)}
|
| 387 |
+
</div>
|
| 388 |
+
|
| 389 |
+
<div className="flex-1">
|
| 390 |
+
<div className="flex items-start justify-between">
|
| 391 |
+
<div className="flex-1">
|
| 392 |
+
<h3 className="text-xl font-semibold text-gray-900 mb-2">
|
| 393 |
+
{index + 1}. {topic.title}
|
| 394 |
+
</h3>
|
| 395 |
+
<p className="text-gray-600 mb-4 leading-relaxed">{topic.description}</p>
|
| 396 |
+
|
| 397 |
+
<div className="flex items-center space-x-6 text-sm text-gray-500 mb-4">
|
| 398 |
+
<div className="flex items-center">
|
| 399 |
+
<Clock className="w-4 h-4 mr-1" />
|
| 400 |
+
<span>{topic.estimatedTime}</span>
|
| 401 |
+
</div>
|
| 402 |
+
<div className="flex items-center">
|
| 403 |
+
<BookOpen className="w-4 h-4 mr-1" />
|
| 404 |
+
<span>{topic.resources.length} resources</span>
|
| 405 |
+
</div>
|
| 406 |
+
</div>
|
| 407 |
+
|
| 408 |
+
{topic.resources.length > 0 && (
|
| 409 |
+
<div className="space-y-3">
|
| 410 |
+
<h4 className="font-medium text-gray-900">📚 Resources:</h4>
|
| 411 |
+
<div className="flex flex-wrap gap-3">
|
| 412 |
+
{topic.resources.map((resource, resourceIndex) => (
|
| 413 |
+
<div key={resourceIndex} className="flex items-center space-x-2">
|
| 414 |
+
<button
|
| 415 |
+
onClick={() => handleViewResource(resource)}
|
| 416 |
+
disabled={loadingResource === resource.url}
|
| 417 |
+
className="inline-flex items-center px-4 py-2 bg-primary-100 text-primary-700 rounded-xl text-sm hover:bg-primary-200 transition-colors disabled:opacity-50 border border-primary-200"
|
| 418 |
+
>
|
| 419 |
+
{loadingResource === resource.url ? (
|
| 420 |
+
<Loader2 className="w-4 h-4 mr-2 animate-spin" />
|
| 421 |
+
) : (
|
| 422 |
+
<Eye className="w-4 h-4 mr-2" />
|
| 423 |
+
)}
|
| 424 |
+
<span className="font-medium">{resource.title}</span>
|
| 425 |
+
</button>
|
| 426 |
+
<a
|
| 427 |
+
href={resource.url}
|
| 428 |
+
target="_blank"
|
| 429 |
+
rel="noopener noreferrer"
|
| 430 |
+
className="p-2 text-gray-400 hover:text-primary-600 transition-colors rounded-lg hover:bg-gray-100"
|
| 431 |
+
>
|
| 432 |
+
<ExternalLink className="w-4 h-4" />
|
| 433 |
+
</a>
|
| 434 |
+
</div>
|
| 435 |
+
))}
|
| 436 |
+
</div>
|
| 437 |
+
</div>
|
| 438 |
+
)}
|
| 439 |
+
</div>
|
| 440 |
+
|
| 441 |
+
<div className="ml-6 flex flex-col space-y-3">
|
| 442 |
+
{!topic.completed ? (
|
| 443 |
+
<>
|
| 444 |
+
<button
|
| 445 |
+
onClick={() => handleTopicAction(selectedPlan.id, topic.id, 'start')}
|
| 446 |
+
disabled={startingTopic === topic.id}
|
| 447 |
+
className="flex items-center space-x-2 px-5 py-3 bg-primary-600 text-white rounded-xl hover:bg-primary-700 transition-colors shadow-md hover:shadow-lg disabled:opacity-50"
|
| 448 |
+
>
|
| 449 |
+
{startingTopic === topic.id ? (
|
| 450 |
+
<>
|
| 451 |
+
<Loader2 className="w-4 h-4 animate-spin" />
|
| 452 |
+
<span className="font-medium">Starting...</span>
|
| 453 |
+
</>
|
| 454 |
+
) : (
|
| 455 |
+
<>
|
| 456 |
+
<Play className="w-4 h-4" />
|
| 457 |
+
<span className="font-medium">Start</span>
|
| 458 |
+
</>
|
| 459 |
+
)}
|
| 460 |
+
</button>
|
| 461 |
+
<button
|
| 462 |
+
onClick={() => handleTopicAction(selectedPlan.id, topic.id, 'complete')}
|
| 463 |
+
className="flex items-center space-x-2 px-5 py-3 border-2 border-emerald-600 text-emerald-600 rounded-xl hover:bg-emerald-50 transition-colors"
|
| 464 |
+
>
|
| 465 |
+
<CheckCircle className="w-4 h-4" />
|
| 466 |
+
<span className="font-medium">Complete</span>
|
| 467 |
+
</button>
|
| 468 |
+
</>
|
| 469 |
+
) : (
|
| 470 |
+
<button className="flex items-center space-x-2 px-5 py-3 bg-emerald-600 text-white rounded-xl shadow-md">
|
| 471 |
+
<CheckCircle className="w-4 h-4" />
|
| 472 |
+
<span className="font-medium">Completed</span>
|
| 473 |
+
</button>
|
| 474 |
+
)}
|
| 475 |
+
</div>
|
| 476 |
+
</div>
|
| 477 |
+
</div>
|
| 478 |
+
</div>
|
| 479 |
+
</div>
|
| 480 |
+
))}
|
| 481 |
+
</div>
|
| 482 |
+
</div>
|
| 483 |
+
);
|
| 484 |
+
}
|
| 485 |
+
|
| 486 |
+
return (
|
| 487 |
+
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
|
| 488 |
+
<div className="mb-8">
|
| 489 |
+
<div className="flex items-center justify-between">
|
| 490 |
+
<div>
|
| 491 |
+
<h1 className="text-4xl font-bold text-gray-900 mb-3">Study Plans</h1>
|
| 492 |
+
<p className="text-gray-600 text-lg">Structured learning paths powered by Wikimedia content</p>
|
| 493 |
+
{studyPlans.length > 0 && (
|
| 494 |
+
<p className="text-primary-600 mt-2 font-medium">
|
| 495 |
+
{studyPlans.length} AI-generated plan{studyPlans.length > 1 ? 's' : ''} available
|
| 496 |
+
</p>
|
| 497 |
+
)}
|
| 498 |
+
</div>
|
| 499 |
+
|
| 500 |
+
<button
|
| 501 |
+
onClick={() => setShowCreateForm(true)}
|
| 502 |
+
className="flex items-center space-x-2 px-6 py-3 bg-primary-600 text-white rounded-xl hover:bg-primary-700 transition-colors shadow-md hover:shadow-lg"
|
| 503 |
+
>
|
| 504 |
+
<Plus className="w-5 h-5" />
|
| 505 |
+
<span className="font-medium">Create Plan</span>
|
| 506 |
+
</button>
|
| 507 |
+
</div>
|
| 508 |
+
</div>
|
| 509 |
+
|
| 510 |
+
{showCreateForm && (
|
| 511 |
+
<div className="mb-8">
|
| 512 |
+
<CreatePlanForm
|
| 513 |
+
onPlanCreated={onPlanCreated}
|
| 514 |
+
onCancel={() => setShowCreateForm(false)}
|
| 515 |
+
/>
|
| 516 |
+
</div>
|
| 517 |
+
)}
|
| 518 |
+
|
| 519 |
+
{studyPlans.length === 0 ? (
|
| 520 |
+
<div className="bg-white rounded-2xl p-12 text-center border border-gray-200 shadow-sm">
|
| 521 |
+
<BookOpen className="w-20 h-20 text-gray-300 mx-auto mb-6" />
|
| 522 |
+
<h3 className="text-2xl font-medium text-gray-900 mb-3">No Study Plans Yet</h3>
|
| 523 |
+
<p className="text-gray-600 mb-6 text-lg">
|
| 524 |
+
Create your first study plan or use the AI Generator to get started with personalized learning paths.
|
| 525 |
+
</p>
|
| 526 |
+
<button
|
| 527 |
+
onClick={() => setShowCreateForm(true)}
|
| 528 |
+
className="px-6 py-3 bg-primary-600 text-white rounded-xl hover:bg-primary-700 transition-colors font-medium"
|
| 529 |
+
>
|
| 530 |
+
Create Your First Plan
|
| 531 |
+
</button>
|
| 532 |
+
</div>
|
| 533 |
+
) : (
|
| 534 |
+
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8">
|
| 535 |
+
{studyPlans.map((plan) => {
|
| 536 |
+
const nextTopic = getNextTopic(plan);
|
| 537 |
+
return (
|
| 538 |
+
<div
|
| 539 |
+
key={plan.id}
|
| 540 |
+
onClick={() => setSelectedPlan(plan)}
|
| 541 |
+
className="bg-white rounded-2xl p-6 border border-gray-200 shadow-sm hover:shadow-lg transition-all duration-300 cursor-pointer hover:border-primary-200 group"
|
| 542 |
+
>
|
| 543 |
+
<div className="flex items-start justify-between mb-4">
|
| 544 |
+
<div className="flex-1">
|
| 545 |
+
<div className="flex items-center space-x-2 mb-3">
|
| 546 |
+
<h3 className="text-xl font-semibold text-gray-900 group-hover:text-primary-600 transition-colors">{plan.title}</h3>
|
| 547 |
+
<span className="px-2 py-1 bg-primary-100 text-primary-700 text-xs rounded-full font-medium border border-primary-200">
|
| 548 |
+
AI
|
| 549 |
+
</span>
|
| 550 |
+
</div>
|
| 551 |
+
<p className="text-gray-600 text-sm mb-4 leading-relaxed">{plan.description}</p>
|
| 552 |
+
|
| 553 |
+
<div className="flex items-center space-x-4 mb-4">
|
| 554 |
+
<span className={`px-3 py-1 rounded-full text-xs font-medium border ${getDifficultyColor(plan.difficulty)}`}>
|
| 555 |
+
{plan.difficulty.charAt(0).toUpperCase() + plan.difficulty.slice(1)}
|
| 556 |
+
</span>
|
| 557 |
+
<div className="flex items-center text-xs text-gray-600">
|
| 558 |
+
<Clock className="w-3 h-3 mr-1" />
|
| 559 |
+
<span>{plan.estimatedTime}</span>
|
| 560 |
+
</div>
|
| 561 |
+
</div>
|
| 562 |
+
|
| 563 |
+
<div className="mb-4">
|
| 564 |
+
<div className="flex items-center justify-between text-sm text-gray-600 mb-2">
|
| 565 |
+
<span>Progress</span>
|
| 566 |
+
<span className="font-medium">{getCompletionPercentage(plan.topics)}%</span>
|
| 567 |
+
</div>
|
| 568 |
+
<div className="w-full bg-gray-200 rounded-full h-2">
|
| 569 |
+
<div
|
| 570 |
+
className="bg-gradient-to-r from-primary-500 to-secondary-500 h-2 rounded-full transition-all duration-300"
|
| 571 |
+
style={{ width: `${getCompletionPercentage(plan.topics)}%` }}
|
| 572 |
+
/>
|
| 573 |
+
</div>
|
| 574 |
+
</div>
|
| 575 |
+
|
| 576 |
+
<div className="flex items-center justify-between text-sm text-gray-600">
|
| 577 |
+
<div className="flex items-center">
|
| 578 |
+
<BookOpen className="w-4 h-4 mr-1" />
|
| 579 |
+
<span>{plan.topics.length} topics</span>
|
| 580 |
+
</div>
|
| 581 |
+
{nextTopic && (
|
| 582 |
+
<div className="text-primary-600 font-medium">
|
| 583 |
+
Next: {nextTopic.title.substring(0, 15)}...
|
| 584 |
+
</div>
|
| 585 |
+
)}
|
| 586 |
+
</div>
|
| 587 |
+
</div>
|
| 588 |
+
|
| 589 |
+
<div className="ml-4">
|
| 590 |
+
<div className="w-14 h-14 bg-gradient-to-br from-primary-100 to-secondary-100 rounded-xl flex items-center justify-center border border-primary-200 group-hover:scale-110 transition-transform">
|
| 591 |
+
<Star className="w-7 h-7 text-primary-600" />
|
| 592 |
+
</div>
|
| 593 |
+
</div>
|
| 594 |
+
</div>
|
| 595 |
+
</div>
|
| 596 |
+
);
|
| 597 |
+
})}
|
| 598 |
+
</div>
|
| 599 |
+
)}
|
| 600 |
+
</div>
|
| 601 |
+
);
|
| 602 |
+
};
|
| 603 |
+
|
| 604 |
+
export default StudyPlansSection;
|
src/index.css
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
@tailwind base;
|
| 2 |
+
@tailwind components;
|
| 3 |
+
@tailwind utilities;
|
src/main.tsx
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { StrictMode } from 'react';
|
| 2 |
+
import { createRoot } from 'react-dom/client';
|
| 3 |
+
import App from './App.tsx';
|
| 4 |
+
import './index.css';
|
| 5 |
+
|
| 6 |
+
createRoot(document.getElementById('root')!).render(
|
| 7 |
+
<StrictMode>
|
| 8 |
+
<App />
|
| 9 |
+
</StrictMode>
|
| 10 |
+
);
|
src/types/index.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export interface WikimediaProject {
|
| 2 |
+
id: string;
|
| 3 |
+
name: string;
|
| 4 |
+
description: string;
|
| 5 |
+
apiUrl: string;
|
| 6 |
+
color: string;
|
| 7 |
+
icon: string;
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
export interface SearchResult {
|
| 11 |
+
title: string;
|
| 12 |
+
pageid: number;
|
| 13 |
+
size: number;
|
| 14 |
+
snippet: string;
|
| 15 |
+
timestamp: string;
|
| 16 |
+
project: string;
|
| 17 |
+
url: string;
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
export interface StudyPlan {
|
| 21 |
+
id: string;
|
| 22 |
+
title: string;
|
| 23 |
+
description: string;
|
| 24 |
+
topics: StudyTopic[];
|
| 25 |
+
estimatedTime: string;
|
| 26 |
+
difficulty: 'beginner' | 'intermediate' | 'advanced';
|
| 27 |
+
created: string;
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
export interface StudyTopic {
|
| 31 |
+
id: string;
|
| 32 |
+
title: string;
|
| 33 |
+
description: string;
|
| 34 |
+
content: string;
|
| 35 |
+
resources: Resource[];
|
| 36 |
+
completed: boolean;
|
| 37 |
+
estimatedTime: string;
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
export interface Resource {
|
| 41 |
+
title: string;
|
| 42 |
+
url: string;
|
| 43 |
+
type: 'article' | 'book' | 'video' | 'reference';
|
| 44 |
+
project: string;
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
export interface Quiz {
|
| 48 |
+
id: string;
|
| 49 |
+
title: string;
|
| 50 |
+
questions: Question[];
|
| 51 |
+
topic: string;
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
export interface Question {
|
| 55 |
+
id: string;
|
| 56 |
+
question: string;
|
| 57 |
+
options: string[];
|
| 58 |
+
correctAnswer: number;
|
| 59 |
+
explanation: string;
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
export interface ContentTransformation {
|
| 63 |
+
id: string;
|
| 64 |
+
sourceUrl: string;
|
| 65 |
+
sourceTitle: string;
|
| 66 |
+
transformationType: 'summary' | 'quiz' | 'outline' | 'flashcards';
|
| 67 |
+
content: any;
|
| 68 |
+
created: string;
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
export interface MultilingualResult {
|
| 72 |
+
language: string;
|
| 73 |
+
languageCode: string;
|
| 74 |
+
title: string;
|
| 75 |
+
snippet: string;
|
| 76 |
+
url: string;
|
| 77 |
+
wordCount: number;
|
| 78 |
+
lastModified: string;
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
export interface ProgressData {
|
| 82 |
+
studyStreak: number;
|
| 83 |
+
topicsCompleted: number;
|
| 84 |
+
totalStudyTime: number;
|
| 85 |
+
achievements: number;
|
| 86 |
+
weeklyGoal: {
|
| 87 |
+
current: number;
|
| 88 |
+
target: number;
|
| 89 |
+
};
|
| 90 |
+
recentActivity: ActivityItem[];
|
| 91 |
+
completedTopics: Set<string>;
|
| 92 |
+
lastStudyDate?: string;
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
export interface ActivityItem {
|
| 96 |
+
type: 'completed' | 'started' | 'achievement';
|
| 97 |
+
title: string;
|
| 98 |
+
course: string;
|
| 99 |
+
time: string;
|
| 100 |
+
icon: string;
|
| 101 |
+
color: string;
|
| 102 |
+
}
|
src/utils/ai-enhanced-wikimedia.ts
ADDED
|
@@ -0,0 +1,491 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { WikimediaAPI } from './wikimedia-api';
|
| 2 |
+
import { AIProviderManager } from './ai-providers';
|
| 3 |
+
import { StudyPlan, StudyTopic } from '../types';
|
| 4 |
+
|
| 5 |
+
export class AIEnhancedWikimedia extends WikimediaAPI {
|
| 6 |
+
|
| 7 |
+
static async generateEnhancedStudyPlan(topic: string, difficulty: 'beginner' | 'intermediate' | 'advanced' = 'beginner'): Promise<StudyPlan> {
|
| 8 |
+
try {
|
| 9 |
+
// First, get base content from Wikimedia
|
| 10 |
+
const searchResults = await this.searchMultipleProjects(topic, ['wikipedia', 'wikibooks', 'wikiversity'], 15);
|
| 11 |
+
|
| 12 |
+
if (searchResults.length === 0) {
|
| 13 |
+
throw new Error('No content found for this topic');
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
// Check if AI is available and configured
|
| 17 |
+
let useAI = false;
|
| 18 |
+
try {
|
| 19 |
+
const config = AIProviderManager.getConfig();
|
| 20 |
+
const provider = AIProviderManager.getProviderById(config.selectedProvider);
|
| 21 |
+
|
| 22 |
+
if (provider) {
|
| 23 |
+
// Check if API key is required and available
|
| 24 |
+
if (provider.requiresApiKey) {
|
| 25 |
+
useAI = !!(config.apiKeys && config.apiKeys[config.selectedProvider]);
|
| 26 |
+
} else {
|
| 27 |
+
useAI = true; // Local providers don't need API keys
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
// Test connection if we think AI should be available
|
| 31 |
+
if (useAI) {
|
| 32 |
+
useAI = await AIProviderManager.testConnection(config.selectedProvider);
|
| 33 |
+
}
|
| 34 |
+
}
|
| 35 |
+
} catch (error) {
|
| 36 |
+
console.log('AI availability check failed, using fallback');
|
| 37 |
+
useAI = false;
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
if (useAI) {
|
| 41 |
+
// Use AI to enhance the study plan
|
| 42 |
+
const aiPrompt = `Create a comprehensive study plan for "${topic}" at ${difficulty} level.
|
| 43 |
+
|
| 44 |
+
Available resources from Wikimedia:
|
| 45 |
+
${searchResults.slice(0, 8).map((result, i) => `${i + 1}. ${result.title} - ${result.snippet.substring(0, 100)}...`).join('\n')}
|
| 46 |
+
|
| 47 |
+
Please create a structured study plan with:
|
| 48 |
+
1. A compelling title and description
|
| 49 |
+
2. 5-8 learning topics in logical order
|
| 50 |
+
3. For each topic, provide:
|
| 51 |
+
- Clear learning objectives
|
| 52 |
+
- Estimated study time
|
| 53 |
+
- Key concepts to focus on
|
| 54 |
+
|
| 55 |
+
Format as JSON with this structure:
|
| 56 |
+
{
|
| 57 |
+
"title": "Study Plan Title",
|
| 58 |
+
"description": "Brief description",
|
| 59 |
+
"topics": [
|
| 60 |
+
{
|
| 61 |
+
"title": "Topic Title",
|
| 62 |
+
"description": "What students will learn",
|
| 63 |
+
"objectives": ["objective1", "objective2"],
|
| 64 |
+
"estimatedTime": "2 hours",
|
| 65 |
+
"keyPoints": ["point1", "point2"]
|
| 66 |
+
}
|
| 67 |
+
]
|
| 68 |
+
}`;
|
| 69 |
+
|
| 70 |
+
try {
|
| 71 |
+
const aiResponse = await AIProviderManager.generateText(aiPrompt, {
|
| 72 |
+
maxTokens: 2000,
|
| 73 |
+
temperature: 0.7,
|
| 74 |
+
systemPrompt: 'You are an expert educational content creator. Create well-structured, engaging study plans that help students learn effectively.'
|
| 75 |
+
});
|
| 76 |
+
|
| 77 |
+
// Parse AI response
|
| 78 |
+
let aiPlan;
|
| 79 |
+
try {
|
| 80 |
+
// Extract JSON from AI response
|
| 81 |
+
const jsonMatch = aiResponse.match(/\{[\s\S]*\}/);
|
| 82 |
+
if (jsonMatch) {
|
| 83 |
+
aiPlan = JSON.parse(jsonMatch[0]);
|
| 84 |
+
} else {
|
| 85 |
+
throw new Error('No valid JSON found in AI response');
|
| 86 |
+
}
|
| 87 |
+
} catch (parseError) {
|
| 88 |
+
console.log('Failed to parse AI response, using fallback');
|
| 89 |
+
return await this.generateStudyPlan(topic, difficulty);
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
// Create enhanced study plan with real Wikimedia resources
|
| 93 |
+
const enhancedTopics: StudyTopic[] = [];
|
| 94 |
+
|
| 95 |
+
for (let i = 0; i < Math.min(aiPlan.topics.length, searchResults.length); i++) {
|
| 96 |
+
const aiTopic = aiPlan.topics[i];
|
| 97 |
+
const resource = searchResults[i];
|
| 98 |
+
|
| 99 |
+
// Get detailed content for each topic
|
| 100 |
+
let detailedContent = resource.snippet;
|
| 101 |
+
try {
|
| 102 |
+
const fullContent = await this.getPageSnippet(resource.title, resource.project);
|
| 103 |
+
if (fullContent && fullContent !== 'No description available') {
|
| 104 |
+
detailedContent = fullContent;
|
| 105 |
+
}
|
| 106 |
+
} catch (error) {
|
| 107 |
+
console.log(`Could not get detailed content for ${resource.title}`);
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
enhancedTopics.push({
|
| 111 |
+
id: `topic-${Date.now()}-${i}`,
|
| 112 |
+
title: aiTopic.title || resource.title,
|
| 113 |
+
description: aiTopic.description || detailedContent.substring(0, 200) + '...',
|
| 114 |
+
content: detailedContent,
|
| 115 |
+
completed: false,
|
| 116 |
+
estimatedTime: aiTopic.estimatedTime || `${Math.ceil(Math.random() * 2 + 1)} hours`,
|
| 117 |
+
resources: [
|
| 118 |
+
{
|
| 119 |
+
title: resource.title,
|
| 120 |
+
url: resource.url,
|
| 121 |
+
type: 'article' as const,
|
| 122 |
+
project: resource.project
|
| 123 |
+
}
|
| 124 |
+
]
|
| 125 |
+
});
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
const studyPlan: StudyPlan = {
|
| 129 |
+
id: `ai-plan-${Date.now()}`,
|
| 130 |
+
title: aiPlan.title || `${topic} Study Plan`,
|
| 131 |
+
description: aiPlan.description || `AI-enhanced ${difficulty} level study plan for ${topic}`,
|
| 132 |
+
difficulty,
|
| 133 |
+
estimatedTime: this.estimateStudyTime(enhancedTopics.length, difficulty),
|
| 134 |
+
created: new Date().toISOString(),
|
| 135 |
+
topics: enhancedTopics
|
| 136 |
+
};
|
| 137 |
+
|
| 138 |
+
return studyPlan;
|
| 139 |
+
} catch (aiError) {
|
| 140 |
+
console.log('AI enhancement failed, using fallback method:', aiError);
|
| 141 |
+
return await this.generateStudyPlan(topic, difficulty);
|
| 142 |
+
}
|
| 143 |
+
} else {
|
| 144 |
+
// AI not available, use standard method
|
| 145 |
+
return await this.generateStudyPlan(topic, difficulty);
|
| 146 |
+
}
|
| 147 |
+
} catch (error) {
|
| 148 |
+
console.error('Enhanced study plan generation failed:', error);
|
| 149 |
+
// Fallback to original method
|
| 150 |
+
return await this.generateStudyPlan(topic, difficulty);
|
| 151 |
+
}
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
static async generateQuizFromContent(content: string, title: string): Promise<any> {
|
| 155 |
+
// Check AI availability first
|
| 156 |
+
let useAI = false;
|
| 157 |
+
try {
|
| 158 |
+
const config = AIProviderManager.getConfig();
|
| 159 |
+
const provider = AIProviderManager.getProviderById(config.selectedProvider);
|
| 160 |
+
|
| 161 |
+
if (provider) {
|
| 162 |
+
if (provider.requiresApiKey) {
|
| 163 |
+
useAI = !!(config.apiKeys && config.apiKeys[config.selectedProvider]);
|
| 164 |
+
} else {
|
| 165 |
+
useAI = true;
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
if (useAI) {
|
| 169 |
+
useAI = await AIProviderManager.testConnection(config.selectedProvider);
|
| 170 |
+
}
|
| 171 |
+
}
|
| 172 |
+
} catch (error) {
|
| 173 |
+
useAI = false;
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
if (!useAI) {
|
| 177 |
+
return this.generateFallbackQuiz(title);
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
const aiPrompt = `Create a quiz based on this content about "${title}":
|
| 181 |
+
|
| 182 |
+
${content.substring(0, 2000)}
|
| 183 |
+
|
| 184 |
+
Generate 5 multiple-choice questions that test understanding of key concepts. Format as JSON:
|
| 185 |
+
{
|
| 186 |
+
"questions": [
|
| 187 |
+
{
|
| 188 |
+
"question": "Question text",
|
| 189 |
+
"options": ["Option A", "Option B", "Option C", "Option D"],
|
| 190 |
+
"correct": 0,
|
| 191 |
+
"explanation": "Why this answer is correct"
|
| 192 |
+
}
|
| 193 |
+
]
|
| 194 |
+
}`;
|
| 195 |
+
|
| 196 |
+
try {
|
| 197 |
+
const aiResponse = await AIProviderManager.generateText(aiPrompt, {
|
| 198 |
+
maxTokens: 1500,
|
| 199 |
+
temperature: 0.5,
|
| 200 |
+
systemPrompt: 'You are an expert quiz creator. Create challenging but fair questions that test real understanding.'
|
| 201 |
+
});
|
| 202 |
+
|
| 203 |
+
const jsonMatch = aiResponse.match(/\{[\s\S]*\}/);
|
| 204 |
+
if (jsonMatch) {
|
| 205 |
+
const quiz = JSON.parse(jsonMatch[0]);
|
| 206 |
+
return {
|
| 207 |
+
type: 'quiz',
|
| 208 |
+
title: `Quiz: ${title}`,
|
| 209 |
+
questions: quiz.questions
|
| 210 |
+
};
|
| 211 |
+
}
|
| 212 |
+
} catch (error) {
|
| 213 |
+
console.error('AI quiz generation failed:', error);
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
// Fallback to rule-based quiz generation
|
| 217 |
+
return this.generateFallbackQuiz(title);
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
static async generateSummaryFromContent(content: string, title: string): Promise<any> {
|
| 221 |
+
// Check AI availability
|
| 222 |
+
let useAI = false;
|
| 223 |
+
try {
|
| 224 |
+
const config = AIProviderManager.getConfig();
|
| 225 |
+
const provider = AIProviderManager.getProviderById(config.selectedProvider);
|
| 226 |
+
|
| 227 |
+
if (provider) {
|
| 228 |
+
if (provider.requiresApiKey) {
|
| 229 |
+
useAI = !!(config.apiKeys && config.apiKeys[config.selectedProvider]);
|
| 230 |
+
} else {
|
| 231 |
+
useAI = true;
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
if (useAI) {
|
| 235 |
+
useAI = await AIProviderManager.testConnection(config.selectedProvider);
|
| 236 |
+
}
|
| 237 |
+
}
|
| 238 |
+
} catch (error) {
|
| 239 |
+
useAI = false;
|
| 240 |
+
}
|
| 241 |
+
|
| 242 |
+
if (!useAI) {
|
| 243 |
+
return this.generateFallbackSummary(content, title);
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
const aiPrompt = `Summarize this content about "${title}" in a clear, educational way:
|
| 247 |
+
|
| 248 |
+
${content.substring(0, 3000)}
|
| 249 |
+
|
| 250 |
+
Create:
|
| 251 |
+
1. A concise summary (2-3 paragraphs)
|
| 252 |
+
2. 5-7 key points that capture the most important information
|
| 253 |
+
|
| 254 |
+
Format as JSON:
|
| 255 |
+
{
|
| 256 |
+
"summary": "Main summary text",
|
| 257 |
+
"keyPoints": ["Point 1", "Point 2", ...]
|
| 258 |
+
}`;
|
| 259 |
+
|
| 260 |
+
try {
|
| 261 |
+
const aiResponse = await AIProviderManager.generateText(aiPrompt, {
|
| 262 |
+
maxTokens: 1000,
|
| 263 |
+
temperature: 0.3,
|
| 264 |
+
systemPrompt: 'You are an expert at creating clear, educational summaries that help students understand complex topics.'
|
| 265 |
+
});
|
| 266 |
+
|
| 267 |
+
const jsonMatch = aiResponse.match(/\{[\s\S]*\}/);
|
| 268 |
+
if (jsonMatch) {
|
| 269 |
+
const summary = JSON.parse(jsonMatch[0]);
|
| 270 |
+
return {
|
| 271 |
+
type: 'summary',
|
| 272 |
+
title: `Summary: ${title}`,
|
| 273 |
+
content: summary.summary,
|
| 274 |
+
keyPoints: summary.keyPoints
|
| 275 |
+
};
|
| 276 |
+
}
|
| 277 |
+
} catch (error) {
|
| 278 |
+
console.error('AI summary generation failed:', error);
|
| 279 |
+
}
|
| 280 |
+
|
| 281 |
+
// Fallback to rule-based summary
|
| 282 |
+
return this.generateFallbackSummary(content, title);
|
| 283 |
+
}
|
| 284 |
+
|
| 285 |
+
static async generateStudyOutline(content: string, title: string): Promise<any> {
|
| 286 |
+
// Check AI availability
|
| 287 |
+
let useAI = false;
|
| 288 |
+
try {
|
| 289 |
+
const config = AIProviderManager.getConfig();
|
| 290 |
+
const provider = AIProviderManager.getProviderById(config.selectedProvider);
|
| 291 |
+
|
| 292 |
+
if (provider) {
|
| 293 |
+
if (provider.requiresApiKey) {
|
| 294 |
+
useAI = !!(config.apiKeys && config.apiKeys[config.selectedProvider]);
|
| 295 |
+
} else {
|
| 296 |
+
useAI = true;
|
| 297 |
+
}
|
| 298 |
+
|
| 299 |
+
if (useAI) {
|
| 300 |
+
useAI = await AIProviderManager.testConnection(config.selectedProvider);
|
| 301 |
+
}
|
| 302 |
+
}
|
| 303 |
+
} catch (error) {
|
| 304 |
+
useAI = false;
|
| 305 |
+
}
|
| 306 |
+
|
| 307 |
+
if (!useAI) {
|
| 308 |
+
return this.generateFallbackOutline(title);
|
| 309 |
+
}
|
| 310 |
+
|
| 311 |
+
const aiPrompt = `Create a detailed study outline for "${title}" based on this content:
|
| 312 |
+
|
| 313 |
+
${content.substring(0, 2500)}
|
| 314 |
+
|
| 315 |
+
Organize into 4-6 main sections with subsections. Format as JSON:
|
| 316 |
+
{
|
| 317 |
+
"sections": [
|
| 318 |
+
{
|
| 319 |
+
"title": "Section Title",
|
| 320 |
+
"points": ["Point 1", "Point 2", "Point 3"]
|
| 321 |
+
}
|
| 322 |
+
]
|
| 323 |
+
}`;
|
| 324 |
+
|
| 325 |
+
try {
|
| 326 |
+
const aiResponse = await AIProviderManager.generateText(aiPrompt, {
|
| 327 |
+
maxTokens: 1200,
|
| 328 |
+
temperature: 0.4,
|
| 329 |
+
systemPrompt: 'You are an expert at creating structured study outlines that help students organize their learning.'
|
| 330 |
+
});
|
| 331 |
+
|
| 332 |
+
const jsonMatch = aiResponse.match(/\{[\s\S]*\}/);
|
| 333 |
+
if (jsonMatch) {
|
| 334 |
+
const outline = JSON.parse(jsonMatch[0]);
|
| 335 |
+
return {
|
| 336 |
+
type: 'outline',
|
| 337 |
+
title: `Study Outline: ${title}`,
|
| 338 |
+
sections: outline.sections
|
| 339 |
+
};
|
| 340 |
+
}
|
| 341 |
+
} catch (error) {
|
| 342 |
+
console.error('AI outline generation failed:', error);
|
| 343 |
+
}
|
| 344 |
+
|
| 345 |
+
// Fallback to rule-based outline
|
| 346 |
+
return this.generateFallbackOutline(title);
|
| 347 |
+
}
|
| 348 |
+
|
| 349 |
+
static async generateFlashcards(content: string, title: string): Promise<any> {
|
| 350 |
+
// Check AI availability
|
| 351 |
+
let useAI = false;
|
| 352 |
+
try {
|
| 353 |
+
const config = AIProviderManager.getConfig();
|
| 354 |
+
const provider = AIProviderManager.getProviderById(config.selectedProvider);
|
| 355 |
+
|
| 356 |
+
if (provider) {
|
| 357 |
+
if (provider.requiresApiKey) {
|
| 358 |
+
useAI = !!(config.apiKeys && config.apiKeys[config.selectedProvider]);
|
| 359 |
+
} else {
|
| 360 |
+
useAI = true;
|
| 361 |
+
}
|
| 362 |
+
|
| 363 |
+
if (useAI) {
|
| 364 |
+
useAI = await AIProviderManager.testConnection(config.selectedProvider);
|
| 365 |
+
}
|
| 366 |
+
}
|
| 367 |
+
} catch (error) {
|
| 368 |
+
useAI = false;
|
| 369 |
+
}
|
| 370 |
+
|
| 371 |
+
if (!useAI) {
|
| 372 |
+
return this.generateFallbackFlashcards(title);
|
| 373 |
+
}
|
| 374 |
+
|
| 375 |
+
const aiPrompt = `Create flashcards for studying "${title}" based on this content:
|
| 376 |
+
|
| 377 |
+
${content.substring(0, 2000)}
|
| 378 |
+
|
| 379 |
+
Generate 6-8 flashcards with questions and answers. Format as JSON:
|
| 380 |
+
{
|
| 381 |
+
"cards": [
|
| 382 |
+
{
|
| 383 |
+
"front": "Question or term",
|
| 384 |
+
"back": "Answer or definition"
|
| 385 |
+
}
|
| 386 |
+
]
|
| 387 |
+
}`;
|
| 388 |
+
|
| 389 |
+
try {
|
| 390 |
+
const aiResponse = await AIProviderManager.generateText(aiPrompt, {
|
| 391 |
+
maxTokens: 1000,
|
| 392 |
+
temperature: 0.5,
|
| 393 |
+
systemPrompt: 'You are an expert at creating effective flashcards that help with memorization and understanding.'
|
| 394 |
+
});
|
| 395 |
+
|
| 396 |
+
const jsonMatch = aiResponse.match(/\{[\s\S]*\}/);
|
| 397 |
+
if (jsonMatch) {
|
| 398 |
+
const flashcards = JSON.parse(jsonMatch[0]);
|
| 399 |
+
return {
|
| 400 |
+
type: 'flashcards',
|
| 401 |
+
title: `Flashcards: ${title}`,
|
| 402 |
+
cards: flashcards.cards
|
| 403 |
+
};
|
| 404 |
+
}
|
| 405 |
+
} catch (error) {
|
| 406 |
+
console.error('AI flashcard generation failed:', error);
|
| 407 |
+
}
|
| 408 |
+
|
| 409 |
+
// Fallback to rule-based flashcards
|
| 410 |
+
return this.generateFallbackFlashcards(title);
|
| 411 |
+
}
|
| 412 |
+
|
| 413 |
+
// Fallback methods (existing rule-based generation)
|
| 414 |
+
private static generateFallbackQuiz(title: string) {
|
| 415 |
+
const questions = [
|
| 416 |
+
{
|
| 417 |
+
question: `What is the main topic discussed in the article about ${title}?`,
|
| 418 |
+
options: [
|
| 419 |
+
`The fundamental concepts and principles of ${title}`,
|
| 420 |
+
`The historical development of ${title}`,
|
| 421 |
+
`The practical applications of ${title}`,
|
| 422 |
+
`The criticism and controversies surrounding ${title}`
|
| 423 |
+
],
|
| 424 |
+
correct: 0,
|
| 425 |
+
explanation: 'This question tests basic comprehension of the main topic.'
|
| 426 |
+
}
|
| 427 |
+
];
|
| 428 |
+
|
| 429 |
+
return {
|
| 430 |
+
type: 'quiz',
|
| 431 |
+
title: `Quiz: ${title}`,
|
| 432 |
+
questions
|
| 433 |
+
};
|
| 434 |
+
}
|
| 435 |
+
|
| 436 |
+
private static generateFallbackSummary(content: string, title: string) {
|
| 437 |
+
const sentences = content.split('.').filter(s => s.trim().length > 20);
|
| 438 |
+
const keySentences = sentences.slice(0, 5);
|
| 439 |
+
|
| 440 |
+
return {
|
| 441 |
+
type: 'summary',
|
| 442 |
+
title: `Summary: ${title}`,
|
| 443 |
+
content: keySentences.join('. ') + '.',
|
| 444 |
+
keyPoints: sentences.slice(5, 10).map(s => s.trim()).filter(s => s.length > 0)
|
| 445 |
+
};
|
| 446 |
+
}
|
| 447 |
+
|
| 448 |
+
private static generateFallbackOutline(title: string) {
|
| 449 |
+
return {
|
| 450 |
+
type: 'outline',
|
| 451 |
+
title: `Study Outline: ${title}`,
|
| 452 |
+
sections: [
|
| 453 |
+
{
|
| 454 |
+
title: 'Introduction',
|
| 455 |
+
points: [
|
| 456 |
+
`Overview and definition of ${title}`,
|
| 457 |
+
'Historical context and background',
|
| 458 |
+
'Key terminology and concepts'
|
| 459 |
+
]
|
| 460 |
+
},
|
| 461 |
+
{
|
| 462 |
+
title: 'Main Concepts',
|
| 463 |
+
points: [
|
| 464 |
+
'Core principles and theories',
|
| 465 |
+
'Important characteristics and features',
|
| 466 |
+
'Fundamental mechanisms and processes'
|
| 467 |
+
]
|
| 468 |
+
}
|
| 469 |
+
]
|
| 470 |
+
};
|
| 471 |
+
}
|
| 472 |
+
|
| 473 |
+
private static generateFallbackFlashcards(title: string) {
|
| 474 |
+
const cards = [
|
| 475 |
+
{
|
| 476 |
+
front: `What is ${title}?`,
|
| 477 |
+
back: `${title} is a comprehensive topic that encompasses various concepts, principles, and applications within its field of study.`
|
| 478 |
+
},
|
| 479 |
+
{
|
| 480 |
+
front: 'Key characteristics',
|
| 481 |
+
back: `The main features include fundamental principles, practical applications, and significant impact on related areas.`
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const token = import.meta.env.VITE_HF_TOKEN;
|
| 2 |
+
|
| 3 |
+
// Open Source AI Providers Integration
|
| 4 |
+
export interface AIProvider {
|
| 5 |
+
id: string;
|
| 6 |
+
name: string;
|
| 7 |
+
description: string;
|
| 8 |
+
apiUrl: string;
|
| 9 |
+
requiresApiKey: boolean;
|
| 10 |
+
models: AIModel[];
|
| 11 |
+
isLocal: boolean;
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
export interface AIModel {
|
| 15 |
+
id: string;
|
| 16 |
+
name: string;
|
| 17 |
+
description: string;
|
| 18 |
+
maxTokens: number;
|
| 19 |
+
capabilities: ('text-generation' | 'summarization' | 'question-answering')[];
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
export const AI_PROVIDERS: AIProvider[] = [
|
| 23 |
+
{
|
| 24 |
+
id: 'huggingface',
|
| 25 |
+
name: 'Hugging Face',
|
| 26 |
+
description: 'Access thousands of open-source models',
|
| 27 |
+
apiUrl: 'https://api-inference.huggingface.co',
|
| 28 |
+
requiresApiKey: true,
|
| 29 |
+
isLocal: false,
|
| 30 |
+
models: [
|
| 31 |
+
{
|
| 32 |
+
id: 'microsoft/DialoGPT-medium',
|
| 33 |
+
name: 'DialoGPT Medium',
|
| 34 |
+
description: 'Conversational AI model for text generation',
|
| 35 |
+
maxTokens: 1024,
|
| 36 |
+
capabilities: ['text-generation']
|
| 37 |
+
},
|
| 38 |
+
{
|
| 39 |
+
id: 'facebook/bart-large-cnn',
|
| 40 |
+
name: 'BART CNN',
|
| 41 |
+
description: 'Summarization specialist',
|
| 42 |
+
maxTokens: 1024,
|
| 43 |
+
capabilities: ['summarization']
|
| 44 |
+
},
|
| 45 |
+
{
|
| 46 |
+
id: 'deepset/roberta-base-squad2',
|
| 47 |
+
name: 'RoBERTa QA',
|
| 48 |
+
description: 'Question answering model',
|
| 49 |
+
maxTokens: 512,
|
| 50 |
+
capabilities: ['question-answering']
|
| 51 |
+
},
|
| 52 |
+
{
|
| 53 |
+
id: 'google/flan-t5-base',
|
| 54 |
+
name: 'FLAN-T5 Base',
|
| 55 |
+
description: 'Instruction-tuned text-to-text model',
|
| 56 |
+
maxTokens: 512,
|
| 57 |
+
capabilities: ['text-generation', 'summarization', 'question-answering']
|
| 58 |
+
}
|
| 59 |
+
]
|
| 60 |
+
},
|
| 61 |
+
{
|
| 62 |
+
id: 'ollama',
|
| 63 |
+
name: 'Ollama (Local)',
|
| 64 |
+
description: 'Run AI models locally on your machine',
|
| 65 |
+
apiUrl: 'http://localhost:11434',
|
| 66 |
+
requiresApiKey: false,
|
| 67 |
+
isLocal: true,
|
| 68 |
+
models: [
|
| 69 |
+
{
|
| 70 |
+
id: 'llama3.2',
|
| 71 |
+
name: 'Llama 3.2',
|
| 72 |
+
description: 'Meta\'s latest open-source model',
|
| 73 |
+
maxTokens: 8192,
|
| 74 |
+
capabilities: ['text-generation', 'summarization', 'question-answering']
|
| 75 |
+
},
|
| 76 |
+
{
|
| 77 |
+
id: 'mistral',
|
| 78 |
+
name: 'Mistral 7B',
|
| 79 |
+
description: 'Efficient open-source model',
|
| 80 |
+
maxTokens: 4096,
|
| 81 |
+
capabilities: ['text-generation', 'summarization']
|
| 82 |
+
},
|
| 83 |
+
{
|
| 84 |
+
id: 'codellama',
|
| 85 |
+
name: 'Code Llama',
|
| 86 |
+
description: 'Specialized for code and technical content',
|
| 87 |
+
maxTokens: 4096,
|
| 88 |
+
capabilities: ['text-generation', 'question-answering']
|
| 89 |
+
}
|
| 90 |
+
]
|
| 91 |
+
},
|
| 92 |
+
{
|
| 93 |
+
id: 'openai-compatible',
|
| 94 |
+
name: 'OpenAI Compatible',
|
| 95 |
+
description: 'Any OpenAI-compatible API (LocalAI, vLLM, etc.)',
|
| 96 |
+
apiUrl: 'http://localhost:8080',
|
| 97 |
+
requiresApiKey: false,
|
| 98 |
+
isLocal: true,
|
| 99 |
+
models: [
|
| 100 |
+
{
|
| 101 |
+
id: 'gpt-3.5-turbo',
|
| 102 |
+
name: 'GPT-3.5 Turbo (Local)',
|
| 103 |
+
description: 'Local implementation of GPT-3.5',
|
| 104 |
+
maxTokens: 4096,
|
| 105 |
+
capabilities: ['text-generation', 'summarization', 'question-answering']
|
| 106 |
+
}
|
| 107 |
+
]
|
| 108 |
+
},
|
| 109 |
+
{
|
| 110 |
+
id: 'together',
|
| 111 |
+
name: 'Together AI',
|
| 112 |
+
description: 'Open-source models via Together AI',
|
| 113 |
+
apiUrl: 'https://api.together.xyz',
|
| 114 |
+
requiresApiKey: true,
|
| 115 |
+
isLocal: false,
|
| 116 |
+
models: [
|
| 117 |
+
{
|
| 118 |
+
id: 'meta-llama/Llama-2-7b-chat-hf',
|
| 119 |
+
name: 'Llama 2 7B Chat',
|
| 120 |
+
description: 'Meta\'s Llama 2 model',
|
| 121 |
+
maxTokens: 4096,
|
| 122 |
+
capabilities: ['text-generation', 'summarization', 'question-answering']
|
| 123 |
+
},
|
| 124 |
+
{
|
| 125 |
+
id: 'mistralai/Mistral-7B-Instruct-v0.1',
|
| 126 |
+
name: 'Mistral 7B Instruct',
|
| 127 |
+
description: 'Mistral\'s instruction-tuned model',
|
| 128 |
+
maxTokens: 8192,
|
| 129 |
+
capabilities: ['text-generation', 'summarization', 'question-answering']
|
| 130 |
+
}
|
| 131 |
+
]
|
| 132 |
+
}
|
| 133 |
+
];
|
| 134 |
+
|
| 135 |
+
export class AIProviderManager {
|
| 136 |
+
private static getStoredConfig(): any {
|
| 137 |
+
try {
|
| 138 |
+
const stored = localStorage.getItem('wikistro-ai-config');
|
| 139 |
+
const defaultConfig = {
|
| 140 |
+
selectedProvider: 'huggingface',
|
| 141 |
+
selectedModel: 'microsoft/DialoGPT-medium',
|
| 142 |
+
apiKeys: {
|
| 143 |
+
huggingface: token
|
| 144 |
+
},
|
| 145 |
+
customEndpoints: {}
|
| 146 |
+
};
|
| 147 |
+
|
| 148 |
+
if (!stored) {
|
| 149 |
+
// Auto-configure with Hugging Face
|
| 150 |
+
this.saveConfig(defaultConfig);
|
| 151 |
+
return defaultConfig;
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
const config = JSON.parse(stored);
|
| 155 |
+
|
| 156 |
+
// Auto-add Hugging Face API key if not present
|
| 157 |
+
if (!config.apiKeys?.huggingface) {
|
| 158 |
+
config.apiKeys = config.apiKeys || {};
|
| 159 |
+
config.apiKeys.huggingface = token;
|
| 160 |
+
config.selectedProvider = 'huggingface';
|
| 161 |
+
config.selectedModel = 'microsoft/DialoGPT-medium';
|
| 162 |
+
this.saveConfig(config);
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
return config;
|
| 166 |
+
} catch (error) {
|
| 167 |
+
const defaultConfig = {
|
| 168 |
+
selectedProvider: 'huggingface',
|
| 169 |
+
selectedModel: 'microsoft/DialoGPT-medium',
|
| 170 |
+
apiKeys: {
|
| 171 |
+
huggingface: token
|
| 172 |
+
},
|
| 173 |
+
customEndpoints: {}
|
| 174 |
+
};
|
| 175 |
+
this.saveConfig(defaultConfig);
|
| 176 |
+
return defaultConfig;
|
| 177 |
+
}
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
private static saveConfig(config: any): void {
|
| 181 |
+
try {
|
| 182 |
+
localStorage.setItem('wikistro-ai-config', JSON.stringify(config));
|
| 183 |
+
} catch (error) {
|
| 184 |
+
console.error('Failed to save AI config:', error);
|
| 185 |
+
}
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
static getConfig() {
|
| 189 |
+
return this.getStoredConfig();
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
static getProviderById(id: string): AIProvider | undefined {
|
| 193 |
+
return AI_PROVIDERS.find(p => p.id === id);
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
static updateConfig(updates: any) {
|
| 197 |
+
const config = this.getStoredConfig();
|
| 198 |
+
const newConfig = { ...config, ...updates };
|
| 199 |
+
this.saveConfig(newConfig);
|
| 200 |
+
return newConfig;
|
| 201 |
+
}
|
| 202 |
+
|
| 203 |
+
static async generateText(prompt: string, options: {
|
| 204 |
+
maxTokens?: number;
|
| 205 |
+
temperature?: number;
|
| 206 |
+
systemPrompt?: string;
|
| 207 |
+
} = {}): Promise<string> {
|
| 208 |
+
const config = this.getStoredConfig();
|
| 209 |
+
const provider = AI_PROVIDERS.find(p => p.id === config.selectedProvider);
|
| 210 |
+
const model = provider?.models.find(m => m.id === config.selectedModel);
|
| 211 |
+
|
| 212 |
+
if (!provider || !model) {
|
| 213 |
+
throw new Error('No AI provider or model configured');
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
const { maxTokens = 1000, temperature = 0.7, systemPrompt } = options;
|
| 217 |
+
|
| 218 |
+
try {
|
| 219 |
+
switch (provider.id) {
|
| 220 |
+
case 'ollama':
|
| 221 |
+
return await this.callOllama(config.selectedModel, prompt, { maxTokens, temperature, systemPrompt });
|
| 222 |
+
|
| 223 |
+
case 'huggingface':
|
| 224 |
+
return await this.callHuggingFace(config.selectedModel, prompt, config.apiKeys.huggingface);
|
| 225 |
+
|
| 226 |
+
case 'openai-compatible':
|
| 227 |
+
return await this.callOpenAICompatible(
|
| 228 |
+
config.customEndpoints.openai || provider.apiUrl,
|
| 229 |
+
config.selectedModel,
|
| 230 |
+
prompt,
|
| 231 |
+
{ maxTokens, temperature, systemPrompt }
|
| 232 |
+
);
|
| 233 |
+
|
| 234 |
+
case 'together':
|
| 235 |
+
return await this.callTogether(config.selectedModel, prompt, config.apiKeys.together, { maxTokens, temperature, systemPrompt });
|
| 236 |
+
|
| 237 |
+
default:
|
| 238 |
+
throw new Error(`Unsupported provider: ${provider.id}`);
|
| 239 |
+
}
|
| 240 |
+
} catch (error) {
|
| 241 |
+
console.error('AI generation failed:', error);
|
| 242 |
+
throw new Error(`AI generation failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
| 243 |
+
}
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
private static async callOllama(model: string, prompt: string, options: any): Promise<string> {
|
| 247 |
+
const response = await fetch('http://localhost:11434/api/generate', {
|
| 248 |
+
method: 'POST',
|
| 249 |
+
headers: { 'Content-Type': 'application/json' },
|
| 250 |
+
body: JSON.stringify({
|
| 251 |
+
model,
|
| 252 |
+
prompt: options.systemPrompt ? `${options.systemPrompt}\n\n${prompt}` : prompt,
|
| 253 |
+
stream: false,
|
| 254 |
+
options: {
|
| 255 |
+
temperature: options.temperature,
|
| 256 |
+
num_predict: options.maxTokens
|
| 257 |
+
}
|
| 258 |
+
})
|
| 259 |
+
});
|
| 260 |
+
|
| 261 |
+
if (!response.ok) {
|
| 262 |
+
throw new Error(`Ollama API error: ${response.statusText}`);
|
| 263 |
+
}
|
| 264 |
+
|
| 265 |
+
const data = await response.json();
|
| 266 |
+
return data.response || '';
|
| 267 |
+
}
|
| 268 |
+
|
| 269 |
+
private static async callHuggingFace(model: string, prompt: string, apiKey: string): Promise<string> {
|
| 270 |
+
if (!apiKey) {
|
| 271 |
+
throw new Error('Hugging Face API key required');
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
+
// For text generation models, use the appropriate format
|
| 275 |
+
let requestBody;
|
| 276 |
+
let endpoint = `https://api-inference.huggingface.co/models/${model}`;
|
| 277 |
+
|
| 278 |
+
if (model.includes('DialoGPT') || model.includes('flan-t5')) {
|
| 279 |
+
requestBody = {
|
| 280 |
+
inputs: prompt,
|
| 281 |
+
parameters: {
|
| 282 |
+
max_new_tokens: 200,
|
| 283 |
+
temperature: 0.7,
|
| 284 |
+
do_sample: true,
|
| 285 |
+
return_full_text: false
|
| 286 |
+
}
|
| 287 |
+
};
|
| 288 |
+
} else {
|
| 289 |
+
requestBody = { inputs: prompt };
|
| 290 |
+
}
|
| 291 |
+
|
| 292 |
+
const response = await fetch(endpoint, {
|
| 293 |
+
method: 'POST',
|
| 294 |
+
headers: {
|
| 295 |
+
'Authorization': `Bearer ${apiKey}`,
|
| 296 |
+
'Content-Type': 'application/json'
|
| 297 |
+
},
|
| 298 |
+
body: JSON.stringify(requestBody)
|
| 299 |
+
});
|
| 300 |
+
|
| 301 |
+
if (!response.ok) {
|
| 302 |
+
const errorText = await response.text();
|
| 303 |
+
throw new Error(`Hugging Face API error: ${response.statusText} - ${errorText}`);
|
| 304 |
+
}
|
| 305 |
+
|
| 306 |
+
const data = await response.json();
|
| 307 |
+
|
| 308 |
+
// Handle different response formats
|
| 309 |
+
if (Array.isArray(data)) {
|
| 310 |
+
return data[0]?.generated_text || data[0]?.summary_text || '';
|
| 311 |
+
} else if (data.generated_text) {
|
| 312 |
+
return data.generated_text;
|
| 313 |
+
} else if (data[0]?.generated_text) {
|
| 314 |
+
return data[0].generated_text;
|
| 315 |
+
}
|
| 316 |
+
|
| 317 |
+
return JSON.stringify(data);
|
| 318 |
+
}
|
| 319 |
+
|
| 320 |
+
private static async callOpenAICompatible(endpoint: string, model: string, prompt: string, options: any): Promise<string> {
|
| 321 |
+
const messages = [];
|
| 322 |
+
if (options.systemPrompt) {
|
| 323 |
+
messages.push({ role: 'system', content: options.systemPrompt });
|
| 324 |
+
}
|
| 325 |
+
messages.push({ role: 'user', content: prompt });
|
| 326 |
+
|
| 327 |
+
const response = await fetch(`${endpoint}/v1/chat/completions`, {
|
| 328 |
+
method: 'POST',
|
| 329 |
+
headers: { 'Content-Type': 'application/json' },
|
| 330 |
+
body: JSON.stringify({
|
| 331 |
+
model,
|
| 332 |
+
messages,
|
| 333 |
+
max_tokens: options.maxTokens,
|
| 334 |
+
temperature: options.temperature
|
| 335 |
+
})
|
| 336 |
+
});
|
| 337 |
+
|
| 338 |
+
if (!response.ok) {
|
| 339 |
+
throw new Error(`OpenAI Compatible API error: ${response.statusText}`);
|
| 340 |
+
}
|
| 341 |
+
|
| 342 |
+
const data = await response.json();
|
| 343 |
+
return data.choices?.[0]?.message?.content || '';
|
| 344 |
+
}
|
| 345 |
+
|
| 346 |
+
private static async callTogether(model: string, prompt: string, apiKey: string, options: any): Promise<string> {
|
| 347 |
+
if (!apiKey) {
|
| 348 |
+
throw new Error('Together AI API key required');
|
| 349 |
+
}
|
| 350 |
+
|
| 351 |
+
const messages = [];
|
| 352 |
+
if (options.systemPrompt) {
|
| 353 |
+
messages.push({ role: 'system', content: options.systemPrompt });
|
| 354 |
+
}
|
| 355 |
+
messages.push({ role: 'user', content: prompt });
|
| 356 |
+
|
| 357 |
+
const response = await fetch('https://api.together.xyz/v1/chat/completions', {
|
| 358 |
+
method: 'POST',
|
| 359 |
+
headers: {
|
| 360 |
+
'Authorization': `Bearer ${apiKey}`,
|
| 361 |
+
'Content-Type': 'application/json'
|
| 362 |
+
},
|
| 363 |
+
body: JSON.stringify({
|
| 364 |
+
model,
|
| 365 |
+
messages,
|
| 366 |
+
max_tokens: options.maxTokens,
|
| 367 |
+
temperature: options.temperature
|
| 368 |
+
})
|
| 369 |
+
});
|
| 370 |
+
|
| 371 |
+
if (!response.ok) {
|
| 372 |
+
throw new Error(`Together AI API error: ${response.statusText}`);
|
| 373 |
+
}
|
| 374 |
+
|
| 375 |
+
const data = await response.json();
|
| 376 |
+
return data.choices?.[0]?.message?.content || '';
|
| 377 |
+
}
|
| 378 |
+
|
| 379 |
+
static async testConnection(providerId: string): Promise<boolean> {
|
| 380 |
+
const provider = AI_PROVIDERS.find(p => p.id === providerId);
|
| 381 |
+
if (!provider) return false;
|
| 382 |
+
|
| 383 |
+
try {
|
| 384 |
+
const config = this.getStoredConfig();
|
| 385 |
+
|
| 386 |
+
switch (providerId) {
|
| 387 |
+
case 'ollama':
|
| 388 |
+
try {
|
| 389 |
+
const ollamaResponse = await fetch('http://localhost:11434/api/tags', {
|
| 390 |
+
method: 'GET',
|
| 391 |
+
signal: AbortSignal.timeout(5000)
|
| 392 |
+
});
|
| 393 |
+
return ollamaResponse.ok;
|
| 394 |
+
} catch (error) {
|
| 395 |
+
return false;
|
| 396 |
+
}
|
| 397 |
+
|
| 398 |
+
case 'huggingface':
|
| 399 |
+
if (!config.apiKeys?.huggingface) return false;
|
| 400 |
+
try {
|
| 401 |
+
// Test with a simple model check
|
| 402 |
+
const hfResponse = await fetch('https://api-inference.huggingface.co/models/microsoft/DialoGPT-medium', {
|
| 403 |
+
method: 'GET',
|
| 404 |
+
headers: { 'Authorization': `Bearer ${config.apiKeys.huggingface}` },
|
| 405 |
+
signal: AbortSignal.timeout(10000)
|
| 406 |
+
});
|
| 407 |
+
return hfResponse.ok;
|
| 408 |
+
} catch (error) {
|
| 409 |
+
return false;
|
| 410 |
+
}
|
| 411 |
+
|
| 412 |
+
case 'openai-compatible':
|
| 413 |
+
try {
|
| 414 |
+
const endpoint = config.customEndpoints?.openai || provider.apiUrl;
|
| 415 |
+
const openaiResponse = await fetch(`${endpoint}/v1/models`, {
|
| 416 |
+
method: 'GET',
|
| 417 |
+
signal: AbortSignal.timeout(5000)
|
| 418 |
+
});
|
| 419 |
+
return openaiResponse.ok;
|
| 420 |
+
} catch (error) {
|
| 421 |
+
return false;
|
| 422 |
+
}
|
| 423 |
+
|
| 424 |
+
case 'together':
|
| 425 |
+
if (!config.apiKeys?.together) return false;
|
| 426 |
+
try {
|
| 427 |
+
const togetherResponse = await fetch('https://api.together.xyz/v1/models', {
|
| 428 |
+
method: 'GET',
|
| 429 |
+
headers: { 'Authorization': `Bearer ${config.apiKeys.together}` },
|
| 430 |
+
signal: AbortSignal.timeout(10000)
|
| 431 |
+
});
|
| 432 |
+
return togetherResponse.ok;
|
| 433 |
+
} catch (error) {
|
| 434 |
+
return false;
|
| 435 |
+
}
|
| 436 |
+
|
| 437 |
+
default:
|
| 438 |
+
return false;
|
| 439 |
+
}
|
| 440 |
+
} catch (error) {
|
| 441 |
+
console.log(`Connection test failed for ${providerId}:`, error);
|
| 442 |
+
return false;
|
| 443 |
+
}
|
| 444 |
+
}
|
| 445 |
+
}
|
src/utils/wikimedia-api.ts
ADDED
|
@@ -0,0 +1,429 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { WikimediaProject, SearchResult, StudyPlan } from '../types';
|
| 2 |
+
|
| 3 |
+
export const WIKIMEDIA_PROJECTS: WikimediaProject[] = [
|
| 4 |
+
{
|
| 5 |
+
id: 'wikipedia',
|
| 6 |
+
name: 'Wikipedia',
|
| 7 |
+
description: 'The free encyclopedia',
|
| 8 |
+
apiUrl: 'https://en.wikipedia.org/w/api.php',
|
| 9 |
+
color: 'bg-primary-500',
|
| 10 |
+
icon: 'Book'
|
| 11 |
+
},
|
| 12 |
+
{
|
| 13 |
+
id: 'wikibooks',
|
| 14 |
+
name: 'Wikibooks',
|
| 15 |
+
description: 'Free textbooks and manuals',
|
| 16 |
+
apiUrl: 'https://en.wikibooks.org/w/api.php',
|
| 17 |
+
color: 'bg-secondary-500',
|
| 18 |
+
icon: 'BookOpen'
|
| 19 |
+
},
|
| 20 |
+
{
|
| 21 |
+
id: 'wikiquote',
|
| 22 |
+
name: 'Wikiquote',
|
| 23 |
+
description: 'Collection of quotations',
|
| 24 |
+
apiUrl: 'https://en.wikiquote.org/w/api.php',
|
| 25 |
+
color: 'bg-accent-500',
|
| 26 |
+
icon: 'Quote'
|
| 27 |
+
},
|
| 28 |
+
{
|
| 29 |
+
id: 'wikiversity',
|
| 30 |
+
name: 'Wikiversity',
|
| 31 |
+
description: 'Free learning resources',
|
| 32 |
+
apiUrl: 'https://en.wikiversity.org/w/api.php',
|
| 33 |
+
color: 'bg-success-500',
|
| 34 |
+
icon: 'GraduationCap'
|
| 35 |
+
},
|
| 36 |
+
{
|
| 37 |
+
id: 'wiktionary',
|
| 38 |
+
name: 'Wiktionary',
|
| 39 |
+
description: 'Free dictionary',
|
| 40 |
+
apiUrl: 'https://en.wiktionary.org/w/api.php',
|
| 41 |
+
color: 'bg-warning-500',
|
| 42 |
+
icon: 'Languages'
|
| 43 |
+
},
|
| 44 |
+
{
|
| 45 |
+
id: 'wikisource',
|
| 46 |
+
name: 'Wikisource',
|
| 47 |
+
description: 'Free library of source texts',
|
| 48 |
+
apiUrl: 'https://en.wikisource.org/w/api.php',
|
| 49 |
+
color: 'bg-error-500',
|
| 50 |
+
icon: 'FileText'
|
| 51 |
+
}
|
| 52 |
+
];
|
| 53 |
+
|
| 54 |
+
export class WikimediaAPI {
|
| 55 |
+
private static async makeRequest(apiUrl: string, params: Record<string, string>): Promise<any> {
|
| 56 |
+
const url = new URL(apiUrl);
|
| 57 |
+
Object.entries(params).forEach(([key, value]) => {
|
| 58 |
+
url.searchParams.append(key, value);
|
| 59 |
+
});
|
| 60 |
+
|
| 61 |
+
try {
|
| 62 |
+
const response = await fetch(url.toString());
|
| 63 |
+
if (!response.ok) {
|
| 64 |
+
throw new Error(`HTTP error! status: ${response.status}`);
|
| 65 |
+
}
|
| 66 |
+
return await response.json();
|
| 67 |
+
} catch (error) {
|
| 68 |
+
console.error('API request failed:', error);
|
| 69 |
+
throw error;
|
| 70 |
+
}
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
static async search(query: string, project: string = 'wikipedia', limit: number = 10): Promise<SearchResult[]> {
|
| 74 |
+
const projectData = WIKIMEDIA_PROJECTS.find(p => p.id === project);
|
| 75 |
+
if (!projectData) {
|
| 76 |
+
throw new Error(`Unknown project: ${project}`);
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
const params = {
|
| 80 |
+
action: 'query',
|
| 81 |
+
format: 'json',
|
| 82 |
+
list: 'search',
|
| 83 |
+
srsearch: query,
|
| 84 |
+
srlimit: limit.toString(),
|
| 85 |
+
srprop: 'snippet|titlesnippet|size|timestamp',
|
| 86 |
+
origin: '*'
|
| 87 |
+
};
|
| 88 |
+
|
| 89 |
+
try {
|
| 90 |
+
const data = await this.makeRequest(projectData.apiUrl, params);
|
| 91 |
+
|
| 92 |
+
return data.query?.search?.map((result: any) => ({
|
| 93 |
+
title: result.title,
|
| 94 |
+
pageid: result.pageid,
|
| 95 |
+
size: result.size,
|
| 96 |
+
snippet: result.snippet || 'No snippet available',
|
| 97 |
+
timestamp: result.timestamp,
|
| 98 |
+
project: project,
|
| 99 |
+
url: this.buildProjectUrl(project, result.title)
|
| 100 |
+
})) || [];
|
| 101 |
+
} catch (error) {
|
| 102 |
+
console.error(`Search failed for ${project}:`, error);
|
| 103 |
+
return [];
|
| 104 |
+
}
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
static async searchMultipleProjects(query: string, projects: string[] = ['wikipedia', 'wikibooks', 'wikiversity'], limit: number = 5): Promise<SearchResult[]> {
|
| 108 |
+
const searchPromises = projects.map(project => this.search(query, project, limit));
|
| 109 |
+
|
| 110 |
+
try {
|
| 111 |
+
const results = await Promise.allSettled(searchPromises);
|
| 112 |
+
const allResults: SearchResult[] = [];
|
| 113 |
+
|
| 114 |
+
results.forEach((result) => {
|
| 115 |
+
if (result.status === 'fulfilled') {
|
| 116 |
+
allResults.push(...result.value);
|
| 117 |
+
}
|
| 118 |
+
});
|
| 119 |
+
|
| 120 |
+
return allResults.sort((a, b) => new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime());
|
| 121 |
+
} catch (error) {
|
| 122 |
+
console.error('Multi-project search failed:', error);
|
| 123 |
+
return [];
|
| 124 |
+
}
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
static async getPageContent(title: string, project: string = 'wikipedia'): Promise<string> {
|
| 128 |
+
const projectData = WIKIMEDIA_PROJECTS.find(p => p.id === project);
|
| 129 |
+
if (!projectData) {
|
| 130 |
+
throw new Error(`Unknown project: ${project}`);
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
// Handle language-specific Wikipedia projects
|
| 134 |
+
let apiUrl = projectData.apiUrl;
|
| 135 |
+
if (project.includes('-wikipedia')) {
|
| 136 |
+
const langCode = project.split('-')[0];
|
| 137 |
+
apiUrl = `https://${langCode}.wikipedia.org/w/api.php`;
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
// Try multiple approaches to get the best content
|
| 141 |
+
const approaches = [
|
| 142 |
+
// Approach 1: Get full extract
|
| 143 |
+
{
|
| 144 |
+
action: 'query',
|
| 145 |
+
format: 'json',
|
| 146 |
+
titles: title,
|
| 147 |
+
prop: 'extracts',
|
| 148 |
+
exintro: 'false',
|
| 149 |
+
explaintext: 'true',
|
| 150 |
+
exsectionformat: 'plain',
|
| 151 |
+
origin: '*'
|
| 152 |
+
},
|
| 153 |
+
// Approach 2: Get intro only if full content fails
|
| 154 |
+
{
|
| 155 |
+
action: 'query',
|
| 156 |
+
format: 'json',
|
| 157 |
+
titles: title,
|
| 158 |
+
prop: 'extracts',
|
| 159 |
+
exintro: 'true',
|
| 160 |
+
explaintext: 'true',
|
| 161 |
+
exsentences: '10',
|
| 162 |
+
origin: '*'
|
| 163 |
+
}
|
| 164 |
+
];
|
| 165 |
+
|
| 166 |
+
for (const params of approaches) {
|
| 167 |
+
try {
|
| 168 |
+
const data = await this.makeRequest(apiUrl, params);
|
| 169 |
+
const pages = data.query?.pages || {};
|
| 170 |
+
const pageId = Object.keys(pages)[0];
|
| 171 |
+
|
| 172 |
+
if (pageId !== '-1' && pages[pageId]?.extract) {
|
| 173 |
+
const content = pages[pageId].extract;
|
| 174 |
+
if (content.length > 200) {
|
| 175 |
+
return content;
|
| 176 |
+
}
|
| 177 |
+
}
|
| 178 |
+
} catch (error) {
|
| 179 |
+
console.log(`Approach failed for ${title}:`, error);
|
| 180 |
+
continue;
|
| 181 |
+
}
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
// Fallback: Return a helpful message
|
| 185 |
+
const projectName = projectData.name;
|
| 186 |
+
return `This article exists but the content could not be fully loaded through the API.
|
| 187 |
+
|
| 188 |
+
The article "${title}" is available on ${projectName}, but due to API limitations or the article's structure, we cannot display the full content here.
|
| 189 |
+
|
| 190 |
+
This might happen because:
|
| 191 |
+
- The article is a disambiguation page
|
| 192 |
+
- The content is primarily in tables, lists, or special formatting
|
| 193 |
+
- The article has restricted content access
|
| 194 |
+
- There are temporary API limitations
|
| 195 |
+
|
| 196 |
+
For the complete article with all formatting, images, and references, please visit the original source using the "View Original" button.`;
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
static async getRandomArticles(project: string = 'wikipedia', count: number = 5): Promise<SearchResult[]> {
|
| 200 |
+
const projectData = WIKIMEDIA_PROJECTS.find(p => p.id === project);
|
| 201 |
+
if (!projectData) {
|
| 202 |
+
throw new Error(`Unknown project: ${project}`);
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
const params = {
|
| 206 |
+
action: 'query',
|
| 207 |
+
format: 'json',
|
| 208 |
+
list: 'random',
|
| 209 |
+
rnnamespace: '0',
|
| 210 |
+
rnlimit: count.toString(),
|
| 211 |
+
origin: '*'
|
| 212 |
+
};
|
| 213 |
+
|
| 214 |
+
try {
|
| 215 |
+
const data = await this.makeRequest(projectData.apiUrl, params);
|
| 216 |
+
const randomPages = data.query?.random || [];
|
| 217 |
+
|
| 218 |
+
// Get snippets for random articles
|
| 219 |
+
const results: SearchResult[] = [];
|
| 220 |
+
for (const page of randomPages) {
|
| 221 |
+
try {
|
| 222 |
+
const snippet = await this.getPageSnippet(page.title, project);
|
| 223 |
+
results.push({
|
| 224 |
+
title: page.title,
|
| 225 |
+
pageid: page.id,
|
| 226 |
+
size: 0,
|
| 227 |
+
snippet: snippet,
|
| 228 |
+
timestamp: new Date().toISOString(),
|
| 229 |
+
project: project,
|
| 230 |
+
url: this.buildProjectUrl(project, page.title)
|
| 231 |
+
});
|
| 232 |
+
} catch (error) {
|
| 233 |
+
// If we can't get snippet, still include the article
|
| 234 |
+
results.push({
|
| 235 |
+
title: page.title,
|
| 236 |
+
pageid: page.id,
|
| 237 |
+
size: 0,
|
| 238 |
+
snippet: `Random article from ${projectData.name}`,
|
| 239 |
+
timestamp: new Date().toISOString(),
|
| 240 |
+
project: project,
|
| 241 |
+
url: this.buildProjectUrl(project, page.title)
|
| 242 |
+
});
|
| 243 |
+
}
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
return results;
|
| 247 |
+
} catch (error) {
|
| 248 |
+
console.error(`Failed to get random articles from ${project}:`, error);
|
| 249 |
+
return [];
|
| 250 |
+
}
|
| 251 |
+
}
|
| 252 |
+
|
| 253 |
+
private static async getPageSnippet(title: string, project: string): Promise<string> {
|
| 254 |
+
const projectData = WIKIMEDIA_PROJECTS.find(p => p.id === project);
|
| 255 |
+
if (!projectData) {
|
| 256 |
+
return 'No description available';
|
| 257 |
+
}
|
| 258 |
+
|
| 259 |
+
const params = {
|
| 260 |
+
action: 'query',
|
| 261 |
+
format: 'json',
|
| 262 |
+
titles: title,
|
| 263 |
+
prop: 'extracts',
|
| 264 |
+
exintro: 'true',
|
| 265 |
+
explaintext: 'true',
|
| 266 |
+
exsentences: '2',
|
| 267 |
+
origin: '*'
|
| 268 |
+
};
|
| 269 |
+
|
| 270 |
+
try {
|
| 271 |
+
const data = await this.makeRequest(projectData.apiUrl, params);
|
| 272 |
+
const pages = data.query?.pages || {};
|
| 273 |
+
const pageId = Object.keys(pages)[0];
|
| 274 |
+
|
| 275 |
+
if (pageId === '-1') {
|
| 276 |
+
return 'No description available';
|
| 277 |
+
}
|
| 278 |
+
|
| 279 |
+
const extract = pages[pageId]?.extract || 'No description available';
|
| 280 |
+
return extract.length > 200 ? extract.substring(0, 200) + '...' : extract;
|
| 281 |
+
} catch (error) {
|
| 282 |
+
return 'No description available';
|
| 283 |
+
}
|
| 284 |
+
}
|
| 285 |
+
|
| 286 |
+
static async generateStudyPlan(topic: string, difficulty: 'beginner' | 'intermediate' | 'advanced' = 'beginner'): Promise<StudyPlan> {
|
| 287 |
+
try {
|
| 288 |
+
// Search across multiple projects for comprehensive content
|
| 289 |
+
const searchResults = await this.searchMultipleProjects(topic, ['wikipedia', 'wikibooks', 'wikiversity'], 15);
|
| 290 |
+
|
| 291 |
+
if (searchResults.length === 0) {
|
| 292 |
+
throw new Error('No content found for this topic');
|
| 293 |
+
}
|
| 294 |
+
|
| 295 |
+
// Filter and organize results
|
| 296 |
+
const topicCount = difficulty === 'beginner' ? 5 : difficulty === 'intermediate' ? 8 : 12;
|
| 297 |
+
const selectedResults = searchResults.slice(0, topicCount);
|
| 298 |
+
|
| 299 |
+
// Generate study plan structure with real data
|
| 300 |
+
const studyPlan: StudyPlan = {
|
| 301 |
+
id: `plan-${Date.now()}`,
|
| 302 |
+
title: `${topic} Study Plan`,
|
| 303 |
+
description: `Comprehensive ${difficulty} level study plan for ${topic} using real Wikimedia content`,
|
| 304 |
+
difficulty,
|
| 305 |
+
estimatedTime: this.estimateStudyTime(selectedResults.length, difficulty),
|
| 306 |
+
created: new Date().toISOString(),
|
| 307 |
+
topics: await this.generateTopicsFromResults(selectedResults, difficulty)
|
| 308 |
+
};
|
| 309 |
+
|
| 310 |
+
return studyPlan;
|
| 311 |
+
} catch (error) {
|
| 312 |
+
console.error('Failed to generate study plan:', error);
|
| 313 |
+
throw error;
|
| 314 |
+
}
|
| 315 |
+
}
|
| 316 |
+
|
| 317 |
+
private static async generateTopicsFromResults(results: SearchResult[], difficulty: string): Promise<any[]> {
|
| 318 |
+
const topics = [];
|
| 319 |
+
|
| 320 |
+
for (let i = 0; i < results.length; i++) {
|
| 321 |
+
const result = results[i];
|
| 322 |
+
|
| 323 |
+
// Get more detailed content for each topic
|
| 324 |
+
let detailedContent = result.snippet;
|
| 325 |
+
try {
|
| 326 |
+
const fullContent = await this.getPageSnippet(result.title, result.project);
|
| 327 |
+
if (fullContent && fullContent !== 'No description available') {
|
| 328 |
+
detailedContent = fullContent;
|
| 329 |
+
}
|
| 330 |
+
} catch (error) {
|
| 331 |
+
console.log(`Could not get detailed content for ${result.title}`);
|
| 332 |
+
}
|
| 333 |
+
|
| 334 |
+
topics.push({
|
| 335 |
+
id: `topic-${Date.now()}-${i}`,
|
| 336 |
+
title: result.title,
|
| 337 |
+
description: detailedContent.replace(/<[^>]*>/g, '').substring(0, 200) + '...',
|
| 338 |
+
content: detailedContent,
|
| 339 |
+
completed: false,
|
| 340 |
+
estimatedTime: `${Math.ceil(Math.random() * 2 + 1)} hours`,
|
| 341 |
+
resources: [
|
| 342 |
+
{
|
| 343 |
+
title: result.title,
|
| 344 |
+
url: result.url,
|
| 345 |
+
type: 'article' as const,
|
| 346 |
+
project: result.project
|
| 347 |
+
}
|
| 348 |
+
]
|
| 349 |
+
});
|
| 350 |
+
}
|
| 351 |
+
|
| 352 |
+
return topics;
|
| 353 |
+
}
|
| 354 |
+
|
| 355 |
+
private static buildProjectUrl(project: string, title: string): string {
|
| 356 |
+
const baseUrls: Record<string, string> = {
|
| 357 |
+
wikipedia: 'https://en.wikipedia.org/wiki/',
|
| 358 |
+
wikibooks: 'https://en.wikibooks.org/wiki/',
|
| 359 |
+
wikiquote: 'https://en.wikiquote.org/wiki/',
|
| 360 |
+
wikiversity: 'https://en.wikiversity.org/wiki/',
|
| 361 |
+
wiktionary: 'https://en.wiktionary.org/wiki/',
|
| 362 |
+
wikisource: 'https://en.wikisource.org/wiki/',
|
| 363 |
+
};
|
| 364 |
+
|
| 365 |
+
const baseUrl = baseUrls[project] || baseUrls.wikipedia;
|
| 366 |
+
return baseUrl + encodeURIComponent(title.replace(/ /g, '_'));
|
| 367 |
+
}
|
| 368 |
+
|
| 369 |
+
private static estimateStudyTime(contentCount: number, difficulty: string): string {
|
| 370 |
+
const baseHours = contentCount * 1.5; // More realistic time estimate
|
| 371 |
+
const multiplier = difficulty === 'beginner' ? 1 : difficulty === 'intermediate' ? 1.5 : 2;
|
| 372 |
+
const totalHours = Math.ceil(baseHours * multiplier);
|
| 373 |
+
|
| 374 |
+
if (totalHours < 24) {
|
| 375 |
+
return `${totalHours} hours`;
|
| 376 |
+
} else {
|
| 377 |
+
const weeks = Math.ceil(totalHours / 10); // Assuming 10 hours per week
|
| 378 |
+
return `${weeks} weeks`;
|
| 379 |
+
}
|
| 380 |
+
}
|
| 381 |
+
|
| 382 |
+
// Real-time progress tracking methods
|
| 383 |
+
static getStoredProgress(): any {
|
| 384 |
+
try {
|
| 385 |
+
const stored = localStorage.getItem('wikistro-progress');
|
| 386 |
+
return stored ? JSON.parse(stored) : this.getDefaultProgress();
|
| 387 |
+
} catch (error) {
|
| 388 |
+
return this.getDefaultProgress();
|
| 389 |
+
}
|
| 390 |
+
}
|
| 391 |
+
|
| 392 |
+
static saveProgress(progressData: any): void {
|
| 393 |
+
try {
|
| 394 |
+
localStorage.setItem('wikistro-progress', JSON.stringify(progressData));
|
| 395 |
+
} catch (error) {
|
| 396 |
+
console.error('Failed to save progress:', error);
|
| 397 |
+
}
|
| 398 |
+
}
|
| 399 |
+
|
| 400 |
+
static getStoredStudyPlans(): StudyPlan[] {
|
| 401 |
+
try {
|
| 402 |
+
const stored = localStorage.getItem('wikistro-study-plans');
|
| 403 |
+
return stored ? JSON.parse(stored) : [];
|
| 404 |
+
} catch (error) {
|
| 405 |
+
return [];
|
| 406 |
+
}
|
| 407 |
+
}
|
| 408 |
+
|
| 409 |
+
static saveStudyPlans(plans: StudyPlan[]): void {
|
| 410 |
+
try {
|
| 411 |
+
localStorage.setItem('wikistro-study-plans', JSON.stringify(plans));
|
| 412 |
+
} catch (error) {
|
| 413 |
+
console.error('Failed to save study plans:', error);
|
| 414 |
+
}
|
| 415 |
+
}
|
| 416 |
+
|
| 417 |
+
private static getDefaultProgress() {
|
| 418 |
+
return {
|
| 419 |
+
studyStreak: 0,
|
| 420 |
+
topicsCompleted: 0,
|
| 421 |
+
totalStudyTime: 0,
|
| 422 |
+
achievements: 0,
|
| 423 |
+
weeklyGoal: { current: 0, target: 12 },
|
| 424 |
+
recentActivity: [],
|
| 425 |
+
completedTopics: [],
|
| 426 |
+
lastStudyDate: null
|
| 427 |
+
};
|
| 428 |
+
}
|
| 429 |
+
}
|
src/vite-env.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
/// <reference types="vite/client" />
|
tailwind.config.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/** @type {import('tailwindcss').Config} */
|
| 2 |
+
export default {
|
| 3 |
+
content: ['./index.html', './src/**/*.{js,ts,jsx,tsx}'],
|
| 4 |
+
theme: {
|
| 5 |
+
extend: {
|
| 6 |
+
colors: {
|
| 7 |
+
primary: {
|
| 8 |
+
50: '#eff6ff',
|
| 9 |
+
100: '#dbeafe',
|
| 10 |
+
200: '#bfdbfe',
|
| 11 |
+
300: '#93c5fd',
|
| 12 |
+
400: '#60a5fa',
|
| 13 |
+
500: '#3b82f6',
|
| 14 |
+
600: '#2563eb',
|
| 15 |
+
700: '#1d4ed8',
|
| 16 |
+
800: '#1e40af',
|
| 17 |
+
900: '#1e3a8a',
|
| 18 |
+
},
|
| 19 |
+
secondary: {
|
| 20 |
+
50: '#f0fdfa',
|
| 21 |
+
100: '#ccfbf1',
|
| 22 |
+
200: '#99f6e4',
|
| 23 |
+
300: '#5eead4',
|
| 24 |
+
400: '#2dd4bf',
|
| 25 |
+
500: '#14b8a6',
|
| 26 |
+
600: '#0d9488',
|
| 27 |
+
700: '#0f766e',
|
| 28 |
+
800: '#115e59',
|
| 29 |
+
900: '#134e4a',
|
| 30 |
+
},
|
| 31 |
+
accent: {
|
| 32 |
+
50: '#fff7ed',
|
| 33 |
+
100: '#ffedd5',
|
| 34 |
+
200: '#fed7aa',
|
| 35 |
+
300: '#fdba74',
|
| 36 |
+
400: '#fb923c',
|
| 37 |
+
500: '#f97316',
|
| 38 |
+
600: '#ea580c',
|
| 39 |
+
700: '#c2410c',
|
| 40 |
+
800: '#9a3412',
|
| 41 |
+
900: '#7c2d12',
|
| 42 |
+
},
|
| 43 |
+
success: {
|
| 44 |
+
50: '#f0fdf4',
|
| 45 |
+
100: '#dcfce7',
|
| 46 |
+
200: '#bbf7d0',
|
| 47 |
+
300: '#86efac',
|
| 48 |
+
400: '#4ade80',
|
| 49 |
+
500: '#22c55e',
|
| 50 |
+
600: '#16a34a',
|
| 51 |
+
700: '#15803d',
|
| 52 |
+
800: '#166534',
|
| 53 |
+
900: '#14532d',
|
| 54 |
+
},
|
| 55 |
+
warning: {
|
| 56 |
+
50: '#fffbeb',
|
| 57 |
+
100: '#fef3c7',
|
| 58 |
+
200: '#fde68a',
|
| 59 |
+
300: '#fcd34d',
|
| 60 |
+
400: '#fbbf24',
|
| 61 |
+
500: '#f59e0b',
|
| 62 |
+
600: '#d97706',
|
| 63 |
+
700: '#b45309',
|
| 64 |
+
800: '#92400e',
|
| 65 |
+
900: '#78350f',
|
| 66 |
+
},
|
| 67 |
+
error: {
|
| 68 |
+
50: '#fef2f2',
|
| 69 |
+
100: '#fee2e2',
|
| 70 |
+
200: '#fecaca',
|
| 71 |
+
300: '#fca5a5',
|
| 72 |
+
400: '#f87171',
|
| 73 |
+
500: '#ef4444',
|
| 74 |
+
600: '#dc2626',
|
| 75 |
+
700: '#b91c1c',
|
| 76 |
+
800: '#991b1b',
|
| 77 |
+
900: '#7f1d1d',
|
| 78 |
+
},
|
| 79 |
+
},
|
| 80 |
+
fontFamily: {
|
| 81 |
+
sans: ['Inter', 'system-ui', '-apple-system', 'sans-serif'],
|
| 82 |
+
},
|
| 83 |
+
spacing: {
|
| 84 |
+
'18': '4.5rem',
|
| 85 |
+
'88': '22rem',
|
| 86 |
+
},
|
| 87 |
+
animation: {
|
| 88 |
+
'fade-in': 'fadeIn 0.5s ease-in-out',
|
| 89 |
+
'slide-up': 'slideUp 0.5s ease-out',
|
| 90 |
+
'bounce-subtle': 'bounceSubtle 2s infinite',
|
| 91 |
+
},
|
| 92 |
+
keyframes: {
|
| 93 |
+
fadeIn: {
|
| 94 |
+
'0%': { opacity: '0' },
|
| 95 |
+
'100%': { opacity: '1' },
|
| 96 |
+
},
|
| 97 |
+
slideUp: {
|
| 98 |
+
'0%': { transform: 'translateY(20px)', opacity: '0' },
|
| 99 |
+
'100%': { transform: 'translateY(0)', opacity: '1' },
|
| 100 |
+
},
|
| 101 |
+
bounceSubtle: {
|
| 102 |
+
'0%, 100%': { transform: 'translateY(0)' },
|
| 103 |
+
'50%': { transform: 'translateY(-4px)' },
|
| 104 |
+
},
|
| 105 |
+
},
|
| 106 |
+
},
|
| 107 |
+
},
|
| 108 |
+
plugins: [],
|
| 109 |
+
};
|
tsconfig.app.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"compilerOptions": {
|
| 3 |
+
"target": "ES2020",
|
| 4 |
+
"useDefineForClassFields": true,
|
| 5 |
+
"lib": ["ES2020", "DOM", "DOM.Iterable"],
|
| 6 |
+
"module": "ESNext",
|
| 7 |
+
"skipLibCheck": true,
|
| 8 |
+
|
| 9 |
+
/* Bundler mode */
|
| 10 |
+
"moduleResolution": "bundler",
|
| 11 |
+
"allowImportingTsExtensions": true,
|
| 12 |
+
"isolatedModules": true,
|
| 13 |
+
"moduleDetection": "force",
|
| 14 |
+
"noEmit": true,
|
| 15 |
+
"jsx": "react-jsx",
|
| 16 |
+
|
| 17 |
+
/* Linting */
|
| 18 |
+
"strict": true,
|
| 19 |
+
"noUnusedLocals": true,
|
| 20 |
+
"noUnusedParameters": true,
|
| 21 |
+
"noFallthroughCasesInSwitch": true
|
| 22 |
+
},
|
| 23 |
+
"include": ["src"]
|
| 24 |
+
}
|
tsconfig.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"files": [],
|
| 3 |
+
"references": [
|
| 4 |
+
{ "path": "./tsconfig.app.json" },
|
| 5 |
+
{ "path": "./tsconfig.node.json" }
|
| 6 |
+
]
|
| 7 |
+
}
|
tsconfig.node.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"compilerOptions": {
|
| 3 |
+
"target": "ES2022",
|
| 4 |
+
"lib": ["ES2023"],
|
| 5 |
+
"module": "ESNext",
|
| 6 |
+
"skipLibCheck": true,
|
| 7 |
+
|
| 8 |
+
/* Bundler mode */
|
| 9 |
+
"moduleResolution": "bundler",
|
| 10 |
+
"allowImportingTsExtensions": true,
|
| 11 |
+
"isolatedModules": true,
|
| 12 |
+
"moduleDetection": "force",
|
| 13 |
+
"noEmit": true,
|
| 14 |
+
|
| 15 |
+
/* Linting */
|
| 16 |
+
"strict": true,
|
| 17 |
+
"noUnusedLocals": true,
|
| 18 |
+
"noUnusedParameters": true,
|
| 19 |
+
"noFallthroughCasesInSwitch": true
|
| 20 |
+
},
|
| 21 |
+
"include": ["vite.config.ts"]
|
| 22 |
+
}
|
vite.config.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { defineConfig } from 'vite';
|
| 2 |
+
import react from '@vitejs/plugin-react';
|
| 3 |
+
|
| 4 |
+
// https://vitejs.dev/config/
|
| 5 |
+
export default defineConfig({
|
| 6 |
+
base: './', // 👈 important for Hugging Face Spaces
|
| 7 |
+
plugins: [react()],
|
| 8 |
+
optimizeDeps: {
|
| 9 |
+
exclude: ['lucide-react'],
|
| 10 |
+
},
|
| 11 |
+
});
|