All files / publisher/pages/Publicise PubliciseCards.tsx

63.33% Statements 19/30
40% Branches 4/10
12.5% Functions 1/8
63.33% Lines 19/30

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                    1x 1x     1x 1x 1x   1x 1x   1x 1x     1x 1x     1x 1x     1x 1x     1x       1x   1x                             1x                                                                                                                                                                                                                                      
import { useState, useRef, SyntheticEvent } from "react";
import { useParams } from "react-router-dom";
import {
  Row,
  Col,
  RadioInput,
  CheckboxInput,
} from "@canonical/react-components";
 
function PubliciseCards(): React.JSX.Element {
  const { snapId } = useParams();
  const [buttonType, setButtonType] = useState<"black" | "white" | "hidden">(
    "black",
  );
  const [showAllChannels, setShowAllChannels] = useState<boolean>(true);
  const [showSummary, setShowSummary] = useState<boolean>(true);
  const [showScreenshot, setShowScreenshot] = useState<boolean>(true);
 
  const iframeRef = useRef<HTMLIFrameElement>(null);
  const iframeQueryParameters: { [key: string]: string } = {};
 
  Eif (buttonType !== "hidden") {
    iframeQueryParameters["button"] = buttonType;
  }
 
  Eif (showAllChannels) {
    iframeQueryParameters["channels"] = "true";
  }
 
  Eif (showSummary) {
    iframeQueryParameters["summary"] = "true";
  }
 
  Eif (showScreenshot) {
    iframeQueryParameters["screenshot"] = "true";
  }
 
  const iframeQueryString = new URLSearchParams(
    iframeQueryParameters,
  ).toString();
 
  const htmlSnippet = `<iframe title="Publicise card" src="https://snapcraft.io/${snapId}/embedded?${iframeQueryString}" style="width: 100%; height: 100%; border: 1px solid #CCC; border-radius: 2px;"></iframe>`;
 
  const adjustIframeHeight = () => {
    const iframe = iframeRef.current;
    if (iframe) {
      try {
        iframe.style.height =
          iframe.contentWindow?.document.body.scrollHeight + "px";
      } catch (error) {
        console.warn(
          "Unable to access iframe content for height adjustment:",
          error,
        );
      }
    }
  };
 
  return (
    <>
      <Row>
        <Col size={2}>
          <p>Snap Store button:</p>
        </Col>
        <Col size={10}>
          <p>
            <RadioInput
              name="button-type"
              label="Dark"
              checked={buttonType === "black"}
              onChange={() => {
                setButtonType("black");
              }}
            />
            <RadioInput
              name="button-type"
              label="Light"
              checked={buttonType === "white"}
              onChange={() => {
                setButtonType("white");
              }}
            />
            <RadioInput
              name="button-type"
              label="Hide button"
              checked={buttonType === "hidden"}
              onChange={() => {
                setButtonType("hidden");
              }}
            />
          </p>
        </Col>
      </Row>
      <Row>
        <Col size={2}>
          <p>Options:</p>
        </Col>
        <Col size={10}>
          <p>
            <CheckboxInput
              label="Show all channels"
              checked={showAllChannels}
              onChange={(
                e: SyntheticEvent<HTMLInputElement> & {
                  target: HTMLInputElement;
                },
              ) => {
                setShowAllChannels(e.target.checked);
              }}
            />
            <CheckboxInput
              label="Show summary"
              checked={showSummary}
              onChange={(
                e: SyntheticEvent<HTMLInputElement> & {
                  target: HTMLInputElement;
                },
              ) => {
                setShowSummary(e.target.checked);
              }}
            />
            <CheckboxInput
              label="Show screenshot"
              checked={showScreenshot}
              onChange={(
                e: SyntheticEvent<HTMLInputElement> & {
                  target: HTMLInputElement;
                },
              ) => {
                setShowScreenshot(e.target.checked);
              }}
            />
          </p>
        </Col>
      </Row>
      <Row>
        <Col size={2}>
          <p>Preview:</p>
        </Col>
        <Col size={8}>
          <p>
            <iframe
              ref={iframeRef}
              title="Card preview"
              src={`/${snapId}/embedded?${iframeQueryString}`}
              width="100%"
              style={{
                border: "1px solid rgb(204, 204, 204)",
                borderRadius: "2px",
                height: "auto",
              }}
              onLoad={adjustIframeHeight}
            ></iframe>
          </p>
        </Col>
      </Row>
      <Row>
        <Col size={2}>
          <p>HTML:</p>
        </Col>
        <Col size={10}>
          <div className="p-code-snippet">
            <pre className="p-code-snippet__block is-wrapped">
              {htmlSnippet}
            </pre>
          </div>
        </Col>
      </Row>
    </>
  );
}
 
export default PubliciseCards;