diff --git a/src/components/content/carousels/templates/CourseTemplate.js b/src/components/content/carousels/templates/CourseTemplate.js index 374a3a8..20345bf 100644 --- a/src/components/content/carousels/templates/CourseTemplate.js +++ b/src/components/content/carousels/templates/CourseTemplate.js @@ -3,31 +3,21 @@ import Image from "next/image"; import { useRouter } from "next/router"; import { formatTimestampToHowLongAgo } from "@/utils/time"; import { useImageProxy } from "@/hooks/useImageProxy"; -import { getSatAmountFromInvoice } from "@/utils/lightning"; +import { getTotalFromZaps } from "@/utils/lightning"; import ZapDisplay from "@/components/zaps/ZapDisplay"; -import { useZapsQuery } from "@/hooks/nostrQueries/zaps/useZapsQuery"; +import { useZapsSubscription } from "@/hooks/nostrQueries/zaps/useZapsSubscription"; const CourseTemplate = ({ course }) => { const [zapAmount, setZapAmount] = useState(null); const router = useRouter(); const { returnImageProxy } = useImageProxy(); - const { zaps, zapsLoading, zapsError } = useZapsQuery({ event: course, type: "course" }); + const { zaps, zapsLoading, zapsError } = useZapsSubscription({ event: course }); useEffect(() => { if (!zaps || zapsLoading || zapsError) return; - let total = 0; - zaps.forEach((zap) => { - // If the zap matches the event or the parameterized event, then add the zap to the total - if (zap.tags.find(tag => tag[0] === "e" && tag[1] === course.id) || zap.tags.find(tag => tag[0] === "a" && tag[1] === `${course.kind}:${course.id}:${course.d}`)) { - const bolt11Tag = zap.tags.find(tag => tag[0] === "bolt11"); - const invoice = bolt11Tag ? bolt11Tag[1] : null; - if (invoice) { - const amount = getSatAmountFromInvoice(invoice); - total += amount; - } - } - }); + const total = getTotalFromZaps(zaps, course); + setZapAmount(total); }, [course, zaps, zapsLoading, zapsError]); diff --git a/src/components/content/carousels/templates/ResourceTemplate.js b/src/components/content/carousels/templates/ResourceTemplate.js index 92e3859..8d8a668 100644 --- a/src/components/content/carousels/templates/ResourceTemplate.js +++ b/src/components/content/carousels/templates/ResourceTemplate.js @@ -3,13 +3,13 @@ import Image from "next/image"; import { useRouter } from "next/router"; import { formatTimestampToHowLongAgo } from "@/utils/time"; import { useImageProxy } from "@/hooks/useImageProxy"; -import { useZapsQuery } from "@/hooks/nostrQueries/zaps/useZapsQuery"; -import { getSatAmountFromInvoice } from "@/utils/lightning"; +import { getTotalFromZaps } from "@/utils/lightning"; import ZapDisplay from "@/components/zaps/ZapDisplay"; +import { useZapsSubscription } from "@/hooks/nostrQueries/zaps/useZapsSubscription"; const ResourceTemplate = ({ resource }) => { const [zapAmount, setZapAmount] = useState(null); - const { zaps, zapsLoading, zapsError } = useZapsQuery({ event: resource, type: "resource" }); + const { zaps, zapsLoading, zapsError } = useZapsSubscription({ event: resource }); const router = useRouter(); const { returnImageProxy } = useImageProxy(); @@ -17,18 +17,8 @@ const ResourceTemplate = ({ resource }) => { useEffect(() => { if (!zaps || zapsLoading || zapsError) return; - let total = 0; - zaps.forEach((zap) => { - // If the zap matches the event or the parameterized event, then add the zap to the total - 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}`)) { - const bolt11Tag = zap.tags.find(tag => tag[0] === "bolt11"); - const invoice = bolt11Tag ? bolt11Tag[1] : null; - if (invoice) { - const amount = getSatAmountFromInvoice(invoice); - total += amount; - } - } - }); + const total = getTotalFromZaps(zaps, resource); + setZapAmount(total); }, [resource, zaps, zapsLoading, zapsError]); diff --git a/src/components/content/carousels/templates/WorkshopTemplate.js b/src/components/content/carousels/templates/WorkshopTemplate.js index 4b85239..12ee82b 100644 --- a/src/components/content/carousels/templates/WorkshopTemplate.js +++ b/src/components/content/carousels/templates/WorkshopTemplate.js @@ -3,31 +3,20 @@ import Image from "next/image"; import { useRouter } from "next/router"; import { formatTimestampToHowLongAgo } from "@/utils/time"; import { useImageProxy } from "@/hooks/useImageProxy"; -import { useZapsQuery } from "@/hooks/nostrQueries/zaps/useZapsQuery"; -import { getSatAmountFromInvoice } from "@/utils/lightning"; +import { getTotalFromZaps } from "@/utils/lightning"; +import { useZapsSubscription } from "@/hooks/nostrQueries/zaps/useZapsSubscription"; import ZapDisplay from "@/components/zaps/ZapDisplay"; const WorkshopTemplate = ({ workshop }) => { const [zapAmount, setZapAmount] = useState(null); const router = useRouter(); const { returnImageProxy } = useImageProxy(); - const { zaps, zapsLoading, zapsError } = useZapsQuery({ event: workshop, type: "workshop" }); + const { zaps, zapsLoading, zapsError } = useZapsSubscription({ event: workshop }); useEffect(() => { if (zapsLoading || !zaps) return; - let total = 0; - zaps.forEach((zap) => { - // If the zap matches the event or the parameterized event, then add the zap to the total - if (zap.tags.find(tag => tag[0] === "e" && tag[1] === workshop.id) || zap.tags.find(tag => tag[0] === "a" && tag[1] === `${workshop.kind}:${workshop.id}:${workshop.d}`)) { - const bolt11Tag = zap.tags.find(tag => tag[0] === "bolt11"); - const invoice = bolt11Tag ? bolt11Tag[1] : null; - if (invoice) { - const amount = getSatAmountFromInvoice(invoice); - total += amount; - } - } - }); + const total = getTotalFromZaps(zaps, workshop); setZapAmount(total); }, [zaps, workshop, zapsLoading, zapsError]); diff --git a/src/components/content/courses/CourseDetails.js b/src/components/content/courses/CourseDetails.js index 203cefb..be4b154 100644 --- a/src/components/content/courses/CourseDetails.js +++ b/src/components/content/courses/CourseDetails.js @@ -2,7 +2,7 @@ import React, { useEffect, useState, useCallback } from 'react'; import { useRouter } from 'next/router'; import { useImageProxy } from '@/hooks/useImageProxy'; import ZapDisplay from '@/components/zaps/ZapDisplay'; -import { getSatAmountFromInvoice } from '@/utils/lightning'; +import { getTotalFromZaps } from '@/utils/lightning'; import { Tag } from 'primereact/tag'; import { nip19 } from 'nostr-tools'; import { useSession } from 'next-auth/react'; @@ -95,17 +95,10 @@ export default function CourseDetails({ processedEvent }) { useEffect(() => { if (!zaps || zaps.length === 0) return; - 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; - } - }); + const total = getTotalFromZaps(zaps, processedEvent); + setZapAmount(total); - }, [zaps]); + }, [zaps, processedEvent]); return (