import { useEffect, useState } from "react"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, } from "../components/ui/dialog"; import { api } from "../utils/api"; import { useAuthStore } from "../stores/authStore"; interface LessonModalProps { lessonId: string | null; open: boolean; onOpenChange: (open: boolean) => void; } export const LessonModal = ({ lessonId, open, onOpenChange, }: LessonModalProps) => { const user = useAuthStore((state) => state.user); const [loading, setLoading] = useState(false); const [lesson, setLesson] = useState(null); useEffect(() => { if (!open || !lessonId || !user) return; const fetchLesson = async () => { try { setLoading(true); const authStorage = localStorage.getItem("auth-storage"); if (!authStorage) return; const parsed = JSON.parse(authStorage) as { state?: { token?: string }; }; const token = parsed.state?.token; if (!token) return; const response = await api.fetchLessonById(token, lessonId); setLesson(response); } catch (err) { console.error("Failed to fetch lesson", err); } finally { setLoading(false); } }; fetchLesson(); }, [open, lessonId, user]); return ( {loading && (
Loading lesson...
)} {lesson ? lesson.title : "Lesson details"} {!loading && lesson && (
{lesson.video_url && (
)}
); };