All files / src/features/Minibuilders/Conversations/hooks useListThreads.tsx

97.36% Statements 37/38
84% Branches 21/25
100% Functions 7/7
97.22% Lines 35/36

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                                          21x                   12x       12x 12x 3x     12x           21x                 33x 33x       33x       33x 33x 33x 33x 33x 33x 33x     33x   33x 13x 13x             13x 7x         7x 7x 7x     13x         33x     12x           12x       11x 6x 6x   6x               33x    
import {
  QKEY_LIST_ACQUIRER_MERCHANTS,
  QKEY_LIST_ENTERPRISE_STATS,
  QKEY_LIST_ENTERPRISES,
} from "@constants/queryKeys";
import { useAppDispatch, useAppSelector } from "@redux/hooks";
import {
  selectConversationsGeneralSearch,
  selectConversationTopic,
  selectReplies,
  setThreads,
  setTopicName,
} from "@redux/slices/conversations";
import { customInstance } from "@services/api";
import { useEffect, useState } from "react";
import { useQuery, useQueryClient } from "react-query";
import { parseInput } from "../utils/functions";
import { selectUser } from "@redux/slices/auth/auth";
import { useGetCurrentMerchantId } from "@hooks/common";
import { checkPortals } from "@utils/routing";
 
const getMessages = ({
  merchantId,
  topicId,
  queryString,
}: {
  isReply: boolean;
  merchantId: number;
  topicId: number;
  queryString: string;
}) => {
  const computedString = queryString.startsWith("#")
    ? parseInput(queryString, { isUrl: true })
    : queryString;
 
  let queryBuilder = `/merchants/${merchantId}/topics/${topicId}/threads?sort=createdAt`;
  if (queryString) {
    queryBuilder += `&q="${computedString}"`;
  }
 
  return customInstance({
    url: queryBuilder,
    method: "GET",
  });
};
 
export const useListThreads = ({
  merchantId,
  hasUnreadMessages,
  topicId,
}: {
  merchantId: number;
  hasUnreadMessages: boolean;
  topicId: number;
}) => {
  const { isRepliesOpen, threadId } = useAppSelector(selectReplies);
  const { queryString, queryKey } = useAppSelector(
    selectConversationsGeneralSearch,
  );
 
  const [enableReadEndpoint, setEnableReadEnpoint] = useState<boolean>(false);
 
  const {
    queryObject: { id, name, challengeId },
  } = useAppSelector(selectConversationTopic);
  const { id: globalMerchantId } = useAppSelector(selectUser);
  const { merchantId: enterpriseID } = useGetCurrentMerchantId();
  const { isEnterprisePortal } = checkPortals();
  const usedID = isEnterprisePortal ? enterpriseID : globalMerchantId;
  const dispatch = useAppDispatch();
  const queryClient = useQueryClient();
 
  const condition =
    (queryKey === "Threads" || queryKey === "Replies") && queryString;
 
  useEffect(() => {
    Eif (hasUnreadMessages) {
      const customData = isRepliesOpen
        ? {
            threadID: threadId,
          }
        : {
            topicID: topicId,
          };
      const unread = async () =>
        await customInstance({
          url: `/merchants/${usedID}/messages/read`,
          method: "POST",
          data: customData,
        }).then(() => {
          queryClient.invalidateQueries(QKEY_LIST_ACQUIRER_MERCHANTS);
          queryClient.invalidateQueries(QKEY_LIST_ENTERPRISES);
          queryClient.invalidateQueries(QKEY_LIST_ENTERPRISE_STATS);
        });
 
      if (enableReadEndpoint || isRepliesOpen) unread();
    }
  }, [enableReadEndpoint, isRepliesOpen]);
 
  const { data, isLoading, isSuccess, isError, isRefetching, isFetching } =
    useQuery(
      ["get-conversation-messages", id, isRepliesOpen, condition],
      async () => {
        const data = await getMessages({
          isReply: isRepliesOpen,
          topicId: topicId,
          merchantId,
          queryString: queryString,
        });
        return data;
      },
      {
        onSuccess: async (res) => {
          if (!res.data) return;
          dispatch(setThreads(res.data));
          setEnableReadEnpoint(true);
 
          Iif (!name) {
            dispatch(setTopicName(res.data[0].topicName));
          }
        },
        enabled: Boolean(merchantId && topicId) && !isRepliesOpen,
      },
    );
 
  return { data, isLoading, isSuccess, isError, isRefetching, isFetching };
};