import React, { useEffect, useState, useRef } from 'react'; import { Tag } from 'primereact/tag'; import Image from 'next/image'; import ZapDisplay from '@/components/zaps/ZapDisplay'; import { useImageProxy } from '@/hooks/useImageProxy'; import { useZapsQuery } from '@/hooks/nostrQueries/zaps/useZapsQuery'; import { nip19 } from 'nostr-tools'; import { Divider } from 'primereact/divider'; import { getTotalFromZaps } from '@/utils/lightning'; import dynamic from 'next/dynamic'; import useWindowWidth from '@/hooks/useWindowWidth'; import appConfig from '@/config/appConfig'; import useTrackDocumentLesson from '@/hooks/tracking/useTrackDocumentLesson'; import { Toast } from 'primereact/toast'; import MoreOptionsMenu from '@/components/ui/MoreOptionsMenu'; import { useSession } from 'next-auth/react'; const MDDisplay = dynamic(() => import('@uiw/react-markdown-preview'), { ssr: false, }); const DocumentLesson = ({ lesson, course, decryptionPerformed, isPaid, setCompleted }) => { const [zapAmount, setZapAmount] = useState(0); const [nAddress, setNAddress] = useState(null); const { zaps, zapsLoading, zapsError } = useZapsQuery({ event: lesson, type: 'lesson' }); const { returnImageProxy } = useImageProxy(); const windowWidth = useWindowWidth(); const isMobileView = windowWidth <= 768; const menuRef = useRef(null); const toastRef = useRef(null); // todo implement real read time needs to be on form const readTime = 120; const { data: session } = useSession(); const { isCompleted, isTracking, markLessonAsCompleted } = useTrackDocumentLesson({ lessonId: lesson?.d, courseId: course?.d, readTime: readTime, paidCourse: isPaid, decryptionPerformed: decryptionPerformed, }); const buildMenuItems = () => { const items = []; const hasAccess = session?.user && (!isPaid || decryptionPerformed || session.user.role?.subscribed); if (hasAccess) { items.push({ label: 'Mark as completed', icon: 'pi pi-check-circle', command: async () => { try { await markLessonAsCompleted(); setCompleted && setCompleted(lesson.id); toastRef.current.show({ severity: 'success', summary: 'Success', detail: 'Lesson marked as completed', life: 3000, }); } catch (error) { console.error('Failed to mark lesson as completed:', error); toastRef.current.show({ severity: 'error', summary: 'Error', detail: 'Failed to mark lesson as completed', life: 3000, }); } }, }); } items.push({ label: 'Open lesson', icon: 'pi pi-arrow-up-right', command: () => { window.open(`/details/${lesson.id}`, '_blank'); }, }); items.push({ label: 'View Nostr note', icon: 'pi pi-globe', command: () => { window.open(`https://habla.news/a/${nAddress}`, '_blank'); }, }); return items; }; useEffect(() => { if (!zaps || zapsLoading || zapsError) return; const total = getTotalFromZaps(zaps, lesson); setZapAmount(total); }, [zaps, zapsLoading, zapsError, lesson]); useEffect(() => { if (lesson) { const addr = nip19.naddrEncode({ pubkey: lesson.pubkey, kind: lesson.kind, identifier: lesson.d, relays: appConfig.defaultRelayUrls, }); setNAddress(addr); } }, [lesson]); useEffect(() => { if (isCompleted && !isTracking) { setCompleted(lesson.id); } }, [isCompleted, lesson.id, setCompleted, isTracking]); const renderContent = () => { if (isPaid && decryptionPerformed) { return ; } if (isPaid && !decryptionPerformed) { return (

This content is paid and needs to be purchased before viewing.

); } if (lesson?.content) { return ; } return null; }; return (
lesson background image

{lesson.title}

{lesson.topics && lesson.topics.length > 0 && lesson.topics.map((topic, index) => ( ))}
{lesson.summary && (
{lesson.summary.split('\n').map((line, index) => (

{line}

))}
)}
{renderContent()}
); }; export default DocumentLesson;