"use client" import { useState, useEffect } from "react" import { useAuth } from "@/context/auth-context" import { useRouter } from "next/navigation" import { toast } from "react-hot-toast" import type { Course } from "@/lib/types" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Progress } from "@/components/ui/progress" import { Button } from "@/components/ui/button" import Link from "next/link" import { Loader2 } from "lucide-react" import api from "@/lib/api" // Corrected import: default import export function CourseList() { const { user, firebaseUser, isLoadingAuth, authMethod, token } = useAuth() // Check token for access const router = useRouter() const [courses, setCourses] = useState([]) const [isLoadingCourses, setIsLoadingCourses] = useState(true) const [error, setError] = useState(null) useEffect(() => { if (!isLoadingAuth && !user && !firebaseUser) { // Allow either MetaMask or Firebase user toast.error("Please login to view courses.") router.push("/") return } const fetchCourses = async () => { setIsLoadingCourses(true) setError(null) try { const response = await api.get("/api/courses") setCourses(response.data) } catch (err: any) { console.error("Failed to fetch courses:", err) if (err.code === "ERR_NETWORK") { setError( "Network Error: Could not connect to the backend server. Please ensure your Flask backend is running and accessible.", ) toast.error("Network Error: Backend server unreachable.") } else { setError(err.response?.data?.message || "Failed to load courses.") toast.error(err.response?.data?.message || "Failed to load courses.") } } finally { setIsLoadingCourses(false) } } if (user || firebaseUser) { // Fetch if either user type is logged in fetchCourses() } }, [user, firebaseUser, isLoadingAuth, router, token]) if (isLoadingAuth || isLoadingCourses) { return (
Loading courses...
) } if (error) { return (

{error}

) } if (courses.length === 0) { return (

No courses available yet.

Check back later for new content!

) } return (
{authMethod === "firebase" && !token && (

Limited Access

You are logged in with email. Full functionality, including progress tracking and data persistence, requires connecting your MetaMask wallet.

)}

Available Courses

{courses.map((course) => ( {course.title} {course.subject}

{course.description}

Progress {course.progress}%
))}
) }