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 | 2x 3x 3x 3x 3x 3x 3x 3x 6x 6x 4x 4x 6x 6x 6x 3x 3x 3x 3x 2x 3x 1x | import { type ReactElement, useEffect, useState } from "react";
import {
Button,
Modal as CanonicalModal,
Spinner,
} from "@canonical/react-components";
import type { Props as ModalProps } from "@canonical/react-components/dist/components/Modal/Modal";
type ShareBadgeModalProps = {
close: () => void;
packageName: string;
packageTitle?: string;
releaseChannel: string;
revision: number;
};
type BadgeData = {
badgeAlt: string;
badgeSrc: string;
htmlSnippet: string;
markdownSnippet: string;
packageUrl: string;
};
type CodeSnippetProps = {
code: string;
label: string;
};
const Modal = (props: ModalProps): ReactElement => {
return CanonicalModal(props) as unknown as ReactElement;
};
function getBadgeData(
packageName: string,
title: string | undefined,
releaseChannel: string,
revision: number
): BadgeData {
const encodedChannel = encodeURIComponent(releaseChannel);
const encodedRevision = encodeURIComponent(revision);
const badgeTitle = title || packageName;
const packageUrl = `https://charmhub.io/${packageName}?channel=${encodedChannel}`;
const badgeUrl = `https://charmhub.io/${packageName}/badge.svg?channel=${encodedChannel}&revision=${encodedRevision}`;
return {
badgeAlt: `${packageName} ${releaseChannel} revision ${revision} GitHub badge`,
badgeSrc: `/${packageName}/badge.svg?channel=${encodedChannel}&revision=${encodedRevision}`,
htmlSnippet: `<a href="${packageUrl}">\n <img alt="" src="${badgeUrl}" />\n</a>`,
markdownSnippet: `[](${packageUrl})`,
packageUrl: `/${packageName}?channel=${encodedChannel}`,
};
}
function resetCopyFlag(duration = 2000) {
const [isActive, setIsActive] = useState(false);
useEffect(() => {
Eif (!isActive) {
return;
}
const timeoutId = window.setTimeout(() => {
setIsActive(false);
}, duration);
return () => {
window.clearTimeout(timeoutId);
};
}, [duration, isActive]);
return [isActive, setIsActive] as const;
}
function CodeSnippet({ code, label }: CodeSnippetProps) {
const [isCopied, setIsCopied] = resetCopyFlag();
return (
<div className="p-code-snippet">
<div
className="p-code-snippet__header"
style={{
alignItems: "center",
display: "flex",
justifyContent: "space-between",
}}
>
<h5 className="p-code-snippet__title u-no-margin--bottom">{label}</h5>
<button
type="button"
className="p-button--base has-icon is-small u-no-margin--bottom"
aria-label={`Copy ${label} snippet`}
disabled={isCopied}
onClick={async () => {
await navigator.clipboard.writeText(code);
setIsCopied(true);
}}
>
{isCopied ? (
<span>Copied</span>
) : (
<i className="p-icon--copy" aria-hidden="true">
Copy to clipboard
</i>
)}
</button>
</div>
<pre className="p-code-snippet__block is-wrapped">
<code>{code}</code>
</pre>
</div>
);
}
function ShareBadgeModal({
close,
packageName,
packageTitle,
releaseChannel,
revision,
}: ShareBadgeModalProps) {
const badgeData = getBadgeData(
packageName,
packageTitle,
releaseChannel,
revision
);
const [isBadgeLoading, setIsBadgeLoading] = useState(true);
const charmhubLink = `https://charmhub.io${badgeData.packageUrl}`;
useEffect(() => {
setIsBadgeLoading(true);
}, [badgeData.badgeSrc]);
return (
<Modal
close={close}
title={`Share ${packageName} ${releaseChannel}`}
buttonRow={
<Button className="u-no-margin--bottom" onClick={close}>
Close
</Button>
}
>
<h5>Charmhub link</h5>
<div className="u-sv3" style={{ display: "flex" }}>
<label className="u-off-screen" htmlFor="charmhub-link-read-only">
Charmhub link
</label>
<input
type="text"
readOnly
value={charmhubLink}
name="charmhub-link-read-only"
id="charmhub-link-read-only"
/>
<button
type="button"
onClick={() => {
navigator.clipboard.writeText(charmhubLink);
}}
>
<i className="p-icon--copy">Copy link</i>
</button>
</div>
<hr />
<h5>Github badge</h5>
<p>{isBadgeLoading && <Spinner text="Loading badge..." />}</p>
<div className="u-sv2">
<a href={badgeData.packageUrl}>
<img
src={badgeData.badgeSrc}
alt={badgeData.badgeAlt}
hidden={isBadgeLoading}
onLoad={() => setIsBadgeLoading(false)}
/>
</a>
</div>
<CodeSnippet code={badgeData.htmlSnippet} label="HTML" />
<CodeSnippet code={badgeData.markdownSnippet} label="Markdown" />
</Modal>
);
}
export default ShareBadgeModal;
|