snort/src/feed/LoginFeed.js

51 lines
1.6 KiB
JavaScript
Raw Normal View History

2023-01-01 10:44:38 +00:00
import { useEffect, useMemo } from "react";
2022-12-28 23:28:28 +00:00
import { useDispatch, useSelector } from "react-redux";
2022-12-29 22:23:41 +00:00
import EventKind from "../nostr/EventKind";
import { Subscriptions } from "../nostr/Subscriptions";
2022-12-30 23:35:02 +00:00
import { addNotifications, setFollows, setRelays } from "../state/Login";
2023-01-01 10:44:38 +00:00
import useSubscription from "./Subscription";
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 pubKey = useSelector(s => s.login.publicKey);
2023-01-01 10:44:38 +00:00
const sub = useMemo(() => {
if(pubKey === null) {
return null;
}
let sub = new Subscriptions();
sub.Id = `login:${sub.Id}`;
sub.Authors.add(pubKey);
sub.Kinds.add(EventKind.ContactList);
let notifications = new Subscriptions();
notifications.Kinds.add(EventKind.TextNote);
notifications.PTags.add(pubKey);
notifications.Limit = 100;
sub.AddSubscription(notifications);
return sub;
}, [pubKey]);
const { notes } = useSubscription(sub, { leaveOpen: true });
2022-12-28 23:28:28 +00:00
useEffect(() => {
2023-01-01 10:44:38 +00:00
let metadatas = notes.filter(a => a.kind === EventKind.ContactList);
let others = notes.filter(a => a.kind !== EventKind.ContactList);
for(let md of metadatas) {
if (md.content !== "") {
let relays = JSON.parse(md.content);
dispatch(setRelays(relays));
2022-12-28 23:28:28 +00:00
}
2023-01-01 10:44:38 +00:00
let pTags = md.tags.filter(a => a[0] === "p").map(a => a[1]);
dispatch(setFollows(pTags));
2022-12-28 23:28:28 +00:00
}
2023-01-01 10:44:38 +00:00
dispatch(addNotifications(others));
}, [notes]);
2022-12-28 23:28:28 +00:00
}