"use client"; import { useEffect, useState } from "react"; import Image from "next/image"; import Link from "next/link"; import { RiArrowRightLine } from "react-icons/ri"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { NDKEvent } from "@nostr-dev-kit/ndk"; import { BANNER } from "@/constants/app"; import { getNameToShow } from "@/lib/utils"; import { nip19 } from "nostr-tools"; import useProfile from "@/lib/hooks/useProfile"; import useEvents from "@/lib/hooks/useEvents"; import { getTagAllValues, getTagValues } from "@/lib/nostr/utils"; import { useNDK } from "@/app/_providers/ndk"; type CalendarCardProps = { calendar: NDKEvent; }; export default function CalendarCard({ calendar }: CalendarCardProps) { const { pubkey, tags, content } = calendar; const { ndk } = useNDK(); const npub = nip19.npubEncode(pubkey); const { profile } = useProfile(pubkey); const encodedEvent = calendar.encode(); const [upcomingEvents, setUpcomingEvents] = useState([]); const [isFetching, setIsFetching] = useState(false); const name = getTagValues("name", tags); const description = content ?? getTagValues("about", tags); const calendarEvents = getTagAllValues("a", tags); const calendarEventIdentifiers = calendarEvents .map((e) => nip19.decode(e)) .filter(({ type }) => type === "naddr") .map((e) => e.data as nip19.AddressPointer); async function handleFetchEvents(data: nip19.AddressPointer[]) { if (!ndk) return; setIsFetching(true); const events: NDKEvent[] = []; const promiseArray = []; for (const info of data) { const calendarEventPromise = ndk .fetchEvent({ authors: [info.pubkey], ["#d"]: [info.identifier], kinds: [info.kind], }) .then((e) => e && events.push(e)) .catch((err) => console.log("err")); promiseArray.push(calendarEventPromise); } await Promise.all(promiseArray); setUpcomingEvents(events); setIsFetching(false); } useEffect(() => { if ( !ndk || calendarEventIdentifiers.length === 0 || isFetching || upcomingEvents.length ) return; handleFetchEvents(calendarEventIdentifiers); }, [ndk, calendarEventIdentifiers]); return ( background
{name} {description} user Upcoming Events:
    {upcomingEvents.map((item) => { const { tags, content } = item; const encodedEvent = item.encode(); const name = getTagValues("name", tags); const description = content; return (
  • {name}

    {description ?? ""}

  • ); })}
); }