2024-08-06 15:50:19 -05:00
|
|
|
import React, { useEffect, useState, useCallback } from "react";
|
2024-02-27 18:29:57 -06:00
|
|
|
import { useRouter } from "next/router";
|
2024-07-30 17:16:09 -05:00
|
|
|
import { parseCourseEvent, parseEvent, findKind0Fields } from "@/utils/nostr";
|
2024-08-13 13:19:22 -05:00
|
|
|
import CourseDetails from "@/components/content/courses/CourseDetails";
|
2024-08-16 17:08:35 -05:00
|
|
|
import CourseLesson from "@/components/content/courses/CourseLesson";
|
2024-07-21 19:56:55 -05:00
|
|
|
import dynamic from 'next/dynamic';
|
2024-08-06 15:50:19 -05:00
|
|
|
import { useNDKContext } from "@/context/NDKContext";
|
2024-08-16 18:00:46 -05:00
|
|
|
import { useToast } from '@/hooks/useToast';
|
|
|
|
import { useSession } from 'next-auth/react';
|
|
|
|
import { nip04 } from 'nostr-tools';
|
2024-08-06 15:50:19 -05:00
|
|
|
|
2024-07-21 19:56:55 -05:00
|
|
|
const MDDisplay = dynamic(
|
|
|
|
() => import("@uiw/react-markdown-preview"),
|
|
|
|
{
|
|
|
|
ssr: false,
|
|
|
|
}
|
|
|
|
);
|
2024-02-27 18:29:57 -06:00
|
|
|
|
|
|
|
const Course = () => {
|
|
|
|
const [course, setCourse] = useState(null);
|
2024-07-30 17:16:09 -05:00
|
|
|
const [lessonIds, setLessonIds] = useState([]);
|
|
|
|
const [lessons, setLessons] = useState([]);
|
2024-08-16 18:00:46 -05:00
|
|
|
const [paidCourse, setPaidCourse] = useState(false);
|
|
|
|
const [decryptedContent, setDecryptedContent] = useState(null);
|
2024-02-27 18:29:57 -06:00
|
|
|
|
|
|
|
const router = useRouter();
|
2024-08-13 16:28:25 -05:00
|
|
|
const {ndk, addSigner} = useNDKContext();
|
2024-08-16 18:00:46 -05:00
|
|
|
const { data: session, update } = useSession();
|
|
|
|
const { showToast } = useToast();
|
|
|
|
const privkey = process.env.NEXT_PUBLIC_APP_PRIV_KEY;
|
|
|
|
const pubkey = process.env.NEXT_PUBLIC_APP_PUBLIC_KEY;
|
2024-02-27 18:29:57 -06:00
|
|
|
|
2024-08-06 15:50:19 -05:00
|
|
|
const fetchAuthor = useCallback(async (pubkey) => {
|
|
|
|
const author = await ndk.getUser({ pubkey });
|
|
|
|
const profile = await author.fetchProfile();
|
|
|
|
const fields = await findKind0Fields(profile);
|
2024-07-30 17:16:09 -05:00
|
|
|
if (fields) {
|
|
|
|
return fields;
|
|
|
|
}
|
2024-08-06 15:50:19 -05:00
|
|
|
}, [ndk]);
|
2024-07-30 17:16:09 -05:00
|
|
|
|
2024-02-27 18:29:57 -06:00
|
|
|
useEffect(() => {
|
2024-08-06 15:50:19 -05:00
|
|
|
if (router.isReady) {
|
|
|
|
const { slug } = router.query;
|
|
|
|
|
|
|
|
const fetchCourse = async (slug) => {
|
|
|
|
try {
|
|
|
|
await ndk.connect();
|
|
|
|
|
|
|
|
const filter = {
|
|
|
|
ids: [slug]
|
|
|
|
}
|
|
|
|
|
|
|
|
const event = await ndk.fetchEvent(filter);
|
|
|
|
|
|
|
|
if (event) {
|
|
|
|
const author = await fetchAuthor(event.pubkey);
|
|
|
|
const aTags = event.tags.filter(tag => tag[0] === 'a');
|
|
|
|
const lessonIds = aTags.map(tag => tag[1].split(':')[2]);
|
|
|
|
setLessonIds(lessonIds);
|
|
|
|
const parsedCourse = {
|
|
|
|
...parseCourseEvent(event),
|
|
|
|
author
|
|
|
|
};
|
|
|
|
setCourse(parsedCourse);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error fetching event:', error);
|
2024-07-30 17:16:09 -05:00
|
|
|
}
|
2024-08-06 15:50:19 -05:00
|
|
|
};
|
|
|
|
if (ndk) {
|
|
|
|
fetchCourse(slug);
|
2024-02-27 18:29:57 -06:00
|
|
|
}
|
|
|
|
}
|
2024-08-06 15:50:19 -05:00
|
|
|
}, [router.isReady, router.query, ndk, fetchAuthor]);
|
2024-02-27 18:29:57 -06:00
|
|
|
|
2024-07-30 17:16:09 -05:00
|
|
|
useEffect(() => {
|
|
|
|
if (lessonIds.length > 0) {
|
|
|
|
|
|
|
|
const fetchLesson = async (lessonId) => {
|
|
|
|
try {
|
2024-08-06 15:50:19 -05:00
|
|
|
await ndk.connect();
|
|
|
|
|
|
|
|
const filter = {
|
|
|
|
"#d": [lessonId]
|
|
|
|
}
|
|
|
|
|
|
|
|
const event = await ndk.fetchEvent(filter);
|
|
|
|
|
|
|
|
if (event) {
|
|
|
|
const author = await fetchAuthor(event.pubkey);
|
|
|
|
const parsedLesson = {
|
|
|
|
...parseEvent(event),
|
|
|
|
author
|
|
|
|
};
|
|
|
|
setLessons(prev => [...prev, parsedLesson]);
|
2024-07-30 17:16:09 -05:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2024-08-06 15:50:19 -05:00
|
|
|
console.error('Error fetching event:', error);
|
2024-07-30 17:16:09 -05:00
|
|
|
}
|
2024-08-06 15:50:19 -05:00
|
|
|
};
|
2024-07-30 17:16:09 -05:00
|
|
|
|
|
|
|
lessonIds.forEach(lessonId => fetchLesson(lessonId));
|
|
|
|
}
|
2024-08-06 15:50:19 -05:00
|
|
|
}, [lessonIds, ndk, fetchAuthor]);
|
2024-07-30 17:16:09 -05:00
|
|
|
|
2024-08-16 18:00:46 -05:00
|
|
|
useEffect(() => {
|
|
|
|
if (course?.price) {
|
|
|
|
setPaidCourse(true);
|
|
|
|
}
|
|
|
|
}, [course]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const decryptContent = async () => {
|
|
|
|
if (session?.user && paidCourse) {
|
|
|
|
if (session.user?.purchased?.length > 0) {
|
|
|
|
const purchasedCourse = session.user.purchased.find(purchase => purchase.resourceId === course.d);
|
|
|
|
if (purchasedCourse) {
|
|
|
|
const decryptedContent = await nip04.decrypt(privkey, pubkey, course.content);
|
|
|
|
setDecryptedContent(decryptedContent);
|
|
|
|
}
|
|
|
|
} else if (session.user?.role && session.user.role.subscribed) {
|
|
|
|
const decryptedContent = await nip04.decrypt(privkey, pubkey, course.content);
|
|
|
|
setDecryptedContent(decryptedContent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
decryptContent();
|
|
|
|
}, [session, paidCourse, course]);
|
|
|
|
|
|
|
|
const handlePaymentSuccess = async (response, newCourse) => {
|
|
|
|
if (response && response?.preimage) {
|
|
|
|
console.log("newCourse", newCourse);
|
|
|
|
const updated = await update();
|
|
|
|
console.log("session after update", updated);
|
|
|
|
} else {
|
|
|
|
showToast('error', 'Error', 'Failed to purchase course. Please try again.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const handlePaymentError = (error) => {
|
|
|
|
showToast('error', 'Payment Error', `Failed to purchase course. Please try again. Error: ${error}`);
|
|
|
|
}
|
|
|
|
|
2024-02-27 18:29:57 -06:00
|
|
|
return (
|
2024-07-30 17:16:09 -05:00
|
|
|
<>
|
2024-08-16 18:00:46 -05:00
|
|
|
<CourseDetails
|
|
|
|
processedEvent={course}
|
|
|
|
paidCourse={paidCourse}
|
|
|
|
decryptedContent={decryptedContent}
|
|
|
|
handlePaymentSuccess={handlePaymentSuccess}
|
|
|
|
handlePaymentError={handlePaymentError}
|
|
|
|
/>
|
2024-08-01 16:31:52 -05:00
|
|
|
{lessons.length > 0 && lessons.map((lesson, index) => (
|
2024-08-01 17:10:43 -05:00
|
|
|
<CourseLesson key={index} lesson={lesson} course={course} />
|
|
|
|
))}
|
2024-02-27 18:29:57 -06:00
|
|
|
<div className="mx-auto my-6">
|
|
|
|
{
|
2024-07-21 19:56:55 -05:00
|
|
|
course?.content && <MDDisplay source={course.content} />
|
2024-02-27 18:29:57 -06:00
|
|
|
}
|
|
|
|
</div>
|
2024-07-30 17:16:09 -05:00
|
|
|
</>
|
2024-02-27 18:29:57 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Course;
|