All files / src/sections/PayBuilder/Forms/About utils.tsx

84.61% Statements 33/39
69.81% Branches 37/53
100% Functions 5/5
83.78% Lines 31/37

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                    47x           1481x 1481x 1481x 284x 39x 245x 1x   244x                     1481x 169x         169x     169x       169x                     169x 169x 168x 168x     1x 1x   1x 1x 1x                             1x                         1481x   1282x     47x 10x 3x     3x           3x                    
import { MediaItem } from "@sections/PayBuilder/provider/provider.type";
import { getDifferenceInUTCDays } from "@sections/PayBuilder/utils";
import GiveText from "@shared/Text/GiveText";
import { formatInUTCMoment } from "@utils/date.helpers";
import { useState, useEffect, useMemo } from "react";
 
import { processImageWithTransforms } from "./processImageWithTransforms";
 
export { processImageWithTransforms };
 
export const useImageSource = (
  inputMediaItem: MediaItem | string | null | undefined,
  options?: {
    rotation: number;
  },
) => {
  const [isLoading, setIsLoading] = useState(false);
  const [imageData, setImageData] = useState<string>("");
  const mediaItem = useMemo(() => {
    if (!inputMediaItem || typeof inputMediaItem === "string")
      return {} as MediaItem;
    if (inputMediaItem.resizeType === "crop" && !inputMediaItem.data) {
      return { ...inputMediaItem };
    }
    return {
      URL: inputMediaItem.URL,
      rotate: inputMediaItem.rotate,
      accountID: inputMediaItem.accountID,
      createdAt: inputMediaItem.createdAt,
      id: inputMediaItem.id,
      label: inputMediaItem.label,
      updatedAt: inputMediaItem.updatedAt,
    };
  }, [inputMediaItem]);
 
  useEffect(() => {
    Iif (!mediaItem) {
      setImageData("");
      return;
    }
 
    const baseUrl = mediaItem.URL ? mediaItem.URL + "/original" : "";
 
    const rotation =
      options?.rotation ||
      (typeof mediaItem !== "string" ? Number(mediaItem.rotate || 0) : 0);
 
    const cropData =
      typeof mediaItem !== "string" && mediaItem.x !== undefined
        ? {
            x: Number(mediaItem.x || 0),
            y: Number(mediaItem.y || 0),
            width: Number(mediaItem.width || 0),
            height: Number(mediaItem.height || 0),
            scaleX: Number(mediaItem.scaleX || 1),
            scaleY: Number(mediaItem.scaleY || 1),
          }
        : undefined;
 
    const needsTransformation = rotation !== 0 || cropData !== undefined;
    if (!isLoading && !needsTransformation) {
      setImageData(baseUrl);
      return;
    }
 
    Eif (needsTransformation) {
      setIsLoading(true);
 
      const applyTransformations = async () => {
        try {
          const processedImage = await processImageWithTransforms(
            baseUrl,
            rotation,
            cropData,
          );
 
          setImageData(processedImage);
        } catch (error) {
          console.error("Error processing image:", error);
          setImageData(baseUrl);
        } finally {
          setIsLoading(false);
        }
      };
 
      applyTransformations();
    }
  }, [
    mediaItem?.URL,
    mediaItem?.rotate,
    mediaItem?.x,
    mediaItem?.y,
    mediaItem?.width,
    mediaItem?.height,
    mediaItem?.scaleX,
    mediaItem?.scaleY,
    options?.rotation,
  ]);
  if (typeof inputMediaItem === "string") return { isLoading, data: "" };
 
  return { isLoading, data: imageData };
};
 
export const getDueDateHelperText = (value: string | number) => {
  if (!value || value === "never" || value === "custom") return "";
  const numberOfDaysToAdd = getDifferenceInUTCDays(value);
 
  const dayText =
    numberOfDaysToAdd === 0
      ? "today"
      : numberOfDaysToAdd === 1
      ? "tomorrow"
      : `in ${numberOfDaysToAdd} days`;
 
  return (
    <GiveText variant="bodyXS" color="secondary">
      This invoice will be due {dayText} after it is finalized or sent{" "}
      <GiveText component="span" color="primary" variant="bodyXS">
        ({formatInUTCMoment(value, "MMMM DD, yyyy")}){" "}
      </GiveText>
      if sent today.
    </GiveText>
  );
};