All files / src/features/Processing/hooks useTransactionsFilters.tsx

86.76% Statements 59/68
73.07% Branches 19/26
93.33% Functions 14/15
89.06% Lines 57/64

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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243                                                      83x   83x 208x   208x     208x     208x                   208x   208x 16x 16x     208x 16x           16x 16x   208x               208x 40x                   40x 40x 29x 29x       208x 86x           3x 3x 3x 2x 2x                 4x 4x 4x 3x                       3x   1x                     1x 1x 1x                       2x 2x 2x 2x                 2x 2x 2x               2x 2x 2x                       1x 1x 1x               1x 1x       1x                       208x 86x                 208x           83x                    
import { useAppDispatch, useAppSelector } from "@redux/hooks";
import { useEffect, useMemo, useRef, useState } from "react";
import {
  addBlockedFilter,
  addQuarantineFilter,
  addStatusFilter,
  clearFilters,
  selectHasUnseenBlockedTransactions,
  setHasUnseenBlockedTransactions,
  setProcessingTab,
  addBlockedAndQuarantinedFilter,
  addPendingFilter,
  addTypeFilter,
} from "@redux/slices/transactions";
 
import { AlertDot } from "../ProcessingTable.atoms";
import { checkPortals } from "@utils/routing";
import { useProcessingFilters } from "./useProcessingFilters";
import { TRANSFERS_TAB_BASE_TYPES } from "@shared/GiveFilter/constants";
 
type Props = {
  isLoading?: boolean;
};
 
// Query param value the daily transfer summary email uses to deep-link
// straight to the Transfers tab — keep in sync with the backend email URL
// (app/transaction/txnsmtp/daily_notification_transaction_transfer_email.go).
const TRANSFERS_TAB_QUERY_VALUE = "transfers";
 
export const useTransactionsFilters = ({ isLoading }: Props) => {
  const { isMerchantPortal, isAcquirerOrProviderManageMoney } = checkPortals();
 
  const dispatch = useAppDispatch();
 
  const shouldStartOnTransfersTab =
    new URLSearchParams(window.location.search).get("tab") ===
      TRANSFERS_TAB_QUERY_VALUE && !isAcquirerOrProviderManageMoney;
 
  const [selectedFilter, setSelectedFilter] = useState<number>(0);
  // The onClickItem closures live inside the tableFilters useMemo, which only
  // rebuilds when isLoading toggles — NOT when selectedFilter changes. When a
  // tab switch is served from the react-query cache (staleTime), no fetch
  // happens, isLoading never toggles, and the closures keep a stale
  // selectedFilter. Comparing against that stale value made the early-return
  // below fire on a real tab change, skipping clearFilters/setSelectedFilter
  // while onClickItem still piled the new tab's filters on top of the old
  // ones (QA: tabs with few/zero rows kept showing "All" data). A ref updated
  // synchronously is always current regardless of when the closure was built.
  const selectedFilterRef = useRef<number>(0);
 
  const selectTab = (index: number) => {
    selectedFilterRef.current = index;
    setSelectedFilter(index);
  };
 
  const handleClick = (index: number) => {
    Iif (selectedFilterRef.current === index) return;
    // clearFilters must be dispatched synchronously here so it runs BEFORE the
    // tab's own setProcessingTab/add*Filter dispatches in onClickItem. It used
    // to live inside the setSelectedFilter updater, which React defers to the
    // render phase — so it ran AFTER the new tab's filters were applied and
    // wiped them, leaving the table stuck on unfiltered ("All") data.
    dispatch(clearFilters());
    selectTab(index);
  };
  const hasUnseenBlockedTransactions = useAppSelector(
    selectHasUnseenBlockedTransactions,
  );
 
  // Mount-only initializer: seed the tab and default filters once for this
  // page lifecycle. Re-running on shouldStartOnTransfersTab /
  // isAcquirerOrProviderManageMoney would wipe filters or tab selections the
  // user has changed mid-session, so the empty dep array is intentional.
  useEffect(() => {
    Iif (shouldStartOnTransfersTab) {
      // Mirror the Transfers tab onClick: wipe the default !declined status
      // filter set in initialState before adding the transfer base-type filter,
      // otherwise the table keeps showing non-declined transactions of all
      // types instead of transfers only.
      dispatch(clearFilters());
      dispatch(setProcessingTab(tabNames.transfers));
      dispatch(addTypeFilter(TRANSFERS_TAB_BASE_TYPES));
      return;
    }
    dispatch(setProcessingTab(tabNames.all));
    if (isAcquirerOrProviderManageMoney) return;
    dispatch(addStatusFilter("!declined"));
    dispatch(addBlockedAndQuarantinedFilter());
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);
 
  const tableFilters = useMemo(
    () => [
      {
        label: "All",
        value: tabNames.all,
        disabled: isLoading,
        onClickItem: (index: number) => {
          handleClick(index);
          dispatch(setProcessingTab(tabNames.all));
          if (!isAcquirerOrProviderManageMoney) {
            dispatch(addStatusFilter("!declined"));
            dispatch(addBlockedAndQuarantinedFilter());
          }
        },
      },
      {
        label: "Pending",
        value: tabNames.pending,
        disabled: isLoading,
        onClickItem: (index: number) => {
          handleClick(index);
          dispatch(setProcessingTab(tabNames.pending));
          if (!isAcquirerOrProviderManageMoney) {
            dispatch(
              addStatusFilter([
                "processing_issue",
                "captured",
                "created",
                "authorized",
                "pending",
                "enqueued",
                "approved",
                "processing",
              ]),
            );
            dispatch(addBlockedAndQuarantinedFilter());
          } else {
            dispatch(addPendingFilter(true));
          }
        },
      },
      ...(isAcquirerOrProviderManageMoney
        ? [
            {
              label: "Posted",
              value: tabNames.posted,
              disabled: isLoading,
              onClickItem: (index: number) => {
                handleClick(index);
                dispatch(setProcessingTab(tabNames.posted));
                dispatch(addPendingFilter(false));
              },
            },
          ]
        : []),
      ...(!isMerchantPortal && !isAcquirerOrProviderManageMoney
        ? [
            {
              label: "Suspected",
              value: tabNames.suspected,
              disabled: isLoading,
              onClickItem: (index: number) => {
                handleClick(index);
                dispatch(setProcessingTab(tabNames.suspected));
                dispatch(addBlockedFilter());
                dispatch(setHasUnseenBlockedTransactions(false));
              },
              endItem: hasUnseenBlockedTransactions ? <AlertDot /> : null,
            },
            {
              label: "Quarantined",
              value: tabNames.quarantined,
              disabled: isLoading,
              onClickItem: (index: number) => {
                handleClick(index);
                dispatch(setProcessingTab(tabNames.quarantined));
                dispatch(addQuarantineFilter());
              },
            },
            {
              label: "Declined",
              value: tabNames.declined,
              disabled: isLoading,
              onClickItem: (index: number) => {
                handleClick(index);
                dispatch(setProcessingTab(tabNames.declined));
                dispatch(addStatusFilter("declined"));
              },
            },
          ]
        : []),
      ...(!isAcquirerOrProviderManageMoney
        ? [
            {
              label: "Settled",
              value: tabNames.settled,
              disabled: isLoading,
              onClickItem: (index: number) => {
                handleClick(index);
                dispatch(setProcessingTab(tabNames.settled));
                dispatch(addStatusFilter("settled"));
              },
            },
            {
              label: "Transfers",
              value: tabNames.transfers,
              disabled: isLoading,
              onClickItem: (index: number) => {
                handleClick(index);
                dispatch(setProcessingTab(tabNames.transfers));
                // Include transfer_return so Returned transfers surface in the
                // tab and a "Returned" (type:"transfer_return") status filter is
                // not AND-ed away by a transfer-only base filter.
                dispatch(addTypeFilter(TRANSFERS_TAB_BASE_TYPES));
              },
            },
          ]
        : []),
    ],
    [hasUnseenBlockedTransactions, isMerchantPortal, isLoading],
  );
 
  // Highlight the Transfers tab once tableFilters is built — the tab's index
  // depends on the portal/role-dependent filter list, so it can't be set in
  // useState's initial value.
  useEffect(() => {
    Eif (!shouldStartOnTransfersTab) return;
    const transfersIndex = tableFilters.findIndex(
      (f) => f.value === tabNames.transfers,
    );
    // Keep the ref in sync too, otherwise handleClick would wrongly
    // early-return on the first click of the "All" tab (index 0).
    if (transfersIndex >= 0) selectTab(transfersIndex);
  }, [shouldStartOnTransfersTab, tableFilters]);
 
  return {
    tableFilters,
    selectedFilter,
  };
};
 
export const tabNames = {
  all: "-all",
  pending: "-pending",
  posted: "-posted",
  suspected: "-fraud",
  quarantined: "-quarantined",
  declined: "-declined",
  settled: "-settled",
  transfers: "-transfers",
};