UNPKG

@frak-labs/components

Version:

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

1 lines 7.46 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"class-variance-authority";import*as a from"preact/hooks";let i="frakClientReady";function s(e,t){if(window.FrakSetup?.client&&"add"===e){t();return}("add"===e?window.addEventListener:window.removeEventListener)(i,t,!1)}async function l(e){console.log("referral",await (0,n.referralInteraction)(e,{modalConfig:window.FrakSetup?.modalWalletConfig}))}async function d(){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(i);window.dispatchEvent(e)}(),window.modalBuilderSteps=(0,n.modalBuilder)(e,window.FrakSetup?.modalConfig??{}),l(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"),function(){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 c="spinner__leaf-xtOJmT",u=({ref:e,className:t,...n})=>(0,o.jsxs)("span",{...n,ref:e,className:(0,r.cx)("spinner-KHk8mw"),children:[(0,o.jsx)("span",{className:c}),(0,o.jsx)("span",{className:c}),(0,o.jsx)("span",{className:c}),(0,o.jsx)("span",{className:c}),(0,o.jsx)("span",{className:c}),(0,o.jsx)("span",{className:c}),(0,o.jsx)("span",{className:c}),(0,o.jsx)("span",{className:c})]});async function p({targetInteraction:e}){let o=window.FrakSetup?.client;if(!o){console.warn("Frak client not ready yet");return}let{maxReferrer:r,rewards:a}=await (0,n.getProductInformation)(o);if(!r)return;let i=(0,t.getCurrencyAmountKey)(o.config.metadata?.currency),s=Math.ceil(r[i]);if(e){let t=a.filter(t=>t.interactionTypeKey===e).map(e=>e.referrer[i]).reduce((e,t)=>t>e?t:e,0);t>0&&(s=Math.ceil(t))}return(0,t.formatAmount)(s,o.config.metadata?.currency)}u.displayName="Spinner";let f={marginTop:"16px",padding:"16px",backgroundColor:"#FEE2E2",border:"1px solid #FCA5A5",borderRadius:"4px",color:"#991B1B"},m={display:"flex",alignItems:"center",gap:"8px",marginBottom:"12px"},w={margin:0,fontSize:"16px",fontWeight:500},g={fontSize:"14px",lineHeight:"1.5",margin:"0 0 12px 0"},x={color:"#991B1B",textDecoration:"underline",textUnderlineOffset:"2px"},k={display:"inline-flex",alignItems:"center",gap:"8px",marginBottom:"10px",padding:"8px 12px",backgroundColor:"white",border:"1px solid #D1D5DB",borderRadius:"4px",color:"black",fontSize:"14px",fontWeight:500};function S({debugInfo:e}){let[t,n]=(0,a.useState)(!1);return(0,o.jsxs)("div",{children:[(0,o.jsx)("button",{type:"button",style:k,onClick:()=>n(!t),children:"Ouvrir les informations"}),t&&(0,o.jsx)("textarea",{style:{display:"block",width:"100%",height:"200px",fontSize:"12px"},children:e})]})}function y({debugInfo:e}){let{copied:t,copy:n}=function(e={}){let{successDuration:t=2e3}=e,[n,o]=(0,a.useState)(!1);return{copy:(0,a.useCallback)(async e=>{try{if(navigator.clipboard&&window.isSecureContext)await navigator.clipboard.writeText(e),o(!0);else{let t=document.createElement("textarea");t.value=e,t.style.position="fixed",t.style.opacity="0",document.body.appendChild(t),t.focus(),t.select();try{document.execCommand("copy"),o(!0)}catch(e){return console.error("Failed to copy text:",e),!1}finally{t.remove()}}return setTimeout(()=>{o(!1)},t),!0}catch(e){return console.error("Failed to copy text:",e),!1}},[t]),copied:n}}();return(0,o.jsxs)("div",{style:f,children:[(0,o.jsx)("div",{style:m,children:(0,o.jsx)("h3",{style:w,children:"Oups ! Nous avons rencontr\xe9 un petit probl\xe8me"})}),(0,o.jsxs)("p",{style:g,children:["Impossible d'ouvrir le menu de partage pour le moment. Si le probl\xe8me persiste, copiez les informations ci-dessous et collez-les dans votre mail \xe0"," ",(0,o.jsx)("a",{href:"mailto:help@frak-labs.com?subject=Debug",style:x,children:"help@frak-labs.com"})," ",(0,o.jsx)("br",{}),"Merci pour votre retour, nous traitons votre demande dans les plus brefs d\xe9lais."]}),(0,o.jsx)("button",{type:"button",onClick:()=>n(e??""),style:k,children:t?"Informations copi\xe9es !":"Copier les informations de d\xe9bogage"}),(0,o.jsx)(S,{debugInfo:e})]})}async function h(){if(!window.FrakSetup?.client)throw Error("Frak client not found");await (0,n.displayEmbeddedWallet)(window.FrakSetup.client,window.FrakSetup?.modalWalletConfig??{})}function b({text:e="Share and earn!",classname:n="",useReward:i,noRewardText:l,targetInteraction:d,showWallet:c}){let f=(0,a.useMemo)(()=>void 0!==i,[i]),m=(0,a.useMemo)(()=>void 0!==c,[c]),{isClientReady:w}=function(){let[e,t]=(0,a.useState)(!0),n=(0,a.useCallback)(()=>{t(!1)},[]);return(0,a.useEffect)(()=>(s("add",n),()=>s("remove",n)),[n]),{isClientReady:!e}}(),{reward:g}=function(e,t){let[n,o]=(0,a.useState)(void 0);return(0,a.useEffect)(()=>{e&&p({targetInteraction:t}).then(e=>{e&&o(e)})},[e,t]),{reward:n}}(f&&w,d),{handleShare:x,isError:k,debugInfo:S}=function(e){let[n,o]=(0,a.useState)(void 0),[r,i]=(0,a.useState)(!1);return{handleShare:(0,a.useCallback)(async()=>{if(!window.FrakSetup?.client){console.error("Frak client not found"),o(t.DebugInfoGatherer.empty().formatDebugInfo("Frak client not found")),i(!0);return}let n=function(){if(!window.modalBuilderSteps)throw Error("modalBuilderSteps not found");return window.modalBuilderSteps}();if(!n)throw Error("modalBuilderSteps not found");try{await n.sharing(window.FrakSetup?.modalShareConfig??{}).display(t=>({...t,targetInteraction:e}))}catch(e){if(e instanceof t.FrakRpcError&&e.code===t.RpcErrorCodes.clientAborted){console.debug("User aborted the modal");return}o(window.FrakSetup.client.debugInfo.formatDebugInfo(e)),i(!0),console.error("Error while opening the modal",e)}},[e]),isError:r,debugInfo:n}}(d),b=(0,a.useMemo)(()=>f?g?e.includes("{REWARD}")?e.replace("{REWARD}",g):`${e} ${g}`:l??e.replace("{REWARD}",""):e,[f,e,l,g]),F=(0,a.useCallback)(async()=>await (m?h():x()),[m,x]);return(0,o.jsxs)(o.Fragment,{children:[(0,o.jsxs)("button",{type:"button",className:(0,r.cx)("buttonShare-F23Y9m",n,"override"),onClick:F,children:[!w&&(0,o.jsx)(u,{})," ",b]}),k&&(0,o.jsx)(y,{debugInfo:S})]})}!function(t,n,o=[],r={shadow:!1}){if("undefined"!=typeof window){var a;a=async function(){await d()},"complete"===document.readyState||"interactive"===document.readyState?setTimeout(a,1):document.addEventListener?document.addEventListener("DOMContentLoaded",a):document.attachEvent("onreadystatechange",()=>{"complete"===document.readyState&&a()}),customElements.get(n)||(0,e.default)(t,n,o,r)}}(b,"frak-button-share",["text"],{shadow:!1});export{b as ButtonShare};