2022-12-28 23:28:28 +00:00
|
|
|
import { useContext, useEffect } from "react";
|
|
|
|
import { useDispatch, useSelector } from "react-redux";
|
2022-12-30 23:35:02 +00:00
|
|
|
import { System } from "..";
|
2022-12-29 22:23:41 +00:00
|
|
|
import Event from "../nostr/Event";
|
|
|
|
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";
|
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);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-12-30 23:35:02 +00:00
|
|
|
if (pubKey) {
|
2022-12-28 23:28:28 +00:00
|
|
|
let sub = new Subscriptions();
|
2022-12-30 23:35:02 +00:00
|
|
|
sub.Id = "login";
|
2022-12-28 23:28:28 +00:00
|
|
|
sub.Authors.add(pubKey);
|
|
|
|
sub.Kinds.add(EventKind.ContactList);
|
2022-12-30 23:35:02 +00:00
|
|
|
|
|
|
|
let notifications = new Subscriptions();
|
|
|
|
notifications.Kinds.add(EventKind.TextNote);
|
|
|
|
notifications.Kinds.add(EventKind.Reaction);
|
|
|
|
notifications.PTags.add(pubKey);
|
|
|
|
sub.AddSubscription(notifications);
|
|
|
|
|
2022-12-28 23:28:28 +00:00
|
|
|
sub.OnEvent = (e) => {
|
|
|
|
let ev = Event.FromObject(e);
|
2022-12-30 23:35:02 +00:00
|
|
|
switch (ev.Kind) {
|
|
|
|
case EventKind.ContactList: {
|
|
|
|
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));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
dispatch(addNotifications(ev.ToObject()));
|
|
|
|
break;
|
|
|
|
}
|
2022-12-28 23:28:28 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-30 23:35:02 +00:00
|
|
|
System.AddSubscription(sub);
|
|
|
|
return () => System.RemoveSubscription(sub.Id);
|
2022-12-28 23:28:28 +00:00
|
|
|
}
|
2022-12-30 23:35:02 +00:00
|
|
|
}, [pubKey]);
|
2022-12-28 23:28:28 +00:00
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|