UNPKG

@frak-labs/components

Version:

Frak Wallet components, helping any person to interact with the Frak wallet.

1 lines 9.86 kB
import*as e from"preact-custom-element";import*as t from"@frak-labs/core-sdk";import*as n from"@frak-labs/core-sdk/actions";import*as o from"preact/jsx-runtime";import*as r from"preact/hooks";import*as i from"class-variance-authority";import*as a from"preact";function l(){if(!window.FrakSetup?.client){console.error("Frak client not found");return}"vibrate"in navigator?navigator.vibrate(10):console.log("Vibration not supported"),(0,n.displayEmbeddedWallet)(window.FrakSetup.client,window.FrakSetup?.modalWalletConfig??{})}let u="frakClientReady";function c(e,t){if(window.FrakSetup?.client&&"add"===e){t();return}("add"===e?window.addEventListener:window.removeEventListener)(u,t,!1)}async function s(e){console.log("referral",await (0,n.referralInteraction)(e,{modalConfig:window.FrakSetup?.modalWalletConfig}))}async function _(){if(!(window.frakSetupInProgress?(console.log("[Frak SDK] Initialization already in progress"),!1):window.FrakSetup?.client?(console.log("[Frak SDK] Client already initialized"),!1):!!window.FrakSetup?.config||(console.error("[Frak SDK] Configuration not found. Please ensure window.FrakSetup.config is set."),!1)))return;if(console.log("[Frak SDK] Starting initialization"),window.frakSetupInProgress=!0,!window.FrakSetup.config){console.error("[Frak SDK] Configuration not found"),window.frakSetupInProgress=!1;return}let e=await (0,t.setupClient)({config:window.FrakSetup.config});if(!e){console.error("[Frak SDK] Failed to create client"),window.frakSetupInProgress=!1;return}window.FrakSetup.client=e,console.log("[Frak SDK] Client initialized successfully"),function(){let e=new CustomEvent(u);window.dispatchEvent(e)}(),window.modalBuilderSteps=(0,n.modalBuilder)(e,window.FrakSetup?.modalConfig??{}),s(e),window.frakSetupInProgress=!1,function(){let e=new URLSearchParams(window.location.search).get("frakAction");e&&"share"===e&&(console.log("[Frak SDK] Auto open query param found"),l())}()}async function f({targetInteraction:e}){let o=window.FrakSetup?.client;if(!o){console.warn("Frak client not ready yet");return}let{maxReferrer:r,rewards:i}=await (0,n.getProductInformation)(o);if(!r)return;let a=(0,t.getCurrencyAmountKey)(o.config.metadata?.currency),l=Math.ceil(r[a]);if(e){let t=i.filter(t=>t.interactionTypeKey===e).map(e=>e.referrer[a]).reduce((e,t)=>t>e?t:e,0);t>0&&(l=Math.ceil(t))}return(0,t.formatAmount)(l,o.config.metadata?.currency)}let p={button:"button-aPZnsb",button__left:"button__left-fQJUnK",button__right:"button__right-TkFFFK",reward:"reward-w5dm3K"};function d(e,t){for(var n in e)if("__source"!==n&&!(n in t))return!0;for(var o in t)if("__source"!==o&&e[o]!==t[o])return!0;return!1}function m(e,t){this.props=e,this.context=t}(m.prototype=new a.Component).isPureReactComponent=!0,m.prototype.shouldComponentUpdate=function(e,t){return d(this.props,e)||d(this.state,t)};var v=a.options.__b;a.options.__b=function(e){e.type&&e.type.__f&&e.ref&&(e.props.ref=e.ref,e.ref=null),v&&v(e)},"undefined"!=typeof Symbol&&Symbol.for&&Symbol.for("react.forward_ref");var h=a.options.__e;a.options.__e=function(e,t,n,o){if(e.then){for(var r,i=t;i=i.__;)if((r=i.__c)&&r.__c)return null==t.__e&&(t.__e=n.__e,t.__k=n.__k),r.__c(e,t)}h(e,t,n,o)};var w=a.options.unmount;function y(){this.__u=0,this.o=null,this.__b=null}function g(e){var t=e.__.__c;return t&&t.__a&&t.__a(e)}function k(){this.i=null,this.l=null}a.options.unmount=function(e){var t=e.__c;t&&t.__R&&t.__R(),t&&32&e.__u&&(e.type=null),w&&w(e)},(y.prototype=new a.Component).__c=function(e,t){var n=t.__c,o=this;null==o.o&&(o.o=[]),o.o.push(n);var r=g(o.__v),i=!1,a=function(){i||(i=!0,n.__R=null,r?r(l):l())};n.__R=a;var l=function(){if(!--o.__u){if(o.state.__a){var e,t=o.state.__a;o.__v.__k[0]=function e(t,n,o){return t&&o&&(t.__v=null,t.__k=t.__k&&t.__k.map(function(t){return e(t,n,o)}),t.__c&&t.__c.__P===n&&(t.__e&&o.appendChild(t.__e),t.__c.__e=!0,t.__c.__P=o)),t}(t,t.__c.__P,t.__c.__O)}for(o.setState({__a:o.__b=null});e=o.o.pop();)e.forceUpdate()}};o.__u++||32&t.__u||o.setState({__a:o.__b=o.__v.__k[0]}),e.then(a,a)},y.prototype.componentWillUnmount=function(){this.o=[]},y.prototype.render=function(e,t){if(this.__b){if(this.__v.__k){var n=document.createElement("div"),o=this.__v.__k[0].__c;this.__v.__k[0]=function e(t,n,o){return t&&(t.__c&&t.__c.__H&&(t.__c.__H.__.forEach(function(e){"function"==typeof e.__c&&e.__c()}),t.__c.__H=null),null!=(t=function(e,t){for(var n in t)e[n]=t[n];return e}({},t)).__c&&(t.__c.__P===o&&(t.__c.__P=n),t.__c=null),t.__k=t.__k&&t.__k.map(function(t){return e(t,n,o)})),t}(this.__b,n,o.__O=o.__P)}this.__b=null}var r=t.__a&&(0,a.createElement)(a.Fragment,null,e.fallback);return r&&(r.__u&=-33),[(0,a.createElement)(a.Fragment,null,t.__a?null:e.children),r]};var b=function(e,t,n){if(++n[1]===n[0]&&e.l.delete(t),e.props.revealOrder&&("t"!==e.props.revealOrder[0]||!e.l.size))for(n=e.i;n;){for(;n.length>3;)n.pop()();if(n[1]<n[0])break;e.i=n=n[2]}};(k.prototype=new a.Component).__a=function(e){var t=this,n=g(t.__v),o=t.l.get(e);return o[0]++,function(r){var i=function(){t.props.revealOrder?(o.push(r),b(t,e,o)):r()};n?n(i):i()}},k.prototype.render=function(e){this.i=null,this.l=new Map;var t=(0,a.toChildArray)(e.children);e.revealOrder&&"b"===e.revealOrder[0]&&t.reverse();for(var n=t.length;n--;)this.l.set(t[n],this.i=[1,0,this.i]);return e.children},k.prototype.componentDidUpdate=k.prototype.componentDidMount=function(){var e=this;this.l.forEach(function(t,n){b(e,n,t)})};var S="undefined"!=typeof Symbol&&Symbol.for&&Symbol.for("react.element")||60103,C=/^(?:accent|alignment|arabic|baseline|cap|clip(?!PathU)|color|dominant|fill|flood|font|glyph(?!R)|horiz|image(!S)|letter|lighting|marker(?!H|W|U)|overline|paint|pointer|shape|stop|strikethrough|stroke|text(?!L)|transform|underline|unicode|units|v|vector|vert|word|writing|x(?!C))[A-Z]/,F=/^on(Ani|Tra|Tou|BeforeInp|Compo)/,P=/[A-Z0-9]/g,E="undefined"!=typeof document;a.Component.prototype.isReactComponent={},["componentWillMount","componentWillReceiveProps","componentWillUpdate"].forEach(function(e){Object.defineProperty(a.Component.prototype,e,{configurable:!0,get:function(){return this["UNSAFE_"+e]},set:function(t){Object.defineProperty(this,e,{configurable:!0,writable:!0,value:t})}})});var x=a.options.event;function O(){}function A(){return this.cancelBubble}function K(){return this.defaultPrevented}a.options.event=function(e){return x&&(e=x(e)),e.persist=O,e.isPropagationStopped=A,e.isDefaultPrevented=K,e.nativeEvent=e};var D={enumerable:!1,configurable:!0,get:function(){return this.class}},R=a.options.vnode;a.options.vnode=function(e){"string"==typeof e.type&&function(e){var t=e.props,n=e.type,o={},r=-1===n.indexOf("-");for(var i in t){var l=t[i];if(!("value"===i&&"defaultValue"in t&&null==l||E&&"children"===i&&"noscript"===n||"class"===i||"className"===i)){var u,c=i.toLowerCase();"defaultValue"===i&&"value"in t&&null==t.value?i="value":"download"===i&&!0===l?l="":"translate"===c&&"no"===l?l=!1:"o"===c[0]&&"n"===c[1]?"ondoubleclick"===c?i="ondblclick":"onchange"!==c||"input"!==n&&"textarea"!==n||(u=t.type,("undefined"!=typeof Symbol&&"symbol"==typeof Symbol()?/fil|che|rad/:/fil|che|ra/).test(u))?"onfocus"===c?i="onfocusin":"onblur"===c?i="onfocusout":F.test(i)&&(i=c):c=i="oninput":r&&C.test(i)?i=i.replace(P,"-$&").toLowerCase():null===l&&(l=void 0),"oninput"===c&&o[i=c]&&(i="oninputCapture"),o[i]=l}}"select"==n&&o.multiple&&Array.isArray(o.value)&&(o.value=(0,a.toChildArray)(t.children).forEach(function(e){e.props.selected=-1!=o.value.indexOf(e.props.value)})),"select"==n&&null!=o.defaultValue&&(o.value=(0,a.toChildArray)(t.children).forEach(function(e){e.props.selected=o.multiple?-1!=o.defaultValue.indexOf(e.props.value):o.defaultValue==e.props.value})),t.class&&!t.className?(o.class=t.class,Object.defineProperty(o,"className",D)):(t.className&&!t.class||t.class&&t.className)&&(o.class=o.className=t.className),e.props=o}(e),e.$$typeof=S,R&&R(e)};var L=a.options.__r;a.options.__r=function(e){L&&L(e),e.__c};var j=a.options.diffed;a.options.diffed=function(e){j&&j(e);var t=e.props,n=e.__e;null!=n&&"textarea"===e.type&&"value"in t&&t.value!==n.value&&(n.value=null==t.value?"":t.value)};let U=e=>(0,o.jsx)("svg",{xmlns:"http://www.w3.org/2000/svg",width:"1em",height:"1em",fill:"none",viewBox:"0 0 28 28",...e,children:(0,o.jsx)("path",{stroke:"#fff",strokeLinecap:"round",strokeLinejoin:"round",d:"M23.143 14v11.428H4.857V14M14 25.428V8.286m0 0H8.857a2.857 2.857 0 0 1 0-5.715c4 0 5.143 5.715 5.143 5.715m0 0h5.143a2.857 2.857 0 1 0 0-5.715c-4 0-5.143 5.715-5.143 5.715m-11.429 0h22.857V14H2.571z"})});function W({classname:e="",useReward:t,targetInteraction:n}){let a=(0,r.useMemo)(()=>void 0!==t,[t]),{isClientReady:u}=function(){let[e,t]=(0,r.useState)(!0),n=(0,r.useCallback)(()=>{t(!1)},[]);return(0,r.useEffect)(()=>(c("add",n),()=>c("remove",n)),[n]),{isClientReady:!e}}(),{reward:s}=function(e,t){let[n,o]=(0,r.useState)(void 0);return(0,r.useEffect)(()=>{e&&f({targetInteraction:t}).then(e=>{e&&o(e)})},[e,t]),{reward:n}}(a&&u,n),[_,d]=(0,r.useState)("right");return(0,r.useEffect)(()=>{d(window.FrakSetup?.modalWalletConfig?.metadata?.position??"right")},[]),(0,o.jsxs)("button",{type:"button","aria-label":"Open wallet",class:(0,i.cx)(p.button,"left"===_?p.button__left:p.button__right,e,"override"),disabled:!u,onClick:l,children:[(0,o.jsx)(U,{}),s&&(0,o.jsx)("span",{className:p.reward,children:s})]})}!function(t,n,o=[],r={shadow:!1}){if("undefined"!=typeof window){var i;i=async function(){await _()},"complete"===document.readyState||"interactive"===document.readyState?setTimeout(i,1):document.addEventListener?document.addEventListener("DOMContentLoaded",i):document.attachEvent("onreadystatechange",()=>{"complete"===document.readyState&&i()}),customElements.get(n)||(0,e.default)(t,n,o,r)}}(W,"frak-button-wallet",[],{shadow:!1});export{W as ButtonWallet};