snort/src/feed/LoginFeed.js

37 lines
1.3 KiB
JavaScript
Raw Normal View History

2022-12-28 23:28:28 +00:00
import { useContext, useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
2022-12-29 22:23:41 +00:00
import { NostrContext } from "..";
import Event from "../nostr/Event";
import EventKind from "../nostr/EventKind";
import { Subscriptions } from "../nostr/Subscriptions";
import { setFollows, setRelays } from "../state/Login";
2022-12-28 23:28:28 +00:00
/**
* Managed loading data for the current logged in user
*/
export default function useLoginFeed() {
const dispatch = useDispatch();
const system = useContext(NostrContext);
const pubKey = useSelector(s => s.login.publicKey);
useEffect(() => {
if (system && pubKey) {
let sub = new Subscriptions();
sub.Authors.add(pubKey);
sub.Kinds.add(EventKind.ContactList);
sub.OnEvent = (e) => {
let ev = Event.FromObject(e);
if (ev.Content !== "") {
let relays = JSON.parse(ev.Content);
dispatch(setRelays(relays));
}
let pTags = ev.Tags.filter(a => a.Key === "p").map(a => a.PubKey);
dispatch(setFollows(pTags));
}
system.AddSubscription(sub);
return () => system.RemoveSubscription(sub.Id);
}
}, [system, pubKey]);
return {};
}