Files
lume/src/utils/hooks/useSocial.tsx
2023-08-10 12:34:11 +07:00

90 lines
2.2 KiB
TypeScript

import { useQuery, useQueryClient } from '@tanstack/react-query';
import { useNDK } from '@libs/ndk/provider';
import { createNote } from '@libs/storage';
import { nHoursAgo } from '@utils/date';
import { useAccount } from '@utils/hooks/useAccount';
import { nip02ToArray } from '@utils/transform';
import { useNostr } from './useNostr';
export function useSocial() {
const queryClient = useQueryClient();
const { publish } = useNostr();
const { fetcher, relayUrls } = useNDK();
const { account } = useAccount();
const { status, data: userFollows } = useQuery(
['userFollows', account.pubkey],
async () => {
const res = await fetcher.fetchLastEvent(relayUrls, {
kinds: [3],
authors: [account.pubkey],
});
const list = nip02ToArray(res.tags);
return list;
},
{
enabled: account ? true : false,
refetchOnReconnect: false,
refetchOnMount: false,
refetchOnWindowFocus: false,
}
);
const unfollow = (pubkey: string) => {
const followsAsSet = new Set(userFollows);
followsAsSet.delete(pubkey);
const tags = [];
followsAsSet.forEach((item) => {
tags.push(['p', item]);
});
// publish event
publish({ content: '', kind: 3, tags: tags });
// invalid cache
queryClient.invalidateQueries({
queryKey: ['userFollows', account.pubkey],
});
};
const follow = async (pubkey: string) => {
const followsAsSet = new Set(userFollows);
followsAsSet.add(pubkey);
const tags = [];
followsAsSet.forEach((item) => {
tags.push(['p', item]);
});
// publish event
publish({ content: '', kind: 3, tags: tags });
// invalid cache
queryClient.invalidateQueries({
queryKey: ['userFollows', account.pubkey],
});
// fetch events
const events = await fetcher.fetchAllEvents(
relayUrls,
{ authors: [pubkey], kinds: [1, 6] },
{ since: nHoursAgo(48) }
);
for (const event of events) {
await createNote(
event.id,
event.pubkey,
event.kind,
event.tags,
event.content,
event.created_at
);
}
};
return { status, userFollows, follow, unfollow };
}