@privy-io/react-auth
Version:
React client for the Privy Auth API
2 lines (1 loc) • 5.34 kB
JavaScript
import{jsx as e,jsxs as o,Fragment as t}from"react/jsx-runtime";import{base64 as i}from"@scure/base";import{Edit as r}from"lucide-react";import{useState as n,useEffect as a}from"react";import{styled as s}from"styled-components";import{isHex as l,hexToString as c}from"viem";import{ProviderErrors as d}from"@privy-io/js-sdk-core";import{cQ as p,ab as m,ac as g,aK as u,by as h,cR as y,X as x,cS as f,cT as C,cU as w}from"./EmbeddedWalletConnectingScreen-D50stmJi.mjs";import{W as v,o as b}from"./events-context-ClaboIDc.mjs";import{aa as T}from"./internal-context-eGSwROSk.mjs";const S=({style:o,color:t,...i})=>/*#__PURE__*/e("svg",{xmlns:"http://www.w3.org/2000/svg",fill:"none",viewBox:"0 0 24 24",strokeWidth:"1.5",stroke:t||"currentColor",style:{height:"1.5rem",width:"1.5rem",...o},...i,children:/*#__PURE__*/e("path",{strokeLinecap:"round",strokeLinejoin:"round",d:"M4.5 12.75l6 6 9-13.5"})}),E=t=>{let[i,r]=n(!1);/*#__PURE__*/return o(N,{color:t.color,onClick:()=>{r(!0),navigator.clipboard.writeText(t.text),setTimeout((()=>r(!1)),1500)},$justCopied:i,children:[i?/*#__PURE__*/e(S,{style:{height:"14px",width:"14px"},strokeWidth:"2"}):/*#__PURE__*/e(p,{style:{height:"14px",width:"14px"}}),i?"Copied":"Copy"," ",t.itemName?t.itemName:"to Clipboard"]})};let N=/*#__PURE__*/s.button.withConfig({displayName:"StyledCopytoClipboardButton",componentId:"sc-46f4187f-0"})(["display:flex;align-items:center;gap:6px;&&{margin:8px 2px;font-size:14px;color:",";font-weight:",";transition:color 350ms ease;:focus,:active{background-color:transparent;border:none;outline:none;box-shadow:none;}:hover{color:",";}:active{color:'var(--privy-color-foreground)';font-weight:medium;}@media (max-width:440px){margin:12px 2px;}}svg{width:14px;height:14px;}"],(e=>e.$justCopied?"var(--privy-color-foreground)":e.color||"var(--privy-color-foreground-3)"),(e=>e.$justCopied?"medium":"normal"),(e=>e.$justCopied?"var(--privy-color-foreground)":"var(--privy-color-foreground-2)"));const R=/*#__PURE__*/s.img.withConfig({displayName:"StyledLogo",componentId:"sc-68c1b1ea-0"})(["&&{height:",";width:",";border-radius:16px;margin-bottom:12px;}"],(e=>"sm"===e.size?"65px":"140px"),(e=>"sm"===e.size?"65px":"140px"));let _=e=>{if(!l(e))return e;try{let o=c(e);return o.includes("�")?e:o}catch{return e}},k=e=>{try{let o=i.decode(e),t=(new TextDecoder).decode(o);return t.includes("�")?e:t}catch{return e}},D=i=>{let{types:r,primaryType:n,...a}=i.typedData;/*#__PURE__*/return o(t,{children:[/*#__PURE__*/e(A,{data:a}),/*#__PURE__*/e(E,{text:(s=i.typedData,JSON.stringify(s,null,2)),itemName:"full payload to clipboard"})," "]});var s};const j=({method:t,messageData:i,copy:n,iconUrl:a,isLoading:s,success:l,walletProxyIsLoading:c,errorMessage:d,isCancellable:p,onSign:m,onCancel:g,onClose:u})=>/*#__PURE__*/e(x,{title:n.title,subtitle:n.description,showClose:!0,onClose:u,icon:r,iconVariant:"subtle",helpText:d?/*#__PURE__*/e(I,{children:d}):void 0,primaryCta:{label:n.buttonText,onClick:m,disabled:s||l||c,loading:s},secondaryCta:p?{label:"Not now",onClick:g,disabled:s||l||c}:void 0,watermark:!0,children:/*#__PURE__*/o(f,{children:[a?/*#__PURE__*/e(R,{style:{alignSelf:"center"},size:"sm",src:a,alt:"app image"}):null,/*#__PURE__*/o(U,{children:["personal_sign"===t&&/*#__PURE__*/e(M,{children:_(i)}),"eth_signTypedData_v4"===t&&/*#__PURE__*/e(D,{typedData:i}),"solana_signMessage"===t&&/*#__PURE__*/e(M,{children:k(i)})]})]})}),L={component:()=>{let{authenticated:o}=m(),{initializeWalletProxy:t,closePrivyModal:i}=T(),{navigate:r,data:s,onUserCloseViaDialogOrKeybindRef:l}=g(),[c,p]=n(!0),[x,f]=n(""),[C,w]=n(),[S,E]=n(null),[N,R]=n(!1);a((()=>{o||r(u)}),[o]),a((()=>{t(v).then((e=>{p(!1),e||(f("An error has occurred, please try again."),w(new h(new y(x,d.E32603_DEFAULT_INTERNAL_ERROR.eipCode))))}))}),[]);let{method:_,data:k,confirmAndSign:D,onSuccess:L,onFailure:U,uiOptions:I}=s.signMessage,A={title:I?.title||"Sign message",description:I?.description||"Signing this message will not cost you any fees.",buttonText:I?.buttonText||"Sign and continue"},M=e=>{e?L(e):U(C||new h(new y("The user rejected the request.",d.E4001_USER_REJECTED_REQUEST.eipCode))),i({shouldCallAuthOnSuccess:!1}),setTimeout((()=>{E(null),f(""),w(void 0)}),200)};l.current=()=>{M(S)};/*#__PURE__*/return e(j,{method:_,messageData:k,copy:A,iconUrl:I?.iconUrl&&"string"==typeof I.iconUrl?I.iconUrl:void 0,isLoading:N,success:null!==S,walletProxyIsLoading:c,errorMessage:x,isCancellable:I?.isCancellable,onSign:async()=>{R(!0),f("");try{let e=await D();E(e),R(!1),setTimeout((()=>{M(e)}),b)}catch(e){console.error(e),f("An error has occurred, please try again."),w(new h(new y(x,d.E32603_DEFAULT_INTERNAL_ERROR.eipCode))),R(!1)}},onCancel:()=>M(null),onClose:()=>M(S)})}};let U=/*#__PURE__*/s.div.withConfig({displayName:"MessageContainer",componentId:"sc-e01d451f-0"})(["flex:1;display:flex;flex-direction:column;gap:16px;"]),I=/*#__PURE__*/s.p.withConfig({displayName:"ErrorText",componentId:"sc-e01d451f-1"})(["&&{margin:0;width:100%;text-align:center;color:var(--privy-color-error-dark);font-size:14px;line-height:22px;}"]),A=/*#__PURE__*/s(w).withConfig({displayName:"StyledDisplayJsonTree",componentId:"sc-e01d451f-2"})(["margin-top:0;"]),M=/*#__PURE__*/s(C).withConfig({displayName:"StyledMessage",componentId:"sc-e01d451f-3"})(["margin-top:0;"]);export{E as C,L as S};