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 | 168x 168x 168x 168x 168x 47x 20x 27x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 27x 27x 27x 168x 1x | import { useEffect, 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));
}
useEffect(() => {
if (!showPreview) {
return;
}
const handlePreviewAction = (event: MessageEvent): void => {
Iif (event.origin !== window.location.origin) {
return;
}
const data = event.data as { type?: string; action?: string } | null;
Iif (!data || data.type !== "snapcraft-preview-action") {
return;
}
const sourceWindow = event.source as Window | null;
const closePreview = (): void => {
Eif (sourceWindow && typeof sourceWindow.close === "function") {
sourceWindow.close();
}
};
if (data.action === "revert") {
Eif (isDirty) {
reset();
}
closePreview();
return;
}
Eif (data.action === "save") {
Iif (!isDirty || isSaving) {
closePreview();
return;
}
const formElement =
(stickyBar.current?.closest("form") as HTMLFormElement | null) ??
null;
Iif (!formElement) {
return;
}
Iif (typeof formElement.requestSubmit === "function") {
formElement.requestSubmit();
} else {
formElement.dispatchEvent(
new Event("submit", { bubbles: true, cancelable: true }),
);
}
closePreview();
}
};
window.addEventListener("message", handlePreviewAction);
return () => {
window.removeEventListener("message", handlePreviewAction);
};
}, [isDirty, isSaving, reset, showPreview]);
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"
data-js="save-and-preview-revert"
onClick={() => {
reset();
}}
>
Revert
</Button>
<Button
appearance="positive"
disabled={!isDirty || isSaving}
type="submit"
style={{ minWidth: "68px" }}
data-js="save-and-preview-save"
>
{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;
|