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

32 lines
952 B
TypeScript
Raw Normal View History

2023-02-10 19:23:52 +00:00
import { useMemo } from "react";
2023-03-28 14:34:01 +00:00
import { HexKey, FullRelaySettings, EventKind } from "@snort/nostr";
import { RequestBuilder } from "System";
import { ReplaceableNoteStore } from "System/NoteCollection";
import useRequestBuilder from "Hooks/useRequestBuilder";
2023-02-10 19:23:52 +00:00
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-03-28 14:34:01 +00:00
const b = new RequestBuilder(`relays:${pubkey.slice(0, 12)}`);
b.withFilter().authors([pubkey]).kinds([EventKind.ContactList]);
return b;
2023-02-10 19:23:52 +00:00
}, [pubkey]);
2023-03-28 14:34:01 +00:00
const relays = useRequestBuilder<ReplaceableNoteStore>(ReplaceableNoteStore, sub);
2023-03-28 14:34:01 +00:00
if (!relays.data?.content) {
return [] as FullRelaySettings[];
}
try {
2023-03-28 14:34:01 +00:00
return Object.entries(JSON.parse(relays.data.content)).map(([url, settings]) => ({
url,
settings,
})) as FullRelaySettings[];
} catch (error) {
console.error(error);
return [] as FullRelaySettings[];
}
2023-02-10 19:23:52 +00:00
}