All files / src/features/Merchants/MerchantSidePanel/components/RiskTransactions RiskTransactionsTab.tsx

84.21% Statements 32/38
63.33% Branches 19/30
78.57% Functions 11/14
86.48% Lines 32/37

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                                            13x 13x 13x 13x 16x   13x 13x     13x   13x         13x   13x 3x 3x         13x                                             13x 6x 6x     6x         13x 6x             13x 3x     13x           13x 3x     13x 6x   3x           13x 6x                 7x                                                                               14x                                                  
import { CardContainer } from "@features/Underwriting/UnderwritingCard/UnderwritingCard.style";
import { useAppTheme } from "@theme/v2/Provider";
import HeaderActions from "./HeaderActions";
import TransactionItem from "./TransactionItem";
import { useGetInfiniteTransactionsV2 } from "@components/VirtualList/hooks/queries";
import { useMerchantSidePanelContext } from "../../Provider/MerchantSidePanelProvider";
import { useAppDispatch, useAppSelector } from "@redux/hooks";
import {
  resetSearchQueryByKey,
  selectQueryString,
  setSearchQueryByKey,
} from "@redux/slices/search";
import { QKEY_RISK_MONITOR_TRANSACTIONS } from "@constants/queryKeys";
import { useEffect, useMemo, useRef, useState } from "react";
import { SortValues, TransactionType } from "./types";
import { Skeleton, Stack } from "@mui/material";
import GiveEmptyStateWrapper from "@shared/EmptyState/GiveEmptyStateWrapper";
import { useFormattedFilters } from "@shared/GiveFilter/hooks/useFormattedFilters";
import { useInView } from "react-intersection-observer";
import { DoubledSkeleton } from "@components/animation/Skeleton";
 
export default function RiskTransactionsTab() {
  const { id, handlers } = useMerchantSidePanelContext();
  const dispatch = useAppDispatch();
  const { palette } = useAppTheme();
  const searchQuery = useAppSelector((state) =>
    selectQueryString(state, QKEY_RISK_MONITOR_TRANSACTIONS),
  );
  const page = useRef(1);
  const { formattedFilterString } = useFormattedFilters({
    queryKey: QKEY_RISK_MONITOR_TRANSACTIONS,
  });
  const [sort, setSort] = useState<SortValues>(SortValues.New);
 
  const { ref: targetRef, inView } = useInView({
    threshold: 0.1,
    triggerOnce: false,
  });
 
  const queryString = formattedFilterString;
  // Reset search query when component unmounts (section closes)
  useEffect(() => {
    return () => {
      dispatch(resetSearchQueryByKey(QKEY_RISK_MONITOR_TRANSACTIONS));
    };
  }, [dispatch]);
 
  const { data, fetchNextPage, hasNextPage, isFetchingNextPage, isLoading } =
    useGetInfiniteTransactionsV2(
      {
        queryString: "",
        rowsPerPage: 20,
        path: `transactions-minimal`,
        sorting: sort || SortValues.New,
        searchQuery,
        merchantId: id,
        filter: queryString || "",
        paginationMethod: "offset",
      },
      {
        queryKey: [
          QKEY_RISK_MONITOR_TRANSACTIONS,
          id,
          queryString,
          searchQuery,
          sort,
        ],
        enabled: Boolean(id),
      },
    );
 
  useEffect(() => {
    const value = data?.pages ? data.pages[0].total : 0;
    Iif (queryString || searchQuery) {
      handlers.setTransactionCounts((prev) => ({ ...prev, filtered: value }));
    } else {
      handlers.setTransactionCounts({ filtered: 0, total: value });
    }
  }, [data, queryString, searchQuery]);
 
  // Fetch next page when target element is in view
  useEffect(() => {
    Iif (inView && hasNextPage && !isFetchingNextPage) {
      fetchNextPage({
        pageParam: ++page.current,
      });
    }
  }, [inView, hasNextPage, isFetchingNextPage, fetchNextPage]);
 
  const resetPagination = () => {
    page.current = 1;
  };
 
  const handleOnSort = (newSort: SortValues) => {
    setSort(newSort);
    resetPagination();
  };
 
  // Reset pagination when search or query changes
  useEffect(() => {
    resetPagination();
  }, [searchQuery, queryString]);
 
  const uiData: TransactionType[] = useMemo(() => {
    return (
      data?.pages
        .map((row: { data: TransactionType[] }) => row.data)
        .flat()
        .filter(Boolean) ?? []
    );
  }, [data]);
 
  if (isLoading) {
    return (
      <Skeleton
        height="100%"
        variant="rectangular"
        sx={{ borderRadius: "16px", flex: 1 }}
      />
    );
  }
 
  return (
    <CardContainer
      backgroundColor={palette.surface?.primary}
      sx={{
        height: "fit-content",
        minHeight: "auto",
        padding: "16px 0px",
        marginBottom: "20px",
        paddingBottom: uiData.length < 1 ? "80px" : undefined,
      }}
    >
      {!uiData || uiData.length < 1 ? (
        <>
          <HeaderActions sort={sort} setSort={handleOnSort} />
          <GiveEmptyStateWrapper
            isEmpty
            section={searchQuery ? "empty-search" : "manage-money"}
            sectionName="risk-monitor"
            searchValue={searchQuery}
            iconContainerSx={{ width: "56px", height: "56px" }}
            giveEmptyStateSx={{
              minHeight: "calc(100dvh - 500px - 14px)",
            }}
            action={{
              handleAction: () => {
                dispatch(
                  setSearchQueryByKey({
                    queryKey: QKEY_RISK_MONITOR_TRANSACTIONS,
                    params: { value: "" },
                  }),
                );
              },
            }}
          />
        </>
      ) : (
        <>
          <HeaderActions sort={sort} setSort={handleOnSort} />
          <Stack spacing={1} minHeight="calc(100dvh - 450px)">
            {uiData.map((transaction, index) => (
              <TransactionItem
                key={index}
                transaction={transaction}
                dataTestId={`risk-transaction-item-${index}`}
              />
            ))}
          </Stack>
          {isFetchingNextPage && !searchQuery && (
            <DoubledSkeleton frequency={4} sx={{ height: "40px" }} />
          )}
          <div
            ref={targetRef}
            style={{
              width: 1,
              height: 22,
              paddingBottom: 5,
              visibility: "hidden",
              marginTop: -40, //we need to aply this for the div bc of the container height to be correct
            }}
          />
        </>
      )}
    </CardContainer>
  );
}