@frak-labs/components
Version:
Frak Wallet components, helping any person to interact with the Frak wallet.
1 lines • 7.42 kB
JavaScript
import*as e from"preact-custom-element";import*as t from"@frak-labs/core-sdk";import*as o from"@frak-labs/core-sdk/actions";import*as n from"preact/jsx-runtime";import*as r from"class-variance-authority";import*as a from"preact/hooks";let i="frakClientReady";function l(e,t){if(window.FrakSetup?.client&&"add"===e)return void t();("add"===e?window.addEventListener:window.removeEventListener)(i,t,!1)}async function s(e){console.log("referral",await (0,o.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");let n=new CustomEvent(i);window.dispatchEvent(n),window.modalBuilderSteps=(0,o.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"),function(){if(!window.FrakSetup?.client)return console.error("Frak client not found");"vibrate"in navigator?navigator.vibrate(10):console.log("Vibration not supported"),(0,o.displayEmbeddedWallet)(window.FrakSetup.client,window.FrakSetup?.modalWalletConfig??{})}())}()}let c="spinner__leaf-xtOJmT",u=({ref:e,className:t,...o})=>(0,n.jsxs)("span",{...o,ref:e,className:(0,r.cx)("spinner-KHk8mw"),children:[(0,n.jsx)("span",{className:c}),(0,n.jsx)("span",{className:c}),(0,n.jsx)("span",{className:c}),(0,n.jsx)("span",{className:c}),(0,n.jsx)("span",{className:c}),(0,n.jsx)("span",{className:c}),(0,n.jsx)("span",{className:c}),(0,n.jsx)("span",{className:c})]});async function p({targetInteraction:e}){let n=window.FrakSetup?.client;if(!n)return void console.warn("Frak client not ready yet");let{maxReferrer:r,rewards:a}=await (0,o.getProductInformation)(n);if(!r)return;let i=(0,t.getCurrencyAmountKey)(n.config.metadata?.currency),l=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&&(l=Math.ceil(t))}return(0,t.formatAmount)(l,n.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,o]=(0,a.useState)(!1);return(0,n.jsxs)("div",{children:[(0,n.jsx)("button",{type:"button",style:k,onClick:()=>o(!t),children:"Ouvrir les informations"}),t&&(0,n.jsx)("textarea",{style:{display:"block",width:"100%",height:"200px",fontSize:"12px"},children:e})]})}function y({debugInfo:e}){let{copied:t,copy:o}=function(e={}){let{successDuration:t=2e3}=e,[o,n]=(0,a.useState)(!1);return{copy:(0,a.useCallback)(async e=>{try{if(navigator.clipboard&&window.isSecureContext)await navigator.clipboard.writeText(e),n(!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"),n(!0)}catch(e){return console.error("Failed to copy text:",e),!1}finally{t.remove()}}return setTimeout(()=>{n(!1)},t),!0}catch(e){return console.error("Failed to copy text:",e),!1}},[t]),copied:o}}();return(0,n.jsxs)("div",{style:f,children:[(0,n.jsx)("div",{style:m,children:(0,n.jsx)("h3",{style:w,children:"Oups ! Nous avons rencontr\xe9 un petit probl\xe8me"})}),(0,n.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,n.jsx)("a",{href:"mailto:help@frak-labs.com?subject=Debug",style:x,children:"help@frak-labs.com"})," ",(0,n.jsx)("br",{}),"Merci pour votre retour, nous traitons votre demande dans les plus brefs d\xe9lais."]}),(0,n.jsx)("button",{type:"button",onClick:()=>o(e??""),style:k,children:t?"Informations copi\xe9es !":"Copier les informations de d\xe9bogage"}),(0,n.jsx)(S,{debugInfo:e})]})}async function h(){if(!window.FrakSetup?.client)throw Error("Frak client not found");await (0,o.displayEmbeddedWallet)(window.FrakSetup.client,window.FrakSetup?.modalWalletConfig??{})}function b({text:e="Share and earn!",classname:o="",useReward:i,noRewardText:s,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),o=(0,a.useCallback)(()=>{t(!1)},[]);return(0,a.useEffect)(()=>(l("add",o),()=>l("remove",o)),[o]),{isClientReady:!e}}(),{reward:g}=function(e,t){let[o,n]=(0,a.useState)(void 0);return(0,a.useEffect)(()=>{e&&p({targetInteraction:t}).then(e=>{e&&n(e)})},[e,t]),{reward:o}}(f&&w,d),{handleShare:x,isError:k,debugInfo:S}=function(e){let[o,n]=(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"),n(t.DebugInfoGatherer.empty().formatDebugInfo("Frak client not found")),i(!0);return}let o=function(){if(!window.modalBuilderSteps)throw Error("modalBuilderSteps not found");return window.modalBuilderSteps}();if(!o)throw Error("modalBuilderSteps not found");try{await o.sharing(window.FrakSetup?.modalShareConfig??{}).display(t=>({...t,targetInteraction:e}))}catch(e){if(e instanceof t.FrakRpcError&&e.code===t.RpcErrorCodes.clientAborted)return void console.debug("User aborted the modal");n(window.FrakSetup.client.debugInfo.formatDebugInfo(e)),i(!0),console.error("Error while opening the modal",e)}},[e]),isError:r,debugInfo:o}}(d),b=(0,a.useMemo)(()=>f?g?e.includes("{REWARD}")?e.replace("{REWARD}",g):`${e} ${g}`:s??e.replace("{REWARD}",""):e,[f,e,s,g]),F=(0,a.useCallback)(async()=>await (m?h():x()),[m,x]);return(0,n.jsxs)(n.Fragment,{children:[(0,n.jsxs)("button",{type:"button",className:(0,r.cx)("buttonShare-F23Y9m",o,"override"),onClick:F,children:[!w&&(0,n.jsx)(u,{})," ",b]}),k&&(0,n.jsx)(y,{debugInfo:S})]})}!function(t,o,n=[],r={shadow:!1}){"undefined"!=typeof window&&("complete"===document.readyState||"interactive"===document.readyState?setTimeout(d,1):document.addEventListener?document.addEventListener("DOMContentLoaded",d):document.attachEvent("onreadystatechange",()=>{"complete"===document.readyState&&d()}),customElements.get(o)||(0,e.default)(t,o,n,r))}(b,"frak-button-share",["text"],{shadow:!1});export{b as ButtonShare};