All files / app/features/connections/friends useFriendList.ts

100% Statements 15/15
100% Branches 6/6
100% Functions 7/7
100% Lines 14/14

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 404x   4x 4x             234x   234x   384x   43x           234x     384x   618x 384x   234x       372x       4x  
import { userKey } from "features/queryKeys";
import { User } from "proto/api_pb";
import { useQueries, useQuery, UseQueryResult } from "react-query";
import { service } from "service";
 
function useFriendList() {
  const {
    data: friendIds,
    error,
    isLoading,
  } = useQuery<number[], Error>("friendIds", service.api.listFriends);
 
  const friendQueries = useQueries<User.AsObject, Error>(
    (friendIds ?? []).map((friendId) => {
      return {
        enabled: !!friendIds,
        queryFn: () => service.user.getUser(friendId.toString()),
        queryKey: userKey(friendId),
      };
    })
  );
 
  const errors = [
    error?.message,
    ...friendQueries.map(
      (query: UseQueryResult<unknown, Error>) => query.error?.message
    ),
  ].filter((e): e is string => typeof e === "string");
  const data = friendIds && friendQueries.map((query) => query.data);
 
  return {
    data,
    errors,
    isError: !!errors.length,
    isLoading: isLoading || friendQueries.some((query) => query.isLoading),
  };
}
 
export default useFriendList;