All files / app/features/mod hooks.ts

0% Statements 0/27
0% Branches 0/14
0% Functions 0/7
0% Lines 0/25

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                                                                                                                                     
import {
  modUserDetailsKey,
  modUserKey,
  newUsersListKey,
} from "features/queryKeys";
import { userStaleTime } from "features/userQueries/constants";
import { RpcError } from "grpc-web";
import { ListUserIdsRes, UserDetails } from "proto/admin_pb";
import { User } from "proto/api_pb";
import { useInfiniteQuery, useQuery } from "react-query";
import { service } from "service";
 
export const useNewUsers = () => {
  const query = useInfiniteQuery<ListUserIdsRes.AsObject, RpcError>(
    newUsersListKey,
    ({ pageParam }) =>
      service.admin.listUserIds({
        startTime: new Date(1970, 0, 0, 0, 0, 1),
        endTime: new Date(2050, 0, 0),
        pageSize: 50,
        pageToken: pageParam,
      }),
    {
      getNextPageParam: (lastPage) => lastPage.nextPageToken || undefined,
      refetchInterval: 60_000,
    },
  );
  const userIds = query.data?.pages.flatMap((page) => page.userIdsList);
  return { ...query, userIds };
};
 
export default function useUserWithDetails(user: string) {
  const query = useQuery<User.AsObject, RpcError>({
    queryFn: () => service.admin.getUser(user),
    queryKey: modUserKey(user),
    staleTime: userStaleTime,
  });
 
  const detailsQuery = useQuery<UserDetails.AsObject, RpcError>({
    queryFn: () => service.admin.getUserDetails(user),
    queryKey: modUserDetailsKey(user),
    staleTime: userStaleTime,
  });
 
  const errors = [];
  Iif (query.error?.message) {
    errors.push(query.error?.message || "");
  }
  Iif (detailsQuery.error?.message) {
    errors.push(detailsQuery.error?.message || "");
  }
 
  const error = errors.join("\n");
  const isLoading = query.isLoading || detailsQuery.isLoading;
  const isFetching = query.isFetching || detailsQuery.isFetching;
  const isError = query.isError || detailsQuery.isError;
 
  return {
    user: query.data,
    userDetails: detailsQuery.data,
    error,
    isError,
    isFetching,
    isLoading,
  };
}