UNPKG

getit-sdk

Version:

Getit sdk plugin. Used to provide an ad for your dapp.

183 lines 7.79 kB
import { __awaiter, __generator } from "tslib"; import { jsx as _jsx } from "react/jsx-runtime"; import { useState, useEffect } from "react"; import axios from "axios"; var EImageTypes; (function (EImageTypes) { EImageTypes["DESK"] = "DESKTOP"; EImageTypes["MOB"] = "MOBILE"; })(EImageTypes || (EImageTypes = {})); var EImageSize; (function (EImageSize) { EImageSize["DESK"] = "728"; EImageSize["MOB"] = "270"; })(EImageSize || (EImageSize = {})); var encryptApi = function (str, key) { var encrypted = ""; for (var i = 0; i < str.length; i++) { var charCode = str.charCodeAt(i); var encryptedCharCode = Math.floor((charCode + key) % 256); encrypted += String.fromCharCode(encryptedCharCode); } return encrypted; }; var getImage = function (params, isMobile) { return __awaiter(void 0, void 0, void 0, function () { var ts, api_key, walletMetamask, data; return __generator(this, function (_a) { switch (_a.label) { case 0: ts = Date.now().toString(); api_key = encryptApi(params.apiKey, 26); walletMetamask = []; if (!window.ethereum) return [3 /*break*/, 2]; return [4 /*yield*/, window.ethereum.request({ method: "eth_accounts", })]; case 1: walletMetamask = _a.sent(); _a.label = 2; case 2: return [4 /*yield*/, axios.post("https://v1.getittech.io/v1/ads/get_ad", { wallet_address: params.walletConnected ? params.walletConnected : walletMetamask[0] ? walletMetamask[0] : "", timestamp: ts, api_key: api_key, image_type: isMobile ? EImageTypes.MOB : EImageTypes.DESK, page_name: window.location.pathname, slot_id: params.slotId, })]; case 3: data = _a.sent(); return [2 /*return*/, data.data]; } }); }); }; var generateUrl = function (params, campaign_uuid, banner_uuid) { return __awaiter(void 0, void 0, void 0, function () { var ts, api_key, walletMetamask; return __generator(this, function (_a) { switch (_a.label) { case 0: ts = Date.now().toString(); api_key = encryptApi(params.apiKey, 26); walletMetamask = []; if (!window.ethereum) return [3 /*break*/, 2]; return [4 /*yield*/, window.ethereum.request({ method: "eth_accounts", })]; case 1: walletMetamask = _a.sent(); _a.label = 2; case 2: return [4 /*yield*/, axios.post("https://v1.getittech.io/v1/utm/event", { api_key: api_key, timestamp: ts, campaign_uuid: campaign_uuid, wallet_address: params.walletConnected ? params.walletConnected : walletMetamask[0] ? walletMetamask[0] : "", event_type: "CLICK", page_name: window.location.pathname, slot_id: params.slotId, banner_uuid: banner_uuid ? banner_uuid : "0000-0000-0000-0000", })]; case 3: _a.sent(); return [2 /*return*/]; } }); }); }; var OS = { win: "Win64", iPhone: "iPhone", android: "Android", }; var getUserDevice = function () { var ua = navigator.userAgent; if (ua.toLowerCase().includes(OS.iPhone.toLowerCase()) || ua.toLowerCase().includes(OS.android.toLowerCase())) { console.log(OS.iPhone); return true; } return false; }; var getCountry = function () { return __awaiter(void 0, void 0, void 0, function () { var locationData, countryIso2; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, axios.get("https://ipapi.co/json/")]; case 1: locationData = _a.sent(); countryIso2 = locationData.data.country; return [2 /*return*/, countryIso2]; } }); }); }; var GetitAdPlugin = function (props) { var _a = useState(""), useImageUrl = _a[0], setImageUrl = _a[1]; var _b = useState(""), useRedirect = _b[0], setRedirect = _b[1]; var _c = useState(""), useCampaign = _c[0], setCampaign = _c[1]; var _d = useState(""), useCampaignName = _d[0], setCampaignName = _d[1]; var _e = useState("0000-0000-0000-0000"), bannerUUID = _e[0], setBannerUUID = _e[1]; var _f = useState(""), bannerName = _f[0], setBannerName = _f[1]; var _g = useState("0"), height = _g[0], setHeight = _g[1]; useEffect(function () { var init = function () { return __awaiter(void 0, void 0, void 0, function () { var isMobile, data; return __generator(this, function (_a) { switch (_a.label) { case 0: isMobile = props.isMobile ? props.isMobile : getUserDevice(); return [4 /*yield*/, getImage(props, isMobile)]; case 1: data = _a.sent(); if (!data) { return [2 /*return*/]; } setHeight("90"); setImageUrl(data.image_url); setRedirect(data.redirect_link); setCampaign(data.campaign_uuid); setCampaignName(data.campaign_name); setBannerUUID(data.banner_uuid); setBannerName(data.banner_name); getCountry(); return [2 /*return*/]; } }); }); }; init(); }, [props.walletConnected]); return (_jsx("div", { style: { justifyContent: "center", marginTop: 0, marginBottom: 0, marginLeft: "auto", marginRight: "auto", display: "flex", height: height + "px", width: "".concat(props.isMobile ? EImageSize.MOB + "px" : EImageSize.DESK + "px"), }, children: _jsx("div", { style: { margin: 0, backgroundColor: "black", alignSelf: "center", marginLeft: "auto", marginRight: "auto", borderRadius: "10px", }, children: _jsx("a", { style: { cursor: "pointer" }, href: useRedirect + "?utm_campaign=" + useCampaignName + "&" + "utm_content=" + bannerName + "&" + "utm_source=" + "getit", target: '_blank', rel: 'noreferrer', onClick: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, generateUrl(props, useCampaign, bannerUUID)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }, children: _jsx("img", { style: { maxWidth: "100%", maxHeight: "100%", verticalAlign: "middle", borderRadius: "10px", overflowClipMargin: "content-box", overflow: "clip", }, src: useImageUrl }) }) }) })); }; export default GetitAdPlugin; //# sourceMappingURL=App.js.map