2024-03-16 14:56:58 -05:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2024-08-08 16:29:16 -05:00
|
|
|
import axios from 'axios';
|
2024-03-16 14:56:58 -05:00
|
|
|
import { useRouter } from 'next/router';
|
2024-08-08 16:29:16 -05:00
|
|
|
import { parseEvent, findKind0Fields } from '@/utils/nostr';
|
|
|
|
import { Button } from 'primereact/button';
|
2024-08-06 14:50:32 -05:00
|
|
|
import { nip19, nip04 } from 'nostr-tools';
|
2024-08-07 16:02:13 -05:00
|
|
|
import { useSession } from 'next-auth/react';
|
2024-04-01 18:13:38 -05:00
|
|
|
import dynamic from 'next/dynamic';
|
2024-04-23 18:52:55 -05:00
|
|
|
import ZapThreadsWrapper from '@/components/ZapThreadsWrapper';
|
2024-08-08 16:29:16 -05:00
|
|
|
import { useToast } from '@/hooks/useToast';
|
2024-08-04 18:00:59 -05:00
|
|
|
import { useNDKContext } from '@/context/NDKContext';
|
2024-08-13 13:19:22 -05:00
|
|
|
import ResourceDetails from '@/components/content/resources/ResourceDetails';
|
2024-09-09 17:35:00 -05:00
|
|
|
import {ProgressSpinner} from 'primereact/progressspinner';
|
2024-03-16 14:56:58 -05:00
|
|
|
import 'primeicons/primeicons.css';
|
2024-08-09 14:28:57 -05:00
|
|
|
|
2024-07-21 19:56:55 -05:00
|
|
|
const MDDisplay = dynamic(
|
|
|
|
() => import("@uiw/react-markdown-preview"),
|
|
|
|
{
|
|
|
|
ssr: false,
|
|
|
|
}
|
|
|
|
);
|
2024-03-19 12:32:05 -05:00
|
|
|
|
2024-08-06 14:50:32 -05:00
|
|
|
const privkey = process.env.NEXT_PUBLIC_APP_PRIV_KEY;
|
2024-08-09 14:28:57 -05:00
|
|
|
const pubkey = process.env.NEXT_PUBLIC_APP_PUBLIC_KEY;
|
2024-08-06 14:50:32 -05:00
|
|
|
|
2024-03-16 14:56:58 -05:00
|
|
|
export default function Details() {
|
|
|
|
const [event, setEvent] = useState(null);
|
|
|
|
const [processedEvent, setProcessedEvent] = useState({});
|
2024-03-16 16:37:47 -05:00
|
|
|
const [author, setAuthor] = useState(null);
|
2024-04-23 18:52:55 -05:00
|
|
|
const [nAddress, setNAddress] = useState(null);
|
2024-08-06 14:50:32 -05:00
|
|
|
const [paidResource, setPaidResource] = useState(false);
|
|
|
|
const [decryptedContent, setDecryptedContent] = useState(null);
|
2024-08-08 16:29:16 -05:00
|
|
|
const [authorView, setAuthorView] = useState(false);
|
2024-09-09 17:35:00 -05:00
|
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
const [error, setError] = useState(null);
|
2024-08-04 18:00:59 -05:00
|
|
|
|
2024-08-13 16:28:25 -05:00
|
|
|
const {ndk, addSigner} = useNDKContext();
|
2024-08-12 17:27:47 -05:00
|
|
|
const { data: session, update } = useSession();
|
2024-08-07 16:02:13 -05:00
|
|
|
const [user, setUser] = useState(null);
|
2024-08-08 16:29:16 -05:00
|
|
|
const { showToast } = useToast();
|
2024-03-16 14:56:58 -05:00
|
|
|
|
|
|
|
const router = useRouter();
|
|
|
|
|
2024-08-07 16:02:13 -05:00
|
|
|
useEffect(() => {
|
|
|
|
if (session) {
|
|
|
|
setUser(session.user);
|
|
|
|
}
|
|
|
|
}, [session]);
|
|
|
|
|
2024-08-06 14:50:32 -05:00
|
|
|
useEffect(() => {
|
|
|
|
if (processedEvent.price) {
|
|
|
|
setPaidResource(true);
|
|
|
|
}
|
|
|
|
}, [processedEvent]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const decryptContent = async () => {
|
|
|
|
if (user && paidResource) {
|
2024-08-12 17:27:47 -05:00
|
|
|
if (user?.purchased?.length > 0) {
|
|
|
|
const purchasedResource = user?.purchased.find(purchase => purchase.resourceId === processedEvent.d);
|
|
|
|
if (purchasedResource) {
|
|
|
|
console.log("purchasedResource", purchasedResource)
|
|
|
|
const decryptedContent = await nip04.decrypt(privkey, pubkey, processedEvent.content);
|
|
|
|
setDecryptedContent(decryptedContent);
|
|
|
|
}
|
|
|
|
} else if (user?.role && user?.role.subscribed) {
|
2024-08-09 14:28:57 -05:00
|
|
|
// decrypt the content
|
|
|
|
const decryptedContent = await nip04.decrypt(privkey, pubkey, processedEvent.content);
|
|
|
|
setDecryptedContent(decryptedContent);
|
|
|
|
}
|
2024-08-06 14:50:32 -05:00
|
|
|
}
|
2024-08-12 17:27:47 -05:00
|
|
|
|
2024-08-06 14:50:32 -05:00
|
|
|
}
|
|
|
|
decryptContent();
|
2024-08-06 15:50:19 -05:00
|
|
|
}, [user, paidResource, processedEvent]);
|
2024-08-06 14:50:32 -05:00
|
|
|
|
2024-03-16 14:56:58 -05:00
|
|
|
useEffect(() => {
|
|
|
|
if (router.isReady) {
|
|
|
|
const { slug } = router.query;
|
|
|
|
|
2024-09-09 17:35:00 -05:00
|
|
|
const fetchEvent = async (slug, retryCount = 0) => {
|
|
|
|
setLoading(true);
|
|
|
|
setError(null);
|
2024-08-04 18:00:59 -05:00
|
|
|
try {
|
|
|
|
await ndk.connect();
|
|
|
|
|
|
|
|
const filter = {
|
|
|
|
ids: [slug]
|
|
|
|
}
|
|
|
|
|
|
|
|
const event = await ndk.fetchEvent(filter);
|
|
|
|
|
|
|
|
if (event) {
|
|
|
|
setEvent(event);
|
2024-08-08 16:29:16 -05:00
|
|
|
if (user && user.pubkey === event.pubkey) {
|
2024-09-09 17:35:00 -05:00
|
|
|
const decryptedContent = await nip04.decrypt(privkey, pubkey, event.content);
|
|
|
|
setDecryptedContent(decryptedContent);
|
2024-08-08 16:29:16 -05:00
|
|
|
setAuthorView(true);
|
|
|
|
}
|
2024-09-09 17:35:00 -05:00
|
|
|
} else {
|
|
|
|
if (retryCount < 1) {
|
|
|
|
// Wait for 2 seconds before retrying
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 3000));
|
|
|
|
return fetchEvent(slug, retryCount + 1);
|
|
|
|
} else {
|
|
|
|
setError("Event not found");
|
|
|
|
}
|
2024-08-04 18:00:59 -05:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error fetching event:', error);
|
2024-09-09 17:35:00 -05:00
|
|
|
if (retryCount < 1) {
|
|
|
|
// Wait for 2 seconds before retrying
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 3000));
|
|
|
|
return fetchEvent(slug, retryCount + 1);
|
|
|
|
} else {
|
|
|
|
setError("Failed to fetch event. Please try again.");
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
setLoading(false);
|
2024-03-16 14:56:58 -05:00
|
|
|
}
|
|
|
|
};
|
2024-09-09 17:35:00 -05:00
|
|
|
|
2024-08-04 18:00:59 -05:00
|
|
|
if (ndk) {
|
|
|
|
fetchEvent(slug);
|
|
|
|
}
|
2024-03-16 14:56:58 -05:00
|
|
|
}
|
2024-08-08 16:29:16 -05:00
|
|
|
}, [router.isReady, router.query, ndk, user]);
|
2024-03-16 14:56:58 -05:00
|
|
|
|
2024-03-16 16:37:47 -05:00
|
|
|
useEffect(() => {
|
|
|
|
const fetchAuthor = async (pubkey) => {
|
2024-08-04 18:00:59 -05:00
|
|
|
try {
|
|
|
|
await ndk.connect();
|
|
|
|
|
|
|
|
const filter = {
|
|
|
|
kinds: [0],
|
|
|
|
authors: [pubkey]
|
|
|
|
}
|
|
|
|
|
2024-08-13 13:19:22 -05:00
|
|
|
const author = await ndk.fetchEvent(filter);
|
2024-08-04 18:00:59 -05:00
|
|
|
if (author) {
|
|
|
|
const fields = await findKind0Fields(JSON.parse(author.content));
|
2024-08-13 13:19:22 -05:00
|
|
|
console.log("fields", fields);
|
2024-08-04 18:00:59 -05:00
|
|
|
setAuthor(fields);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error fetching author:', error);
|
2024-03-16 16:37:47 -05:00
|
|
|
}
|
|
|
|
}
|
2024-08-04 18:00:59 -05:00
|
|
|
if (event && ndk) {
|
2024-03-16 16:37:47 -05:00
|
|
|
fetchAuthor(event.pubkey);
|
|
|
|
}
|
2024-08-04 18:00:59 -05:00
|
|
|
}, [ndk, event]);
|
2024-03-16 16:37:47 -05:00
|
|
|
|
2024-03-16 14:56:58 -05:00
|
|
|
useEffect(() => {
|
|
|
|
if (event) {
|
2024-08-05 17:27:19 -05:00
|
|
|
const parsedEvent = parseEvent(event);
|
|
|
|
setProcessedEvent(parsedEvent);
|
2024-03-16 14:56:58 -05:00
|
|
|
}
|
|
|
|
}, [event]);
|
|
|
|
|
2024-04-23 18:52:55 -05:00
|
|
|
useEffect(() => {
|
|
|
|
if (processedEvent?.d) {
|
|
|
|
const naddr = nip19.naddrEncode({
|
|
|
|
pubkey: processedEvent.pubkey,
|
|
|
|
kind: processedEvent.kind,
|
|
|
|
identifier: processedEvent.d,
|
|
|
|
});
|
|
|
|
setNAddress(naddr);
|
|
|
|
}
|
|
|
|
}, [processedEvent]);
|
|
|
|
|
2024-08-08 16:29:16 -05:00
|
|
|
const handleDelete = async () => {
|
|
|
|
try {
|
2024-08-09 14:28:57 -05:00
|
|
|
const response = await axios.delete(`/api/resources/${processedEvent.d}`);
|
2024-08-08 16:29:16 -05:00
|
|
|
if (response.status === 204) {
|
|
|
|
showToast('success', 'Success', 'Resource deleted successfully.');
|
|
|
|
router.push('/');
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (error.response && error.response.data && error.response.data.error.includes("Invalid `prisma.resource.delete()`")) {
|
|
|
|
showToast('error', 'Error', 'Resource cannot be deleted because it is part of a course, delete the course first.');
|
|
|
|
}
|
|
|
|
else if (error.response && error.response.data && error.response.data.error) {
|
|
|
|
showToast('error', 'Error', error.response.data.error);
|
|
|
|
} else {
|
|
|
|
showToast('error', 'Error', 'Failed to delete resource. Please try again.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-13 13:19:22 -05:00
|
|
|
const handlePaymentSuccess = async (response, newResource) => {
|
|
|
|
if (response && response?.preimage) {
|
|
|
|
console.log("newResource", newResource);
|
|
|
|
const updated = await update();
|
|
|
|
console.log("session after update", updated);
|
|
|
|
} else {
|
|
|
|
showToast('error', 'Error', 'Failed to purchase resource. Please try again.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const handlePaymentError = (error) => {
|
|
|
|
showToast('error', 'Payment Error', `Failed to purchase resource. Please try again. Error: ${error}`);
|
|
|
|
}
|
|
|
|
|
2024-08-09 19:00:31 -05:00
|
|
|
const renderContent = () => {
|
|
|
|
if (decryptedContent) {
|
2024-09-03 17:02:24 -05:00
|
|
|
return <MDDisplay className='p-4 rounded-lg' source={decryptedContent} />;
|
2024-08-09 19:00:31 -05:00
|
|
|
}
|
|
|
|
if (paidResource && !decryptedContent) {
|
|
|
|
return <p className="text-center text-xl text-red-500">This content is paid and needs to be purchased before viewing.</p>;
|
|
|
|
}
|
|
|
|
if (processedEvent?.content) {
|
2024-09-03 17:02:24 -05:00
|
|
|
return <MDDisplay className='p-4 rounded-lg' source={processedEvent.content} />;
|
2024-08-09 19:00:31 -05:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-09-09 17:35:00 -05:00
|
|
|
if (loading) {
|
|
|
|
return <div className="mx-auto">
|
|
|
|
<ProgressSpinner />
|
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
return <div className="w-full mx-auto h-screen">
|
|
|
|
<div className="text-red-500 text-xl">{error}</div>
|
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
|
2024-03-16 14:56:58 -05:00
|
|
|
return (
|
2024-03-19 13:04:46 -05:00
|
|
|
<div className='w-full px-24 pt-12 mx-auto mt-4 max-tab:px-0 max-mob:px-0 max-tab:pt-2 max-mob:pt-2'>
|
2024-09-09 17:35:00 -05:00
|
|
|
{processedEvent && (
|
|
|
|
<ResourceDetails
|
|
|
|
processedEvent={processedEvent}
|
|
|
|
topics={processedEvent.topics}
|
|
|
|
title={processedEvent.title}
|
|
|
|
summary={processedEvent.summary}
|
|
|
|
image={processedEvent.image}
|
|
|
|
price={processedEvent.price}
|
|
|
|
author={author}
|
|
|
|
paidResource={paidResource}
|
|
|
|
decryptedContent={decryptedContent}
|
|
|
|
handlePaymentSuccess={handlePaymentSuccess}
|
|
|
|
handlePaymentError={handlePaymentError}
|
|
|
|
/>
|
|
|
|
)}
|
2024-08-08 16:29:16 -05:00
|
|
|
{authorView && (
|
|
|
|
<div className='w-[75vw] mx-auto flex flex-row justify-end mt-12'>
|
|
|
|
<div className='w-fit flex flex-row justify-between'>
|
2024-08-09 19:00:31 -05:00
|
|
|
<Button onClick={() => router.push(`/details/${processedEvent.id}/edit`)} label="Edit" severity='warning' outlined className="w-auto m-2" />
|
|
|
|
<Button onClick={handleDelete} label="Delete" severity='danger' outlined className="w-auto m-2 mr-0" />
|
2024-08-08 16:29:16 -05:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
2024-04-23 18:52:55 -05:00
|
|
|
{typeof window !== 'undefined' && nAddress !== null && (
|
|
|
|
<div className='px-24'>
|
|
|
|
<ZapThreadsWrapper
|
|
|
|
anchor={nAddress}
|
|
|
|
user={user?.pubkey || null}
|
|
|
|
relays="wss://nos.lol/, wss://relay.damus.io/, wss://relay.snort.social/, wss://relay.nostr.band/, wss://nostr.mutinywallet.com/, wss://relay.mutinywallet.com/, wss://relay.primal.net/"
|
|
|
|
disable=""
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
2024-03-28 20:11:51 -05:00
|
|
|
<div className='w-[75vw] mx-auto mt-12 p-12 border-t-2 border-gray-300 max-tab:p-0 max-mob:p-0 max-tab:max-w-[100vw] max-mob:max-w-[100vw]'>
|
2024-09-09 17:35:00 -05:00
|
|
|
{
|
|
|
|
processedEvent && processedEvent.content && renderContent()
|
|
|
|
}
|
2024-03-16 14:56:58 -05:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
2024-08-09 14:28:57 -05:00
|
|
|
}
|