|
| 1 | +import { cache } from 'react' |
| 2 | +import { createAdminClient } from '@/lib/supabase/admin' |
| 3 | +import { getCurrentTenantId } from '@/lib/supabase/tenant' |
| 4 | +import { getTranslations } from 'next-intl/server' |
| 5 | +import { notFound } from 'next/navigation' |
| 6 | +import Link from 'next/link' |
| 7 | +import { ArrowLeft, Sparkles } from 'lucide-react' |
| 8 | +import { Button } from '@/components/ui/button' |
| 9 | +import { LessonContent } from '@/app/[locale]/dashboard/student/courses/[courseId]/lessons/[lessonId]/lesson-content' |
| 10 | +import { serializeLessonMdx } from '@/app/[locale]/dashboard/student/courses/[courseId]/lessons/[lessonId]/serialize-lesson' |
| 11 | +import type { Metadata } from 'next' |
| 12 | +import { buildPageMetadata } from '@/lib/seo' |
| 13 | + |
| 14 | +export const dynamic = 'force-dynamic' |
| 15 | + |
| 16 | +interface PageProps { |
| 17 | + params: Promise<{ id: string; lessonId: string; locale: string }> |
| 18 | +} |
| 19 | + |
| 20 | +// Public free-preview lesson view (#426). Reachable by anyone — including |
| 21 | +// logged-out visitors (/courses/* is in the proxy public-route allowlist). |
| 22 | +// Uses the admin client with explicit guards mirroring the anon RLS policy: |
| 23 | +// published preview lesson, published course, current tenant. Read-only: |
| 24 | +// no completion tracking, no checkpoints, no comments, no AI task. |
| 25 | +// cache() dedupes the generateMetadata + page calls within a request. |
| 26 | +const getPreviewLesson = cache(async (courseId: number, lessonId: number, tenantId: string) => { |
| 27 | + if (!Number.isInteger(courseId) || !Number.isInteger(lessonId)) return null |
| 28 | + const admin = createAdminClient() |
| 29 | + |
| 30 | + const [{ data: course }, { data: lesson }] = await Promise.all([ |
| 31 | + admin |
| 32 | + .from('courses') |
| 33 | + .select('course_id, title') |
| 34 | + .eq('course_id', courseId) |
| 35 | + .eq('tenant_id', tenantId) |
| 36 | + .eq('status', 'published') |
| 37 | + .single(), |
| 38 | + admin |
| 39 | + .from('lessons') |
| 40 | + .select('id, title, sequence, description, content, video_url, embed_code') |
| 41 | + .eq('id', lessonId) |
| 42 | + .eq('course_id', courseId) |
| 43 | + .eq('tenant_id', tenantId) |
| 44 | + .eq('status', 'published') |
| 45 | + .eq('is_preview', true) |
| 46 | + .single(), |
| 47 | + ]) |
| 48 | + if (!course || !lesson) return null |
| 49 | + |
| 50 | + return { course, lesson } |
| 51 | +}) |
| 52 | + |
| 53 | +export async function generateMetadata(props: PageProps): Promise<Metadata> { |
| 54 | + const { id, lessonId, locale } = await props.params |
| 55 | + const tenantId = await getCurrentTenantId() |
| 56 | + const data = await getPreviewLesson(Number(id), Number(lessonId), tenantId) |
| 57 | + if (!data) notFound() |
| 58 | + return buildPageMetadata({ |
| 59 | + title: `${data.lesson.title} — ${data.course.title}`, |
| 60 | + description: data.lesson.description?.replace(/\s+/g, ' ').trim().slice(0, 160) || data.course.title, |
| 61 | + path: `/courses/${id}/lessons/${lessonId}`, |
| 62 | + locale, |
| 63 | + }) |
| 64 | +} |
| 65 | + |
| 66 | +export default async function PublicLessonPreviewPage(props: PageProps) { |
| 67 | + const { id, lessonId } = await props.params |
| 68 | + const [t, tenantId] = await Promise.all([ |
| 69 | + getTranslations('coursePublicDetails'), |
| 70 | + getCurrentTenantId(), |
| 71 | + ]) |
| 72 | + |
| 73 | + const data = await getPreviewLesson(Number(id), Number(lessonId), tenantId) |
| 74 | + if (!data) notFound() |
| 75 | + const { course, lesson } = data |
| 76 | + |
| 77 | + return ( |
| 78 | + // pt-16 clears the public layout's fixed navbar |
| 79 | + <div className="min-h-screen bg-background pt-16"> |
| 80 | + {/* Preview notice banner */} |
| 81 | + <div className="border-b bg-primary/5"> |
| 82 | + <div className="container mx-auto px-4 py-3 flex flex-wrap items-center justify-between gap-3"> |
| 83 | + <div className="flex items-center gap-2 min-w-0 text-sm"> |
| 84 | + <Sparkles className="w-4 h-4 text-primary flex-shrink-0" aria-hidden="true" /> |
| 85 | + <span className="font-semibold text-primary flex-shrink-0">{t('preview.badge')}</span> |
| 86 | + <span className="text-muted-foreground truncate">{t('preview.notice')}</span> |
| 87 | + </div> |
| 88 | + <Link href={`/courses/${id}`} className="flex-shrink-0"> |
| 89 | + <Button variant="outline" size="sm" className="gap-1.5"> |
| 90 | + <ArrowLeft className="w-4 h-4" aria-hidden="true" /> |
| 91 | + {t('preview.backToCourse')} |
| 92 | + </Button> |
| 93 | + </Link> |
| 94 | + </div> |
| 95 | + </div> |
| 96 | + |
| 97 | + <div className="mx-auto max-w-4xl px-4 py-8 md:px-6 md:py-10 space-y-10"> |
| 98 | + {/* Lesson header */} |
| 99 | + <header> |
| 100 | + <p className="text-[10px] font-bold uppercase tracking-widest text-muted-foreground/60 mb-1"> |
| 101 | + {course.title} |
| 102 | + </p> |
| 103 | + <h1 className="text-2xl md:text-3xl font-bold tracking-tight text-balance"> |
| 104 | + {lesson.title} |
| 105 | + </h1> |
| 106 | + {lesson.description && ( |
| 107 | + <p className="mt-2 text-muted-foreground leading-relaxed">{lesson.description}</p> |
| 108 | + )} |
| 109 | + </header> |
| 110 | + |
| 111 | + <LessonContent |
| 112 | + mdx={await serializeLessonMdx(lesson.content)} |
| 113 | + videoUrl={lesson.video_url} |
| 114 | + embedCode={lesson.embed_code} |
| 115 | + embedMode="sandboxed" |
| 116 | + /> |
| 117 | + |
| 118 | + {/* Enroll CTA */} |
| 119 | + <section className="rounded-2xl border-2 border-primary/10 bg-gradient-to-b from-primary/[0.06] to-transparent p-6 md:p-8 text-center space-y-4"> |
| 120 | + <h2 className="text-xl font-bold">{t('preview.enrollTitle')}</h2> |
| 121 | + <Link href={`/courses/${id}`} className="inline-block"> |
| 122 | + <Button size="lg" className="gap-2"> |
| 123 | + {t('preview.enrollCta')} |
| 124 | + </Button> |
| 125 | + </Link> |
| 126 | + </section> |
| 127 | + </div> |
| 128 | + </div> |
| 129 | + ) |
| 130 | +} |
0 commit comments