2024-04-23 18:52:55 -05:00
|
|
|
import React, { useEffect, useState, useMemo } from "react";
|
2024-03-26 18:14:32 -05:00
|
|
|
import Image from "next/image";
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import { formatTimestampToHowLongAgo } from "@/utils/time";
|
|
|
|
import { useImageProxy } from "@/hooks/useImageProxy";
|
2024-08-04 18:00:59 -05:00
|
|
|
import { useResourceZapsQuery } from "@/hooks/nostrQueries/useResourceZapsQuery";
|
2024-04-21 18:06:23 -05:00
|
|
|
import { getSatAmountFromInvoice } from "@/utils/lightning";
|
2024-08-04 17:02:34 -05:00
|
|
|
import ZapDisplay from "@/components/zaps/ZapDisplay";
|
|
|
|
|
|
|
|
const ResourceTemplate = ({ resource }) => {
|
|
|
|
const [zapAmount, setZapAmount] = useState(0);
|
2024-08-04 18:00:59 -05:00
|
|
|
const { zaps, zapsLoading, zapsError, refetchZaps } = useResourceZapsQuery({ event: resource })
|
2024-03-26 18:14:32 -05:00
|
|
|
|
2024-04-22 10:45:52 -05:00
|
|
|
const router = useRouter();
|
|
|
|
const { returnImageProxy } = useImageProxy();
|
2024-04-21 18:06:23 -05:00
|
|
|
|
2024-04-22 10:45:52 -05:00
|
|
|
useEffect(() => {
|
2024-08-04 18:16:56 -05:00
|
|
|
if (!zaps || zapsLoading || zapsError) return;
|
2024-08-04 17:02:34 -05:00
|
|
|
|
2024-04-23 18:52:55 -05:00
|
|
|
let total = 0;
|
2024-08-04 18:00:59 -05:00
|
|
|
zaps.forEach((zap) => {
|
2024-08-04 17:02:34 -05:00
|
|
|
// If the zap matches the event or the parameterized event, then add the zap to the total
|
2024-08-04 18:00:59 -05:00
|
|
|
if (zap.tags.find(tag => tag[0] === "e" && tag[1] === resource.id) || zap.tags.find(tag => tag[0] === "a" && tag[1] === `${resource.kind}:${resource.id}:${resource.d}`)) {
|
2024-04-23 18:52:55 -05:00
|
|
|
const bolt11Tag = zap.tags.find(tag => tag[0] === "bolt11");
|
2024-04-22 10:45:52 -05:00
|
|
|
const invoice = bolt11Tag ? bolt11Tag[1] : null;
|
|
|
|
if (invoice) {
|
2024-08-04 17:02:34 -05:00
|
|
|
const amount = getSatAmountFromInvoice(invoice);
|
|
|
|
total += amount;
|
2024-04-21 18:06:23 -05:00
|
|
|
}
|
2024-08-04 18:00:59 -05:00
|
|
|
}
|
2024-04-23 18:52:55 -05:00
|
|
|
});
|
|
|
|
setZapAmount(total);
|
2024-08-04 18:16:56 -05:00
|
|
|
}, [resource, zaps, zapsLoading, zapsError]);
|
|
|
|
|
|
|
|
if (zapsLoading) return <div>Loading...</div>;
|
|
|
|
if (zapsError) return <div>Error: {zapsError}</div>;
|
2024-04-21 18:06:23 -05:00
|
|
|
|
2024-04-22 10:45:52 -05:00
|
|
|
return (
|
|
|
|
<div
|
2024-04-22 15:46:52 -05:00
|
|
|
className="flex flex-col items-center mx-auto px-4 mt-8 rounded-md"
|
2024-04-22 10:45:52 -05:00
|
|
|
>
|
2024-08-04 17:02:34 -05:00
|
|
|
{/* Wrap the image in a div with a relative class with a padding-bottom of 56.25% representing the aspect ratio of 16:9 */}
|
|
|
|
<div
|
2024-04-22 11:59:20 -05:00
|
|
|
onClick={() => router.push(`/details/${resource.id}`)}
|
2024-04-22 19:09:46 -05:00
|
|
|
className="relative w-full h-0 hover:opacity-80 transition-opacity duration-300 cursor-pointer"
|
2024-08-04 17:02:34 -05:00
|
|
|
style={{ paddingBottom: "56.25%" }}
|
2024-04-22 11:59:20 -05:00
|
|
|
>
|
2024-04-22 10:45:52 -05:00
|
|
|
<Image
|
|
|
|
alt="resource thumbnail"
|
|
|
|
src={returnImageProxy(resource.image)}
|
|
|
|
quality={100}
|
|
|
|
layout="fill"
|
|
|
|
objectFit="cover"
|
|
|
|
className="rounded-md"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="flex flex-col justify-start w-full mt-4">
|
|
|
|
<h4 className="mb-1 font-bold text-lg font-blinker line-clamp-2">
|
|
|
|
{resource.title}
|
|
|
|
</h4>
|
2024-04-24 11:19:31 -05:00
|
|
|
<p className="text-sm text-gray-500 min-h-[40px] line-clamp-2">{resource.summary}</p>
|
2024-04-22 10:45:52 -05:00
|
|
|
<div className="flex flex-row justify-between items-center mt-2">
|
|
|
|
<p className="text-xs text-gray-400">
|
|
|
|
{formatTimestampToHowLongAgo(resource.published_at)}
|
|
|
|
</p>
|
2024-08-04 17:02:34 -05:00
|
|
|
<ZapDisplay zapAmount={zapAmount} event={resource} />
|
2024-03-26 18:14:32 -05:00
|
|
|
</div>
|
2024-04-22 10:45:52 -05:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
2024-03-26 18:14:32 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
export default ResourceTemplate;
|