2024-08-13 16:28:25 -05:00
|
|
|
import React, { useEffect, useState } from "react";
|
|
|
|
import { useNDKContext } from "@/context/NDKContext";
|
|
|
|
import { parseEvent } from "@/utils/nostr";
|
|
|
|
import { ProgressSpinner } from "primereact/progressspinner";
|
2024-09-24 09:36:28 -05:00
|
|
|
import { nip19 } from "nostr-tools";
|
|
|
|
import appConfig from "@/config/appConfig";
|
2024-08-13 16:28:25 -05:00
|
|
|
|
|
|
|
const PurchasedListItem = ({ eventId, category }) => {
|
|
|
|
const { ndk } = useNDKContext();
|
|
|
|
const [event, setEvent] = useState(null);
|
2024-09-24 09:36:28 -05:00
|
|
|
const [naddr, setNaddr] = useState(null);
|
2024-08-13 16:28:25 -05:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const fetchEvent = async () => {
|
|
|
|
if (!eventId) return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await ndk.connect();
|
|
|
|
const event = await ndk.fetchEvent(eventId);
|
|
|
|
if (event) {
|
|
|
|
setEvent(parseEvent(event));
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error fetching event:", error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fetchEvent();
|
|
|
|
}, [eventId, ndk]);
|
|
|
|
|
2024-09-24 09:36:28 -05:00
|
|
|
useEffect(() => {
|
|
|
|
if (event) {
|
|
|
|
encodeNaddr();
|
|
|
|
}
|
|
|
|
}, [event]);
|
|
|
|
|
|
|
|
const encodeNaddr = () => {
|
|
|
|
setNaddr(nip19.naddrEncode({
|
|
|
|
pubkey: event.pubkey,
|
|
|
|
identifier: event.id,
|
|
|
|
kind: event.kind,
|
|
|
|
relayUrls: appConfig.defaultRelayUrls
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
return !event || !ndk ? <ProgressSpinner className="w-[40px] h-[40px]" /> : (<a className="text-blue-500 underline hover:text-blue-600" href={category === "courses" ? `/course/${naddr}` : `/details/${naddr}`}>{event.title}</a>);
|
2024-08-13 16:28:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export default PurchasedListItem;
|