From 8d42a957fc3bc7582638f55d076e741d4d30bbca Mon Sep 17 00:00:00 2001 From: zmeyer44 Date: Fri, 20 Oct 2023 16:28:55 -0400 Subject: [PATCH] following --- .../[npub]/_components/MySubscription.tsx | 11 ++++-- components/SubscriptionCard/index.tsx | 39 +++++++++++-------- lib/actions/create.ts | 15 +++++++ 3 files changed, 46 insertions(+), 19 deletions(-) diff --git a/app/(app)/(profile)/[npub]/_components/MySubscription.tsx b/app/(app)/(profile)/[npub]/_components/MySubscription.tsx index a90c4b0..b8f6d25 100644 --- a/app/(app)/(profile)/[npub]/_components/MySubscription.tsx +++ b/app/(app)/(profile)/[npub]/_components/MySubscription.tsx @@ -10,20 +10,25 @@ type MySubscription = { }; export default function MySubscription({ pubkey }: MySubscription) { - const { fetchEvents } = useNDK(); + const { ndk, fetchEvents } = useNDK(); const { currentUser, mySubscription, follows } = useCurrentUser(); const [subscriptionTiers, setSubscriptionTiers] = useState([]); useEffect(() => { - void handleFetchSubscriptionTiers(); - }, [pubkey]); + if (ndk) { + void handleFetchSubscriptionTiers(); + } + }, [pubkey, ndk]); async function handleFetchSubscriptionTiers() { try { + console.log("FETCHING", pubkey); const events = await fetchEvents({ kinds: [30044 as NDKKind], authors: [pubkey], }); + console.log("events", events); + setSubscriptionTiers(events); } catch (err) { console.log("error", err); diff --git a/components/SubscriptionCard/index.tsx b/components/SubscriptionCard/index.tsx index 81a4604..af9a5a4 100644 --- a/components/SubscriptionCard/index.tsx +++ b/components/SubscriptionCard/index.tsx @@ -9,15 +9,16 @@ import { CardHeader, CardTitle, } from "@/components/ui/card"; -import { cn } from "@/lib/utils"; -import { NDKEvent, NDKUser, NostrEvent } from "@nostr-dev-kit/ndk"; +import { cn, log } from "@/lib/utils"; +import { NDKEvent, NDKUser, NDKNip07Signer } from "@nostr-dev-kit/ndk"; import { useNDK } from "@/app/_providers/ndk"; import useCurrentUser from "@/lib/hooks/useCurrentUser"; import { toast } from "sonner"; import { getTagValues, getTagsValues } from "@/lib/nostr/utils"; import { sendZap, checkPayment } from "@/lib/actions/zap"; import { btcToSats, formatNumber } from "@/lib/utils"; - +import { BANNER } from "@/constants"; +import { follow } from "@/lib/actions/create"; export default function SubscriptionCard({ event }: { event: NDKEvent }) { const { currentUser } = useCurrentUser(); const { ndk } = useNDK(); @@ -27,20 +28,20 @@ export default function SubscriptionCard({ event }: { event: NDKEvent }) { const rawEvent = event.rawEvent(); const title = getTagValues("title", tags) ?? getTagValues("name", tags) ?? ""; const image = - getTagValues("image", tags) ?? getTagValues("picture", tags) ?? ""; + getTagValues("image", tags) ?? getTagValues("picture", tags) ?? BANNER; const description = getTagValues("description", tags) ?? getTagValues("summary", tags) ?? ""; const delegate = getTagValues("delegate", tags); const priceInBTC = parseFloat(getTagValues("price", rawEvent.tags) ?? "0"); async function handleSubscribe() { + log("func", "handleSubscribe"); try { - if (!currentUser) return; - await currentUser.follow( - new NDKUser({ - hexpubkey: delegate, - }), - ); + if (!currentUser || !ndk?.signer) return; + if (delegate) { + await follow(ndk, currentUser, delegate); + log("info", "followed"); + } const result = await sendZap( ndk!, btcToSats(priceInBTC), @@ -76,9 +77,15 @@ export default function SubscriptionCard({ event }: { event: NDKEvent }) { setCheckingPayment(false); } } + useEffect(() => { + if (!currentUser) return; + if (!checkingPayment && !hasValidPayment) { + void handleCheckPayment(); + } + }, [currentUser]); return ( - -
+ +
{title}
-
+
{title} {description} - + {hasValidPayment ? ( - + diff --git a/lib/actions/create.ts b/lib/actions/create.ts index 6a491b7..2845eb7 100644 --- a/lib/actions/create.ts +++ b/lib/actions/create.ts @@ -280,3 +280,18 @@ export async function unlockEvent( await deleteEvent(ndk, [["e", event.id ?? ""]], "Content unlocked"); return publishedEvent; } + +export async function follow(ndk: NDK, currentUser: NDKUser, pubkey: string) { + const userContacts = await ndk.fetchEvent({ + kinds: [3], + authors: [currentUser.pubkey], + }); + if (!userContacts) return; + const newEvent = { + kind: 3, + ...userContacts.rawEvent(), + tags: [...userContacts.tags, ["p", pubkey]], + }; + const newContacts = await createEvent(ndk, newEvent); + return newContacts; +}