All files / public/icon-validator index.tsx

24.24% Statements 16/66
10% Branches 7/70
42.85% Functions 3/7
24.24% Lines 16/66

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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269                                      2x 2x 2x 2x 2x 2x     2x       2x                 2x 2x       2x 2x 2x                                                                                                                                     2x                                                                                                                                                                                                                                                                                                       1x   1x        
import { useState, useCallback, useMemo, useEffect } from "react";
import {
  Form,
  Input,
  Row,
  Col,
  Icon,
  Notification,
  ICONS,
  Strip,
  Button,
  Navigation,
  Theme,
} from "@canonical/react-components";
 
import { useDropzone } from "react-dropzone";
import ReactDOM from "react-dom/client";
 
export default function App() {
  const [icon, setIcon] = useState<string | null>(null);
  const [isImage, setIsImage] = useState<boolean>(false);
  const [isSVG, setIsSVG] = useState<boolean>(false);
  const [isCorrectSize, setIsCorrectSize] = useState<boolean>(false);
  const [imageSize, setImageSize] = useState<number[]>([0, 0]);
  const [hasConstraints, setHasConstraints] = useState<boolean>(true);
 
  const { acceptedFiles, getRootProps, getInputProps, isDragAccept } =
    useDropzone({
      maxFiles: 1,
    });
 
  const handleReset = useCallback(() => {
    setIcon(null);
    setIsImage(false);
    setIsSVG(false);
    setIsCorrectSize(false);
    setImageSize([0, 0]);
    setHasConstraints(true);
  }, []);
 
  const isValid = useMemo(
    () => isImage && isSVG && isCorrectSize && hasConstraints,
    [isImage, isSVG, isCorrectSize, hasConstraints]
  );
 
  useEffect(() => {
    Eif (!acceptedFiles.length) {
      return;
    }
    handleReset();
    const file = acceptedFiles[0];
 
    if (!file.type.startsWith("image/")) {
      return;
    }
 
    setIsImage(true);
 
    if (file.type === "image/svg+xml") {
      setIsSVG(true);
    }
 
    const urlReader = new FileReader();
    urlReader.addEventListener("load", (e) => {
      if (!e.target || !e.target.result) {
        setIsImage(false);
        return;
      }
 
      const dataURL = e.target.result as string;
      const svgString = window.atob(dataURL.split("base64,")[1]);
 
      if (svgString.indexOf("<svg") === -1) {
        setIsSVG(false);
        setHasConstraints(false);
      } else {
        const svgImage = document.createElement("div");
        svgImage.innerHTML = svgString;
        const svgEl = svgImage.querySelector("svg");
        const svgElWidth = svgEl?.getAttribute("width");
        const svgElHeight = svgEl?.getAttribute("height");
        const viewBoxSize = svgEl?.getAttribute("viewBox")?.split(" ");
 
        if (svgElHeight && svgElWidth) {
          setImageSize([parseInt(svgElWidth), parseInt(svgElHeight)]);
        } else if (viewBoxSize && viewBoxSize[2] && viewBoxSize[2]) {
          setImageSize([parseInt(viewBoxSize[2]), parseInt(viewBoxSize[2])]);
        } else {
          setHasConstraints(false);
        }
      }
 
      const img = new Image();
      img.addEventListener("load", () => {
        setIcon(dataURL);
        if (imageSize[0] === 0 && imageSize[1] === 0) {
          setImageSize([img.width, img.height]);
          if (img.width !== 100 || img.height !== 100) {
            setIsCorrectSize(false);
          } else {
            setIsCorrectSize(true);
          }
        } else {
          if (imageSize[0] !== 100 || imageSize[1] !== 100) {
            setIsCorrectSize(false);
          } else {
            setIsCorrectSize(true);
          }
        }
      });
      img.src = dataURL;
    });
    urlReader.readAsDataURL(file);
  }, [acceptedFiles, handleReset]);
  return (
    <>
      <Row>
        <Col size={8} emptyLarge={4}>
          <h1>Charm Icon Validator</h1>
          <p>
            Checks are based on the{" "}
            <a href="https://juju.is/docs/sdk/create-an-icon-for-your-charm">
              Charm Icon Specification
            </a>
            .
          </p>
          <p>Drag and drop, or use the file picker to choose your icon.</p>
          <Form stacked={true} style={{ marginBottom: "1rem" }}>
            <div
              {...getRootProps({ className: "dropzone" })}
              style={{
                outline: isDragAccept
                  ? "3px dashed #0e8420"
                  : "3px solid transparent",
                borderRadius: "3px",
                display: "block",
              }}
            >
              <Input type="file" onClick={(e) => e.preventDefault()} />
              <input {...getInputProps()} />
              {icon && (
                <>
                  <Row>
                    <Col size={2}>
                      <img src={icon} alt="" style={{ maxWidth: "100%" }} />
                    </Col>
                    <Col size={6}>
                      <p>
                        <Icon name={isImage ? ICONS.success : ICONS.error} />{" "}
                        Image
                      </p>
                      <p>
                        <Icon name={isSVG ? ICONS.success : ICONS.error} /> SVG
                      </p>
                      <p>
                        <Icon
                          name={hasConstraints ? ICONS.success : ICONS.error}
                        />{" "}
                        Root <code>svg</code> element has{" "}
                        <code>width="100"</code> and <code>height="100"</code>{" "}
                        attributes or a valid viewbox of <code>100 100</code>
                      </p>
                      <p>
                        <Icon
                          name={
                            imageSize[0] === 100 ? ICONS.success : ICONS.error
                          }
                        />{" "}
                        Image width is {imageSize[0]}px
                        {imageSize[0] !== 100 ? ", not 100px" : ""}
                      </p>
                      <p>
                        <Icon
                          name={
                            imageSize[1] === 100 ? ICONS.success : ICONS.error
                          }
                        />{" "}
                        Image height is {imageSize[1]}px
                        {imageSize[1] !== 100 ? ", not 100px" : ""}
                      </p>
                    </Col>
                  </Row>
                </>
              )}
            </div>
          </Form>
          {icon && (
            <>
              <Notification
                severity={isValid ? "positive" : "negative"}
                title={isValid ? "Valid icon" : "Invalid icon"}
              >
                {isValid ? (
                  <>
                    Based on the automated checks above your icon is valid, but
                    you should manually check the{" "}
                    <a href="https://juju.is/docs/sdk/create-an-icon-for-your-charm">
                      Charm Icon Specification
                    </a>
                    .
                  </>
                ) : (
                  <>
                    Based on the automated checks above your icon is not valid.
                    Please check the{" "}
                    <a href="https://juju.is/docs/sdk/create-an-icon-for-your-charm">
                      Charm Icon Specification
                    </a>
                    , update your icon, and try again.
                  </>
                )}
              </Notification>
              {isValid && <h2>Charmhub.io Preview</h2>}
            </>
          )}
        </Col>
      </Row>
      {icon && isValid && (
        <>
          <Navigation
            theme={Theme.DARK}
            logo={{
              src: "https://assets.ubuntu.com/v1/a603c7c9-Favicon - Juju.svg",
              title: "Charmhub",
              url: "#",
            }}
          />
          <Strip type="light" shallow>
            <Row>
              <Col size={8}>
                <div className="p-media-object--large">
                  <img
                    src={icon}
                    alt="Your Charm Icon"
                    className="p-media-object__image is-round"
                  />
                  <div className="p-media-object__details">
                    <h1 className="p-media-object__title">Your Charm</h1>
                    <div className="p-media-object__content u-no-margin--bottom">
                      <ul className="p-inline-list--middot">
                        <li className="p-inline-list__item">By You!</li>
                      </ul>
                    </div>
                  </div>
                </div>
                <div style={{ marginLeft: "-1rem" }}>
                  <Button inline styles={{ marginLeft: 0 }}>
                    stable 10
                  </Button>
                  <code style={{ backgroundColor: "transparent" }}>
                    juju deploy yourcharm --channel stable
                  </code>
                </div>
              </Col>
            </Row>
          </Strip>
        </>
      )}
    </>
  );
}
 
const rootElement = document.getElementById("main-content");
 
Iif (rootElement) {
  const root = ReactDOM.createRoot(rootElement);
  root.render(<App />);
}