All files Thumbnail.jsx

74.35% Statements 29/39
46.66% Branches 14/30
66.66% Functions 2/3
74.35% Lines 29/39

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        1x                     9x 9x 9x 9x 6x                   9x 9x           9x 6x   3x     9x 9x 9x               9x 9x 9x 9x 9x             9x 9x   9x               9x         9x               9x             9x           9x                                                                                                       1x                                                                     1x        
import React, { useState, useRef, useEffect } from "react";
import PropTypes from "prop-types";
import Skeleton from "./Skeleton";
 
export const Thumbnail = ({
  url = "https://nodejs.org/en/download/",
  height: heightProp,
  width: widthProp,
  interactive = false,
  iframeWidth: iframeWidthProp,
  iframeHeight: iframeHeightProp,
  style: customStyle,
  onLoad,
  ...props
}) => {
  const [loading, setLoading] = useState(true);
  const [calculatedSize, setCalculatedSize] = useState({});
  const ref = useRef(null);
  useEffect(() => {
    setCalculatedSize({
      width: ref.current.offsetWidth,
      height: ref.current.offsetHeight,
    });
    // console.log("width", ref.current ? ref.current.offsetWidth : 0);
    // console.log("height", ref.current ? ref.current.offsetHeight : 0);
    // console.log("----------------------------");
  }, [ref.current?.offsetWidth, ref.current?.offsetHeight]);
 
  let width;
  if (!widthProp) {
    width = "100%";
  } else E{
    width = `${widthProp}px`;
  }
 
  let height;
  if (!heightProp) {
    height = "100%";
  } else {
    height = `${heightProp}px`;
  }
 
  let aspectRatioHeight = 3;
  let aspectRatioWidth = 4;
  Iif (widthProp && heightProp) {
    aspectRatioHeight = height;
    aspectRatioWidth = width;
  }
 
  // Dealing with Height and Width
  // Variables to do with size and scale:
  // - width, height, iframeWidth, iframeHeight, xscale, yscale
  let iframeWidth = iframeWidthProp;
  let iframeHeight = iframeHeightProp;
  if (!iframeWidth && !iframeHeight) {
    iframeWidth = 1440;
    iframeHeight = 1080;
  } else Eif (iframeWidth && !iframeHeight) {
    iframeHeight = iframeWidth * (aspectRatioHeight / aspectRatioWidth);
  } else if (!iframeWidth && iframeHeight) {
    iframeWidth = iframeHeight * (aspectRatioWidth / aspectRatioHeight);
  }
 
  const xscale = calculatedSize.width / iframeWidth;
  const yscale = calculatedSize.height / iframeHeight;
 
  const outerWrapper = {
    display: "flex",
    alignItems: "center",
    justifyContent: "center",
    width,
    height,
  };
 
  const iframeWrapper = {
    width: calculatedSize.width,
    maxHeight: calculatedSize.height,
  };
 
  const iframe = {
    transform: `scaleX(${xscale}) scaleY(${yscale})`,
    transformOrigin: "0 0",
    width: `${iframeWidth}px`,
    height: `${iframeHeight}px`,
    border: "0px",
  };
 
  const iframeShade = {
    position: "absolute",
    width: calculatedSize.width,
    height: calculatedSize.height,
    zIndex: "10",
  };
 
  const combinedOnLoad = () => {
    if (onLoad) {
      onLoad();
    }
    setLoading(false);
  };
  return (
    <div
      ref={ref}
      style={{ ...outerWrapper, ...customStyle }}
      data-testid="div-outer-wrapper"
    >
      {ref.current && (
        <div style={iframeWrapper} data-testid="div-iframe-wrapper">
          {!interactive && <div style={iframeShade}> </div>}
          {loading && (
            <div
              className="skeleton-wrapper"
              style={{
                width: calculatedSize.width,
                height: calculatedSize.height,
                position: "absolute",
                zIndex: "15",
              }}
            >
              <Skeleton
                style={{
                  width: calculatedSize.width,
                  height: calculatedSize.height,
                }}
                type="thumbnail"
                width={calculatedSize.width}
                height={calculatedSize.height}
              />
              <div className="shimmer-wrapper">
                <div className="shimmer"></div>
              </div>
            </div>
          )}
 
          <iframe
            tabIndex="-1"
            role="iframe"
            style={iframe}
            title="webpage-thumbnail"
            src={url}
            sandbox="allow-scripts allow-same-origin"
            scrolling="no"
            onLoad={combinedOnLoad}
            {...props}
          />
        </div>
      )}
    </div>
  );
};
export default Thumbnail;
 
Thumbnail.propTypes = {
  /**
   * The url of the webpage to render the thumbnail for
   */
  url: PropTypes.string,
  /**
   * The height of the entire component. iframe will be scaled down to this height. Defaults to be 100% of parent element
   */
  height: PropTypes.number,
  /**
   * The width of the entire component. iframe will be scaled down to this width. Defaults to be 100% of parent element
   */
  width: PropTypes.number,
  /**
   * Whether this iframe is to be static or interactive (clickable).
   */
  interactive: PropTypes.bool,
  /**
   * The width of the iframe viewport. Defaults to 1440px if both iframewidth and iframeheight is not provided
   */
  iframeWidth: PropTypes.number,
  /**
   * The height of the iframe viewport. Defaults to 1080px if both iframewidth and iframeheight is not provided
   */
  iframeHeight: PropTypes.number,
  /**
   * customized styling to be passed to the wrapper (div) of the component consisting the iframe
   */
  style: PropTypes.object,
  /**
   * customized function which will be called when iframed webpage finishes loading
   */
  onLoad: PropTypes.func,
};
 
Thumbnail.defaultProps = {
  url: "https://nodejs.org/en/download/",
  interactive: false,
};