import React, { useEffect, useState, useCallback } from 'react';
import axios from 'axios';
import { useToast } from "@/hooks/useToast";
import { Tag } from 'primereact/tag';
import Image from 'next/image';
import { useRouter } from 'next/router';
import CoursePaymentButton from "@/components/bitcoinConnect/CoursePaymentButton";
import ZapDisplay from '@/components/zaps/ZapDisplay';
import GenericButton from '@/components/buttons/GenericButton';
import { nip19 } from 'nostr-tools';
import { useImageProxy } from '@/hooks/useImageProxy';
import { useZapsSubscription } from '@/hooks/nostrQueries/zaps/useZapsSubscription';
import { getTotalFromZaps } from '@/utils/lightning';
import { useSession } from 'next-auth/react';
import useWindowWidth from "@/hooks/useWindowWidth";
import { useNDKContext } from "@/context/NDKContext";
import { findKind0Fields } from '@/utils/nostr';
import appConfig from "@/config/appConfig";
import useTrackCourse from '@/hooks/tracking/useTrackCourse';
import { ProgressSpinner } from 'primereact/progressspinner';
export default function CourseDetails({ processedEvent, paidCourse, lessons, decryptionPerformed, handlePaymentSuccess, handlePaymentError }) {
const [zapAmount, setZapAmount] = useState(0);
const [author, setAuthor] = useState(null);
const [nAddress, setNAddress] = useState(null);
const router = useRouter();
const { returnImageProxy } = useImageProxy();
const { zaps, zapsLoading, zapsError } = useZapsSubscription({ event: processedEvent });
const { data: session, status } = useSession();
const { showToast } = useToast();
const windowWidth = useWindowWidth();
const isMobileView = windowWidth <= 768;
const { ndk } = useNDKContext();
const { isCompleted } = useTrackCourse({
courseId: processedEvent?.d,
paidCourse,
decryptionPerformed
});
const fetchAuthor = useCallback(async (pubkey) => {
if (!pubkey) return;
const author = await ndk.getUser({ pubkey });
const profile = await author.fetchProfile();
const fields = await findKind0Fields(profile);
if (fields) {
setAuthor(fields);
}
}, [ndk]);
useEffect(() => {
if (processedEvent) {
const naddr = nip19.naddrEncode({
pubkey: processedEvent.pubkey,
kind: processedEvent.kind,
identifier: processedEvent.d,
relays: appConfig.defaultRelayUrls
});
setNAddress(naddr);
}
}, [processedEvent]);
useEffect(() => {
if (processedEvent) {
fetchAuthor(processedEvent.pubkey);
}
}, [fetchAuthor, processedEvent]);
useEffect(() => {
if (zaps.length > 0) {
const total = getTotalFromZaps(zaps, processedEvent);
setZapAmount(total);
}
}, [zaps, processedEvent]);
const handleDelete = async () => {
try {
const response = await axios.delete(`/api/courses/${processedEvent.d}`);
if (response.status === 204) {
showToast('success', 'Success', 'Course deleted successfully.');
router.push('/');
}
} catch (error) {
showToast('error', 'Error', 'Failed to delete course. Please try again.');
}
}
const renderPaymentMessage = () => {
if (session?.user && session.user?.role?.subscribed && decryptionPerformed) {
return
Additional Links:
{processedEvent.additionalLinks.map((link, index) => ( ))}{line}
)) )}