2024-09-12 17:39:47 -05:00
import React , { useEffect , useState } 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 ResourcePaymentButton from "@/components/bitcoinConnect/ResourcePaymentButton" ;
import ZapDisplay from "@/components/zaps/ZapDisplay" ;
import GenericButton from "@/components/buttons/GenericButton" ;
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 dynamic from "next/dynamic" ;
const MDDisplay = dynamic (
( ) => import ( "@uiw/react-markdown-preview" ) ,
{
ssr : false ,
}
) ;
const lnAddress = process . env . NEXT _PUBLIC _LIGHTNING _ADDRESS ;
2024-09-14 17:05:05 -05:00
const VideoDetails = ( { processedEvent , topics , title , summary , image , price , author , paidResource , decryptedContent , nAddress , handlePaymentSuccess , handlePaymentError , authorView } ) => {
2024-09-12 17:39:47 -05:00
const [ zapAmount , setZapAmount ] = useState ( 0 ) ;
const router = useRouter ( ) ;
const { returnImageProxy } = useImageProxy ( ) ;
const { zaps , zapsLoading , zapsError } = useZapsSubscription ( { event : processedEvent } ) ;
const { data : session , status } = useSession ( ) ;
const { showToast } = useToast ( ) ;
const windowWidth = useWindowWidth ( ) ;
useEffect ( ( ) => {
if ( zaps . length > 0 ) {
const total = getTotalFromZaps ( zaps , processedEvent ) ;
setZapAmount ( total ) ;
}
} , [ zaps , processedEvent ] ) ;
const handleDelete = async ( ) => {
try {
const response = await axios . delete ( ` /api/resources/ ${ processedEvent . d } ` ) ;
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.' ) ;
}
}
}
const renderPaymentMessage = ( ) => {
if ( session ? . user && session . user ? . role ? . subscribed && decryptedContent ) {
return < GenericButton tooltipOptions = { { position : 'top' } } tooltip = { ` You are subscribed so you can access all paid content ` } icon = "pi pi-check" label = "Subscribed" severity = "success" outlined size = "small" className = "cursor-default hover:opacity-100 hover:bg-transparent focus:ring-0" / >
}
if ( paidResource && decryptedContent && author && processedEvent ? . pubkey !== session ? . user ? . pubkey && ! session ? . user ? . role ? . subscribed ) {
return < GenericButton icon = "pi pi-check" label = { ` Paid ${ processedEvent . price } sats ` } severity = "success" outlined size = "small" className = "cursor-default hover:opacity-100 hover:bg-transparent focus:ring-0" / >
}
if ( paidResource && author && processedEvent ? . pubkey === session ? . user ? . pubkey ) {
return < GenericButton tooltipOptions = { { position : 'top' } } tooltip = { ` You created this paid content, users must pay ${ processedEvent . price } sats to access it ` } icon = "pi pi-check" label = { ` Price ${ processedEvent . price } sats ` } severity = "success" outlined size = "small" className = "cursor-default hover:opacity-100 hover:bg-transparent focus:ring-0" / >
}
return null ;
} ;
const renderContent = ( ) => {
if ( decryptedContent ) {
return < MDDisplay className = 'p-4 rounded-lg w-full' source = { decryptedContent } / > ;
}
if ( paidResource && ! decryptedContent ) {
return (
< div className = "w-full aspect-video rounded-lg flex flex-col items-center justify-center relative overflow-hidden" >
2024-09-14 17:39:01 -05:00
< div
2024-09-12 17:39:47 -05:00
className = "absolute inset-0 opacity-50"
style = { {
backgroundImage : ` url( ${ image } ) ` ,
backgroundSize : 'cover' ,
backgroundPosition : 'center' ,
} }
> < / d i v >
< div className = "absolute inset-0 bg-black bg-opacity-50" > < / d i v >
< div className = "mx-auto py-auto z-10" >
< i className = "pi pi-lock text-[100px] text-red-500" > < / i >
< / d i v >
< p className = "text-center text-xl text-red-500 z-10 mt-4" >
This content is paid and needs to be purchased before viewing .
< / p >
< div className = "flex flex-row items-center justify-center w-full mt-4 z-10" >
< ResourcePaymentButton
lnAddress = { lnAddress }
amount = { price }
onSuccess = { handlePaymentSuccess }
onError = { handlePaymentError }
resourceId = { processedEvent . d }
/ >
< / d i v >
< / d i v >
) ;
}
if ( processedEvent ? . content ) {
return < MDDisplay className = 'p-4 rounded-lg w-full' source = { processedEvent . content } / > ;
}
return null ;
}
return (
< div className = "w-full" >
{ renderContent ( ) }
< div className = "bg-gray-800/90 rounded-lg p-4 m-4" >
2024-09-14 17:39:01 -05:00
< div className = "w-full flex flex-col items-start justify-start mt-2 px-2" >
< div className = "flex flex-row items-center gap-2 w-full" >
< h1 className = 'text-4xl' > { title } < / h 1 >
{ topics && topics . length > 0 && (
topics . map ( ( topic , index ) => (
< Tag className = 'mt-2 text-white' key = { index } value = { topic } > < / T a g >
) )
)
}
< / d i v >
< div className = 'flex flex-row items-center justify-between w-full' >
2024-09-15 13:27:37 -05:00
< p className = 'text-xl mt-4' > { summary && (
< div className = "text-xl mt-4" >
{ summary . split ( '\n' ) . map ( ( line , index ) => (
< p key = { index } > { line } < / p >
) ) }
< / d i v >
) }
< / p >
2024-09-14 17:39:01 -05:00
< ZapDisplay
zapAmount = { zapAmount }
event = { processedEvent }
zapsLoading = { zapsLoading && zapAmount === 0 }
2024-09-12 17:39:47 -05:00
/ >
2024-09-14 17:39:01 -05:00
< / d i v >
2024-09-12 17:39:47 -05:00
< / d i v >
2024-09-14 17:39:01 -05:00
< div className = 'w-full flex flex-col space-y-4 mt-4' >
< div className = 'flex flex-row justify-between items-center' >
< div className = 'flex flex-row w-fit items-center' >
< Image
alt = "avatar image"
src = { returnImageProxy ( author ? . avatar , author ? . username ) }
width = { 50 }
height = { 50 }
className = "rounded-full mr-4"
2024-09-12 17:39:47 -05:00
/ >
2024-09-14 17:39:01 -05:00
< p className = 'text-lg' >
Created by { ' ' }
< a rel = 'noreferrer noopener' target = '_blank' className = 'text-blue-500 hover:underline' >
{ author ? . username }
< / a >
< / p >
2024-09-12 17:39:47 -05:00
< / d i v >
2024-09-14 17:39:01 -05:00
{ authorView ? (
< div className = 'flex flex-row justify-center items-center space-x-2' >
2024-09-15 13:27:37 -05:00
< GenericButton onClick = { ( ) => router . push ( ` /details/ ${ nAddress } /edit ` ) } label = "Edit" severity = 'warning' outlined / >
2024-09-14 17:39:01 -05:00
< GenericButton onClick = { handleDelete } label = "Delete" severity = 'danger' outlined / >
< GenericButton outlined icon = "pi pi-external-link" onClick = { ( ) => window . open ( ` https://nostr.band/ ${ nAddress } ` , '_blank' ) } tooltip = "View Nostr Event" tooltipOptions = { { position : 'right' } } / >
< / d i v >
) : (
< div className = 'flex flex-row justify-center items-center space-x-2' >
2024-09-15 13:27:37 -05:00
< GenericButton outlined icon = "pi pi-external-link" onClick = { ( ) => window . open ( ` https://nostr.band/ ${ nAddress } ` , '_blank' ) } tooltip = "View Nostr Event" tooltipOptions = { { position : paidResource ? 'left' : 'right' } } / >
2024-09-14 17:39:01 -05:00
< / d i v >
) }
< / d i v >
2024-09-12 17:39:47 -05:00
< / d i v >
2024-09-15 13:27:37 -05:00
< div className = "w-full flex flex-row justify-end mt-4" >
{ renderPaymentMessage ( ) }
< / d i v >
2024-09-12 17:39:47 -05:00
< / d i v >
< / d i v >
)
}
export default VideoDetails ;