"use client"; import { useEffect, useState } from "react"; import Link from "next/link"; import { useI18n } from "@/lib/i18n"; import { useAuth } from "@/lib/auth"; import { api } from "@/lib/api"; import type { DigitalCourse, DigitalLesson } from "@/lib/types"; import RingNext from "@/components/RingNext"; import { useRingSteps } from "@/lib/rings"; export default function DigitalLessonClient({ moduleIndex, lessonIndex, }: { moduleIndex: number; lessonIndex: number; }) { const { t } = useI18n(); const { token } = useAuth(); const rings = useRingSteps(); const [lesson, setLesson] = useState(null); const [locked, setLocked] = useState(false); const [missing, setMissing] = useState(false); const [loading, setLoading] = useState(true); const [hasNext, setHasNext] = useState(false); const [nextHref, setNextHref] = useState(null); const [prevHref, setPrevHref] = useState(null); useEffect(() => { let cancelled = false; setLoading(true); (async () => { try { const course: DigitalCourse = await api.getDigitalCourse(); const modules = course.modules || []; const mod = modules[moduleIndex]; const meta = mod?.lessons?.[lessonIndex]; if (!meta) { if (!cancelled) { setMissing(true); setLocked(false); setLesson(null); setHasNext(false); setNextHref(null); setPrevHref(null); } return; } let next: string | null = null; if (mod.lessons[lessonIndex + 1]) { next = `/digital/course/${moduleIndex}/${lessonIndex + 1}`; } else if (modules[moduleIndex + 1]?.lessons?.length) { next = `/digital/course/${moduleIndex + 1}/0`; } let prev: string | null = null; if (lessonIndex > 0) { prev = `/digital/course/${moduleIndex}/${lessonIndex - 1}`; } else if (moduleIndex > 0) { const prevMod = modules[moduleIndex - 1]; const last = (prevMod?.lessons?.length || 1) - 1; if (prevMod?.lessons?.length) { prev = `/digital/course/${moduleIndex - 1}/${last}`; } } if (!cancelled) { setHasNext(Boolean(next)); setNextHref(next); setPrevHref(prev); } try { const data = await api.getDigitalLesson(moduleIndex, lessonIndex, token || undefined); if (!cancelled) { setLesson(data); setLocked(false); setMissing(false); } } catch { if (!cancelled) { setLesson(null); setLocked(!meta.free); setMissing(meta.free); } } } catch { if (!cancelled) { setMissing(true); setLesson(null); } } finally { if (!cancelled) setLoading(false); } })(); return () => { cancelled = true; }; }, [moduleIndex, lessonIndex, token]); if (loading) { return (

{t.digital.loadingLesson}

); } if (missing) { return (

{t.digital.missingLesson}

{t.digital.backCourse}
); } if (locked || !lesson) { return (

{t.digital.locked}

{t.digital.lockedDesc}

{!token && ( {t.nav.login} )} {t.join.payBtn} {t.digital.backCourse}
); } const prev = prevHref; const next = hasNext ? nextHref : null; return (
{lesson.duration}

{lesson.title}

{lesson.content.split("\n").filter(Boolean).map((p, i) => (

{p}

))}
{prev && ( ← {t.digital.prev} )} {next ? ( {t.digital.next} → ) : (
{t.digital.backCatalog} {t.nav.gigs} {t.nav.meetups}
)}
); }