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";
|
|
|
|
|
|
|
|
export default function useRelaysFeed(pubkey: HexKey) {
|
|
|
|
const sub = useMemo(() => {
|
|
|
|
const x = new Subscriptions();
|
|
|
|
x.Id = `relays:${pubkey.slice(0, 12)}`;
|
|
|
|
x.Kinds = new Set([EventKind.Relays]);
|
|
|
|
x.Authors = new Set([pubkey]);
|
|
|
|
x.Limit = 1;
|
|
|
|
return x;
|
|
|
|
}, [pubkey]);
|
|
|
|
|
|
|
|
const relays = useSubscription(sub, { leaveOpen: false, cache: true });
|
|
|
|
const notes = relays.store.notes;
|
|
|
|
const tags = notes.slice(-1)[0]?.tags || [];
|
|
|
|
return tags.reduce((rs, tag) => {
|
|
|
|
const [t, url, ...settings] = tag;
|
|
|
|
if (t === "r") {
|
|
|
|
return [
|
|
|
|
...rs,
|
|
|
|
{
|
|
|
|
url,
|
|
|
|
settings: {
|
|
|
|
read: settings.length === 0 || settings.includes("read"),
|
|
|
|
write: settings.length === 0 || settings.includes("write"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
return rs;
|
2023-02-11 20:05:46 +00:00
|
|
|
}, [] as FullRelaySettings[]);
|
2023-02-10 19:23:52 +00:00
|
|
|
}
|