All files / src/features/GiveTransfers/hooks useGetTransfersTableColumns.tsx

89.65% Statements 26/29
70% Branches 14/20
90% Functions 9/10
89.65% Lines 26/29

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                                                              1x 151x 1x 25x 25x 25x 25x 25x   25x 151x         25x                                                                           25x 151x     25x 151x 151x                                   25x 151x 151x                                               25x 151x 151x                   25x 7x                                                                                                                                                   151x             25x    
import { useMemo } from "react";
import GiveDateTooltip from "@shared/Tooltip/GiveDateTooltip";
import { Box, Stack } from "@mui/material";
import {
  ArrowUpIcon,
  ArrowDownIcon,
  ArrowRightIcon,
} from "@phosphor-icons/react";
import { useAppSelector } from "@redux/hooks";
import { selectQueryFilters } from "@redux/slices/transactions";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { useAppTheme } from "@theme/v2/Provider";
import { useFormatDateInTimezone } from "@utils/date.helpers";
import { checkPortals } from "@utils/routing";
import { parseAmount } from "@utils/index";
import GiveText from "@shared/Text/GiveText";
import GiveTruncateText from "@shared/Text/GiveTruncateText";
import { moveFundsArray } from "@components/ManageMoney/TransactionTable/transactions.types";
import {
  getFormattedDescription,
  parseTransfer,
} from "@components/ManageMoney/TransactionTable/transactions.helpers";
import { CURRENCY } from "@constants/constants";
import { TransferTableRow } from "@components/Tranfers/utils";
import { findBankByRoutingNumber } from "@utils/bank_list";
import {
  StatusColumn,
  StatusTextColumn,
} from "@features/MerchantPortal/ManageMoney/ManageMoneyTable.atoms";
import { TransactionTableRow } from "@components/ManageMoney/TransactionTable/transactions.types";
 
const isReserveFund = (row: TransferTableRow) =>
  moveFundsArray.includes(row?.transactionTypeName);
export const useGetTransfersTableColumns = () => {
  const theme = useAppTheme();
  const { isNarrowView: showMobileView } = useCustomThemeV2();
  const { formatInTimezone } = useFormatDateInTimezone();
  const { isMerchantPortal, isEnterprisePortal } = checkPortals();
  const queryFilters = useAppSelector(selectQueryFilters);
 
  const formatDate = (date?: number) =>
    date
      ? formatInTimezone(date, "MMM dd, yyyy HH:mm")?.toString()
      : "Unknown Date";
 
  // 🔹 Column renderers
  const renderMobileDateColumn = (row: TransferTableRow) => {
    const isReserve = isReserveFund(row);
    const parsedRow = parseTransfer(row);
 
    return (
      <Stack spacing={0.5} alignItems="flex-start">
        <GiveDateTooltip>
          <GiveText variant="bodyS">{formatDate(row?.createdAt)}</GiveText>
        </GiveDateTooltip>
 
        <Stack
          spacing={1}
          direction="row"
          alignItems="center"
          sx={{ marginTop: "8px!important" }}
        >
          <GiveTruncateText variant="bodyXS" whiteSpace="nowrap" lineClamp={1}>
            {row?.merchAccName}
          </GiveTruncateText>
 
          {!isReserve && (
            <>
              <ArrowRightIcon
                size="14px"
                color={theme.palette.icon?.["icon-secondary"]}
              />
              <GiveTruncateText variant="bodyXS" lineClamp={1}>
                {row?.recipientAccountName}
              </GiveTruncateText>
            </>
          )}
        </Stack>
 
        <StatusTextColumn row={parsedRow as any} sx={{ marginTop: "6px!important" }} />
      </Stack>
    );
  };
 
  const renderDesktopDateColumn = (row: TransferTableRow) => (
    <GiveText variant="bodyS">{formatDate(row?.createdAt)}</GiveText>
  );
 
  const renderSenderColumn = (row: TransferTableRow) => {
    const showSenderParent = row?.merchAccType !== "enterprise";
    return (
      <Stack spacing={0.5} alignItems="flex-start">
        <GiveTruncateText variant="bodyS" lineClamp={1}>
          {row?.merchAccName}
        </GiveTruncateText>
        {showSenderParent && !isEnterprisePortal && (
          <GiveTruncateText
            variant={row?.merchAccName ? "bodyXS" : "bodyS"}
            color={"secondary"}
            lineClamp={1}
          >
            {row?.parentAccName}
          </GiveTruncateText>
        )}
      </Stack>
    );
  };
 
  const renderRecipientColumn = (row: TransferTableRow) => {
    const isReserve = isReserveFund(row);
    return (
      <Stack spacing={0.5}>
        <GiveText variant="bodyS">
          {isReserve
            ? getFormattedDescription(
                "",
                row?.transactionTypeName,
                false,
                true,
                row?.description,
              )
            : row?.recipientAccountBankName ||
              findBankByRoutingNumber(row?.recipientAccountRoutingNumber) ||
              "Unknown Bank"}
        </GiveText>
        {!isReserve && (
          <GiveText variant="bodyXS" color="secondary">
            {row?.recipientAccountName} •••• {row?.recipientAccountNumberLast4}
          </GiveText>
        )}
      </Stack>
    );
  };
 
  const renderAmountColumn = (row: TransferTableRow) => {
    const amount = parseAmount(row?.cost / 100);
    return (
      <Box display="flex" width="100%" justifyContent="flex-end">
        <GiveText variant="bodyS" whiteSpace="nowrap">
          {amount} {showMobileView && ` ${CURRENCY}`}
        </GiveText>
      </Box>
    );
  };
 
  // 🧠 Columns definition
  const columns = useMemo(
    () => [
      {
        key: "createdAt",
        title: "Date",
        sortable: false,
        hideColumn: !showMobileView,
        renderColumn: renderMobileDateColumn,
      },
      {
        key: "createdAt",
        title: "Date",
        columnWidth: "20%",
        sortable: true,
        hideColumn: showMobileView,
        renderColumn: renderDesktopDateColumn,
        isComplexSort: true,
        complexSortOptions: [
          {
            key: "createdAt",
            label: "Created - Oldest",
            order: "asc" as const,
            IconLeft: ArrowUpIcon,
          },
          {
            key: "createdAt",
            label: "Created - Newest",
            order: "desc" as const,
            IconLeft: ArrowDownIcon,
          },
          {
            key: "updatedAt",
            label: "Updated - Oldest",
            order: "asc" as const,
            IconLeft: ArrowUpIcon,
          },
          {
            key: "updatedAt",
            label: "Updated - Newest",
            order: "desc" as const,
            IconLeft: ArrowDownIcon,
          },
        ],
      },
      {
        key: "sender",
        title: "Sender",
        columnWidth: "25%",
        hideColumn: showMobileView,
        renderColumn: renderSenderColumn,
      },
      {
        key: "recipient",
        title: "Recipient",
        columnWidth: "25%",
        hideColumn: showMobileView,
        renderColumn: renderRecipientColumn,
      },
      {
        key: "cost",
        title: "Amount (USD)",
        headerPosition: "right" as const,
        columnWidth: showMobileView ? "auto" : "13.5%",
        sortable: true,
        renderColumn: renderAmountColumn,
      },
      {
        key: "sortOrderPriority",
        title: "Status",
        columnWidth: "14.4%",
        sortable: true,
        headerPosition: "left" as const,
        type: "status",
        columnType: "status-round" as const,
        renderColumn: (row: TransferTableRow) =>
          StatusColumn(parseTransfer(row) as unknown as TransactionTableRow),
        hideColumn: showMobileView,
      },
    ],
    [showMobileView, isMerchantPortal, queryFilters.blocked],
  );
 
  return { columns };
};