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

81.81% Statements 18/22
41.17% Branches 14/34
83.33% Functions 5/6
81.81% Lines 18/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                                                                  14x 14x 14x 14x 14x   14x 14x   14x     14x                                                 14x 14x           14x           14x     14x                                                                                                                                                               21x 77x 14x                             21x            
import { CURRENCY } from "@constants/constants";
import { GiveTransactionStatus } from "@features/MerchantPortal/ManageMoney/ManageMoneyTable.atoms";
import { Stack, Box } from "@mui/material";
import { ShieldCheckIcon, ShieldWarningIcon } from "@phosphor-icons/react";
import GiveChip, { TChipColors } from "@shared/Chip/GiveChip";
import GiveText from "@shared/Text/GiveText";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { styled, useAppTheme } from "@theme/v2/Provider";
import { useFormatDateInTimezone } from "@utils/date.helpers";
import GiveDateTooltip from "@shared/Tooltip/GiveDateTooltip";
import { TransactionType } from "./types";
import { parseAmount } from "@utils/index";
import GiveTruncateText from "@shared/Text/GiveTruncateText";
import { useMerchantSidePanelContext } from "@features/Merchants/MerchantSidePanel/Provider/MerchantSidePanelProvider";
 
interface TransactionItemProps {
  transaction?: TransactionType;
  dataTestId?: string;
}
 
export default function TransactionItem({
  transaction,
  dataTestId,
}: TransactionItemProps) {
  const {
    createdAt,
    emailStatus = "pending",
    email,
    isBlocked,
    isFalsePositive,
    txnDisplayStatus = "Pending",
    amount = 0,
    riskLevel = 0,
  } = transaction || {};
  const { formatInTimezone } = useFormatDateInTimezone();
  const { isMobileView } = useCustomThemeV2();
  const { palette } = useAppTheme();
  const { handlers } = useMerchantSidePanelContext();
 
  const usedDisplayStatus = (() => {
    Iif (txnDisplayStatus === "Pre-Chargeback Refund")
      return "Chargeback Refund";
    return txnDisplayStatus;
  })();
 
  const RISK_ITEMS = {
    0: {
      icon: (
        <ShieldCheckIcon size={20} color={palette.primitive?.success[100]} />
      ),
      text: "success",
    },
    1: {
      icon: (
        <ShieldWarningIcon size={20} color={palette.primitive?.warning[50]} />
      ),
      text: "warning2",
    },
    2: {
      icon: (
        <ShieldWarningIcon size={20} color={palette.primitive?.warning[100]} />
      ),
      text: "warning",
    },
    3: {
      icon: <ShieldCheckIcon size={20} color={palette.primitive?.error[100]} />,
      text: "error",
    },
  };
 
  const chipData = (() => {
    Iif (isFalsePositive) {
      return {
        text: "False Positive",
        color: "success",
      };
    }
    Iif (isBlocked) {
      return {
        text: "Suspected",
        color: "error",
      };
    }
    return undefined;
  })();
 
  return (
    <Container
      data-testid={dataTestId}
      isMobileView={isMobileView}
      onClick={() =>
        handlers.handleOpenTransactionRiskProfile((transaction as any) || null)
      }
    >
      <Stack
        data-testid={`icon-${riskLevel}`}
        direction="row"
        gap="4px"
        alignItems="center"
      >
        {RISK_ITEMS[riskLevel].icon}
        <GiveText variant="bodyS" color={RISK_ITEMS[riskLevel].text}>
          {riskLevel}
        </GiveText>
      </Stack>
      <Stack gap="8px" flex={1}>
        <GiveDateTooltip>
          <GiveText variant="bodyS">
            {createdAt
              ? formatInTimezone(createdAt, "MMM dd, yyyy HH:mm")?.toString()
              : "-"}
          </GiveText>
        </GiveDateTooltip>
        <Stack
          direction={isMobileView ? "column" : "row"}
          gap="4px"
          alignItems={isMobileView ? "flex-start" : "center"}
        >
          <GiveTruncateText variant="bodyS" color="secondary" lineClamp={1}>
            {email || "-"}
          </GiveTruncateText>
          <GiveText
            variant="bodyS"
            color={VALIDITY_COLORS[emailStatus]}
            textTransform="capitalize"
          >
            {emailStatus || "-"}
          </GiveText>
        </Stack>
      </Stack>
      <Stack
        direction={isMobileView ? "row-reverse" : "column"}
        gap="8px"
        alignItems={isMobileView ? "center" : "flex-end"}
        justifyContent={isMobileView ? "space-between" : "center"}
      >
        <GiveText variant="bodyS">
          {parseAmount(amount / 100)} {CURRENCY}
        </GiveText>
        <Stack direction="row" gap="8px" alignItems="center">
          {chipData && (
            <GiveChip
              variant="light"
              label={chipData.text}
              color={chipData.color as TChipColors}
            />
          )}
          <GiveTransactionStatus
            status={usedDisplayStatus?.toLowerCase() as TChipColors}
            label={usedDisplayStatus}
            sx={{
              borderRadius: "8px",
              padding: "4px 8px",
              height: "24px",
              "& span": {
                fontSize: "12px",
                lineHeight: "14px !important",
              },
            }}
          />
        </Stack>
      </Stack>
    </Container>
  );
}
 
const Container = styled(Box, {
  shouldForwardProp: (prop) => prop !== "isMobileView",
})<{ isMobileView?: boolean }>(({ theme, isMobileView }) => ({
  display: "flex",
  borderBottom: `1px solid ${theme.palette.border?.primary}`,
  gap: isMobileView ? "12px" : "24px",
  padding: "16px",
  overflow: "hidden",
  cursor: "pointer",
  "&:hover": {
    background: theme.palette.primitive?.transparent["darken-2"],
  },
  [theme.breakpoints.down("v2_sm")]: {
    flexDirection: "column",
  },
}));
 
const VALIDITY_COLORS = {
  valid: "success",
  pending: "secondary",
  undeliverable: "error",
  invalid: "error",
};