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-03 12:06:53 +00:00
|
|
|
import { setUserData } from "../state/Users";
|
2023-01-01 10:44:38 +00:00
|
|
|
import useSubscription from "./Subscription";
|
2023-01-03 12:06:53 +00:00
|
|
|
import { mapEventToProfile } from "./UsersFeed";
|
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);
|
2023-01-03 12:06:53 +00:00
|
|
|
sub.Kinds.add(EventKind.SetMetadata);
|
2023-01-01 10:44:38 +00:00
|
|
|
|
|
|
|
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-03 12:06:53 +00:00
|
|
|
let contactList = notes.filter(a => a.kind === EventKind.ContactList);
|
|
|
|
let notifications = notes.filter(a => a.kind === EventKind.TextNote);
|
|
|
|
let metadata = notes.filter(a => a.kind === EventKind.SetMetadata).map(a => mapEventToProfile(a));
|
2023-01-01 10:44:38 +00:00
|
|
|
|
2023-01-03 12:06:53 +00:00
|
|
|
for(let cl of contactList) {
|
|
|
|
if (cl.content !== "") {
|
|
|
|
let relays = JSON.parse(cl.content);
|
2023-01-01 10:44:38 +00:00
|
|
|
dispatch(setRelays(relays));
|
2022-12-28 23:28:28 +00:00
|
|
|
}
|
2023-01-03 12:06:53 +00:00
|
|
|
let pTags = cl.tags.filter(a => a[0] === "p").map(a => a[1]);
|
2023-01-01 10:44:38 +00:00
|
|
|
dispatch(setFollows(pTags));
|
2022-12-28 23:28:28 +00:00
|
|
|
}
|
|
|
|
|
2023-01-03 12:06:53 +00:00
|
|
|
dispatch(addNotifications(notifications));
|
|
|
|
dispatch(setUserData(metadata));
|
2023-01-01 10:44:38 +00:00
|
|
|
}, [notes]);
|
2022-12-28 23:28:28 +00:00
|
|
|
}
|