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';
|
2024-03-16 14:56:58 -05:00
|
|
|
import { useImageProxy } from '@/hooks/useImageProxy';
|
2024-08-01 17:10:43 -05:00
|
|
|
import { getSatAmountFromInvoice } from '@/utils/lightning';
|
|
|
|
import ZapDisplay from '@/components/zaps/ZapDisplay';
|
2024-03-16 16:37:47 -05:00
|
|
|
import { Tag } from 'primereact/tag';
|
2024-08-08 16:29:16 -05:00
|
|
|
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-03-16 14:56:58 -05:00
|
|
|
import Image from 'next/image';
|
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-05 15:25:04 -05:00
|
|
|
import { useZapsSubscription } from '@/hooks/nostrQueries/zaps/useZapsSubscription';
|
2024-08-09 19:00:31 -05:00
|
|
|
import { LightningAddress } from "@getalby/lightning-tools";
|
|
|
|
import PaymentButton from '@/components/bitcoinConnect/PaymentButton';
|
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-04-01 18:13:38 -05:00
|
|
|
const BitcoinConnectPayButton = dynamic(
|
|
|
|
() => import('@getalby/bitcoin-connect-react').then((mod) => mod.PayButton),
|
|
|
|
{
|
|
|
|
ssr: false,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
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-01 18:13:38 -05:00
|
|
|
const [bitcoinConnect, setBitcoinConnect] = useState(false);
|
2024-04-23 18:52:55 -05:00
|
|
|
const [nAddress, setNAddress] = useState(null);
|
2024-08-05 19:45:26 -05:00
|
|
|
const [zapAmount, setZapAmount] = 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-08-04 18:00:59 -05:00
|
|
|
|
|
|
|
const ndk = useNDKContext();
|
2024-08-07 16:02:13 -05:00
|
|
|
const { data: session, status } = useSession();
|
|
|
|
const [user, setUser] = useState(null);
|
2024-03-16 14:56:58 -05:00
|
|
|
const { returnImageProxy } = useImageProxy();
|
2024-08-08 16:29:16 -05:00
|
|
|
const { showToast } = useToast();
|
2024-08-05 17:27:19 -05:00
|
|
|
const { zaps, zapsLoading, zapsError } = useZapsSubscription({ event: processedEvent });
|
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]);
|
|
|
|
|
2024-04-01 18:13:38 -05:00
|
|
|
useEffect(() => {
|
|
|
|
if (typeof window === 'undefined') return;
|
|
|
|
|
|
|
|
const bitcoinConnectConfig = window.localStorage.getItem('bc:config');
|
|
|
|
|
|
|
|
if (bitcoinConnectConfig) {
|
|
|
|
setBitcoinConnect(true);
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
2024-08-06 14:50:32 -05:00
|
|
|
useEffect(() => {
|
|
|
|
const decryptContent = async () => {
|
|
|
|
if (user && paidResource) {
|
2024-08-09 19:00:31 -05:00
|
|
|
if (user?.purchased?.includes(processedEvent.id) || (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
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
|
|
|
const fetchEvent = async (slug) => {
|
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) {
|
|
|
|
setAuthorView(true);
|
|
|
|
}
|
2024-08-04 18:00:59 -05:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error fetching event:', error);
|
2024-03-16 14:56:58 -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]
|
|
|
|
}
|
|
|
|
|
|
|
|
const author = await ndk.fetchEvent(filter);
|
|
|
|
if (author) {
|
|
|
|
const fields = await findKind0Fields(JSON.parse(author.content));
|
|
|
|
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-01 17:10:43 -05:00
|
|
|
useEffect(() => {
|
2024-08-05 19:45:26 -05:00
|
|
|
if (!zaps) return;
|
2024-08-04 18:00:59 -05:00
|
|
|
|
2024-08-01 17:10:43 -05:00
|
|
|
let total = 0;
|
|
|
|
zaps.forEach((zap) => {
|
|
|
|
const bolt11Tag = zap.tags.find(tag => tag[0] === "bolt11");
|
|
|
|
const invoice = bolt11Tag ? bolt11Tag[1] : null;
|
|
|
|
if (invoice) {
|
|
|
|
const amount = getSatAmountFromInvoice(invoice);
|
|
|
|
total += amount;
|
|
|
|
}
|
|
|
|
});
|
2024-08-05 19:45:26 -05:00
|
|
|
|
2024-08-01 17:10:43 -05:00
|
|
|
setZapAmount(total);
|
|
|
|
}, [zaps]);
|
|
|
|
|
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-09 19:00:31 -05:00
|
|
|
const renderContent = () => {
|
|
|
|
if (decryptedContent) {
|
|
|
|
return <MDDisplay source={decryptedContent} />;
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
return <MDDisplay source={processedEvent.content} />;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
const handlePaymentSuccess = (response) => {
|
|
|
|
console.log("response in higher level", response)
|
|
|
|
}
|
|
|
|
|
|
|
|
const handlePaymentError = (error) => {
|
|
|
|
console.log("error in higher level", error)
|
|
|
|
}
|
|
|
|
|
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'>
|
|
|
|
<div className='w-full flex flex-row justify-between max-tab:flex-col max-mob:flex-col'>
|
2024-04-24 11:19:31 -05:00
|
|
|
<i className='pi pi-arrow-left pl-8 cursor-pointer hover:opacity-75 max-tab:pl-2 max-mob:pl-2' onClick={() => router.push('/')} />
|
2024-03-19 13:04:46 -05:00
|
|
|
<div className='w-[75vw] mx-auto flex flex-row items-start justify-between max-tab:flex-col max-mob:flex-col max-tab:w-[95vw] max-mob:w-[95vw]'>
|
|
|
|
<div className='flex flex-col items-start max-w-[45vw] max-tab:max-w-[100vw] max-mob:max-w-[100vw]'>
|
2024-03-19 12:32:05 -05:00
|
|
|
<div className='pt-2 flex flex-row justify-start w-full'>
|
2024-04-24 11:19:31 -05:00
|
|
|
{processedEvent && processedEvent.topics && processedEvent.topics.length > 0 && (
|
|
|
|
processedEvent.topics.map((topic, index) => (
|
|
|
|
<Tag className='mr-2 text-white' key={index} value={topic}></Tag>
|
|
|
|
))
|
|
|
|
)
|
|
|
|
}
|
2024-03-19 12:32:05 -05:00
|
|
|
</div>
|
|
|
|
<h1 className='text-4xl mt-6'>{processedEvent?.title}</h1>
|
|
|
|
<p className='text-xl mt-6'>{processedEvent?.summary}</p>
|
|
|
|
<div className='flex flex-row w-full mt-6 items-center'>
|
|
|
|
<Image
|
2024-07-30 17:16:09 -05:00
|
|
|
alt="avatar image"
|
2024-08-01 16:31:52 -05:00
|
|
|
src={returnImageProxy(author?.avatar, author?.pubkey)}
|
2024-03-19 12:32:05 -05:00
|
|
|
width={50}
|
|
|
|
height={50}
|
|
|
|
className="rounded-full mr-4"
|
|
|
|
/>
|
|
|
|
<p className='text-lg'>
|
|
|
|
Created by{' '}
|
|
|
|
<a rel='noreferrer noopener' target='_blank' className='text-blue-500 hover:underline'>
|
|
|
|
{author?.username}
|
|
|
|
</a>
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
</div>
|
2024-03-19 13:04:46 -05:00
|
|
|
<div className='flex flex-col max-tab:mt-12 max-mob:mt-12'>
|
2024-03-19 12:32:05 -05:00
|
|
|
{processedEvent && (
|
|
|
|
<div className='flex flex-col items-center justify-between rounded-lg h-72 p-4 bg-gray-700 drop-shadow-md'>
|
|
|
|
<Image
|
|
|
|
alt="resource thumbnail"
|
|
|
|
src={returnImageProxy(processedEvent.image)}
|
|
|
|
width={344}
|
|
|
|
height={194}
|
2024-07-30 17:16:09 -05:00
|
|
|
className="w-[344px] h-[194px] object-cover object-top rounded-lg"
|
2024-03-19 12:32:05 -05:00
|
|
|
/>
|
2024-08-09 19:00:31 -05:00
|
|
|
<div className='w-full flex flex-row justify-between'>
|
2024-08-11 19:10:08 -05:00
|
|
|
{paidResource && !decryptedContent && <PaymentButton
|
|
|
|
lnAddress={'bitcoinplebdev@stacker.news'}
|
|
|
|
amount={processedEvent.price}
|
|
|
|
onSuccess={handlePaymentSuccess}
|
|
|
|
onError={handlePaymentError}
|
|
|
|
userId={user.id} // Pass the user ID
|
|
|
|
resourceId={processedEvent.id} // Pass the course/resource ID
|
|
|
|
/>}
|
|
|
|
|
2024-08-09 19:00:31 -05:00
|
|
|
<ZapDisplay zapAmount={zapAmount} event={processedEvent} zapsLoading={zapsLoading} />
|
|
|
|
</div>
|
2024-03-19 12:32:05 -05:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
2024-03-16 16:37:47 -05:00
|
|
|
</div>
|
|
|
|
</div>
|
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-08-09 19:00:31 -05:00
|
|
|
{renderContent()}
|
2024-03-16 14:56:58 -05:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
2024-08-09 14:28:57 -05:00
|
|
|
}
|