All files / app/features/connections/friends/hooks index.ts

80.32% Statements 49/61
43.75% Branches 7/16
72.22% Functions 13/18
82.14% Lines 46/56

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 1736x 6x     6x   6x       6x 13x   13x           1x   1x 1x     1x   3x   1x       1x                                 1x       13x     6x 96x           96x             1x       1x 1x 1x     1x   1x                   1x       1x   1x   1x   1x     1x 1x                                       96x             6x 7x   7x           1x   1x 1x   1x 1x   1x       1x                   1x       7x     96x  
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { blockedUsersKey, friendIdsKey, userKey } from "features/queryKeys";
import { Empty } from "google-protobuf/google/protobuf/empty_pb";
import { RpcError } from "grpc-web";
import Sentry from "platform/sentry";
import { BlockedUser, GetBlockedUsersRes } from "proto/blocking_pb";
import { service } from "service";
 
import { LiteUser, User } from "../../../../proto/api_pb";
 
const useUnblockUser = () => {
  const queryClient = useQueryClient();
 
  const { mutate: unblockUserMutation, isPending: isUnblocking } = useMutation<
    Empty,
    Error,
    { username: string },
    { previousBlockedUsers?: BlockedUser.AsObject[] }
  >({
    mutationFn: ({ username }) => service.blocking.unblockUser({ username }),
    onMutate: async ({ username }) => {
      await queryClient.cancelQueries({ queryKey: [blockedUsersKey] });
      await queryClient.removeQueries({ queryKey: ["liteUsers"] });
 
      const previousBlockedUsers =
        queryClient.getQueryData<GetBlockedUsersRes.AsObject>([blockedUsersKey])?.blockedUsersList || [];
 
      const updatedBlockedUsers = previousBlockedUsers.filter((user) => user.username !== username);
 
      queryClient.setQueryData<GetBlockedUsersRes.AsObject>([blockedUsersKey], {
        blockedUsersList: updatedBlockedUsers,
      });
 
      return { previousBlockedUsers };
    },
    onError: (error, user, context: { previousBlockedUsers?: BlockedUser.AsObject[] } | undefined) => {
      if (context?.previousBlockedUsers) {
        queryClient.setQueryData<GetBlockedUsersRes.AsObject>([blockedUsersKey], {
          blockedUsersList: context.previousBlockedUsers,
        });
      }
      Sentry.captureException(error, {
        tags: {
          component: "useUnblockUser",
          action: "unblockUserMutation",
          username: user.username,
        },
      });
    },
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: [friendIdsKey] });
    },
  });
 
  return { unblockUserMutation, isUnblocking };
};
 
const useBlockUser = () => {
  const queryClient = useQueryClient();
 
  const {
    error,
    isPending,
    mutate: blockUserMutation,
  } = useMutation<
    Empty,
    RpcError,
    LiteUser.AsObject | User.AsObject,
    { previousBlockedUsers?: BlockedUser.AsObject[] }
  >({
    mutationFn: ({ username }) =>
      service.blocking.blockUser({
        username,
      }),
    onMutate: async ({ avatarThumbnailUrl, name, username, userId }) => {
      await queryClient.cancelQueries({ queryKey: [blockedUsersKey] });
      await queryClient.cancelQueries({ queryKey: [friendIdsKey] });
      await queryClient.removeQueries({ queryKey: ["liteUsers"] });
 
      const currentBlockedUsers =
        queryClient.getQueryData<GetBlockedUsersRes.AsObject>([blockedUsersKey])?.blockedUsersList || [];
 
      const updatedBlockedUsers = [
        {
          userId,
          username,
          name,
          avatarThumbnailUrl,
        },
        ...currentBlockedUsers,
      ];
 
      queryClient.setQueryData<GetBlockedUsersRes.AsObject>([blockedUsersKey], {
        blockedUsersList: updatedBlockedUsers,
      });
 
      const previousFriendIds = queryClient.getQueryData<number[]>([friendIdsKey]) || [];
 
      const updatedFriendIds = previousFriendIds.filter((id) => id !== userId);
 
      queryClient.setQueryData([friendIdsKey], updatedFriendIds);
 
      return { previousBlockedUsers: currentBlockedUsers };
    },
    onSuccess: (_res, { userId }) => {
      Eif (userId) {
        queryClient.removeQueries({ queryKey: userKey(userId) });
      }
    },
    onError: (err, user, context: { previousBlockedUsers?: BlockedUser.AsObject[] } | undefined) => {
      if (context?.previousBlockedUsers) {
        queryClient.setQueryData<GetBlockedUsersRes.AsObject>([blockedUsersKey], {
          blockedUsersList: context?.previousBlockedUsers,
        });
      }
 
      Sentry.captureException(error, {
        tags: {
          component: "useBlockUser",
          action: "blockUserMutation",
          username: user.username,
        },
      });
    },
  });
 
  return {
    error,
    isPending,
    blockUserMutation,
  };
};
 
const useRemoveFriend = () => {
  const queryClient = useQueryClient();
 
  const { mutate: removeFriendMutation, isPending } = useMutation<
    Empty,
    Error,
    { friendId: number; onError: (error: Error | null) => void },
    { previousFriendIds?: number[]; onError: (error: Error | null) => void }
  >({
    mutationFn: ({ friendId }) => service.api.removeFriend(friendId),
    onMutate: async ({ friendId, onError }) => {
      onError(null);
      await queryClient.cancelQueries({ queryKey: [friendIdsKey] });
 
      const previousFriendIds = queryClient.getQueryData<number[]>([friendIdsKey]);
      const newFriendIds = previousFriendIds?.filter((id) => id !== friendId);
 
      Iif (newFriendIds) {
        queryClient.setQueryData<number[]>([friendIdsKey], newFriendIds);
      }
 
      return { previousFriendIds, onError };
    },
    onError: (err, _, context) => {
      context?.onError(err);
 
      if (context?.previousFriendIds) {
        queryClient.setQueryData([friendIdsKey], context.previousFriendIds);
      }
    },
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: [friendIdsKey] });
    },
  });
 
  return { removeFriendMutation, isPending };
};
 
export { useBlockUser, useRemoveFriend, useUnblockUser };