All files / src/components/Merchants/MerchantPreview/modals/shared GivePreviousChecksSection.tsx

100% Statements 27/27
57.14% Branches 12/21
100% Functions 6/6
100% Lines 26/26

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                                                    4x                   32x   32x 32x     32x                   32x                                                       4x         112x 112x 112x   112x 86x 86x   26x 26x     112x 112x   112x 67x     45x 19x                                                                       26x                                             32x                     4x 56x 26x                            
import { useLayoutEffect, useRef, useState } from "react";
import { Box, Stack } from "@mui/material";
import { styled } from "@theme/v2/Provider";
import TaskCard from "@features/Merchants/MerchantSidePanel/UnderwritingTasks/components/TaskCard";
import GiveChip, { TChipColors } from "@shared/Chip/GiveChip";
import GiveTooltip from "@shared/Tooltip/GiveTooltip";
import GiveTruncateText from "@shared/Text/GiveTruncateText";
import { useAppTheme } from "@theme/v2/Provider";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { getOfacPepStatus } from "@features/Merchants/MerchantSidePanel/UnderwritingTasks/components/Accordion.atom";
import {
  ExtendedPEPStatusType,
  ofacDataTypes,
} from "@features/Merchants/MerchantSidePanel/UnderwritingTasks/types";
import { formatTimestampWithTimezone } from "@utils/index";
import { checkoutScrollbarStyles } from "@sections/PayBuilder/Checkout/helpers";
import GivePreviousChecksSectionSkeleton from "./skeletons/GivePreviousChecksSectionSkeleton";
import GiveOFACEmptyState from "./emptyStates/GiveOFACEmptyState";
import { useHeightThreshold } from "@hooks/useHeightThreshold";
 
interface GivePreviousChecksSectionProps {
  previousChecks?: ofacDataTypes[];
  isLoading?: boolean;
  isEmpty?: boolean;
}
 
const PreviousCheckItem = ({
  check,
  ofacPepStatus,
}: {
  check: ofacDataTypes;
  ofacPepStatus: Record<
    ExtendedPEPStatusType,
    { color: string; label: string }
  >;
}) => {
  const { isMobileView } = useCustomThemeV2();
  const { color, label } =
    ofacPepStatus[check.statusName as ExtendedPEPStatusType] || {};
  const dateText = check.createdAt ? formatTimestampWithTimezone(check.createdAt) : "";
 
  const dateContent = (
    <GiveTruncateText
      color="secondary"
      variant="bodyS"
      lineClamp={1}
      sx={{ flex: 1 }}
    >
      {dateText}
    </GiveTruncateText>
  );
 
  return (
    <Stack spacing={0} sx={{ width: "100%" }}>
      <Stack
        direction="row"
        justifyContent="space-between"
        alignItems="center"
        py="12px"
      >
        <GiveTooltip
          title={dateText}
          fluidWidth
          disableHoverListener={!isMobileView}
          disableFocusListener={!isMobileView}
        >
          {dateContent}
        </GiveTooltip>
        <GiveChip
          variant="light"
          size="small"
          color={color as TChipColors}
          label={label}
          sx={{ maxHeight: "24px" }}
        />
      </Stack>
    </Stack>
  );
};
 
const GivePreviousChecksSection = ({
  previousChecks = [],
  isLoading = false,
  isEmpty = false,
}: GivePreviousChecksSectionProps) => {
  const [scrollbarWidth, setScrollbarWidth] = useState(0);
  const scrollRef = useRef<HTMLDivElement | null>(null);
  const { ref, isBelowThreshold } = useHeightThreshold(200);
 
  useLayoutEffect(() => {
    const el = scrollRef.current;
    if (!el) return;
 
    const width = el.offsetWidth - el.clientWidth;
    setScrollbarWidth(width);
  }, [previousChecks]);
 
  const { palette } = useAppTheme();
  const ofacPepStatus = getOfacPepStatus(palette);
 
  if (isLoading) {
    return <GivePreviousChecksSectionSkeleton />;
  }
 
  if (isEmpty) {
    return (
      <TaskCard
        ref={ref}
        title="Previous Checks"
        containerSx={{
          display: "flex",
          flexDirection: "column",
          flex: 1,
          minHeight: isBelowThreshold ? "auto" : 0,
          "& > div:first-of-type": { minHeight: 54, p: "8px 20px" },
        }}
        sx={{
          p: 0,
          display: "flex",
          flexDirection: "column",
          flex: 1,
          minHeight: 0,
        }}
      >
        <Box
          sx={{
            flex: 1,
            minHeight: 200,
            display: "flex",
            flexDirection: "column",
            overflowY: "auto",
            overflowX: "hidden",
            ...checkoutScrollbarStyles,
          }}
        >
          <GiveOFACEmptyState showBorder={false} minHeight={200} />
        </Box>
      </TaskCard>
    );
  }
 
  return (
    <TaskCard
      ref={ref}
      title="Previous Checks"
      containerSx={{
        display: "flex",
        flexDirection: "column",
        flex: 1,
        minHeight: isBelowThreshold ? "auto" : 0,
        maxHeight: isBelowThreshold ? 200 : "auto",
        "& > div:first-of-type": { minHeight: 54, p: "8px 20px" },
      }}
      sx={{
        p: 0,
        display: "flex",
        flexDirection: "column",
        flex: 1,
        minHeight: 0,
        mb: isBelowThreshold ? 2 : 0,
      }}
    >
      <TaskCardScrollContainer ref={scrollRef} scrollbarWidth={scrollbarWidth}>
        {previousChecks.map((check) => (
          <PreviousCheckItem
            key={check.id}
            check={check}
            ofacPepStatus={ofacPepStatus}
          />
        ))}
      </TaskCardScrollContainer>
    </TaskCard>
  );
};
 
const TaskCardScrollContainer = styled(Box, {
  shouldForwardProp: (prop) => prop !== "scrollbarWidth",
})<{ scrollbarWidth: number }>(({ scrollbarWidth }) => ({
  flex: 1,
  minHeight: 0,
  maxHeight: 500,
  overflowY: "auto",
  overflowX: "hidden",
  paddingLeft: "20px",
  paddingRight: `calc(20px - ${scrollbarWidth}px)`,
  display: "flex",
  flexDirection: "column",
  ...checkoutScrollbarStyles,
}));
 
export default GivePreviousChecksSection;