All files / publisher/shared/SaveAndPreview SaveAndPreview.tsx

85.71% Statements 6/7
100% Branches 6/6
75% Functions 3/4
85.71% Lines 6/7

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                                          13x 13x             13x 13x     13x                                                                   1x                                                              
import { useEffect, useRef } from "react";
import { Row, Col, Button } from "@canonical/react-components";
 
import debounce from "../../../libs/debounce";
 
type Props = {
  snapName: string | undefined;
  isDirty: boolean;
  reset: Function;
  isSaving: boolean;
  isValid: boolean;
  showPreview?: boolean;
};
 
function SaveAndPreview({
  snapName,
  isDirty,
  reset,
  isSaving,
  showPreview,
}: Props) {
  const stickyBar = useRef<HTMLDivElement>(null);
  const handleScroll = () => {
    stickyBar?.current?.classList.toggle(
      "sticky-shadow",
      stickyBar?.current?.getBoundingClientRect()?.top === 0,
    );
  };
 
  useEffect(() => {
    document.addEventListener("scroll", debounce(handleScroll, 10, false));
  }, []);
 
  return (
    <>
      <div className="snapcraft-p-sticky js-sticky-bar" ref={stickyBar}>
        <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;