All files / src/components/Disputes/DisputeTable/hooks useDisputesTableColumns.tsx

100% Statements 22/22
65% Branches 26/40
100% Functions 6/6
100% Lines 22/22

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                                      1x   1x 12x 12x               12x 12x   12x             21x                                                                                                                     21x 21x 21x 21x 21x 21x       21x     21x   21x                                                                               21x                         21x   21x                                       21x                           12x        
import { TableColumnType } from "@common/Table";
import GiveDateTooltip from "@shared/Tooltip/GiveDateTooltip";
import { TableOrder } from "componentsV2/Table/Table.types";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { cardIconsMap } from "features/MerchantPortal/ManageMoney/ManageMoneyTable.atoms";
import { ArrowDownIcon, ArrowUpIcon } from "@phosphor-icons/react";
import { useFormatDateInTimezone } from "@utils/date.helpers";
import { DisputesRowType } from "@components/Disputes/RespondToDispute/types";
import GiveText from "@shared/Text/GiveText";
import { Box, Stack } from "@mui/material";
import { useDisputeTableNewTable } from "@components/Disputes/DisputesList/hooks/useDisputeTableNewTable";
import GiveTruncateText from "@shared/Text/GiveTruncateText";
import { capitalizeCardType, parseAmount } from "@utils/index";
import { getCaseTypeInfo } from "@components/Disputes/utils";
import DisputeStatus from "../components/DisputeStatus";
import { DotComponent } from "@shared/atoms";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
import {  getDisputeReasonCode } from "../utils";
 
const columnsNumber = 6;
 
export const useDisputesTableColumns = () => {
  const { isWideView } = useCustomThemeV2();
  const { isMerchantProcessorEnabled } = useGetFeatureFlagValues();
  const {
    tabs: {
      isClosedTabSelected,
      isUnderReviewTabSelected,
      isOpenTabSelected,
      isAllTabSelected,
    },
  } = useDisputeTableNewTable();
  const { formatInTimezone } = useFormatDateInTimezone();
 
  const columns = [
    {
      key: "createdAt",
      columnWidth: 1.2 / columnsNumber,
      sortable: true,
      title: "Date",
      renderColumn: (row: DisputesRowType) => {
        return (
          <Stack gap="4px">
            <GiveDateTooltip>
              <GiveText variant="bodyS">
                {formatInTimezone(
                  row?.createdAt,
                  "MMM d, yyyy, HH:mm",
                )?.toString()}
              </GiveText>
            </GiveDateTooltip>
            <GiveTruncateText lineClamp={2} variant="bodyXS" color="secondary">
              {row?.merchant?.name}
            </GiveTruncateText>
          </Stack>
        );
      },
      isComplexSort: true,
      sx: {
        paddingLeft: "26px",
      },
      complexSortOptions: [
        {
          key: "createdAt",
          label: "Created - Oldest",
          order: "asc" as TableOrder,
          IconLeft: ArrowUpIcon,
        },
        {
          key: "createdAt",
          label: "Created - Newest",
          order: "desc" as TableOrder,
          IconLeft: ArrowDownIcon,
        },
        {
          key: "updatedAt",
          label: "Updated - Oldest",
          order: "asc" as TableOrder,
          IconLeft: ArrowUpIcon,
        },
        {
          key: "updatedAt",
          label: "Updated - Newest",
          order: "desc" as TableOrder,
          IconLeft: ArrowDownIcon,
        },
      ],
    },
    {
      key: "cardholder.cardBrand",
      columnWidth: (isClosedTabSelected ? 2.8 : 1.6) / columnsNumber,
      sortable: true,
      hideColumn: !isWideView,
      title: "Card Type",
      renderColumn: (row: DisputesRowType) => {
        const {
          cardholder,
          mspReasonCode,
          reason,
          preChargebackRefundTransactionID,
        } = row || {};
        const { nameOnCard, cardBrand, cardNumberLast4 } = cardholder || {};
        const hasData = nameOnCard || cardBrand || cardNumberLast4;
        const label = (reason || mspReasonCode || "").replace(/\./g, "");
        const {code: disputeCode, reason: disputeReason} = getDisputeReasonCode(reason)
        const description = [disputeCode, disputeReason]
          .filter(Boolean)
          .join(" - ");
 
        const isVisaRDRRefund = Boolean(preChargebackRefundTransactionID);
        
        const useDescription =
          isVisaRDRRefund ? description : label;
 
        return (
          <Stack gap="4px">
            {hasData && (
              <Stack spacing={1} direction="row" alignItems="center">
                <Box height="18px">
                  {cardIconsMap()[cardBrand as keyof typeof cardIconsMap]}
                </Box>
                <GiveText variant="bodyS">{cardNumberLast4}</GiveText>
                <DotComponent size={3} />
                <GiveTruncateText variant="bodyS" lineClamp={1} maxWidth="auto">
                  {capitalizeCardType(nameOnCard)}
                </GiveTruncateText>
              </Stack>
            )}
            {useDescription && (
              <GiveTruncateText
                variant="bodyXS"
                color="secondary"
                lineClamp={1}
                sx={{
                  wordBreak: "break-all",
                }}
              >
                {useDescription}
              </GiveTruncateText>
            )}
          </Stack>
        );
      },
    },
    {
      key: "disputedAmount",
      columnWidth:
        (isOpenTabSelected || isUnderReviewTabSelected ? 1 : 0.7) /
        columnsNumber,
      sortable: false,
      hideColumn: !isWideView,
      title: "Amount (USD)",
      headerPosition: "right" as TableColumnType["headerPosition"],
      renderColumn: (row: DisputesRowType) => (
        <GiveText variant="bodyS" textAlign="right" width="100%">
          {parseAmount(row?.disputedAmount ? row.disputedAmount / 100 : 0)}
        </GiveText>
      ),
    },
    {
      key: "mspCaseTypeName",
      columnWidth:
        (isOpenTabSelected || isUnderReviewTabSelected ? 0.8 : 1) /
        columnsNumber,
      sortable: false,
      title: isMerchantProcessorEnabled ? "Type" : "Case Type",
      renderColumn: (row: DisputesRowType) => {
        const { label } = getCaseTypeInfo(row);
 
        return (
          <GiveTruncateText variant="bodyS" textAlign="left" lineClamp={1}>
            {label}
          </GiveTruncateText>
        );
      },
      hideColumn:
        isClosedTabSelected ||
        ((isAllTabSelected || isClosedTabSelected) && !isWideView),
    },
    ...(isOpenTabSelected || isUnderReviewTabSelected
      ? []
      : [
          {
            key: "statusSortOrder",
            columnWidth: 1 / columnsNumber,
            sortable: true,
            title: "Status",
            columnType: "status-round" as const,
            renderColumn: (row: DisputesRowType) => {
              return (
                <Stack
                  width="100%"
                  alignItems="center"
                  maxWidth={isWideView ? "none" : "124px"}
                >
                  <DisputeStatus row={row} />
                </Stack>
              );
            },
          },
        ]),
  ];
 
  return {
    columns,
  };
};