diff --git a/src/pages/details/[slug]/index.js b/src/pages/details/[slug]/index.js index 93569ca..4473f00 100644 --- a/src/pages/details/[slug]/index.js +++ b/src/pages/details/[slug]/index.js @@ -30,15 +30,21 @@ const Details = () => { const { showToast } = useToast(); useEffect(() => { - axios.get('/api/lessons').then(res => { - if (res.data) { - res.data.forEach(lesson => { - setLessons(prev => [...prev, { resourceId: lesson?.resourceId, courseId: lesson?.courseId || null }]); - }); + const fetchLessons = async () => { + try { + const res = await axios.get('/api/lessons'); + if (res.data) { + const lessonData = res.data.map(lesson => ({ + resourceId: lesson?.resourceId, + courseId: lesson?.courseId || null + })); + setLessons(lessonData); + } + } catch (err) { + console.error('err', err); } - }).catch(err => { - console.error('err', err); - }); + }; + fetchLessons(); }, []); const fetchAuthor = useCallback(async (pubkey) => { @@ -106,21 +112,7 @@ const Details = () => { const parsedEvent = parseEvent(event); setEvent(parsedEvent); await fetchAuthor(event.pubkey); - - const isAuthor = session?.user?.pubkey === event.pubkey; - setAuthorView(isAuthor); - - if (parsedEvent.price || (isAuthor && event.kind === 30402)) { - const shouldDecrypt = isAuthor || - session?.user?.role?.subscribed || - session?.user?.purchased?.some(purchase => purchase.resourceId === parsedEvent.d) || - lessons.some(lesson => lesson.resourceId === parsedEvent.d && session?.user?.purchased?.some(purchase => purchase.courseId === lesson.courseId)); - - if (shouldDecrypt) { - const decrypted = await decryptContent(event.content); - setDecryptedContent(decrypted); - } - } + setAuthorView(session?.user?.pubkey === event.pubkey); } } catch (error) { console.error('Error fetching event:', error); @@ -131,7 +123,34 @@ const Details = () => { }; fetchAndProcessEvent(); - }, [router.isReady, router.query, ndk, session, decryptContent, fetchAuthor, showToast]); + }, [router.isReady, router.query, ndk, session?.user?.pubkey, fetchAuthor, showToast]); + + useEffect(() => { + const handleDecryption = async () => { + if (!event || !session || !lessons.length) return; + + const isAuthor = session?.user?.pubkey === event.pubkey; + + if (event.price || (isAuthor && event.kind === 30402)) { + const shouldDecrypt = isAuthor || + session?.user?.role?.subscribed || + session?.user?.purchased?.some(purchase => purchase.resourceId === event.d) || + lessons.some(lesson => + lesson.resourceId === event.d && + session?.user?.purchased?.some(purchase => + purchase.courseId === lesson.courseId + ) + ); + + if (shouldDecrypt) { + const decrypted = await decryptContent(event.content); + setDecryptedContent(decrypted); + } + } + }; + + handleDecryption(); + }, [event, session, lessons, decryptContent]); const handlePaymentSuccess = (response) => { if (response && response?.preimage) {