import { prisma } from "@/lib/prisma"; import { notFound } from "next/navigation"; import Link from "next/link"; import { headers } from "next/headers"; import { auth } from "@/lib/auth"; import { KinescopePlayer } from "@/components/player/kinescope-player"; import { LessonContent } from "@/components/student/lesson-content"; import { LessonCompleteButton } from "@/components/student/lesson-complete-button"; import { HomeworkSection } from "@/components/student/homework-section"; import { QuizSection } from "@/components/student/quiz-section"; import { LessonComments } from "@/components/student/lesson-comments"; import { FileFormatBadge } from "@/components/shared/file-format-badge"; interface Props { params: Promise<{ slug: string; lessonId: string }>; } export default async function LessonPage({ params }: Props) { const { slug, lessonId } = await params; const session = await auth.api.getSession({ headers: await headers() }); const isAdmin = session?.user.role === "admin"; const [lesson, progress, comments] = await Promise.all([ prisma.lesson.findUnique({ where: { id: lessonId, ...(isAdmin ? {} : { published: true }) }, include: { files: { orderBy: { createdAt: "asc" } }, homework: true, quiz: { include: { questions: { orderBy: { order: "asc" } } }, }, module: { include: { course: { include: { modules: { orderBy: { order: "asc" }, include: { lessons: { where: { published: true }, orderBy: { order: "asc" }, select: { id: true, title: true }, }, }, }, }, }, }, }, }, }), session && !isAdmin ? prisma.lessonProgress.findUnique({ where: { userId_lessonId: { userId: session.user.id, lessonId } }, }) : null, prisma.lessonComment.findMany({ where: { lessonId, parentId: null }, orderBy: { createdAt: "asc" }, include: { user: { select: { id: true, name: true } }, replies: { orderBy: { createdAt: "asc" }, include: { user: { select: { id: true, name: true } } }, }, }, }), ]); // Fetch homework submission for this student const homeworkSubmission = lesson?.homework && session && !isAdmin ? await prisma.homeworkSubmission.findFirst({ where: { homeworkId: lesson.homework.id, userId: session.user.id }, include: { feedbacks: { include: { curator: { select: { name: true } } }, orderBy: { createdAt: "asc" }, }, }, }) : null; const quizAttempt = lesson?.quiz && session && !isAdmin ? await prisma.quizAttempt.findFirst({ where: { quizId: lesson.quiz.id, userId: session.user.id }, }) : null; if (!lesson || lesson.module.course.slug !== slug) notFound(); const isCompleted = !!progress; // Build ordered flat list of all lessons for prev/next const allLessons = lesson.module.course.modules.flatMap((m) => m.lessons); const idx = allLessons.findIndex((l) => l.id === lessonId); const prevLesson = idx > 0 ? allLessons[idx - 1] : null; const nextLesson = idx < allLessons.length - 1 ? allLessons[idx + 1] : null; const hasContent = lesson.content && Object.keys(lesson.content as object).length > 0; function formatSize(bytes: number) { if (bytes < 1024) return `${bytes} Б`; if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(0)} КБ`; return `${(bytes / 1024 / 1024).toFixed(1)} МБ`; } return (
{/* Admin edit bar */} {isAdmin && (
Редактировать урок
)} {/* Title */}

{lesson.title}

{/* Video */} {lesson.kinescopeId && (
{/* DEBUG: remove after fix */} {isAdmin &&

coverImage: {lesson.coverImage ?? "NULL"}

}
)} {/* Text content */} {hasContent && (
)} {/* Files */} {lesson.files.length > 0 && (

Материалы урока

{lesson.files.map((file) => ( {file.name} {formatSize(file.size)} Скачать ))}
)} {/* Homework */} {lesson.homework && !isAdmin && (

Домашнее задание

({ ...fb, files: (fb.files as { name: string; url: string; size: number }[]) ?? [], audioUrl: fb.audioUrl ?? null, })), } : null} slug={slug} lessonId={lessonId} allowAudio={lesson.module.course.allowAudio} />
)} {/* Quiz */} {lesson.quiz && (

Тест{isAdmin && (предпросмотр)}

{isAdmin ? (
{lesson.quiz.questions.map((q, idx) => (

{idx + 1}. {q.text}

Поле для ответа студента
))}
) : ( } : null} slug={slug} lessonId={lessonId} /> )}
)} {/* Complete button + Prev/Next navigation */}
{prevLesson ? ( ← {prevLesson.title} ) : (
)} {!isAdmin && !lesson.homework && !lesson.quiz && ( )} {!isAdmin && (lesson.homework || lesson.quiz) && isCompleted && ( )} {nextLesson ? ( {nextLesson.title} → ) : (
{isAdmin ? "Последний урок курса" : ""}
)}
{/* Comments */} {session && (

Обсуждение ({ comments.filter(c => !c.deleted).length + comments.flatMap(c => c.replies).filter(r => !r.deleted).length })

)}
); }