From f243847216279cbd43879de8b5ef6dcceb3a2f1d Mon Sep 17 00:00:00 2001
From: polwex <polwex@sortug.com>
Date: Thu, 29 May 2025 14:08:02 +0700
Subject: lets see

---
 src/pages/study.tsx | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 128 insertions(+)
 create mode 100644 src/pages/study.tsx

(limited to 'src/pages/study.tsx')

diff --git a/src/pages/study.tsx b/src/pages/study.tsx
new file mode 100644
index 0000000..db7dde7
--- /dev/null
+++ b/src/pages/study.tsx
@@ -0,0 +1,128 @@
+import { useState } from "react";
+import { getState } from "@/lib/db";
+import { startStudySession } from "@/actions/srs";
+import StudySession from "@/components/Flashcard/StudySession";
+import { Button } from "@/components/ui/button";
+import { Card } from "@/components/ui/card";
+import { Input } from "@/components/ui/input";
+import { Label } from "@/components/ui/label";
+
+// This is a server component that gets the initial data
+export default async function StudyPage({ searchParams }: { searchParams: { lessonId?: string } }) {
+  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 use the study session feature.</p>
+          <Button asChild>
+            <a href="/login">Login</a>
+          </Button>
+        </Card>
+      </div>
+    );
+  }
+  
+  const lessonId = searchParams.lessonId ? parseInt(searchParams.lessonId, 10) : null;
+  
+  // If no lesson ID provided, show lesson selector
+  if (!lessonId) {
+    return <LessonSelector userId={userId} />;
+  }
+  
+  // Get initial data for the study session
+  let initialData;
+  try {
+    initialData = await startStudySession(userId, lessonId, true);
+  } catch (error) {
+    console.error("Error starting study session:", error);
+  }
+  
+  return (
+    <div className="container mx-auto py-8">
+      <StudySession
+        userId={userId}
+        lessonId={lessonId}
+        initialData={initialData && !('error' in initialData) ? initialData : undefined}
+      />
+    </div>
+  );
+}
+
+// Client component for selecting a lesson
+function LessonSelector({ userId }: { userId: number }) {
+  const [lessonId, setLessonId] = useState<string>("");
+  
+  return (
+    <div className="container mx-auto py-8">
+      <Card className="p-6 max-w-md mx-auto">
+        <h1 className="text-2xl font-bold mb-6">Start Study Session</h1>
+        
+        <form action={`/study?lessonId=${lessonId}`}>
+          <div className="space-y-4">
+            <div>
+              <Label htmlFor="lessonId">Lesson ID</Label>
+              <Input
+                id="lessonId"
+                value={lessonId}
+                onChange={(e) => setLessonId(e.target.value)}
+                placeholder="Enter lesson ID"
+                type="number"
+                required
+              />
+            </div>
+            
+            <Button type="submit" className="w-full">
+              Start Study Session
+            </Button>
+          </div>
+        </form>
+        
+        <div className="mt-6 pt-6 border-t border-gray-200">
+          <h2 className="text-lg font-medium mb-3">Available Lessons</h2>
+          <p className="text-sm text-gray-500 mb-4">
+            Here are some example lesson IDs you can use:
+          </p>
+          <div className="flex flex-wrap gap-2">
+            <Button
+              variant="outline"
+              size="sm"
+              onClick={() => setLessonId("1")}
+            >
+              Lesson 1
+            </Button>
+            <Button
+              variant="outline"
+              size="sm"
+              onClick={() => setLessonId("2")}
+            >
+              Lesson 2
+            </Button>
+            <Button
+              variant="outline"
+              size="sm"
+              onClick={() => setLessonId("5")}
+            >
+              Lesson 5
+            </Button>
+          </div>
+          
+          <div className="mt-4">
+            <Button
+              variant="ghost"
+              size="sm"
+              asChild
+              className="text-blue-500"
+            >
+              <a href="/">Back to Home</a>
+            </Button>
+          </div>
+        </div>
+      </Card>
+    </div>
+  );
+}
\ No newline at end of file
-- 
cgit v1.2.3

