snort/packages/app/src/Feed/RelaysFeed.tsx

34 lines
956 B
TypeScript
Raw Normal View History

2023-02-10 19:23:52 +00:00
import { useMemo } from "react";
2023-02-11 20:05:46 +00:00
import { HexKey, FullRelaySettings } from "@snort/nostr";
import { EventKind, Subscriptions } from "@snort/nostr";
2023-02-10 19:23:52 +00:00
import useSubscription from "./Subscription";
2023-02-27 13:17:13 +00:00
export default function useRelaysFeed(pubkey?: HexKey) {
2023-02-10 19:23:52 +00:00
const sub = useMemo(() => {
2023-02-27 13:17:13 +00:00
if (!pubkey) return null;
2023-02-10 19:23:52 +00:00
const x = new Subscriptions();
x.Id = `relays:${pubkey.slice(0, 12)}`;
x.Kinds = new Set([EventKind.ContactList]);
2023-02-10 19:23:52 +00:00
x.Authors = new Set([pubkey]);
x.Limit = 1;
return x;
}, [pubkey]);
const relays = useSubscription(sub, { leaveOpen: false, cache: false });
const eventContent = relays.store.notes[0]?.content;
if (!eventContent) {
return [] as FullRelaySettings[];
}
try {
return Object.entries(JSON.parse(eventContent)).map(([url, settings]) => ({
url,
settings,
})) as FullRelaySettings[];
} catch (error) {
console.error(error);
return [] as FullRelaySettings[];
}
2023-02-10 19:23:52 +00:00
}