@blocklet/payment-react
Version:
Reusable react components for payment kit v2
46 lines (45 loc) • 1.22 kB
JavaScript
import { joinURL } from "ufo";
import { getPrefix, PAYMENT_KIT_DID } from "./util.js";
export function isInPaymentKit() {
const componentId = (window.blocklet?.componentId || "").split("/").pop();
return componentId === PAYMENT_KIT_DID;
}
export function createLink(path, external = false) {
const isAbsoluteUrl = /^https?:\/\//.test(path);
const isExternal = external || isAbsoluteUrl;
const url = isExternal ? path : joinURL(getPrefix(), path);
return {
url,
external: isExternal,
path
};
}
export function getLinkProps(link, target = "_self") {
const props = {
href: link.url
};
if (link.external) {
props.target = target;
props.rel = target === "_blank" ? "noopener noreferrer" : void 0;
}
return props;
}
export function handleNavigation(e, link, navigate, options = {}) {
if (e) {
e.preventDefault();
}
const { replace = false, target = "_self" } = options;
if (link.external || target === "_blank") {
window.open(link.url, target);
return;
}
if (isInPaymentKit() && navigate) {
navigate(link.path, { replace });
return;
}
if (replace) {
window.location.replace(link.url);
} else {
window.location.href = link.url;
}
}