snort/packages/app/src/Hooks/useFollowControls.ts

33 lines
854 B
TypeScript
Raw Normal View History

2024-04-22 13:38:14 +00:00
import { NostrLink } from "@snort/system";
2024-01-30 22:38:23 +00:00
import useLogin from "./useLogin";
/**
* Simple hook for adding / removing follows
*/
export default function useFollowsControls() {
2024-04-22 13:38:14 +00:00
const state = useLogin(s => s.state);
2024-01-30 22:38:23 +00:00
2024-04-22 13:38:14 +00:00
return {
isFollowing: (pk: string) => {
return state.follows?.includes(pk);
},
addFollow: async (pk: Array<string>) => {
for (const p of pk) {
await state.follow(NostrLink.publicKey(p), false);
}
await state.saveContacts();
},
removeFollow: async (pk: Array<string>) => {
for (const p of pk) {
await state.unfollow(NostrLink.publicKey(p), false);
}
await state.saveContacts();
},
setFollows: async (pk: Array<string>) => {
await state.replaceFollows(pk.map(a => NostrLink.publicKey(a)));
},
followList: state.follows ?? [],
};
2024-01-30 22:38:23 +00:00
}