add fetch events when follow user

This commit is contained in:
Ren Amamiya
2023-07-04 11:16:39 +07:00
parent 1b8eaa2988
commit 744fbd5683
5 changed files with 53 additions and 20 deletions

View File

@@ -1,7 +1,10 @@
import { useAccount } from "./useAccount";
import { usePublish } from "@libs/ndk";
import { createNote } from "@libs/storage";
import { NDKEvent, NDKFilter } from "@nostr-dev-kit/ndk";
import { RelayContext } from "@shared/relayProvider";
import { useQuery, useQueryClient } from "@tanstack/react-query";
import { dateToUnix, getHourAgo } from "@utils/date";
import { nip02ToArray } from "@utils/transform";
import { useContext } from "react";
@@ -47,7 +50,7 @@ export function useSocial() {
});
};
const follow = (pubkey: string) => {
const follow = async (pubkey: string) => {
const followsAsSet = new Set(userFollows);
followsAsSet.add(pubkey);
@@ -62,6 +65,24 @@ export function useSocial() {
queryClient.invalidateQueries({
queryKey: ["userFollows", account.pubkey],
});
// fetch events
const filter: NDKFilter = {
authors: [pubkey],
kinds: [1, 6],
since: dateToUnix(getHourAgo(48, new Date())),
};
const events = await ndk.fetchEvents(filter);
events.forEach((event: NDKEvent) => {
createNote(
event.id,
event.pubkey,
event.kind,
event.tags,
event.content,
event.created_at,
);
});
};
return { status, userFollows, follow, unfollow };