"use client" import { Progress } from "@/components/ui/progress" import { useState, useEffect } from "react" import { useAuth } from "@/context/auth-context" import { useRouter } from "next/navigation" import { toast } from "react-hot-toast" import type { Quiz } from "@/lib/types" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Badge } from "@/components/ui/badge" import { Button } from "@/components/ui/button" import Link from "next/link" import { Loader2 } from "lucide-react" import api from "@/lib/api" // Import api export function QuizList() { const { user, isLoadingAuth, authMethod, token } = useAuth() // Check token for access const router = useRouter() const [quizzes, setQuizzes] = useState([]) const [isLoadingQuizzes, setIsLoadingQuizzes] = useState(true) const [error, setError] = useState(null) useEffect(() => { if (!isLoadingAuth && !user) { // Allow MetaMask or email auth toast.error("Please login to view quizzes.") router.push("/") return } const fetchQuizzes = async () => { setIsLoadingQuizzes(true) setError(null) try { // --- ORIGINAL API CALL (UNCOMMENT WHEN BACKEND IS READY) --- const response = await api.get("/api/quizzes") setQuizzes(response.data) // --- ORIGINAL API CALL (UNCOMMENT WHEN BACKEND IS READY) --- } catch (err: any) { console.error("Failed to fetch quizzes:", err) setError(err.response?.data?.message || "Failed to load quizzes.") toast.error(err.response?.data?.message || "Failed to load quizzes.") } finally { setIsLoadingQuizzes(false) // Handled by setTimeout } } if (user) { // Fetch if user is logged in fetchQuizzes() } }, [user, isLoadingAuth, router, token]) const getDifficultyColor = (difficulty: Quiz["difficulty"]) => { switch (difficulty) { case "Easy": return "bg-success text-white" case "Medium": return "bg-warning text-white" case "Hard": return "bg-destructive text-white" default: return "bg-gray-500 text-white" } } if (isLoadingAuth || isLoadingQuizzes) { return (
Loading quizzes...
) } if (error) { return (

{error}

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

No quizzes available yet.

Check back later for new challenges!

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

Limited Access

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

)}

Available Quizzes

{quizzes.map((quiz) => ( {quiz.title} {quiz.difficulty} {quiz.topic} {quiz.recent_performance !== undefined && (
Recent Performance {quiz.recent_performance}%
)}
))}
) }