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-02-20 22:52:03 +00:00
|
|
|
|
2023-03-28 14:34:01 +00:00
|
|
|
if (!relays.data?.content) {
|
2023-02-20 22:52:03 +00:00
|
|
|
return [] as FullRelaySettings[];
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-03-28 14:34:01 +00:00
|
|
|
return Object.entries(JSON.parse(relays.data.content)).map(([url, settings]) => ({
|
2023-02-20 22:52:03 +00:00
|
|
|
url,
|
|
|
|
settings,
|
|
|
|
})) as FullRelaySettings[];
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
return [] as FullRelaySettings[];
|
|
|
|
}
|
2023-02-10 19:23:52 +00:00
|
|
|
}
|