summaryrefslogtreecommitdiff
path: root/src/pages/lessons.tsx
blob: ef8aa49043b000b01a87e8470ccb81e9afc7a55d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { useState } from "react";
import { getState } from "@/lib/db";
import { getUserLessons, getLessonProgress } from "@/actions/srs";
import { Button } from "@/components/ui/button";
import { Card } from "@/components/ui/card";
import { Progress } from "@/components/ui/progress";

// This is a server component that gets the initial data
export default async function LessonsPage() {
  const state = getState(null);
  const userId = state.user?.id;
  
  // If not logged in, show login required message
  if (!userId) {
    return (
      <div className="container mx-auto py-8">
        <Card className="p-6 text-center">
          <h1 className="text-2xl font-bold mb-4">Login Required</h1>
          <p className="mb-4">You need to be logged in to view your lessons.</p>
          <Button asChild>
            <a href="/login">Login</a>
          </Button>
        </Card>
      </div>
    );
  }
  
  // Get user lessons data
  let lessons;
  try {
    lessons = await getUserLessons(userId);
  } catch (error) {
    console.error("Error fetching lessons:", error);
  }
  
  return (
    <div className="container mx-auto py-8">
      <div className="flex justify-between items-center mb-6">
        <h1 className="text-3xl font-bold">Your Lessons</h1>
        <Button asChild>
          <a href="/">Back to Home</a>
        </Button>
      </div>
      
      {!lessons || lessons.length === 0 ? (
        <NoLessonsFound />
      ) : (
        <div className="grid gap-6 md:grid-cols-2 lg:grid-cols-3">
          {lessons.map((lesson) => (
            <LessonCard key={lesson.id} lesson={lesson} userId={userId} />
          ))}
        </div>
      )}
    </div>
  );
}

// Component to display when no lessons are found
function NoLessonsFound() {
  return (
    <Card className="p-6 text-center">
      <h2 className="text-xl font-semibold mb-3">No Lessons Found</h2>
      <p className="text-gray-500 mb-4">
        You don't have any lessons available yet.
      </p>
    </Card>
  );
}

// Component to display a lesson card
function LessonCard({ lesson, userId }: { lesson: any; userId: number }) {
  const [isHovered, setIsHovered] = useState(false);
  
  // Calculate progress percentage
  const progressPercentage = lesson.progress || 0;
  
  // Determine progress color
  const getProgressColor = (percentage: number) => {
    if (percentage >= 80) return "bg-green-500";
    if (percentage >= 50) return "bg-yellow-500";
    return "bg-blue-500";
  };
  
  return (
    <Card
      className="overflow-hidden transition-shadow duration-300 hover:shadow-lg"
      onMouseEnter={() => setIsHovered(true)}
      onMouseLeave={() => setIsHovered(false)}
    >
      <div className="p-6">
        <h3 className="text-xl font-bold mb-2">{lesson.name}</h3>
        <p className="text-gray-500 text-sm mb-4 line-clamp-2">
          {lesson.description || "No description available."}
        </p>
        
        <div className="flex items-center justify-between mb-4">
          <div className="text-sm">
            <span className="font-medium">{lesson.masteredCards}</span>
            <span className="text-gray-500"> / {lesson.totalCards} cards</span>
          </div>
          <div className="text-sm">
            <span className="font-medium text-amber-600">
              {lesson.dueCards} due
            </span>
          </div>
        </div>
        
        <div className="mb-6">
          <Progress
            value={progressPercentage}
            className={`h-2 ${getProgressColor(progressPercentage)}`}
          />
        </div>
        
        <div className="flex gap-3">
          <Button asChild className="flex-1">
            <a href={`/study?lessonId=${lesson.id}`}>Study Now</a>
          </Button>
          <Button variant="outline" asChild className="flex-1">
            <a href={`/lesson/${lesson.id}`}>View Details</a>
          </Button>
        </div>
      </div>
    </Card>
  );
}