"use client"; import React, { useState, useEffect } from "react"; import { useParams, useRouter } from "next/navigation"; import { cmsApi, Cohort, Course, CourseAnalytics, lmsApi } from "@/lib/api"; import { useAuth } from "@/context/AuthContext"; import { BarChart3, Users, TrendingUp, AlertTriangle, CheckCircle2, BookOpen, Layers, ShieldAlert } from "lucide-react"; import DropoutRiskDashboard from "@/components/Analytics/DropoutRiskDashboard"; import LiveSessions from "@/components/Courses/LiveSessions"; import { Video } from "lucide-react"; import CourseEditorLayout from "@/components/CourseEditorLayout"; export default function AnalyticsPage() { const { id } = useParams() as { id: string }; const router = useRouter(); const { user } = useAuth(); const [course, setCourse] = useState(null); const [analytics, setAnalytics] = useState(null); const [loading, setLoading] = useState(true); const [authError, setAuthError] = useState(null); const [cohorts, setCohorts] = useState([]); const [selectedCohortId, setSelectedCohortId] = useState(""); const [activeAnalyticsTab, setActiveAnalyticsTab] = useState<"overview" | "risks" | "live">("overview"); useEffect(() => { const fetchData = async () => { if (!user) return; if (user.role !== 'admin' && user.role !== 'instructor') { router.push('/'); return; } try { const cohortsData = await lmsApi.getCohorts(); setCohorts(cohortsData); const [courseData, analyticsData] = await Promise.all([ cmsApi.getCourseWithFullOutline(id), cmsApi.getCourseAnalytics(id, selectedCohortId || undefined) ]); setCourse(courseData); setAnalytics(analyticsData); } catch (err: unknown) { console.error("Failed to load analytics", err); setAuthError(err instanceof Error ? err.message : "Failed to load data"); } finally { setLoading(false); } }; fetchData(); }, [id, user, router, selectedCohortId]); if (loading) return (
); if (authError) return (

Access Denied

{authError}

); if (!course || !analytics) return (
Course not found or analytics unavailable.
); const difficultLessons = analytics.lessons .filter(l => l.average_score < 0.7 && l.submission_count > 0) .sort((a, b) => a.average_score - b.average_score); return (
{user?.role} View
} >
{/* Tab Selector */}
{activeAnalyticsTab === "overview" && (
Enrollments
{analytics.total_enrollments}
Active Learners
Average Score
{Math.round(analytics.average_score * 100)}%
Across all assessments
Attention Needed
{difficultLessons.length}
Struggling Lessons

Rendimiento de Lecciones

{analytics.lessons.map((lesson) => (

{lesson.lesson_title}

{lesson.submission_count} submissions

{Math.round(lesson.average_score * 100)}%
))}

Lecciones con Dificultad

{difficultLessons.length > 0 ? (
{difficultLessons.map(l => (

{l.lesson_title}

Average score is below 70%. Consider reviewing the material or difficulty of questions.

{Math.round(l.average_score * 100)}%
))}
) : (

All set!

No lessons currently fall below the difficulty threshold.

)}

Content Strategy Tip

High submission counts with low average scores often indicate that the assessment might be misleading or the prerequisites aren't clearly explained in previous lessons.

)} {activeAnalyticsTab === "risks" && ( )} {activeAnalyticsTab === "live" && ( )}
); }