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

80.32% Statements 49/61
54.54% Branches 6/11
72.22% Functions 13/18
81.03% Lines 47/58

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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 1915x     5x     5x 5x   5x 13x   13x         1x   1x 1x     1x     1x 3x     1x       1x                                         1x       13x     5x 83x           83x             1x         1x 1x 1x     1x     1x                   1x         1x   1x       1x   1x     1x 1x                                                       83x             5x 7x   7x         1x   1x 1x     1x 1x   1x       1x                   1x       7x     83x  
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 { LiteUser, User } from "proto/api_pb";
import { BlockedUser, GetBlockedUsersRes } from "proto/blocking_pb";
import { useMutation, useQueryClient } from "react-query";
import { service } from "service";
 
const useUnblockUser = () => {
  const queryClient = useQueryClient();
 
  const { mutate: unblockUserMutation, isLoading: isUnblocking } = useMutation<
    Empty,
    Error,
    { username: string },
    { previousBlockedUsers?: BlockedUser.AsObject[] }
  >(({ username }) => service.blocking.unblockUser({ username }), {
    onMutate: async ({ username }) => {
      await queryClient.cancelQueries(blockedUsersKey);
      await queryClient.removeQueries(["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,
    ) => {
      Iif (context?.previousBlockedUsers) {
        queryClient.setQueryData<GetBlockedUsersRes.AsObject>(blockedUsersKey, {
          blockedUsersList: context.previousBlockedUsers,
        });
      }
      Sentry.captureException(error, {
        tags: {
          component: "useUnblockUser",
          action: "unblockUserMutation",
          username: user.username,
        },
      });
    },
    onSuccess: () => {
      queryClient.invalidateQueries(friendIdsKey);
    },
  });
 
  return { unblockUserMutation, isUnblocking };
};
 
const useBlockUser = () => {
  const queryClient = useQueryClient();
 
  const {
    error,
    isLoading,
    mutate: blockUserMutation,
  } = useMutation<
    Empty,
    RpcError,
    LiteUser.AsObject | User.AsObject,
    { previousBlockedUsers?: BlockedUser.AsObject[] }
  >(
    ({ username }) =>
      service.blocking.blockUser({
        username,
      }),
    {
      onMutate: async ({ avatarThumbnailUrl, name, username, userId }) => {
        await queryClient.cancelQueries(blockedUsersKey);
        await queryClient.cancelQueries(friendIdsKey);
        await queryClient.removeQueries(["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 }) => {
        if (userId) {
          queryClient.removeQueries(userKey(userId));
        }
      },
      onError: (
        err,
        user,
        context: { previousBlockedUsers?: BlockedUser.AsObject[] } | undefined,
      ) => {
        Iif (context?.previousBlockedUsers) {
          queryClient.setQueryData<GetBlockedUsersRes.AsObject>(
            blockedUsersKey,
            {
              blockedUsersList: context?.previousBlockedUsers,
            },
          );
        }
 
        Sentry.captureException(error, {
          tags: {
            component: "useBlockUser",
            action: "blockUserMutation",
            username: user.username,
          },
        });
      },
    },
  );
 
  return {
    error,
    isLoading,
    blockUserMutation,
  };
};
 
const useRemoveFriend = () => {
  const queryClient = useQueryClient();
 
  const { mutate: removeFriendMutation, isLoading } = useMutation<
    Empty,
    Error,
    { friendId: number; onError: (error: Error | null) => void },
    { previousFriendIds?: number[]; onError: (error: Error | null) => void }
  >(({ friendId }) => service.api.removeFriend(friendId), {
    onMutate: async ({ friendId, onError }) => {
      onError(null);
      await queryClient.cancelQueries(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);
 
      Iif (context?.previousFriendIds) {
        queryClient.setQueryData(friendIdsKey, context.previousFriendIds);
      }
    },
    onSuccess: () => {
      queryClient.invalidateQueries(friendIdsKey);
    },
  });
 
  return { removeFriendMutation, isLoading };
};
 
export { useBlockUser, useRemoveFriend, useUnblockUser };