diff --git a/app/(app)/(profile)/[npub]/_components/FollowButton.tsx b/app/(app)/(profile)/[npub]/_components/FollowButton.tsx index c0ad6fa..6bcfda5 100644 --- a/app/(app)/(profile)/[npub]/_components/FollowButton.tsx +++ b/app/(app)/(profile)/[npub]/_components/FollowButton.tsx @@ -9,11 +9,10 @@ import { NDKUser } from "@nostr-dev-kit/ndk"; type FollowButtonProps = { pubkey: string; - follows: Set; }; -export default function FollowButton({ pubkey, follows }: FollowButtonProps) { - const { currentUser } = useCurrentUser(); +export default function FollowButton({ pubkey }: FollowButtonProps) { + const { currentUser, follows, addFollow, setFollows } = useCurrentUser(); const { ndk } = useNDK(); const [loading, setLoading] = useState(false); @@ -22,13 +21,27 @@ export default function FollowButton({ pubkey, follows }: FollowButtonProps) { setLoading(true); try { await follow(ndk, currentUser, pubkey); - toast.success("Payment Sent!"); + addFollow(new NDKUser({ hexpubkey: pubkey })); + toast.success("Following!"); } catch (err) { console.log("Error", err); } setLoading(false); } - if (Array.from(follows).find((i) => i.pubkey === pubkey)) { + async function handleUnfollow() { + if (!ndk || !currentUser) return; + setLoading(true); + try { + await follow(ndk, currentUser, pubkey, true); + const newFollows = Array.from(follows).filter((i) => i.pubkey !== pubkey); + setFollows(new Set(newFollows)); + toast.success("Unfollowed!"); + } catch (err) { + console.log("Error", err); + } + setLoading(false); + } + if (!Array.from(follows).find((i) => i.pubkey === pubkey)) { return ( + ); } - return null; } diff --git a/app/(app)/(profile)/[npub]/page.tsx b/app/(app)/(profile)/[npub]/page.tsx index 0745c9c..18c9288 100644 --- a/app/(app)/(profile)/[npub]/page.tsx +++ b/app/(app)/(profile)/[npub]/page.tsx @@ -107,7 +107,7 @@ export default function ProfilePage({ Edit )} - {currentUser && } + {currentUser && }
diff --git a/lib/actions/create.ts b/lib/actions/create.ts index 2845eb7..fa182b2 100644 --- a/lib/actions/create.ts +++ b/lib/actions/create.ts @@ -281,16 +281,29 @@ export async function unlockEvent( return publishedEvent; } -export async function follow(ndk: NDK, currentUser: NDKUser, pubkey: string) { +export async function follow( + ndk: NDK, + currentUser: NDKUser, + pubkey: string, + unfollow?: boolean, +) { const userContacts = await ndk.fetchEvent({ kinds: [3], authors: [currentUser.pubkey], }); if (!userContacts) return; + let newTags = userContacts.tags; + if (unfollow) { + newTags = newTags.filter(([t, k]) => + t === "p" && k === pubkey ? false : true, + ); + } else { + newTags.push(["p", pubkey]); + } const newEvent = { kind: 3, ...userContacts.rawEvent(), - tags: [...userContacts.tags, ["p", pubkey]], + tags: newTags, }; const newContacts = await createEvent(ndk, newEvent); return newContacts; diff --git a/lib/hooks/useCurrentUser.ts b/lib/hooks/useCurrentUser.ts index 8bf6b36..dc78a95 100644 --- a/lib/hooks/useCurrentUser.ts +++ b/lib/hooks/useCurrentUser.ts @@ -15,6 +15,7 @@ export default function useCurrentUser() { updateCurrentUser, follows, setFollows, + addFollow, } = currentUserStore(); const { loginWithNip07, getProfile, ndk, fetchEvents } = useNDK(); const { init } = useLists(); @@ -90,5 +91,7 @@ export default function useCurrentUser() { attemptLogin, initSubscriptions, mySubscription, + addFollow, + setFollows, }; } diff --git a/lib/stores/currentUser.ts b/lib/stores/currentUser.ts index 175e76e..c8a40b0 100644 --- a/lib/stores/currentUser.ts +++ b/lib/stores/currentUser.ts @@ -10,6 +10,7 @@ interface CurrentUserState { setCurrentUser: (user: NDKUser | null) => void; updateCurrentUser: (user: Partial) => void; setFollows: (follows: Set) => void; + addFollow: (follow: NDKUser) => void; } const currentUserStore = create()((set) => ({ @@ -23,6 +24,8 @@ const currentUserStore = create()((set) => ({ currentUser: { ...state.currentUser, ...user } as NDKUser, })), setFollows: (follows) => set((state) => ({ ...state, follows: follows })), + addFollow: (follow) => + set((state) => ({ ...state, follows: new Set(state.follows).add(follow) })), })); export default currentUserStore;