diff --git a/packages/frontend-main/src/composables/useCreatePost.ts b/packages/frontend-main/src/composables/useCreatePost.ts index b7d792a8..d99f1324 100644 --- a/packages/frontend-main/src/composables/useCreatePost.ts +++ b/packages/frontend-main/src/composables/useCreatePost.ts @@ -10,7 +10,7 @@ import { userPosts } from './useUserPosts'; import { useWallet } from './useWallet'; import { fractionalDigits } from '@/utility/atomics'; -import { infiniteDataWithNewItem, newPost } from '@/utility/optimisticBuilders'; +import { infiniteDataWithNewItem, newPost as newPostFn } from '@/utility/optimisticBuilders'; interface CreatePostRequestMutation { message: string; @@ -26,9 +26,7 @@ export function useCreatePost( const isToastShown = ref(false); useTxNotification('Post', txSuccess, txError); - const { - mutateAsync, - } = useMutation({ + const { mutateAsync } = useMutation({ mutationFn: async ({ message, amountAtomics }: CreatePostRequestMutation) => { txError.value = undefined; txSuccess.value = undefined; @@ -55,43 +53,36 @@ export function useCreatePost( return txSuccess.value; } }, - onMutate: async () => { - const feedOpts = feed(queryClient); - const userPostsOpts = userPosts({ userAddress: wallet.address }); - await Promise.all([ - queryClient.cancelQueries(feedOpts), - queryClient.cancelQueries(userPostsOpts), - ]); - - const previousFeed = queryClient.getQueryData( - feedOpts.queryKey, - ) as InfiniteData | undefined; - const previousUserPosts = queryClient.getQueryData( - userPostsOpts.queryKey, - ) as InfiniteData | undefined; - - return { previousFeed, previousUserPosts }; - }, - onSuccess: (hash, variables, context) => { + onSuccess: (hash, variables) => { if (!hash) throw new Error('Error: No hash in TX'); const feedOpts = feed(queryClient); const userPostsOpts = userPosts({ userAddress: wallet.address }); - // Created Post - const optimisticNewPost: Post = newPost({ message: variables.message, quantity: Decimal.fromAtomics(variables.amountAtomics, fractionalDigits).atomics, hash, author: wallet.address.value, postHash: null }); - const newFeedData = infiniteDataWithNewItem({ previousItems: context.previousFeed, newItem: optimisticNewPost }); - const newUserPostsData = infiniteDataWithNewItem({ previousItems: context.previousUserPosts, newItem: optimisticNewPost }); + const newPost: Post = newPostFn({ + message: variables.message, + quantity: Decimal.fromAtomics(variables.amountAtomics, fractionalDigits).atomics, + hash, + author: wallet.address.value, + postHash: null, + }); + + const previousFeed = queryClient.getQueryData>(feedOpts.queryKey); + const previousUserPosts = queryClient.getQueryData>(userPostsOpts.queryKey); + + const newFeedData = infiniteDataWithNewItem({ + previousItems: previousFeed, + newItem: newPost, + }); + + const newUserPostsData = infiniteDataWithNewItem({ + previousItems: previousUserPosts, + newItem: newPost, + }); queryClient.setQueryData(feedOpts.queryKey, newFeedData); queryClient.setQueryData(userPostsOpts.queryKey, newUserPostsData); }, - onError: (_, __, context) => { - const feedOpts = feed(queryClient); - const userPostsOpts = userPosts({ userAddress: wallet.address }); - queryClient.setQueryData(feedOpts.queryKey, context?.previousFeed); - queryClient.setQueryData(userPostsOpts.queryKey, context?.previousUserPosts); - }, onSettled: () => { isToastShown.value = false; }, diff --git a/packages/frontend-main/src/composables/useCreateReply.ts b/packages/frontend-main/src/composables/useCreateReply.ts index b48a7144..43c3cdb7 100644 --- a/packages/frontend-main/src/composables/useCreateReply.ts +++ b/packages/frontend-main/src/composables/useCreateReply.ts @@ -2,7 +2,7 @@ import type { Post, ReplyWithParent } from 'api-main/types/feed'; import { type Ref, ref } from 'vue'; import { Decimal } from '@cosmjs/math'; -import { type InfiniteData, useMutation, useQueryClient } from '@tanstack/vue-query'; +import { useMutation, useQueryClient } from '@tanstack/vue-query'; import { post } from './usePost'; import { replies } from './useReplies'; @@ -28,9 +28,7 @@ export function useCreateReply( const isToastShown = ref(false); useTxNotification('Reply', txSuccess, txError); - const { - mutateAsync, - } = useMutation({ + const { mutateAsync } = useMutation({ mutationFn: async ({ parentPost, message, amountAtomics }: CreateReplyRequestMutation) => { txError.value = undefined; txSuccess.value = undefined; @@ -57,67 +55,39 @@ export function useCreateReply( return txSuccess.value; } }, - onMutate: async (variables) => { - const parentPostOpts = post({ hash: ref(variables.parentPost.value.hash) }); - const repliesOpts = replies({ hash: ref(variables.parentPost.value.hash) }); - const userRepliesOpts = userReplies({ userAddress: wallet.address }); - - await Promise.all([ - queryClient.cancelQueries(parentPostOpts), - queryClient.cancelQueries(repliesOpts), - queryClient.cancelQueries(userRepliesOpts), - ]); - - const previousParentPost = queryClient.getQueryData( - parentPostOpts.queryKey, - ) as Post | undefined; - const previousReplies = queryClient.getQueryData( - repliesOpts.queryKey, - ) as InfiniteData | undefined; - const previousUserReplies = queryClient.getQueryData( - userRepliesOpts.queryKey, - ) as InfiniteData | undefined; - - return { previousParentPost, previousReplies, previousUserReplies }; - }, - onSuccess: (createdHash, variables, context) => { + onSuccess: (createdHash, variables) => { if (!createdHash) throw new Error('Error: No hash in TX'); const parentPostOpts = post({ hash: ref(variables.parentPost.value.hash) }); const repliesOpts = replies({ hash: ref(variables.parentPost.value.hash) }); const userRepliesOpts = userReplies({ userAddress: wallet.address }); + const previousParentPost = queryClient.getQueryData(parentPostOpts.queryKey); + const previousReplies = queryClient.getQueryData(repliesOpts.queryKey); + const previousUserReplies = queryClient.getQueryData(userRepliesOpts.queryKey); + // Parent Post with updated replies count - const optimisticParentPost: Post - = context.previousParentPost - ? { ...context.previousParentPost, replies: (context.previousParentPost.replies || 0) + 1 } + const newParentPost: Post + = previousParentPost + ? { ...previousParentPost, replies: (previousParentPost.replies || 0) + 1 } : { ...variables.parentPost.value, replies: (variables.parentPost.value.replies || 0) + 1 }; // Created Post with parent hash as post_hash - const optimisticNewReply: Post = newPost({ message: variables.message, quantity: Decimal.fromAtomics(variables.amountAtomics, fractionalDigits).atomics, hash: createdHash, postHash: variables.parentPost.value.hash, author: wallet.address.value }); + const newReply: Post = newPost({ message: variables.message, quantity: Decimal.fromAtomics(variables.amountAtomics, fractionalDigits).atomics, hash: createdHash, postHash: variables.parentPost.value.hash, author: wallet.address.value }); // Created Post in ReplyWithParent - const optimisticNewUserReply: ReplyWithParent = { + const newUserReply: ReplyWithParent = { reply: newPost( { message: variables.message, quantity: Decimal.fromAtomics(variables.amountAtomics, fractionalDigits).atomics, hash: createdHash, postHash: variables.parentPost.value.hash, author: wallet.address.value }, ), - parent: optimisticParentPost, + parent: newParentPost, }; - const newRepliesData = infiniteDataWithNewItem({ previousItems: context.previousReplies, newItem: optimisticNewReply }); - const newUserRepliesData = infiniteDataWithNewItem({ previousItems: context.previousUserReplies, newItem: optimisticNewUserReply }); + const newRepliesData = infiniteDataWithNewItem({ previousItems: previousReplies, newItem: newReply }); + const newUserRepliesData = infiniteDataWithNewItem({ previousItems: previousUserReplies, newItem: newUserReply }); - queryClient.setQueryData(parentPostOpts.queryKey, optimisticParentPost); + queryClient.setQueryData(parentPostOpts.queryKey, newParentPost); queryClient.setQueryData(repliesOpts.queryKey, newRepliesData); queryClient.setQueryData(userRepliesOpts.queryKey, newUserRepliesData); }, - onError: (_, variables, context) => { - const parentPostOpts = post({ hash: ref(variables.parentPost.value.hash) }); - const repliesOpts = replies({ hash: ref(variables.parentPost.value.hash) }); - const userRepliesOpts = userReplies({ userAddress: wallet.address }); - - queryClient.setQueryData(parentPostOpts.queryKey, context?.previousParentPost); - queryClient.setQueryData(repliesOpts.queryKey, context?.previousReplies); - queryClient.setQueryData(userRepliesOpts.queryKey, context?.previousUserReplies); - }, onSettled: () => { isToastShown.value = false; }, diff --git a/packages/frontend-main/src/composables/useDislikePost.ts b/packages/frontend-main/src/composables/useDislikePost.ts index a24953ee..3615d2e6 100644 --- a/packages/frontend-main/src/composables/useDislikePost.ts +++ b/packages/frontend-main/src/composables/useDislikePost.ts @@ -14,8 +14,7 @@ interface DislikePostRequestMutation { amountAtomics: string; } -export function useDislikePost( -) { +export function useDislikePost() { const queryClient = useQueryClient(); const wallet = useWallet(); const txError = ref(); @@ -23,9 +22,7 @@ export function useDislikePost( const isToastShown = ref(false); useTxNotification('Dislike', txSuccess, txError); - const { - mutateAsync, - } = useMutation({ + const { mutateAsync } = useMutation({ mutationFn: async ({ post, amountAtomics }: DislikePostRequestMutation) => { txError.value = undefined; txSuccess.value = undefined; @@ -41,34 +38,14 @@ export function useDislikePost( } txSuccess.value = result.tx?.transactionHash; }, - onMutate: async (variables) => { + onSuccess: (_, variables) => { const postOpts = post({ hash: ref(variables.post.value.hash) }); - - await Promise.all([ - queryClient.cancelQueries(postOpts), - ]); - - const previousPost = queryClient.getQueryData( - postOpts.queryKey, - ) as Post | undefined; - - return { - previousPost, - }; - }, - onSuccess: (_, variables, context) => { - const postOpts = post({ hash: ref(variables.post.value.hash) }); - // Post with updated dislikes_burnt - const optimisticPost: Post - = context.previousPost - ? { ...context.previousPost, dislikes_burnt: addAtomics((context.previousPost.dislikes_burnt ?? 0).toString(), variables.amountAtomics) } - : { ...variables.post.value, dislikes_burnt: addAtomics((variables.post.value.dislikes_burnt ?? 0).toString(), variables.amountAtomics) }; - - queryClient.setQueryData(postOpts.queryKey, optimisticPost); - }, - onError: (_, variables, context) => { - const postOpts = post({ hash: ref(variables.post.value.hash) }); - queryClient.setQueryData(postOpts.queryKey, context?.previousPost); + // Update post's dislikes_burnt + queryClient.setQueryData(postOpts.queryKey, (current) => { + const currentDislikesBurnt = current?.dislikes_burnt ?? variables.post.value.dislikes_burnt ?? '0'; + const newDislikeBurnt = addAtomics(currentDislikesBurnt, variables.amountAtomics); + return { ...(current ?? variables.post.value), dislikes_burnt: newDislikeBurnt }; + }); }, onSettled: () => { isToastShown.value = false; diff --git a/packages/frontend-main/src/composables/useFlagPost.ts b/packages/frontend-main/src/composables/useFlagPost.ts index 1d7f1041..2687f207 100644 --- a/packages/frontend-main/src/composables/useFlagPost.ts +++ b/packages/frontend-main/src/composables/useFlagPost.ts @@ -23,9 +23,7 @@ export function useFlagPost( const isToastShown = ref(false); useTxNotification('Flag', txSuccess, txError); - const { - mutateAsync, - } = useMutation({ + const { mutateAsync } = useMutation({ mutationFn: async ({ post, amountAtomics }: FlagPostRequestMutation) => { txError.value = undefined; txSuccess.value = undefined; @@ -45,34 +43,14 @@ export function useFlagPost( return txSuccess.value; } }, - onMutate: async (variables) => { + onSuccess: (_, variables) => { const postOpts = post({ hash: ref(variables.post.value.hash) }); - - await Promise.all([ - queryClient.cancelQueries(postOpts), - ]); - - const previousPost = queryClient.getQueryData( - postOpts.queryKey, - ) as Post | undefined; - - return { - previousPost, - }; - }, - onSuccess: (_, variables, context) => { - const postOpts = post({ hash: ref(variables.post.value.hash) }); - // Post with updated flags_burnt - const optimisticPost: Post - = context.previousPost - ? { ...context.previousPost, flags_burnt: addAtomics((context.previousPost.flags_burnt ?? 0).toString(), variables.amountAtomics) } - : { ...variables.post.value, flags_burnt: addAtomics((variables.post.value.flags_burnt ?? 0).toString(), variables.amountAtomics) }; - - queryClient.setQueryData(postOpts.queryKey, optimisticPost); - }, - onError: (_, variables, context) => { - const postOpts = post({ hash: ref(variables.post.value.hash) }); - queryClient.setQueryData(postOpts.queryKey, context?.previousPost); + // Update post's flags_burnt + queryClient.setQueryData(postOpts.queryKey, (current) => { + const currentFlagsBurnt = current?.flags_burnt ?? variables.post.value.flags_burnt ?? '0'; + const newFlagsBurnt = addAtomics(currentFlagsBurnt, variables.amountAtomics); + return { ...(current ?? variables.post.value), flags_burnt: newFlagsBurnt }; + }); }, onSettled: () => { isToastShown.value = false; diff --git a/packages/frontend-main/src/composables/useFollowUser.ts b/packages/frontend-main/src/composables/useFollowUser.ts index 66779ee2..57d8963a 100644 --- a/packages/frontend-main/src/composables/useFollowUser.ts +++ b/packages/frontend-main/src/composables/useFollowUser.ts @@ -25,9 +25,7 @@ export function useFollowUser( const isToastShown = ref(false); useTxNotification('Follow', txSuccess, txError); - const { - mutateAsync, - } = useMutation({ + const { mutateAsync } = useMutation({ mutationFn: async ({ userAddress, amountAtomics }: FollowUserRequestMutation) => { txError.value = undefined; txSuccess.value = undefined; @@ -44,49 +42,26 @@ export function useFollowUser( } txSuccess.value = result.tx?.transactionHash; }, - onMutate: async (variables) => { - const isFollowingOpts = isFollowing({ followerAddress: wallet.address, followingAddress: variables.userAddress }); - const followingOpts = following({ userAddress: wallet.address }); - - await Promise.all([ - await queryClient.cancelQueries(isFollowingOpts), - await queryClient.cancelQueries(followingOpts), - ]); - - const previousIsFollowing = queryClient.getQueryData( - isFollowingOpts.queryKey, - ) as boolean | undefined; - const previousFollowing = queryClient.getQueryData( - followingOpts.queryKey, - ) as InfiniteData | undefined; - - return { - previousIsFollowing, - previousFollowing, - }; - }, - onSuccess: (_, variables, context) => { - const isFollowingOpts = isFollowing({ followerAddress: wallet.address, followingAddress: variables.userAddress }); + onSuccess: (_, variables) => { + const isFollowingOpts = isFollowing({ + followerAddress: wallet.address, + followingAddress: variables.userAddress, + }); const followingOpts = following({ userAddress: wallet.address }); const followingPostsOpts = followingPosts({ userAddress: wallet.address }); - - const optimisticNewFollowing: Following = newFollowing({ address: variables.userAddress.value }); - const newFollowingData = infiniteDataWithNewItem({ - previousItems: context.previousFollowing, - newItem: optimisticNewFollowing, + const newFollowingItem: Following = newFollowing({ + address: variables.userAddress.value, }); - queryClient.setQueryData(isFollowingOpts.queryKey, true); - queryClient.setQueryData(followingOpts.queryKey, newFollowingData); + queryClient.setQueryData(isFollowingOpts.queryKey, () => true); + queryClient.setQueryData>(followingOpts.queryKey, current => + infiniteDataWithNewItem({ + previousItems: current ?? { pages: [], pageParams: [] }, + newItem: newFollowingItem, + }), + ); queryClient.invalidateQueries(followingPostsOpts); }, - onError: (_, variables, context) => { - const isFollowingOpts = isFollowing({ followerAddress: wallet.address, followingAddress: variables.userAddress }); - const followingOpts = following({ userAddress: wallet.address }); - - queryClient.setQueryData(isFollowingOpts.queryKey, context?.previousIsFollowing); - queryClient.setQueryData(followingOpts.queryKey, context?.previousFollowing); - }, onSettled: () => { isToastShown.value = false; }, diff --git a/packages/frontend-main/src/composables/useLikePost.ts b/packages/frontend-main/src/composables/useLikePost.ts index 832d9f95..49de33eb 100644 --- a/packages/frontend-main/src/composables/useLikePost.ts +++ b/packages/frontend-main/src/composables/useLikePost.ts @@ -14,8 +14,7 @@ interface LikePostRequestMutation { amountAtomics: string; } -export function useLikePost( -) { +export function useLikePost() { const queryClient = useQueryClient(); const wallet = useWallet(); const txError = ref(); @@ -23,9 +22,7 @@ export function useLikePost( const isToastShown = ref(false); useTxNotification('Like', txSuccess, txError); - const { - mutateAsync, - } = useMutation({ + const { mutateAsync } = useMutation({ mutationFn: async ({ post, amountAtomics }: LikePostRequestMutation) => { txError.value = undefined; txSuccess.value = undefined; @@ -35,41 +32,20 @@ export function useLikePost( 'Like', { args: [post.value.hash], amount: amountAtomics }, ); - if (!result.broadcast) { txError.value = result.msg; throw new Error(result.msg); } txSuccess.value = result.tx?.transactionHash; }, - onMutate: async (variables) => { - const postOpts = post({ hash: ref(variables.post.value.hash) }); - - await Promise.all([ - queryClient.cancelQueries(postOpts), - ]); - - const previousPost = queryClient.getQueryData( - postOpts.queryKey, - ) as Post | undefined; - - return { - previousPost, - }; - }, - onSuccess: (_, variables, context) => { - const postOpts = post({ hash: ref(variables.post.value.hash) }); - // Post with updated likes_burnt - const optimisticPost: Post - = context.previousPost - ? { ...context.previousPost, likes_burnt: addAtomics((context.previousPost.likes_burnt ?? 0).toString(), variables.amountAtomics) } - : { ...variables.post.value, likes_burnt: addAtomics((variables.post.value.likes_burnt ?? 0).toString(), variables.amountAtomics) }; - - queryClient.setQueryData(postOpts.queryKey, optimisticPost); - }, - onError: (_, variables, context) => { + onSuccess: (_, variables) => { const postOpts = post({ hash: ref(variables.post.value.hash) }); - queryClient.setQueryData(postOpts.queryKey, context?.previousPost); + // Update post's likes_burnt + queryClient.setQueryData(postOpts.queryKey, (current) => { + const currentLikesBurnt = current?.likes_burnt ?? variables.post.value.likes_burnt ?? '0'; + const newLikesBurnt = addAtomics(currentLikesBurnt, variables.amountAtomics); + return { ...(current ?? variables.post.value), likes_burnt: newLikesBurnt }; + }); }, onSettled: () => { isToastShown.value = false; diff --git a/packages/frontend-main/src/composables/useReadNotification.ts b/packages/frontend-main/src/composables/useReadNotification.ts index 0d61cef9..55f33b37 100644 --- a/packages/frontend-main/src/composables/useReadNotification.ts +++ b/packages/frontend-main/src/composables/useReadNotification.ts @@ -1,7 +1,7 @@ import type { Notification } from 'api-main/types/notifications'; import { ref } from 'vue'; -import { type InfiniteData, useMutation, useQueryClient } from '@tanstack/vue-query'; +import { useMutation, useQueryClient } from '@tanstack/vue-query'; import { notifications } from './useNotifications'; import { notificationsCount } from './useNotificationsCount'; @@ -21,9 +21,7 @@ export function useReadNotification( const wallet = useWallet(); const txError = ref(); const txSuccess = ref(); - const { - mutateAsync, - } = useMutation({ + const { mutateAsync } = useMutation({ mutationFn: async ({ notification }: FollowUserRequestMutation) => { const resVerifyRaw = await fetch(apiRoot + `/notification-read?hash=${notification.hash}&address=${wallet.address}`, { method: 'POST', @@ -40,45 +38,20 @@ export function useReadNotification( return; } }, - onMutate: async () => { + onSuccess: (_, variables) => { const notificationsOpts = notifications({ userAddress: wallet.address }); const notificationsCountOpts = notificationsCount({ userAddress: wallet.address }); - await Promise.all([ - queryClient.cancelQueries(notificationsOpts), - queryClient.cancelQueries(notificationsCountOpts), - ]); - - const previousNotifications = queryClient.getQueryData( - notificationsOpts.queryKey, - ) as InfiniteData | undefined; - const previousNotificationsCount = queryClient.getQueryData( - notificationsCountOpts.queryKey, - ) as number; - - return { - previousNotifications, - previousNotificationsCount, - }; - }, - onSuccess: (_, variables, context) => { - const notificationsOpts = notifications({ userAddress: wallet.address }); - const notificationsCountOpts = notificationsCount({ userAddress: wallet.address }); + const previousNotifications = queryClient.getQueryData(notificationsOpts.queryKey); + const previousNotificationsCount = queryClient.getQueryData(notificationsCountOpts.queryKey) || 0; const newNotificationsData = infiniteDataWithoutItem({ - previousItems: context.previousNotifications, + previousItems: previousNotifications, predicate: notification => notification.hash === variables.notification.hash, }); queryClient.setQueryData(notificationsOpts.queryKey, newNotificationsData); - queryClient.setQueryData(notificationsCountOpts.queryKey, context?.previousNotificationsCount - 1); - }, - onError: (_, __, context) => { - const notificationsOpts = notifications({ userAddress: wallet.address }); - const notificationsCountOpts = notificationsCount({ userAddress: wallet.address }); - - queryClient.setQueryData(notificationsOpts.queryKey, context?.previousNotifications); - queryClient.setQueryData(notificationsCountOpts.queryKey, context?.previousNotificationsCount); + queryClient.setQueryData(notificationsCountOpts.queryKey, previousNotificationsCount - 1); }, }); diff --git a/packages/frontend-main/src/composables/useTipUser.ts b/packages/frontend-main/src/composables/useTipUser.ts index 1e29b92b..1c4a3406 100644 --- a/packages/frontend-main/src/composables/useTipUser.ts +++ b/packages/frontend-main/src/composables/useTipUser.ts @@ -30,10 +30,6 @@ export function useTipUser() { txSuccess.value = result.tx?.transactionHash; return txSuccess.value; }, - // TODO: To be implemented when integrating with useUserTips - onMutate: async (_) => {}, - onSuccess: () => {}, - onError: () => {}, onSettled: () => { isToastShown.value = false; }, diff --git a/packages/frontend-main/src/composables/useUnfollowUser.ts b/packages/frontend-main/src/composables/useUnfollowUser.ts index 619ed8c4..58664963 100644 --- a/packages/frontend-main/src/composables/useUnfollowUser.ts +++ b/packages/frontend-main/src/composables/useUnfollowUser.ts @@ -25,9 +25,7 @@ export function useUnfollowUser( const isToastShown = ref(false); useTxNotification('Unfollow', txSuccess, txError); - const { - mutateAsync, - } = useMutation({ + const { mutateAsync } = useMutation({ mutationFn: async ({ userAddress, amountAtomics }: UnfollowUserRequestMutation) => { txError.value = undefined; txSuccess.value = undefined; @@ -44,48 +42,25 @@ export function useUnfollowUser( } txSuccess.value = result.tx?.transactionHash; }, - onMutate: async (variables) => { - const isFollowingOpts = isFollowing({ followerAddress: wallet.address, followingAddress: variables.userAddress }); - const followingOpts = following({ userAddress: wallet.address }); - - await Promise.all([ - await queryClient.cancelQueries(isFollowingOpts), - await queryClient.cancelQueries(followingOpts), - ]); - - const previousIsFollowing = queryClient.getQueryData( - isFollowingOpts.queryKey, - ) as boolean | undefined; - const previousFollowing = queryClient.getQueryData( - followingOpts.queryKey, - ) as InfiniteData | undefined; - - return { - previousIsFollowing, - previousFollowing, - }; - }, - onSuccess: (_, variables, context) => { - const isFollowingOpts = isFollowing({ followerAddress: wallet.address, followingAddress: variables.userAddress }); + onSuccess: (_, variables) => { + const isFollowingOpts = isFollowing({ + followerAddress: wallet.address, + followingAddress: variables.userAddress, + }); const followingOpts = following({ userAddress: wallet.address }); const followingPostsOpts = followingPosts({ userAddress: wallet.address }); - const newFollowingData = infiniteDataWithoutItem({ - previousItems: context.previousFollowing, - predicate: followUser => followUser.address === variables.userAddress.value, - }); - - queryClient.setQueryData(isFollowingOpts.queryKey, false); - queryClient.setQueryData(followingOpts.queryKey, newFollowingData); + queryClient.setQueryData(isFollowingOpts.queryKey, () => true); + queryClient.setQueryData>( + followingOpts.queryKey, + current => + infiniteDataWithoutItem({ + previousItems: current ?? { pages: [], pageParams: [] }, + predicate: item => item.address === variables.userAddress.value, + }), + ); queryClient.invalidateQueries(followingPostsOpts); }, - onError: (_, variables, context) => { - const isFollowingOpts = isFollowing({ followerAddress: wallet.address, followingAddress: variables.userAddress }); - const followingOpts = following({ userAddress: wallet.address }); - - queryClient.setQueryData(isFollowingOpts.queryKey, context?.previousIsFollowing); - queryClient.setQueryData(followingOpts.queryKey, context?.previousFollowing); - }, onSettled: () => { isToastShown.value = false; },