All files / publisher/components/SaveAndPreview SaveAndPreview.tsx

75% Statements 6/8
87.5% Branches 7/8
66.66% Functions 2/3
75% Lines 6/8

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                                          21x 21x   21x             21x       21x                                                                           1x                                                              
import { useRef } from "react";
import { Row, Col, Button } from "@canonical/react-components";
 
import debounce from "../../../libs/debounce";
 
type Props = {
  snapName: string;
  isDirty: boolean;
  reset: () => void;
  isSaving: boolean;
  isValid: boolean;
  showPreview?: boolean;
};
 
function SaveAndPreview({
  snapName,
  isDirty,
  reset,
  isSaving,
  showPreview,
}: Props): React.JSX.Element {
  const stickyBar = useRef<HTMLDivElement>(null);
  const mainPanel = document.querySelector(".l-main") as HTMLElement;
 
  const handleScroll = (): void => {
    stickyBar?.current?.classList.toggle(
      "sticky-shadow",
      stickyBar?.current?.getBoundingClientRect()?.top === 0,
    );
  };
 
  Iif (mainPanel) {
    mainPanel.addEventListener("scroll", debounce(handleScroll, 10, false));
  }
 
  return (
    <>
      <div
        className="snapcraft-p-sticky js-sticky-bar"
        ref={stickyBar}
        style={{ margin: "0 -1.5rem", padding: "0 1.5rem" }}
      >
        <Row>
          <Col size={7}>
            <p className="u-no-margin--bottom">
              Updates to this information will appear immediately on the{" "}
              <a href={`/${snapName}`}>snap listing page</a>.
            </p>
          </Col>
          <Col size={5}>
            <div className="u-align--right">
              {showPreview && (
                <Button
                  type="submit"
                  className="p-button--base p-tooltip--btm-center"
                  aria-describedby="preview-tooltip"
                  form="preview-form"
                >
                  Preview
                  <span
                    className="p-tooltip__message"
                    role="tooltip"
                    id="preview-tooltip"
                  >
                    Previews will only work in the same browser, locally
                  </span>
                </Button>
              )}
              <Button
                appearance="default"
                disabled={!isDirty}
                type="reset"
                onClick={() => {
                  reset();
                }}
              >
                Revert
              </Button>
              <Button
                appearance="positive"
                disabled={!isDirty || isSaving}
                type="submit"
                style={{ minWidth: "68px" }}
              >
                {isSaving ? (
                  <i className="p-icon--spinner is-light u-animation--spin">
                    Saving
                  </i>
                ) : (
                  "Save"
                )}
              </Button>
            </div>
          </Col>
        </Row>
      </div>
      <div className="u-fixed-width">
        <hr className="u-no-margin--bottom" />
      </div>
    </>
  );
}
 
export default SaveAndPreview;