shellphone.app/app/settings/hooks/use-paddle.ts

39 lines
808 B
TypeScript
Raw Normal View History

2021-09-26 22:44:26 +00:00
import { useEffect } from "react";
import { getConfig } from "blitz";
2021-09-26 22:44:26 +00:00
declare global {
interface Window {
Paddle: any;
}
}
const { publicRuntimeConfig } = getConfig();
const vendor = parseInt(publicRuntimeConfig.paddle.vendorId, 10);
export default function usePaddle({ eventCallback }: { eventCallback: (data: any) => void }) {
useEffect(() => {
if (!window.Paddle) {
const script = document.createElement("script");
script.onload = () => {
window.Paddle.Setup({
vendor,
eventCallback(data: any) {
eventCallback(data);
},
});
};
script.src = "https://cdn.paddle.com/paddle/paddle.js";
document.head.appendChild(script);
return;
}
}, []);
if (typeof window === "undefined") {
return { Paddle: null };
}
return { Paddle: window.Paddle };
}