mcp-quiz-server
Version:
🧠AI-Powered Quiz Management via Model Context Protocol (MCP) - Create, manage, and take quizzes directly from VS Code, Claude, and other AI agents.
332 lines (326 loc) • 14.1 kB
JavaScript
import { DashboardService } from '../services/DashboardService';
import { AppStore } from '../store/AppStore';
import { AuthManager } from './AuthManager';
import { Component } from './Component';
export class Dashboard extends Component {
constructor() {
if (!document.getElementById('dashboard-app')) {
const container = document.createElement('div');
container.id = 'dashboard-app';
container.className = 'min-h-screen bg-gray-50';
document.body.appendChild(container);
}
super('#dashboard-app');
this.unsubscribeStore = null;
this.isLoading = true;
this.dashboardData = null;
this.error = null;
this.authManager = AuthManager.getInstance();
this.dashboardService = new DashboardService();
this.appStore = AppStore.getInstance();
}
async onMount() {
this.setupStoreSubscription();
this.render();
try {
if (!this.authManager.isAuthenticated()) {
const authenticated = await this.authManager.requireAuthentication();
if (!authenticated) {
this.redirectToLogin();
return;
}
}
await this.loadDashboardData();
}
catch (error) {
console.error('Dashboard initialization failed:', error);
this.error = 'Failed to load dashboard. Please try again.';
this.isLoading = false;
this.render();
}
}
render() {
if (this.isLoading) {
this.renderLoading();
}
else if (this.error) {
this.renderError();
}
else {
this.renderDashboard();
}
}
bindEvents() {
const createQuizBtn = this.element.querySelector('.create-quiz-btn');
createQuizBtn?.addEventListener('click', this.handleCreateQuiz.bind(this));
const refreshBtn = this.element.querySelector('.refresh-btn');
refreshBtn?.addEventListener('click', this.handleRefresh.bind(this));
this.bindQuizListEvents();
}
renderLoading() {
this.element.innerHTML = `
<div class="dashboard-loading">
<div class="min-h-screen flex items-center justify-center">
<div class="text-center">
<div class="animate-spin rounded-full h-12 w-12 border-b-2 border-blue-600 mx-auto mb-4"></div>
<h2 class="text-lg font-medium text-gray-900 mb-2">Loading Dashboard</h2>
<p class="text-gray-600">Please wait while we load your information...</p>
</div>
</div>
</div>
`;
}
renderError() {
this.element.innerHTML = `
<div class="dashboard-error">
<div class="min-h-screen flex items-center justify-center">
<div class="text-center max-w-md mx-auto">
<div class="text-red-500 mb-4">
<svg class="w-16 h-16 mx-auto" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
</svg>
</div>
<h2 class="text-xl font-semibold text-gray-900 mb-2">Dashboard Error</h2>
<p class="text-gray-600 mb-6">${this.error}</p>
<button class="retry-btn bg-blue-600 hover:bg-blue-700 text-white px-6 py-2 rounded-lg font-medium transition-colors focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2">
Try Again
</button>
</div>
</div>
</div>
`;
const retryBtn = this.element.querySelector('.retry-btn');
retryBtn?.addEventListener('click', this.handleRetry.bind(this));
}
renderDashboard() {
const user = this.authManager.getCurrentUser();
const stats = this.dashboardData?.stats || {};
const recentQuizzes = this.dashboardData?.recentQuizzes || [];
this.element.innerHTML = `
<div class="dashboard">
<!-- Navigation -->
<nav class="bg-white shadow-sm border-b">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
<div class="flex justify-between h-16">
<div class="flex items-center">
<h1 class="text-xl font-semibold text-gray-900">Quiz Dashboard</h1>
</div>
<div class="auth-container"></div>
</div>
</div>
</nav>
<!-- Main Content -->
<main class="max-w-7xl mx-auto py-6 sm:px-6 lg:px-8">
<!-- Welcome Section -->
<div class="mb-8">
<h2 class="text-2xl font-bold text-gray-900 mb-2">
Welcome back, ${user?.username || 'User'}!
</h2>
<p class="text-gray-600">Manage your quizzes and track your progress</p>
</div>
<!-- Quick Actions -->
<div class="mb-8">
<div class="flex flex-wrap gap-4">
<button class="create-quiz-btn bg-blue-600 hover:bg-blue-700 text-white px-6 py-3 rounded-lg font-medium transition-colors focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 flex items-center">
<svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6v6m0 0v6m0-6h6m-6 0H6"></path>
</svg>
Create New Quiz
</button>
<button class="refresh-btn bg-gray-100 hover:bg-gray-200 text-gray-700 px-6 py-3 rounded-lg font-medium transition-colors focus:outline-none focus:ring-2 focus:ring-gray-500 focus:ring-offset-2 flex items-center">
<svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"></path>
</svg>
Refresh
</button>
</div>
</div>
<!-- Dashboard Cards -->
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6 mb-8">
<!-- My Quizzes Card -->
<div class="card bg-white rounded-lg shadow p-6">
<div class="flex items-center justify-between mb-4">
<h3 class="text-lg font-medium text-gray-900">My Quizzes</h3>
<div class="text-3xl font-bold text-blue-600">${stats.totalQuizzes || 0}</div>
</div>
<p class="text-gray-600 text-sm mb-4">Total quizzes created</p>
<a href="/dashboard/quizzes" class="text-blue-600 hover:text-blue-700 text-sm font-medium">
View all →
</a>
</div>
<!-- Recent Activity Card -->
<div class="card bg-white rounded-lg shadow p-6">
<div class="flex items-center justify-between mb-4">
<h3 class="text-lg font-medium text-gray-900">Recent Activity</h3>
<div class="text-3xl font-bold text-green-600">${stats.recentActivity || 0}</div>
</div>
<p class="text-gray-600 text-sm mb-4">Actions this week</p>
<a href="/dashboard/analytics" class="text-green-600 hover:text-green-700 text-sm font-medium">
View analytics →
</a>
</div>
<!-- Profile Card -->
<div class="card bg-white rounded-lg shadow p-6">
<div class="flex items-center justify-between mb-4">
<h3 class="text-lg font-medium text-gray-900">Profile</h3>
<div class="w-12 h-12 bg-blue-600 rounded-full flex items-center justify-center">
<span class="text-white font-medium">${this.getUserInitials(user?.username)}</span>
</div>
</div>
<p class="text-gray-600 text-sm mb-4">${user?.email || 'No email'}</p>
<a href="/dashboard/profile" class="text-blue-600 hover:text-blue-700 text-sm font-medium">
Manage profile →
</a>
</div>
</div>
<!-- Recent Quizzes -->
<div class="bg-white rounded-lg shadow">
<div class="px-6 py-4 border-b border-gray-200">
<h3 class="text-lg font-medium text-gray-900">Recent Quizzes</h3>
</div>
<div class="quiz-list">
${this.renderQuizList(recentQuizzes)}
</div>
</div>
</main>
</div>
`;
const authContainer = this.element.querySelector('.auth-container');
if (authContainer) {
this.authManager.mount();
}
this.bindEvents();
}
renderQuizList(quizzes) {
if (!quizzes || quizzes.length === 0) {
return `
<div class="px-6 py-8 text-center">
<div class="text-gray-400 mb-4">
<svg class="w-12 h-12 mx-auto" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path>
</svg>
</div>
<h4 class="text-lg font-medium text-gray-900 mb-2">No quizzes yet</h4>
<p class="text-gray-600 mb-4">Get started by creating your first quiz</p>
<button class="create-quiz-btn bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded-lg font-medium transition-colors">
Create Quiz
</button>
</div>
`;
}
return quizzes
.map(quiz => `
<div class="quiz-item px-6 py-4 border-b border-gray-200 hover:bg-gray-50 transition-colors">
<div class="flex items-center justify-between">
<div class="flex-1">
<h4 class="text-md font-medium text-gray-900 mb-1">${quiz.title}</h4>
<p class="text-sm text-gray-600 mb-2">${quiz.description || 'No description'}</p>
<div class="flex items-center text-xs text-gray-500 space-x-4">
<span>${quiz.questions?.length || 0} questions</span>
<span>Created ${this.formatDate(quiz.createdAt)}</span>
<span class="quiz-status ${quiz.status}">${quiz.status || 'draft'}</span>
</div>
</div>
<div class="flex items-center space-x-2">
<button class="edit-quiz-btn text-blue-600 hover:text-blue-700 text-sm font-medium" data-quiz-id="${quiz.id}">
Edit
</button>
<button class="view-quiz-btn text-green-600 hover:text-green-700 text-sm font-medium" data-quiz-id="${quiz.id}">
View
</button>
</div>
</div>
</div>
`)
.join('');
}
setupStoreSubscription() {
this.unsubscribeStore = this.appStore.subscribe(state => {
if (!state.auth.isAuthenticated) {
this.redirectToLogin();
}
});
}
async loadDashboardData() {
try {
this.isLoading = true;
this.render();
const data = await this.dashboardService.getDashboardData();
this.dashboardData = data;
this.error = null;
}
catch (error) {
console.error('Failed to load dashboard data:', error);
this.error = 'Failed to load dashboard data. Please try again.';
}
finally {
this.isLoading = false;
this.render();
}
}
handleCreateQuiz() {
window.location.href = '/quiz/create';
}
async handleRefresh() {
await this.loadDashboardData();
}
async handleRetry() {
this.error = null;
await this.onMount();
}
bindQuizListEvents() {
const editButtons = this.element.querySelectorAll('.edit-quiz-btn');
editButtons.forEach(btn => {
btn.addEventListener('click', e => {
const quizId = e.target.dataset.quizId;
if (quizId)
this.handleEditQuiz(quizId);
});
});
const viewButtons = this.element.querySelectorAll('.view-quiz-btn');
viewButtons.forEach(btn => {
btn.addEventListener('click', e => {
const quizId = e.target.dataset.quizId;
if (quizId)
this.handleViewQuiz(quizId);
});
});
}
handleEditQuiz(quizId) {
window.location.href = `/quiz/edit/${quizId}`;
}
handleViewQuiz(quizId) {
window.location.href = `/quiz/view/${quizId}`;
}
getUserInitials(username) {
if (!username)
return 'U';
const parts = username.split(/[\s@_.-]+/);
if (parts.length >= 2) {
return (parts[0][0] + parts[1][0]).toUpperCase();
}
return username.substring(0, 2).toUpperCase();
}
formatDate(date) {
if (!date)
return 'Unknown';
const d = new Date(date);
const now = new Date();
const diffTime = Math.abs(now.getTime() - d.getTime());
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
if (diffDays === 1)
return 'Today';
if (diffDays === 2)
return 'Yesterday';
if (diffDays <= 7)
return `${diffDays} days ago`;
return d.toLocaleDateString();
}
redirectToLogin() {
window.location.href = '/auth/login.html';
}
onUnmount() {
this.unsubscribeStore?.();
}
}
//# sourceMappingURL=Dashboard.js.map