2024-03-16 14:56:58 -05:00
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
import { useRouter } from 'next/router';
|
2024-08-08 16:29:16 -05:00
|
|
|
import { parseEvent, findKind0Fields } from '@/utils/nostr';
|
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-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-09-12 17:39:47 -05:00
|
|
|
import VideoDetails from '@/components/content/videos/VideoDetails';
|
|
|
|
import DocumentDetails from '@/components/content/documents/DocumentDetails';
|
2024-09-12 12:07:38 -05:00
|
|
|
import { ProgressSpinner } from 'primereact/progressspinner';
|
2024-09-17 13:55:51 -05:00
|
|
|
import appConfig from "@/config/appConfig";
|
2024-09-17 16:00:00 -05:00
|
|
|
import { useDecryptContent } from '@/hooks/encryption/useDecryptContent';
|
|
|
|
import { useEncryptContent } from '@/hooks/encryption/useEncryptContent';
|
2024-03-16 14:56:58 -05:00
|
|
|
import 'primeicons/primeicons.css';
|
2024-08-09 14:28:57 -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-09-12 12:07:38 -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-09-17 16:00:00 -05:00
|
|
|
const { decryptContent } = useDecryptContent();
|
|
|
|
const { encryptContent } = useEncryptContent();
|
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(() => {
|
2024-09-17 16:00:00 -05:00
|
|
|
const decrypt = async () => {
|
2024-09-12 17:39:47 -05:00
|
|
|
if (paidResource && processedEvent.content) {
|
|
|
|
// Check if user is subscribed first
|
|
|
|
if (user?.role?.subscribed) {
|
2024-09-17 16:00:00 -05:00
|
|
|
const decryptedContent = await decryptContent(processedEvent.content);
|
2024-09-12 17:39:47 -05:00
|
|
|
setDecryptedContent(decryptedContent);
|
|
|
|
}
|
|
|
|
// If not subscribed, check if they have purchased
|
|
|
|
else if (user?.purchased?.some(purchase => purchase.resourceId === processedEvent.d)) {
|
2024-09-17 16:00:00 -05:00
|
|
|
const decryptedContent = await decryptContent(processedEvent.content);
|
2024-08-09 14:28:57 -05:00
|
|
|
setDecryptedContent(decryptedContent);
|
|
|
|
}
|
2024-09-12 17:39:47 -05:00
|
|
|
// If neither subscribed nor purchased, decryptedContent remains null
|
2024-08-06 14:50:32 -05:00
|
|
|
}
|
2024-09-12 17:39:47 -05:00
|
|
|
};
|
2024-08-12 17:27:47 -05:00
|
|
|
|
2024-09-17 16:00:00 -05:00
|
|
|
decrypt();
|
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-14 17:05:05 -05:00
|
|
|
if (!slug) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { data } = nip19.decode(slug)
|
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
showToast('error', 'Error', 'Resource not found');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const id = data?.identifier;
|
|
|
|
|
|
|
|
const fetchEvent = async (id, retryCount = 0) => {
|
2024-09-09 17:35:00 -05:00
|
|
|
setLoading(true);
|
|
|
|
setError(null);
|
2024-08-04 18:00:59 -05:00
|
|
|
try {
|
|
|
|
await ndk.connect();
|
|
|
|
|
|
|
|
const filter = {
|
2024-09-14 17:05:05 -05:00
|
|
|
ids: [id]
|
2024-08-04 18:00:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const event = await ndk.fetchEvent(filter);
|
|
|
|
|
|
|
|
if (event) {
|
|
|
|
setEvent(event);
|
2024-08-08 16:29:16 -05:00
|
|
|
if (user && user.pubkey === event.pubkey) {
|
|
|
|
setAuthorView(true);
|
2024-09-12 12:07:38 -05:00
|
|
|
if (event.kind === 30402) {
|
2024-09-17 16:00:00 -05:00
|
|
|
const decryptedContent = await decryptContent(event.content);
|
2024-09-12 12:07:38 -05:00
|
|
|
setDecryptedContent(decryptedContent);
|
|
|
|
}
|
2024-08-08 16:29:16 -05:00
|
|
|
}
|
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));
|
2024-09-14 17:05:05 -05:00
|
|
|
return fetchEvent(id, retryCount + 1);
|
2024-09-09 17:35:00 -05:00
|
|
|
} 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));
|
2024-09-14 17:05:05 -05:00
|
|
|
return fetchEvent(id, retryCount + 1);
|
2024-09-09 17:35:00 -05:00
|
|
|
} 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-09-14 17:05:05 -05:00
|
|
|
if (ndk && id) {
|
|
|
|
fetchEvent(id);
|
2024-08-04 18:00:59 -05:00
|
|
|
}
|
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-09-12 12:07:38 -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);
|
2024-09-12 17:39:47 -05:00
|
|
|
console.log("parsedEvent", parsedEvent);
|
2024-08-05 17:27:19 -05:00
|
|
|
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,
|
2024-09-17 13:55:51 -05:00
|
|
|
relayUrls: appConfig.defaultRelayUrls
|
2024-04-23 18:52:55 -05:00
|
|
|
});
|
|
|
|
setNAddress(naddr);
|
|
|
|
}
|
|
|
|
}, [processedEvent]);
|
|
|
|
|
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-09-09 17:35:00 -05:00
|
|
|
if (loading) {
|
2024-09-17 13:28:58 -05:00
|
|
|
return <div className='w-full h-full flex items-center justify-center'><ProgressSpinner /></div>
|
2024-09-09 17:35:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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-09-12 17:39:47 -05:00
|
|
|
<div>
|
2024-09-15 13:27:37 -05:00
|
|
|
{processedEvent && processedEvent.type !== "video" ? (
|
2024-09-12 17:39:47 -05:00
|
|
|
<DocumentDetails
|
2024-09-09 17:35:00 -05:00
|
|
|
processedEvent={processedEvent}
|
|
|
|
topics={processedEvent.topics}
|
|
|
|
title={processedEvent.title}
|
|
|
|
summary={processedEvent.summary}
|
|
|
|
image={processedEvent.image}
|
|
|
|
price={processedEvent.price}
|
|
|
|
author={author}
|
|
|
|
paidResource={paidResource}
|
2024-09-14 17:05:05 -05:00
|
|
|
nAddress={nAddress}
|
2024-09-09 17:35:00 -05:00
|
|
|
decryptedContent={decryptedContent}
|
|
|
|
handlePaymentSuccess={handlePaymentSuccess}
|
|
|
|
handlePaymentError={handlePaymentError}
|
2024-09-12 17:39:47 -05:00
|
|
|
authorView={authorView}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<VideoDetails
|
|
|
|
processedEvent={processedEvent}
|
|
|
|
topics={processedEvent.topics}
|
|
|
|
title={processedEvent.title}
|
|
|
|
summary={processedEvent.summary}
|
|
|
|
image={processedEvent.image}
|
|
|
|
price={processedEvent.price}
|
|
|
|
author={author}
|
|
|
|
paidResource={paidResource}
|
|
|
|
decryptedContent={decryptedContent}
|
2024-09-14 17:05:05 -05:00
|
|
|
nAddress={nAddress}
|
2024-09-12 17:39:47 -05:00
|
|
|
handlePaymentSuccess={handlePaymentSuccess}
|
|
|
|
handlePaymentError={handlePaymentError}
|
|
|
|
authorView={authorView}
|
2024-09-09 17:35:00 -05:00
|
|
|
/>
|
2024-08-08 16:29:16 -05:00
|
|
|
)}
|
2024-04-23 18:52:55 -05:00
|
|
|
{typeof window !== 'undefined' && nAddress !== null && (
|
2024-09-14 17:05:05 -05:00
|
|
|
<div className='px-4'>
|
2024-04-23 18:52:55 -05:00
|
|
|
<ZapThreadsWrapper
|
|
|
|
anchor={nAddress}
|
|
|
|
user={user?.pubkey || null}
|
2024-09-12 12:07:38 -05:00
|
|
|
relays="wss://nos.lol/, wss://relay.damus.io/, wss://relay.snort.social/, wss://relay.nostr.band/, wss://relay.mutinywallet.com/, wss://relay.primal.net/"
|
2024-09-12 17:39:47 -05:00
|
|
|
disable="zaps"
|
2024-04-23 18:52:55 -05:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
2024-03-16 14:56:58 -05:00
|
|
|
</div>
|
|
|
|
);
|
2024-08-09 14:28:57 -05:00
|
|
|
}
|