All files / publisher/listing/components/ListingDetails Screenshots.tsx

39.13% Statements 9/23
30% Branches 3/10
25% Functions 1/4
39.13% Lines 9/23

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                                  5x 5x 5x           5x         5x         5x   5x               5x                                                                     5x                                                                                                                                                                              
import { useState } from "react";
import { useFieldArray } from "react-hook-form";
import { nanoid } from "nanoid";
import { Row, Col, Notification } from "@canonical/react-components";
 
import { validateImageDimensions } from "../../utils";
 
import ScreenshotList from "./ScreenshotList";
 
type Props = {
  register: Function;
  control: any;
  getValues: Function;
  setValue: Function;
};
 
function Screenshots({ register, control, getValues, setValue }: Props) {
  const [showImageRestrictions, setShowImageRestrictions] = useState(false);
  const [imageVaidationError, setImageValidationError] = useState("");
  const [imageIsValid, setImageIsValid] = useState(true);
 
  const {
    append: addScreenshotUrl,
    remove: removeScreenshotUrl,
    move: moveScreenshotUrl,
  } = useFieldArray({
    control,
    name: "screenshot_urls",
  });
 
  const { fields: screenshots } = useFieldArray({
    control,
    name: "screenshots",
  });
 
  const fieldId = nanoid();
 
  const validationSchema = {
    maxFileSize: 2000000,
    minWidth: 480,
    maxWidth: 4320,
    minHeight: 480,
    maxHeight: 2160,
  };
 
  const setImage = (image: File) => {
    if (image.size > validationSchema?.maxFileSize) {
      setImageIsValid(false);
      setImageValidationError(
        `${image.name} file size is over ${
          validationSchema?.maxFileSize / 1000000
        }KB`,
      );
      return;
    }
 
    const renderedImage = new Image();
    const renderedImageUrl = URL.createObjectURL(image);
 
    renderedImage.src = renderedImageUrl;
    renderedImage.onload = () => {
      if (
        !validateImageDimensions(renderedImage.width, renderedImage.height, {
          minWidth: validationSchema?.minWidth,
          maxWidth: validationSchema?.maxWidth,
          minHeight: validationSchema?.minHeight,
          maxHeight: validationSchema?.maxHeight,
        })
      ) {
        setImageIsValid(false);
        setImageValidationError(
          `${image.name} has dimension ${renderedImage.width} x ${renderedImage.height} pixels. It needs to be at least ${validationSchema?.minWidth} x ${validationSchema?.minHeight} and at most ${validationSchema?.maxWidth} x ${validationSchema?.maxHeight} pixels.`,
        );
      } else {
        setImageIsValid(true);
        addScreenshotUrl(renderedImageUrl);
      }
    };
  };
 
  return (
    <Row className="p-form__group p-form__group--top">
      <Col size={2} data-tour="listing-images">
        <label htmlFor={fieldId} className="p-form__label">
          Images:
        </label>
      </Col>
      <Col size={8} data-tour="listing-images">
        {!imageIsValid && (
          <Notification severity="negative">{imageVaidationError}</Notification>
        )}
 
        <ScreenshotList
          screenshots={screenshots}
          screenshotUrls={getValues("screenshot_urls")}
          getValues={getValues}
          removeScreenshotUrl={removeScreenshotUrl}
          setValue={setValue}
          register={register}
          setImage={setImage}
          moveScreenshotUrl={moveScreenshotUrl}
        />
 
        <button
          type="button"
          className="p-button--link"
          onClick={() => {
            setShowImageRestrictions(!showImageRestrictions);
          }}
        >
          <small>
            {showImageRestrictions
              ? "Hide image restrictions"
              : "Show image restrictions"}
          </small>
        </button>
        {showImageRestrictions && (
          <ul>
            <li>
              <small>
                Accepted image formats include:{" "}
                <strong>GIF, JPEG & PNG files</strong>
              </small>
            </li>
            <li>
              <small>
                Min resolution: <strong>480 x 480 pixels</strong>
              </small>
            </li>
            <li>
              <small>
                Max resolution: <strong>3840 x 2160 pixels</strong>
              </small>
            </li>
            <li>
              <small>
                Aspect ratio: <strong>Between 1:2 and 2:1</strong>
              </small>
            </li>
            <li>
              <small>
                File size limit: <strong>2MB</strong>
              </small>
            </li>
            <li>
              <small>
                Animation min fps: <strong>1</strong>
              </small>
            </li>
            <li>
              <small>
                Animation max fps: <strong>30</strong>
              </small>
            </li>
            <li>
              <small>
                Animation max length: <strong>40 seconds</strong>
              </small>
            </li>
          </ul>
        )}
      </Col>
    </Row>
  );
}
 
export default Screenshots;