following

This commit is contained in:
zmeyer44 2023-10-20 16:28:55 -04:00
parent 1c2a3962cb
commit 8d42a957fc
3 changed files with 46 additions and 19 deletions

View File

@ -10,20 +10,25 @@ type MySubscription = {
}; };
export default function MySubscription({ pubkey }: MySubscription) { export default function MySubscription({ pubkey }: MySubscription) {
const { fetchEvents } = useNDK(); const { ndk, fetchEvents } = useNDK();
const { currentUser, mySubscription, follows } = useCurrentUser(); const { currentUser, mySubscription, follows } = useCurrentUser();
const [subscriptionTiers, setSubscriptionTiers] = useState<NDKEvent[]>([]); const [subscriptionTiers, setSubscriptionTiers] = useState<NDKEvent[]>([]);
useEffect(() => { useEffect(() => {
if (ndk) {
void handleFetchSubscriptionTiers(); void handleFetchSubscriptionTiers();
}, [pubkey]); }
}, [pubkey, ndk]);
async function handleFetchSubscriptionTiers() { async function handleFetchSubscriptionTiers() {
try { try {
console.log("FETCHING", pubkey);
const events = await fetchEvents({ const events = await fetchEvents({
kinds: [30044 as NDKKind], kinds: [30044 as NDKKind],
authors: [pubkey], authors: [pubkey],
}); });
console.log("events", events);
setSubscriptionTiers(events); setSubscriptionTiers(events);
} catch (err) { } catch (err) {
console.log("error", err); console.log("error", err);

View File

@ -9,15 +9,16 @@ import {
CardHeader, CardHeader,
CardTitle, CardTitle,
} from "@/components/ui/card"; } from "@/components/ui/card";
import { cn } from "@/lib/utils"; import { cn, log } from "@/lib/utils";
import { NDKEvent, NDKUser, NostrEvent } from "@nostr-dev-kit/ndk"; import { NDKEvent, NDKUser, NDKNip07Signer } from "@nostr-dev-kit/ndk";
import { useNDK } from "@/app/_providers/ndk"; import { useNDK } from "@/app/_providers/ndk";
import useCurrentUser from "@/lib/hooks/useCurrentUser"; import useCurrentUser from "@/lib/hooks/useCurrentUser";
import { toast } from "sonner"; import { toast } from "sonner";
import { getTagValues, getTagsValues } from "@/lib/nostr/utils"; import { getTagValues, getTagsValues } from "@/lib/nostr/utils";
import { sendZap, checkPayment } from "@/lib/actions/zap"; import { sendZap, checkPayment } from "@/lib/actions/zap";
import { btcToSats, formatNumber } from "@/lib/utils"; import { btcToSats, formatNumber } from "@/lib/utils";
import { BANNER } from "@/constants";
import { follow } from "@/lib/actions/create";
export default function SubscriptionCard({ event }: { event: NDKEvent }) { export default function SubscriptionCard({ event }: { event: NDKEvent }) {
const { currentUser } = useCurrentUser(); const { currentUser } = useCurrentUser();
const { ndk } = useNDK(); const { ndk } = useNDK();
@ -27,20 +28,20 @@ export default function SubscriptionCard({ event }: { event: NDKEvent }) {
const rawEvent = event.rawEvent(); const rawEvent = event.rawEvent();
const title = getTagValues("title", tags) ?? getTagValues("name", tags) ?? ""; const title = getTagValues("title", tags) ?? getTagValues("name", tags) ?? "";
const image = const image =
getTagValues("image", tags) ?? getTagValues("picture", tags) ?? ""; getTagValues("image", tags) ?? getTagValues("picture", tags) ?? BANNER;
const description = const description =
getTagValues("description", tags) ?? getTagValues("summary", tags) ?? ""; getTagValues("description", tags) ?? getTagValues("summary", tags) ?? "";
const delegate = getTagValues("delegate", tags); const delegate = getTagValues("delegate", tags);
const priceInBTC = parseFloat(getTagValues("price", rawEvent.tags) ?? "0"); const priceInBTC = parseFloat(getTagValues("price", rawEvent.tags) ?? "0");
async function handleSubscribe() { async function handleSubscribe() {
log("func", "handleSubscribe");
try { try {
if (!currentUser) return; if (!currentUser || !ndk?.signer) return;
await currentUser.follow( if (delegate) {
new NDKUser({ await follow(ndk, currentUser, delegate);
hexpubkey: delegate, log("info", "followed");
}), }
);
const result = await sendZap( const result = await sendZap(
ndk!, ndk!,
btcToSats(priceInBTC), btcToSats(priceInBTC),
@ -76,9 +77,15 @@ export default function SubscriptionCard({ event }: { event: NDKEvent }) {
setCheckingPayment(false); setCheckingPayment(false);
} }
} }
useEffect(() => {
if (!currentUser) return;
if (!checkingPayment && !hasValidPayment) {
void handleCheckPayment();
}
}, [currentUser]);
return ( return (
<Card className="group sm:flex"> <Card className="group sm:flex sm:items-stretch">
<div className="overflow-hidden max-sm:h-[100px] max-sm:rounded-t-md sm:w-[250px] sm:rounded-l-md"> <div className="max-h-full overflow-hidden max-sm:h-[100px] max-sm:rounded-t-md sm:w-[250px] sm:rounded-l-md">
<Image <Image
width={250} width={250}
height={150} height={150}
@ -86,18 +93,18 @@ export default function SubscriptionCard({ event }: { event: NDKEvent }) {
alt={title} alt={title}
unoptimized unoptimized
className={cn( className={cn(
"w-auto object-cover object-center transition-all group-hover:scale-105 sm:h-full", "h-full w-full object-cover object-center transition-all group-hover:scale-105 sm:h-full sm:w-auto",
)} )}
/> />
</div> </div>
<div className=""> <div className="h-full flex-1">
<CardHeader className=""> <CardHeader className="">
<CardTitle className="line-clamp-2">{title}</CardTitle> <CardTitle className="line-clamp-2">{title}</CardTitle>
<CardDescription className="line-clamp-3"> <CardDescription className="line-clamp-3">
{description} {description}
</CardDescription> </CardDescription>
</CardHeader> </CardHeader>
<CardContent className="items-strech flex w-full flex-col items-center gap-2 sm:max-w-md sm:flex-row sm:gap-4"> <CardContent className="items-strech mt-auto flex w-full flex-col items-center gap-2 sm:max-w-md sm:flex-row sm:gap-4">
{hasValidPayment ? ( {hasValidPayment ? (
<Button disabled={true} variant={"ghost"} className="w-full"> <Button disabled={true} variant={"ghost"} className="w-full">
Pending sync Pending sync
@ -111,7 +118,7 @@ export default function SubscriptionCard({ event }: { event: NDKEvent }) {
> >
Join now Join now
</Button> </Button>
<Link href={`/sub/${event.encode()}`}> <Link href={`/sub/${event.encode()}`} className="w-full">
<Button variant={"secondary"} className="w-full"> <Button variant={"secondary"} className="w-full">
Details Details
</Button> </Button>

View File

@ -280,3 +280,18 @@ export async function unlockEvent(
await deleteEvent(ndk, [["e", event.id ?? ""]], "Content unlocked"); await deleteEvent(ndk, [["e", event.id ?? ""]], "Content unlocked");
return publishedEvent; 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;
}