@stacknet/keyutils 0.1.1 → 0.1.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/index.cjs +2 -2
- package/dist/components/index.d.cts +2 -1
- package/dist/components/index.d.ts +2 -1
- package/dist/components/index.js +2 -2
- package/dist/hooks/index.cjs +1 -1
- package/dist/hooks/index.js +1 -1
- package/dist/index.cjs +2 -2
- package/dist/index.js +2 -2
- package/package.json +1 -1
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
'use strict';var at=require('react'),jsxRuntime=require('react/jsx-runtime'),clsx=require('clsx'),tailwindMerge=require('tailwind-merge'),lucideReact=require('lucide-react');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var at__default=/*#__PURE__*/_interopDefault(at);var he=at.createContext(null);function N(){let e=at.useContext(he);if(!e)throw new Error("useKeyUtilsContext must be used within a KeyUtilsProvider");return e}function ee({config:e,callbacks:t={},children:r}){typeof e.apiBaseUrl!="string"&&console.error("[KeyUtils] apiBaseUrl must be a string");let s=at.useMemo(()=>({config:e,callbacks:t}),[e,t]);return jsxRuntime.jsx(he.Provider,{value:s,children:r})}function m(...e){return tailwindMerge.twMerge(clsx.clsx(e))}function L(e){return e==null?"0":e>=1e9?`${(e/1e9).toFixed(1)}B`:e>=1e6?`${(e/1e6).toFixed(1)}M`:e>=1e3?`${(e/1e3).toFixed(1)}K`:e.toLocaleString()}function Ne(e,t){return t<=0?0:Math.min(100,Math.round(e/t*100))}function ve(e){return e>=70?"bg-green-500":e>=30?"bg-yellow-500":"bg-red-500"}function F(e){let t=typeof e=="string"&&/^\d+$/.test(e)?Number(e):e,r=new Date(t);return isNaN(r.getTime())?String(e):r.toLocaleDateString("en-US",{month:"short",day:"numeric",year:"numeric"})}function ke(){if(typeof navigator>"u")return "mac";let e=navigator.userAgent.toLowerCase();return /iphone|ipad|ipod/.test(e)?"ios":/android/.test(e)?"android":/win/.test(e)?"windows":/linux/.test(e)?"linux":"mac"}function Pe(){let{config:e}=N(),[t,r]=at.useState(null),[s,g]=at.useState(true),[c,f]=at.useState(null),a=at.useRef(true),n=at.useRef(e.apiBaseUrl);n.current=e.apiBaseUrl;let d=async()=>{try{a.current&&(g(!0),f(null));let p=await fetch(`${n.current}/api/keys/pricing`);if(!p.ok){let i=`Pricing unavailable (${p.status})`;try{let o=await p.json();o.error&&(i=o.error);}catch{}throw new Error(i)}let u=await p.json();a.current&&r(u);}catch(p){a.current&&f(p instanceof Error?p.message:"Failed to fetch pricing");}finally{a.current&&g(false);}};return at.useEffect(()=>{a.current=true,d();let p=setInterval(d,6e4);return ()=>{a.current=false,clearInterval(p);}},[]),{pricing:t,loading:s,error:c,refresh:d}}function Ce(){let{config:e,callbacks:t}=N(),[r,s]=at.useState(false),[g,c]=at.useState(null),[f,a]=at.useState(null);return {mint:at.useCallback(async(d,p,u=1)=>{try{s(!0),a(null),t.onPaymentComplete?.(d,p);let i=await fetch(`${e.apiBaseUrl}/api/keys/mint`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({paymentMethod:d,paymentRef:p,quantity:u})});if(!i.ok){let P="Key generation failed";try{let w=await i.json();w.error&&(P=w.error);}catch{}throw new Error(P)}let o=await i.json(),l=o.keys?.[0]||o,v={success:!0,key:{id:l.keyId||l.id||"",key:l.key||"",userId:l.userId||"",status:"active",tokenBalance:l.tokenBalance??l.currentTokenBalance??0,maxTokens:l.initialTokenBalance??l.tokenBalance??0,createdAt:String(l.createdAt||Date.now())},transactionId:p};return c(v),t.onMintSuccess?.(v),v}catch(i){let o=i instanceof Error?i:new Error("Key generation failed");return a(o.message),t.onMintError?.(o),{success:false,error:o.message}}finally{s(false);}},[e.apiBaseUrl,t]),minting:r,result:g,error:f}}function Ke(){let{config:e}=N(),[t,r]=at.useState(false),[s,g]=at.useState(null),c=at.useRef(true);return {createSession:at.useCallback(async(a,n)=>{try{r(!0),g(null);let d=await fetch(`${e.apiBaseUrl}/api/keys/stripe-session`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({priceCents:a,quantity:n,stackId:e.stackId})});if(!d.ok){let u=`Checkout failed (${d.status})`;try{let i=await d.json();i.error&&(u=i.error);}catch{}throw new Error(u)}let{url:p}=await d.json();if(p){try{sessionStorage.setItem("keyutils_stripe_pending","1");}catch{}window.location.href=p;}else throw new Error("No checkout URL returned")}catch(d){if(c.current){let p=d instanceof Error?d.message:"Checkout failed";g(p),r(false);}}},[e.apiBaseUrl,e.stackId]),loading:t,error:s}}var Ze=/^[1-9A-HJ-NP-Za-km-z]{32,44}$/;function oe({amountSol:e,onSuccess:t,onError:r,disabled:s,className:g,style:c}){let{config:f}=N(),[a,n]=at.useState(false),[d,p]=at.useState(null),u=!!(f.merchantWallet&&Ze.test(f.merchantWallet)),i=at.useCallback(async()=>{if(u)try{n(!0),p(null);let{Connection:o,PublicKey:l,Transaction:v,SystemProgram:P,LAMPORTS_PER_SOL:w}=await import('@solana/web3.js'),R=typeof window<"u"?window.solana:void 0;if(!R||typeof R!="object"||!R.isPhantom)throw new Error("Phantom wallet not found. Please install Phantom.");let B=R,M;try{M=new l(f.merchantWallet);}catch{throw new Error("Invalid merchant wallet address.")}let _=await B.connect(),k=new l(_.publicKey.toString()),W=new o(f.solanaRpcUrl||"https://api.mainnet-beta.solana.com"),{blockhash:A}=await W.getLatestBlockhash(),Z=BigInt(Math.floor(e*1e9)),X=new v({recentBlockhash:A,feePayer:k}).add(P.transfer({fromPubkey:k,toPubkey:M,lamports:Number(Z)})),{signature:K}=await B.signAndSendTransaction(X);t(K);}catch(o){let l=o instanceof Error?o:new Error("Payment failed");p(l.message),r?.(l);}finally{n(false);}},[e,f.merchantWallet,u,t,r]);return jsxRuntime.jsxs("div",{children:[jsxRuntime.jsx("button",{onClick:i,disabled:s||a||!u,"aria-label":`Pay ${e.toFixed(4)} SOL`,className:m("flex w-full items-center justify-center gap-2 rounded-lg bg-purple-600 px-4 py-3 font-medium text-white transition-colors hover:bg-purple-700 disabled:opacity-50 disabled:cursor-not-allowed",g),style:c,children:a?jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[jsxRuntime.jsx(lucideReact.Loader2,{className:"h-4 w-4 animate-spin"}),"Processing..."]}):jsxRuntime.jsxs(jsxRuntime.Fragment,{children:["Pay ",e.toFixed(4)," SOL"]})}),d&&jsxRuntime.jsx("p",{className:"mt-2 text-center text-xs text-red-500",children:d})]})}function ae({result:e,className:t,style:r}){let[s,g]=at.useState(false),c=at.useRef(null);at.useEffect(()=>()=>{c.current&&clearTimeout(c.current);},[]);let f=at.useCallback(()=>{e.key?.key&&(navigator.clipboard.writeText(e.key.key),g(true),c.current&&clearTimeout(c.current),c.current=setTimeout(()=>g(false),2e3));},[e.key?.key]);return !e.success||!e.key?null:jsxRuntime.jsxs("div",{className:m("rounded-lg border border-green-500/20 bg-green-500/10 p-6 text-center",t),style:r,children:[jsxRuntime.jsx(lucideReact.CheckCircle,{className:"mx-auto mb-3 h-12 w-12 text-green-500"}),jsxRuntime.jsx("h3",{className:"mb-2 text-lg font-semibold text-foreground",children:"Key Generated!"}),jsxRuntime.jsx("p",{className:"mb-4 text-sm text-muted-foreground",children:"Your node key has been successfully generated."}),jsxRuntime.jsxs("div",{className:"mx-auto flex max-w-md items-center gap-2 rounded-md border bg-background p-3",children:[jsxRuntime.jsx("code",{className:"flex-1 truncate text-xs text-foreground",children:e.key.key}),jsxRuntime.jsx("button",{onClick:f,"aria-label":"Copy key to clipboard",className:"shrink-0 rounded-md p-1.5 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground",children:s?jsxRuntime.jsx(lucideReact.Check,{className:"h-4 w-4 text-green-500"}):jsxRuntime.jsx(lucideReact.Copy,{className:"h-4 w-4"})})]})]})}function I({className:e}){return jsxRuntime.jsx("div",{className:m("animate-pulse rounded-xl bg-muted",e)})}function ie(){return jsxRuntime.jsxs("div",{className:"space-y-6",children:[jsxRuntime.jsxs("div",{className:"px-6 py-10 flex flex-col items-center gap-3",children:[jsxRuntime.jsx(I,{className:"h-14 w-48"}),jsxRuntime.jsx(I,{className:"h-5 w-64"})]}),jsxRuntime.jsx(I,{className:"h-[72px] w-full rounded-2xl"}),jsxRuntime.jsx(I,{className:"h-[68px] w-full rounded-2xl"}),jsxRuntime.jsx(I,{className:"h-[60px] w-full rounded-2xl"})]})}function Y({count:e=3}){return jsxRuntime.jsx("div",{className:"space-y-3",children:Array.from({length:e}).map((t,r)=>jsxRuntime.jsxs("div",{className:"rounded-xl border p-4 space-y-3",children:[jsxRuntime.jsxs("div",{className:"flex items-center justify-between",children:[jsxRuntime.jsx(I,{className:"h-4 w-32"}),jsxRuntime.jsx(I,{className:"h-4 w-16"})]}),jsxRuntime.jsx(I,{className:"h-2 w-full rounded-full"}),jsxRuntime.jsxs("div",{className:"flex justify-between",children:[jsxRuntime.jsx(I,{className:"h-3 w-24"}),jsxRuntime.jsx(I,{className:"h-3 w-20"})]})]},r))})}function pt({quantity:e,max:t,onChange:r}){return jsxRuntime.jsxs("div",{className:"flex items-center justify-between rounded-2xl border bg-muted/20 px-6 py-5",children:[jsxRuntime.jsx("span",{className:"text-sm font-medium text-foreground",children:"Quantity"}),jsxRuntime.jsxs("div",{className:"flex items-center gap-0 rounded-xl border bg-background",children:[jsxRuntime.jsx("button",{onClick:()=>r(Math.max(1,e-1)),disabled:e<=1,"aria-label":"Decrease quantity",className:"flex h-12 w-12 items-center justify-center rounded-l-xl text-muted-foreground transition-colors hover:bg-muted hover:text-foreground disabled:opacity-30",children:jsxRuntime.jsx(lucideReact.Minus,{className:"h-5 w-5"})}),jsxRuntime.jsx("span",{className:"flex h-12 w-14 items-center justify-center border-x text-xl font-bold text-foreground","aria-live":"polite",children:e}),jsxRuntime.jsx("button",{onClick:()=>r(Math.min(t,e+1)),disabled:e>=t,"aria-label":"Increase quantity",className:"flex h-12 w-12 items-center justify-center rounded-r-xl text-muted-foreground transition-colors hover:bg-muted hover:text-foreground disabled:opacity-30",children:jsxRuntime.jsx(lucideReact.Plus,{className:"h-5 w-5"})})]})]})}function Te({pricing:e,quantity:t}){let[r,s]=at.useState(false),g=e.priceUsd*t;return jsxRuntime.jsxs("div",{className:"rounded-2xl border bg-background",children:[jsxRuntime.jsxs("button",{onClick:()=>s(!r),"aria-expanded":r,className:"flex w-full items-center justify-between px-6 py-5 text-left",children:[jsxRuntime.jsxs("span",{className:"text-foreground",children:[jsxRuntime.jsxs("span",{className:"text-xl font-bold",children:["$",g.toFixed(2)]})," ",jsxRuntime.jsx("span",{className:"text-sm text-muted-foreground",children:"total fees included"})]}),r?jsxRuntime.jsx(lucideReact.ChevronUp,{className:"h-5 w-5 text-muted-foreground"}):jsxRuntime.jsx(lucideReact.ChevronDown,{className:"h-5 w-5 text-muted-foreground"})]}),r&&jsxRuntime.jsxs("div",{className:"border-t px-6 pb-8 pt-4 space-y-3 text-sm",children:[jsxRuntime.jsxs("div",{className:"flex justify-between",children:[jsxRuntime.jsxs("span",{className:"text-muted-foreground",children:["Node Key \xD7 ",t]}),jsxRuntime.jsxs("span",{className:"text-foreground",children:["$",(e.priceUsd*t).toFixed(2)]})]}),jsxRuntime.jsxs("div",{className:"flex justify-between",children:[jsxRuntime.jsx("span",{className:"text-muted-foreground",children:"Keys sold"}),jsxRuntime.jsx("span",{className:"text-foreground",children:e.keysSold})]}),e.daysUntilHalving>0&&jsxRuntime.jsxs("div",{className:"flex justify-between text-muted-foreground",children:[jsxRuntime.jsx("span",{children:"Next refresh"}),jsxRuntime.jsxs("span",{children:[e.daysUntilHalving," day",e.daysUntilHalving!==1?"s":""]})]}),jsxRuntime.jsxs("div",{className:"flex justify-between border-t pt-3 font-semibold",children:[jsxRuntime.jsx("span",{className:"text-foreground",children:"Total"}),jsxRuntime.jsxs("span",{className:"text-foreground",children:["$",g.toFixed(2)]})]})]})]})}function le({className:e,style:t}){let{config:r}=N(),{pricing:s,loading:g,error:c,refresh:f}=Pe(),{mint:a,minting:n,result:d,error:p}=Ce(),{createSession:u,loading:i,error:o}=Ke(),[l,v]=at.useState(1),[P,w]=at.useState(r.paymentMethods?.includes("stripe")?"stripe":"solana"),[R,B]=at.useState(0),[M,_]=at.useState(false),k=at__default.default.useRef(l);k.current=l;let W=Math.max(1,r.maxQuantity||5),A=[...r.paymentMethods||["solana"]].sort((K,G)=>K==="stripe"?-1:G==="stripe"?1:0);if(at.useEffect(()=>{if(typeof window>"u")return;let G=new URLSearchParams(window.location.search).get("session_id");if(G&&/^cs_(test|live)_/.test(G)&&!M&&!d?.success){let ye=false;try{ye=sessionStorage.getItem("keyutils_stripe_pending")==="1";}catch{}if(!ye)return;try{sessionStorage.removeItem("keyutils_stripe_pending");}catch{}_(true);let be=new URL(window.location.href);be.searchParams.delete("session_id"),window.history.replaceState({},"",be.toString()),a("stripe",G,k.current);}},[M,d,a]),d?.success)return jsxRuntime.jsx("div",{className:m("flex flex-col items-center justify-center",e),style:t,children:jsxRuntime.jsx(ae,{result:d})});if(M||n)return jsxRuntime.jsx("div",{className:m("flex flex-col",e),style:t,children:jsxRuntime.jsx(ie,{})});if(g)return jsxRuntime.jsx("div",{className:m("flex flex-col",e),style:t,children:jsxRuntime.jsx(ie,{})});if(c||!s)return jsxRuntime.jsxs("div",{className:m("flex flex-col items-center justify-center gap-4 py-24",e),style:t,children:[jsxRuntime.jsxs("div",{className:"rounded-xl border border-red-500/20 bg-red-500/5 px-6 py-5 text-center max-w-sm",children:[jsxRuntime.jsx("p",{className:"text-sm font-medium text-foreground",children:"Unable to load pricing"}),jsxRuntime.jsx("p",{className:"mt-1 text-xs text-muted-foreground",children:c||"Please check your connection and try again."})]}),jsxRuntime.jsx("button",{onClick:()=>f(),className:"text-sm text-muted-foreground hover:text-foreground transition-colors",children:"Try again"})]});let Z=async K=>{B(2),await a(P,K,l);},X=()=>{u(s.priceCents,l);};return jsxRuntime.jsx("div",{className:m("flex flex-col",e),style:t,children:jsxRuntime.jsxs("div",{className:"space-y-6",children:[R===0&&jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[r.keyImage&&jsxRuntime.jsx("img",{src:r.keyImage,alt:"Node Key",className:r.keyImageClass||"w-1/2 mx-auto"}),jsxRuntime.jsxs("div",{className:"px-6 py-10 text-center",children:[jsxRuntime.jsxs("p",{className:"text-5xl font-bold tracking-tight text-foreground md:text-6xl",children:["$",s.priceUsd.toFixed(2)]}),jsxRuntime.jsxs("p",{className:"mt-2 text-base text-muted-foreground",children:["per key \xB7 ",s.tokenAllocationFormatted||L(s.tokenAllocation)," tokens included"]})]}),jsxRuntime.jsx(pt,{quantity:l,max:W,onChange:v}),jsxRuntime.jsx(Te,{pricing:s,quantity:l}),jsxRuntime.jsx("button",{onClick:()=>B(1),className:"w-full rounded-2xl bg-foreground py-5 text-center text-base font-semibold text-background transition-colors hover:bg-foreground/90",children:"Continue to Payment"})]}),R===1&&jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[A.length>1&&jsxRuntime.jsx("div",{role:"tablist",className:"flex gap-1 rounded-2xl bg-muted/50 p-1.5",children:A.map(K=>jsxRuntime.jsxs("button",{role:"tab","aria-selected":P===K,onClick:()=>w(K),className:m("flex flex-1 items-center justify-center gap-2.5 rounded-xl py-4 text-sm font-medium transition-colors",P===K?"bg-background text-foreground shadow-sm":"text-muted-foreground hover:text-foreground"),children:[K==="stripe"?jsxRuntime.jsx(lucideReact.CreditCard,{className:"h-5 w-5"}):jsxRuntime.jsx(lucideReact.Wallet,{className:"h-5 w-5"}),K==="stripe"?"Credit Card":"Crypto"]},K))}),jsxRuntime.jsx(Te,{pricing:s,quantity:l}),P==="stripe"?jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[jsxRuntime.jsx("button",{onClick:X,disabled:i,className:"flex w-full items-center justify-center gap-2 rounded-2xl bg-foreground py-5 text-base font-semibold text-background transition-colors hover:bg-foreground/90 disabled:opacity-50",children:i?jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[jsxRuntime.jsx(lucideReact.Loader2,{className:"h-5 w-5 animate-spin"}),"Redirecting to Stripe..."]}):jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[jsxRuntime.jsx(lucideReact.CreditCard,{className:"h-5 w-5"}),"Pay with Card"]})}),o&&jsxRuntime.jsx("p",{className:"text-center text-sm text-red-500",children:o})]}):P==="solana"?jsxRuntime.jsx(oe,{amountSol:s.priceSol??0,onSuccess:Z,className:"rounded-2xl py-5 text-base"}):null,p&&jsxRuntime.jsx("p",{className:"text-center text-sm text-red-500",children:p}),jsxRuntime.jsx("button",{onClick:()=>B(0),className:"w-full py-3 text-center text-sm text-muted-foreground hover:text-foreground transition-colors",children:"\u2190 Back to Configure"})]})]})})}var bt={ios:"iOS",android:"Android",windows:"Windows",mac:"macOS",linux:"Linux"},ht=["mac","windows","linux","ios","android"];function ce({downloads:e,className:t,style:r}){let{config:s}=N(),g=at.useMemo(()=>ke(),[]),c=e||ht.map(n=>({platform:n,label:bt[n],url:"#",compatible:n===g})),f=c.find(n=>n.compatible)||c[0],a=c.filter(n=>n.platform!==f.platform);return jsxRuntime.jsxs("div",{className:m("space-y-8",t),style:r,children:[jsxRuntime.jsxs("div",{className:"flex flex-col items-center gap-6 pt-4 mt-4",children:[s.aispImage&&jsxRuntime.jsx("img",{src:s.aispImage,alt:"aiSP",className:"w-full sm:w-1/2 mx-auto px-4 sm:px-0"}),jsxRuntime.jsxs("div",{className:"text-center",children:[jsxRuntime.jsx("p",{className:"text-lg sm:text-2xl font-medium text-foreground",children:"Download the AI Service Provider App"}),jsxRuntime.jsx("p",{className:"mt-1 text-sm sm:text-base text-muted-foreground",children:"Load your keys and run your node."})]}),jsxRuntime.jsxs("a",{href:f.url,className:"inline-flex items-center gap-3 rounded-full px-10 py-4 text-lg font-semibold text-white transition-colors hover:opacity-90",style:{backgroundColor:"#0900f4"},children:[jsxRuntime.jsx(lucideReact.Download,{className:"h-5 w-5"}),"Get aiSP for ",f.label]})]}),jsxRuntime.jsxs("p",{className:"text-center text-sm text-muted-foreground",children:["aiSP is also available on"," ",a.map((n,d)=>jsxRuntime.jsxs(at__default.default.Fragment,{children:[d>0&&(d===a.length-1?" and ":", "),jsxRuntime.jsx("a",{href:n.url,className:"font-medium underline transition-colors hover:text-foreground",style:{color:"#066bff"},children:n.label})]},n.platform)),"."]}),jsxRuntime.jsxs("div",{className:"flex flex-col items-center gap-2 mt-12",children:[jsxRuntime.jsxs("p",{className:"text-center text-xs text-muted-foreground flex justify-center gap-0.5 flex-wrap",children:["Source code is available on",jsxRuntime.jsx("a",{href:"https://github.com/stack-net",target:"_blank",rel:"noopener noreferrer",className:"text-foreground underline hover:text-foreground/80 color-[#0900f4]",children:"Github"})]}),jsxRuntime.jsxs("p",{className:"text-center text-xs text-muted-foreground flex justify-center gap-0.5 flex-wrap",children:["By downloading, you agree to the",jsxRuntime.jsx("a",{href:s.termsUrl||"/terms",className:"text-foreground underline hover:text-foreground/80",children:"Open Source Applications Terms"})]})]})]})}function wt(e){return {id:e.id||"",key:e.key||e.keyPrefix||"",userId:e.userId||e.user_id||"",status:e.status||"active",tokenBalance:e.tokenBalance??e.currentTokenBalance??e.current_token_balance??0,maxTokens:e.maxTokens??e.initialTokenBalance??e.initial_token_balance??0,createdAt:String(e.createdAt??e.created_at??""),expiresAt:e.expiresAt,label:e.label||e.keyPrefix||e.key_prefix||void 0,paperWork:e.paperWork||e.paper_work||void 0,stackId:e.stackId,stackName:e.stackName}}function J(){let{config:e}=N(),[t,r]=at.useState([]),[s,g]=at.useState(true),[c,f]=at.useState(null),a=at.useRef(true),n=e.apiBaseUrl,d=at.useCallback(async()=>{try{g(!0),f(null);let u=await fetch(`${n}/api/keys`);if(!u.ok){let l=`Failed to fetch keys (${u.status})`;try{let v=await u.json();v.error&&(l=v.error);}catch{}throw new Error(l)}let i=await u.json(),o=Array.isArray(i)?i:i.keys??[];a.current&&r(o.map(wt));}catch(u){a.current&&f(u instanceof Error?u.message:"Failed to fetch keys");}finally{a.current&&g(false);}},[n]);at.useEffect(()=>(a.current=true,d(),()=>{a.current=false;}),[d]);let p=t.reduce((u,i)=>u+i.tokenBalance,0);return {keys:t,totalBalance:p,loading:s,error:c,refresh:d}}function Ie(){let{config:e,callbacks:t}=N(),[r,s]=at.useState(false),[g,c]=at.useState(null),[f,a]=at.useState(null),n=at.useRef(true),d=at.useCallback(async(u,i)=>{try{s(!0),a(null);let o=await fetch(`${e.apiBaseUrl}/api/keys/${u}/list`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({askPriceCents:i,stackId:e.stackId,stackName:e.stackName})});if(!o.ok){let v=`Listing failed (${o.status})`;try{let P=await o.json();P.error&&(v=P.error);}catch{}throw new Error(v)}let l=await o.json();return n.current&&(c(l),t.onListingCreated?.(l)),l}catch(o){let l=o instanceof Error?o.message:"Listing failed";return n.current&&a(l),null}finally{n.current&&s(false);}},[e.apiBaseUrl,e.stackId,e.stackName,t]),p=at.useCallback(async u=>{try{s(!0),a(null);let i=await fetch(`${e.apiBaseUrl}/api/keys/${u}/list`,{method:"DELETE"});if(!i.ok){let o=`Delist failed (${i.status})`;try{let l=await i.json();l.error&&(o=l.error);}catch{}throw new Error(o)}return n.current&&c(null),!0}catch(i){let o=i instanceof Error?i.message:"Delist failed";return n.current&&a(o),false}finally{n.current&&s(false);}},[e.apiBaseUrl]);return {listKey:d,delistKey:p,listing:r,result:g,error:f}}function z({current:e,max:t,className:r,style:s,showLabel:g=true}){let c=Ne(e,t),f=ve(c);return jsxRuntime.jsxs("div",{className:m("w-full",r),style:s,children:[g&&jsxRuntime.jsxs("div",{className:"mb-1 flex items-center justify-between text-xs text-muted-foreground",children:[jsxRuntime.jsxs("span",{children:[L(e)," / ",L(t)]}),jsxRuntime.jsxs("span",{children:[c,"%"]})]}),jsxRuntime.jsx("div",{className:"h-2 w-full overflow-hidden rounded-full bg-muted",children:jsxRuntime.jsx("div",{className:m("h-full rounded-full transition-all duration-500",f),style:{width:`${c}%`}})})]})}function Ut({nodeKey:e,selected:t,onSelect:r}){return jsxRuntime.jsxs("button",{onClick:r,className:m("flex w-full items-start gap-3 rounded-xl border p-4 text-left transition-all",t?"border-foreground/30 bg-foreground/5 ring-1 ring-foreground/10":"border-border hover:border-foreground/20"),children:[jsxRuntime.jsx("div",{className:m("mt-0.5 flex h-5 w-5 shrink-0 items-center justify-center rounded-full border-2 transition-colors",t?"border-foreground bg-foreground":"border-muted-foreground/40"),children:t&&jsxRuntime.jsx(lucideReact.Check,{className:"h-3 w-3 text-background"})}),jsxRuntime.jsxs("div",{className:"min-w-0 flex-1",children:[jsxRuntime.jsxs("div",{className:"flex items-center justify-between",children:[jsxRuntime.jsx("span",{className:"text-sm font-medium text-foreground",children:e.label||e.id.slice(0,16)}),jsxRuntime.jsx("span",{className:m("text-xs font-medium capitalize",e.status==="active"?"text-green-500":"text-muted-foreground"),children:e.status})]}),jsxRuntime.jsx(z,{current:e.tokenBalance,max:e.maxTokens,className:"mt-2"})]})]})}function fe({className:e,style:t}){let{config:r}=N(),{keys:s,loading:g,error:c,refresh:f}=J(),{listKey:a,listing:n,result:d,error:p}=Ie(),[u,i]=at.useState(null),[o,l]=at.useState(""),[v,P]=at.useState(false),w=s.find(k=>k.id===u),R=parseInt(o.replace(/[^0-9]/g,""),10)||0,B=w&&R>0&&v&&!n,M=async()=>{if(!B||!w)return;let k=R*100;await a(w.id,k)&&f();};if(d)return jsxRuntime.jsxs("div",{className:m("flex flex-col items-center gap-4 rounded-xl border border-green-500/20 bg-green-500/10 py-12",e),style:t,children:[jsxRuntime.jsx(lucideReact.CheckCircle,{className:"h-12 w-12 text-green-500"}),jsxRuntime.jsx("h3",{className:"text-lg font-semibold text-foreground",children:"Key Listed!"}),jsxRuntime.jsx("p",{className:"text-sm text-muted-foreground",children:"Your key is now on the Global Open AI Network marketplace."}),jsxRuntime.jsxs("p",{className:"text-xs text-muted-foreground",children:["Listing ID: ",jsxRuntime.jsx("code",{children:d.listingId})]})]});if(g)return jsxRuntime.jsxs("div",{className:m("space-y-5 px-1",e),style:t,children:[jsxRuntime.jsx("h3",{className:"text-2xl mt-3 mb-2 font-semibold text-foreground",children:"Your Keys"}),jsxRuntime.jsx(Y,{count:2})]});if(c)return jsxRuntime.jsxs("div",{className:m("flex flex-col items-center justify-center gap-4 py-16",e),style:t,children:[jsxRuntime.jsxs("div",{className:"rounded-xl border border-red-500/20 bg-red-500/5 px-6 py-5 text-center max-w-sm",children:[jsxRuntime.jsx("p",{className:"text-sm font-medium text-foreground",children:"Unable to load your keys"}),jsxRuntime.jsx("p",{className:"mt-1 text-xs text-muted-foreground",children:c})]}),jsxRuntime.jsx("button",{onClick:()=>f(),className:"text-sm text-muted-foreground hover:text-foreground transition-colors",children:"Try again"})]});let _=s.filter(k=>k.status==="active");return _.length===0?jsxRuntime.jsxs("div",{className:m("flex flex-col items-center justify-center gap-3 rounded-xl border border-dashed py-16",e),style:t,children:[jsxRuntime.jsx(lucideReact.AlertTriangle,{className:"h-8 w-8 text-muted-foreground"}),jsxRuntime.jsx("p",{className:"text-sm text-muted-foreground",children:"No active keys to sell."})]}):jsxRuntime.jsxs("div",{className:m("space-y-5",e),style:t,children:[jsxRuntime.jsxs("div",{children:[jsxRuntime.jsx("h3",{className:"text-2xl mt-3 mb-2 font-semibold text-foreground",children:"Your Keys"}),jsxRuntime.jsx("div",{className:"space-y-2",children:_.map(k=>jsxRuntime.jsx(Ut,{nodeKey:k,selected:u===k.id,onSelect:()=>i(k.id)},k.id))})]}),w&&jsxRuntime.jsxs("div",{className:"rounded-xl border bg-muted/30 p-5 space-y-2 text-sm",children:[jsxRuntime.jsxs("div",{className:"flex justify-between",children:[jsxRuntime.jsx("span",{className:"text-muted-foreground",children:"Key ID"}),jsxRuntime.jsx("code",{className:"text-xs text-foreground",children:w.id})]}),jsxRuntime.jsxs("div",{className:"flex justify-between",children:[jsxRuntime.jsx("span",{className:"text-muted-foreground",children:"Originating Stack"}),jsxRuntime.jsx("span",{className:"text-foreground",children:r.stackName||"Unknown"})]}),jsxRuntime.jsxs("div",{className:"flex justify-between",children:[jsxRuntime.jsx("span",{className:"text-muted-foreground",children:"Tokens (remaining / total)"}),jsxRuntime.jsxs("span",{className:"text-foreground",children:[L(w.tokenBalance)," / ",L(w.maxTokens)]})]}),w.paperWork&&jsxRuntime.jsxs("div",{className:"flex justify-between",children:[jsxRuntime.jsx("span",{className:"text-muted-foreground",children:"Paper earned"}),jsxRuntime.jsx("span",{className:"text-foreground",children:w.paperWork})]}),jsxRuntime.jsxs("div",{className:"flex justify-between",children:[jsxRuntime.jsx("span",{className:"text-muted-foreground",children:"Created"}),jsxRuntime.jsx("span",{className:"text-foreground",children:F(w.createdAt)})]})]}),jsxRuntime.jsxs("div",{children:[jsxRuntime.jsx("label",{htmlFor:"listing-price",className:"mb-2 block text-sm font-semibold text-foreground",children:"Listing Price (USD)"}),jsxRuntime.jsxs("div",{className:"relative",children:[jsxRuntime.jsx("span",{className:"absolute left-4 top-1/2 -translate-y-1/2 text-2xl font-bold text-muted-foreground",children:"$"}),jsxRuntime.jsx("input",{id:"listing-price",type:"text",inputMode:"numeric",value:o,onChange:k=>{let W=k.target.value.replace(/[^0-9]/g,""),A=parseInt(W,10);l(isNaN(A)?"":A.toLocaleString("en-US"));},placeholder:"0",className:"w-full rounded-xl border bg-background py-5 pl-10 pr-4 text-3xl font-bold text-foreground placeholder:text-muted-foreground/30 focus:outline-none focus:ring-2 focus:ring-foreground/20"})]})]}),jsxRuntime.jsxs("label",{className:"flex items-start gap-3 cursor-pointer",children:[jsxRuntime.jsx("input",{type:"checkbox",checked:v,onChange:k=>P(k.target.checked),className:"sr-only"}),jsxRuntime.jsx("div",{"aria-hidden":"true",className:m("mt-0.5 flex h-5 w-5 shrink-0 items-center justify-center rounded border-2 transition-colors",v?"border-foreground bg-foreground":"border-muted-foreground/40"),children:v&&jsxRuntime.jsx(lucideReact.Check,{className:"h-3 w-3 text-background"})}),jsxRuntime.jsxs("span",{className:"text-xs text-muted-foreground leading-relaxed",children:["I agree to list this key on the Global Open AI Network marketplace. The key will be deactivated upon sale and transferred to the buyer. Listing fees may apply."," ",jsxRuntime.jsx("a",{href:r.termsUrl||"/terms",className:"text-foreground underline",children:"Terms"})]})]}),p&&jsxRuntime.jsx("p",{className:"text-sm text-red-500",children:p}),jsxRuntime.jsxs("button",{onClick:M,disabled:!B,className:m("flex w-full items-center justify-center gap-2 rounded-xl py-4 text-sm font-semibold transition-colors",B?"bg-foreground text-background hover:bg-foreground/90":"bg-muted text-muted-foreground cursor-not-allowed"),children:[jsxRuntime.jsx(lucideReact.ExternalLink,{className:"h-4 w-4"}),n?"Listing...":"List on Marketplace"]})]})}var Bt=[{id:"buy",label:"Buy"},{id:"use",label:"Use"},{id:"sell",label:"Sell"}];function Lt({active:e,onChange:t}){return jsxRuntime.jsx("div",{role:"tablist","aria-label":"Key actions",className:"flex gap-1 rounded-2xl bg-muted/50 p-1.5",children:Bt.map(({id:r,label:s})=>jsxRuntime.jsx("button",{role:"tab","aria-selected":e===r,"aria-controls":`panel-${r}`,id:`tab-${r}`,onClick:()=>t(r),className:m("flex flex-1 items-center justify-center gap-2.5 rounded-xl py-3.5 text-sm font-medium transition-all",e===r?"bg-background text-foreground shadow-sm":"text-muted-foreground hover:text-foreground"),children:jsxRuntime.jsx("span",{className:"inline",children:s})},r))})}function Tt({defaultTab:e="buy",className:t,style:r}){let[s,g]=at.useState(e);return jsxRuntime.jsxs("div",{className:m("flex flex-col",t),style:r,children:[jsxRuntime.jsx("div",{className:"sticky top-0 z-10 bg-background/80 px-5 pb-3 pt-2 sm:pt-5 backdrop-blur-sm sm:px-10 md:px-16 lg:px-20",children:jsxRuntime.jsx("div",{className:"mx-auto max-w-xl",children:jsxRuntime.jsx(Lt,{active:s,onChange:g})})}),jsxRuntime.jsx("div",{className:"flex flex-1 flex-col px-5 pb-8 sm:px-10 md:px-16 md:pb-12 lg:px-20",children:jsxRuntime.jsx("div",{className:"mx-auto w-full max-w-xl flex-1",children:jsxRuntime.jsxs("div",{role:"tabpanel",id:`panel-${s}`,"aria-labelledby":`tab-${s}`,children:[s==="buy"&&jsxRuntime.jsx(le,{className:"flex-1"}),s==="use"&&jsxRuntime.jsx(ce,{className:"flex-1"}),s==="sell"&&jsxRuntime.jsx(fe,{className:"flex-1"})]})})})]})}function Et({apiBaseUrl:e,stackId:t,stackName:r,paymentMethods:s,merchantWallet:g,stripePublicKey:c,theme:f,maxQuantity:a,keyImage:n,keyImageClass:d,aispImage:p,termsUrl:u,onMintSuccess:i,onMintError:o,onPaymentStart:l,onPaymentComplete:v,onListingCreated:P,defaultTab:w,className:R,style:B}){return jsxRuntime.jsx(ee,{config:{apiBaseUrl:e,stackId:t,stackName:r,paymentMethods:s,merchantWallet:g,stripePublicKey:c,theme:f,maxQuantity:a,keyImage:n,keyImageClass:d,aispImage:p,termsUrl:u},callbacks:{onMintSuccess:i,onMintError:o,onPaymentStart:l,onPaymentComplete:v,onListingCreated:P},children:jsxRuntime.jsx(Tt,{defaultTab:w,className:R,style:B})})}function Ae(e){let{config:t}=N(),[r,s]=at.useState([]),[g,c]=at.useState(false),[f,a]=at.useState(null),n=at.useRef(true),d=t.apiBaseUrl,p=at.useCallback(async()=>{if(e)try{c(!0),a(null);let u=await fetch(`${d}/api/keys/${e}/ledger`);if(!u.ok){let o=`Failed to fetch ledger (${u.status})`;try{let l=await u.json();l.error&&(o=l.error);}catch{}throw new Error(o)}let i=await u.json();n.current&&s(Array.isArray(i)?i:i.entries??[]);}catch(u){n.current&&a(u instanceof Error?u.message:"Failed to fetch ledger");}finally{n.current&&c(false);}},[d,e]);return at.useEffect(()=>(n.current=true,p(),()=>{n.current=false;}),[p]),{entries:r,loading:g,error:f,refresh:p}}var ge=20;function xe({nodeKey:e,className:t,style:r}){let[s,g]=at.useState(false),[c,f]=at.useState(false),a=at.useRef(null),{entries:n,loading:d}=Ae(s?e.id:null);at.useEffect(()=>()=>{a.current&&clearTimeout(a.current);},[]);let p=at.useCallback(()=>{e.key&&(navigator.clipboard.writeText(e.key),f(true),a.current&&clearTimeout(a.current),a.current=setTimeout(()=>f(false),2e3));},[e.key]),u=e.status==="active"?"text-green-500":e.status==="expired"?"text-red-500":"text-yellow-500",i=n.slice(0,ge);return jsxRuntime.jsxs("div",{className:m("rounded-lg border bg-background",t),style:r,children:[jsxRuntime.jsxs("div",{className:"p-4",children:[jsxRuntime.jsxs("div",{className:"mb-3 flex items-center justify-between",children:[jsxRuntime.jsxs("div",{className:"flex items-center gap-2",children:[jsxRuntime.jsx("button",{onClick:()=>g(!s),"aria-expanded":s,"aria-label":s?"Collapse ledger":"Expand ledger",className:"text-muted-foreground hover:text-foreground",children:s?jsxRuntime.jsx(lucideReact.ChevronDown,{className:"h-4 w-4"}):jsxRuntime.jsx(lucideReact.ChevronRight,{className:"h-4 w-4"})}),jsxRuntime.jsx("span",{className:"text-sm font-medium text-foreground",children:e.label||`Key ${e.id.slice(0,8)}`})]}),jsxRuntime.jsx("span",{className:m("text-xs font-medium capitalize",u),children:e.status})]}),e.key&&jsxRuntime.jsxs("div",{className:"mb-3 flex items-center gap-2",children:[jsxRuntime.jsx("code",{className:"flex-1 truncate rounded bg-muted px-2 py-1 text-xs text-muted-foreground",children:e.key}),jsxRuntime.jsx("button",{onClick:p,"aria-label":"Copy key to clipboard",className:"shrink-0 rounded p-1 text-muted-foreground hover:bg-muted hover:text-foreground",children:c?jsxRuntime.jsx(lucideReact.Check,{className:"h-3.5 w-3.5 text-green-500"}):jsxRuntime.jsx(lucideReact.Copy,{className:"h-3.5 w-3.5"})})]}),jsxRuntime.jsx(z,{current:e.tokenBalance,max:e.maxTokens}),jsxRuntime.jsxs("div",{className:"mt-2 flex items-center justify-between text-xs text-muted-foreground",children:[jsxRuntime.jsxs("span",{children:["Created ",F(e.createdAt)]}),e.expiresAt&&jsxRuntime.jsxs("span",{children:["Expires ",F(e.expiresAt)]})]})]}),s&&jsxRuntime.jsxs("div",{className:"border-t px-4 py-3",children:[jsxRuntime.jsx("h4",{className:"mb-2 text-xs font-medium text-muted-foreground",children:"Ledger"}),d?jsxRuntime.jsx("p",{className:"text-xs text-muted-foreground",children:"Loading..."}):i.length===0?jsxRuntime.jsx("p",{className:"text-xs text-muted-foreground",children:"No ledger entries."}):jsxRuntime.jsxs("div",{className:"space-y-1.5",children:[i.map(o=>jsxRuntime.jsxs("div",{className:"flex items-center justify-between text-xs",children:[jsxRuntime.jsxs("div",{className:"flex items-center gap-2",children:[jsxRuntime.jsxs("span",{className:m("font-medium",o.type==="credit"||o.type==="reward"?"text-green-500":"text-red-500"),children:[o.type==="credit"||o.type==="reward"?"+":"-",L(Math.abs(o.amount))]}),jsxRuntime.jsx("span",{className:"text-muted-foreground",children:o.description})]}),jsxRuntime.jsx("span",{className:"text-muted-foreground",children:F(o.createdAt)})]},o.id)),n.length>ge&&jsxRuntime.jsxs("p",{className:"pt-1 text-xs text-muted-foreground",children:["Showing ",ge," of ",n.length," entries"]})]})]})]})}function Qt({getKeyHref:e="/keys/get",className:t,style:r}){let{keys:s,totalBalance:g,loading:c,error:f,refresh:a}=J();return c?jsxRuntime.jsx("div",{className:m("space-y-4",t),style:r,children:jsxRuntime.jsx(Y,{count:3})}):f?jsxRuntime.jsxs("div",{className:m("flex flex-col items-center justify-center gap-4 py-12",t),style:r,children:[jsxRuntime.jsxs("div",{className:"rounded-xl border border-red-500/20 bg-red-500/5 px-6 py-5 text-center max-w-sm",children:[jsxRuntime.jsx("p",{className:"text-sm font-medium text-foreground",children:"Unable to load your keys"}),jsxRuntime.jsx("p",{className:"mt-1 text-xs text-muted-foreground",children:f})]}),jsxRuntime.jsx("button",{onClick:()=>a(),className:"text-sm text-muted-foreground hover:text-foreground transition-colors",children:"Try again"})]}):s.length===0?jsxRuntime.jsxs("div",{className:m("flex flex-col items-center justify-center gap-4 rounded-lg border border-dashed py-12",t),style:r,children:[jsxRuntime.jsx(lucideReact.KeyRound,{className:"h-10 w-10 text-muted-foreground"}),jsxRuntime.jsxs("div",{className:"text-center",children:[jsxRuntime.jsx("p",{className:"text-sm font-medium text-foreground",children:"No keys yet"}),jsxRuntime.jsx("p",{className:"text-xs text-muted-foreground",children:"Get your first node key to start earning."})]}),e&&jsxRuntime.jsx("a",{href:e,className:"rounded-lg bg-foreground px-4 py-2 text-sm font-medium text-background transition-colors hover:bg-foreground/90",children:"Get a Key"})]}):jsxRuntime.jsxs("div",{className:m("space-y-4",t),style:r,children:[jsxRuntime.jsxs("div",{className:"flex items-center justify-between",children:[jsxRuntime.jsxs("p",{className:"text-sm text-muted-foreground",children:[s.length," key",s.length!==1?"s":""," \xB7 Total balance: ",L(g)]}),e&&jsxRuntime.jsx("a",{href:e,className:"rounded-md bg-foreground px-3 py-1.5 text-xs font-medium text-background transition-colors hover:bg-foreground/90",children:"Get a Key"})]}),s.map(n=>jsxRuntime.jsx(xe,{nodeKey:n},n.id))]})}
|
|
2
|
-
exports.BuyKeyWidget=
|
|
1
|
+
'use strict';var it=require('react'),jsxRuntime=require('react/jsx-runtime'),clsx=require('clsx'),tailwindMerge=require('tailwind-merge'),lucideReact=require('lucide-react');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var it__default=/*#__PURE__*/_interopDefault(it);var ve=it.createContext(null);function v(){let e=it.useContext(ve);if(!e)throw new Error("useKeyUtilsContext must be used within a KeyUtilsProvider");return e}function te({config:e,callbacks:t={},children:r}){typeof e.apiBaseUrl!="string"&&console.error("[KeyUtils] apiBaseUrl must be a string");let s=it.useMemo(()=>({config:e,callbacks:t}),[e,t]);return jsxRuntime.jsx(ve.Provider,{value:s,children:r})}function f(...e){return tailwindMerge.twMerge(clsx.clsx(e))}function T(e){return e==null?"0":e>=1e9?`${(e/1e9).toFixed(1)}B`:e>=1e6?`${(e/1e6).toFixed(1)}M`:e>=1e3?`${(e/1e3).toFixed(1)}K`:e.toLocaleString()}function Ne(e,t){return t<=0?0:Math.min(100,Math.round(e/t*100))}function ke(e){return e>=70?"bg-green-500":e>=30?"bg-yellow-500":"bg-red-500"}function F(e){let t=typeof e=="string"&&/^\d+$/.test(e)?Number(e):e,r=new Date(t);return isNaN(r.getTime())?String(e):r.toLocaleDateString("en-US",{month:"short",day:"numeric",year:"numeric"})}function we(){if(typeof navigator>"u")return "mac";let e=navigator.userAgent.toLowerCase();return /iphone|ipad|ipod/.test(e)?"ios":/android/.test(e)?"android":/win/.test(e)?"windows":/linux/.test(e)?"linux":"mac"}function Ce(){let{config:e}=v(),[t,r]=it.useState(null),[s,d]=it.useState(true),[u,g]=it.useState(null),a=it.useRef(true),n=it.useRef(e.apiBaseUrl);n.current=e.apiBaseUrl;let i=async()=>{try{a.current&&(d(!0),g(null));let p=await fetch(`${n.current}/api/keys/pricing`);if(!p.ok){let l=`Pricing unavailable (${p.status})`;try{let o=await p.json();o.error&&(l=o.error);}catch{}throw new Error(l)}let m=await p.json();a.current&&r(m);}catch(p){a.current&&g(p instanceof Error?p.message:"Failed to fetch pricing");}finally{a.current&&d(false);}};return it.useEffect(()=>{a.current=true,i();let p=setInterval(i,6e4);return ()=>{a.current=false,clearInterval(p);}},[]),{pricing:t,loading:s,error:u,refresh:i}}function Se(){let{config:e,callbacks:t}=v(),[r,s]=it.useState(false),[d,u]=it.useState(null),[g,a]=it.useState(null);return {mint:it.useCallback(async(i,p,m=1)=>{try{s(!0),a(null),t.onPaymentComplete?.(i,p);let l=await fetch(`${e.apiBaseUrl}/api/keys/mint`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({paymentMethod:i,paymentRef:p,quantity:m})});if(!l.ok){let P="Key generation failed";try{let w=await l.json();w.error&&(P=w.error);}catch{}throw new Error(P)}let o=await l.json(),c=o.keys?.[0]||o,N={success:!0,key:{id:c.keyId||c.id||"",key:c.key||"",userId:c.userId||"",status:"active",tokenBalance:c.tokenBalance??c.currentTokenBalance??0,maxTokens:c.initialTokenBalance??c.tokenBalance??0,createdAt:String(c.createdAt||Date.now())},transactionId:p};return u(N),t.onMintSuccess?.(N),N}catch(l){let o=l instanceof Error?l:new Error("Key generation failed");return a(o.message),t.onMintError?.(o),{success:false,error:o.message}}finally{s(false);}},[e.apiBaseUrl,t]),minting:r,result:d,error:g}}function Ue(){let{config:e}=v(),[t,r]=it.useState(false),[s,d]=it.useState(null),u=it.useRef(true);return {createSession:it.useCallback(async(a,n)=>{try{r(!0),d(null);let i=await fetch(`${e.apiBaseUrl}/api/keys/stripe-session`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({priceCents:a,quantity:n,stackId:e.stackId,referrerUrl:window.location.href})});if(!i.ok){let m=`Checkout failed (${i.status})`;try{let l=await i.json();l.error&&(m=l.error);}catch{}throw new Error(m)}let{url:p}=await i.json();if(p){try{sessionStorage.setItem("keyutils_stripe_pending","1");}catch{}window.location.href=p;}else throw new Error("No checkout URL returned")}catch(i){if(u.current){let p=i instanceof Error?i.message:"Checkout failed";d(p),r(false);}}},[e.apiBaseUrl,e.stackId]),loading:t,error:s}}var He=/^[1-9A-HJ-NP-Za-km-z]{32,44}$/;function ae({amountSol:e,onSuccess:t,onError:r,disabled:s,className:d,style:u}){let{config:g}=v(),[a,n]=it.useState(false),[i,p]=it.useState(null),m=!!(g.merchantWallet&&He.test(g.merchantWallet)),l=it.useCallback(async()=>{if(m)try{n(!0),p(null);let{Connection:o,PublicKey:c,Transaction:N,SystemProgram:P,LAMPORTS_PER_SOL:w}=await import('@solana/web3.js'),B=typeof window<"u"?window.solana:void 0;if(!B||typeof B!="object"||!B.isPhantom)throw new Error("Phantom wallet not found. Please install Phantom.");let L=B,j;try{j=new c(g.merchantWallet);}catch{throw new Error("Invalid merchant wallet address.")}let A=await L.connect(),k=new c(A.publicKey.toString()),W=new o(g.solanaRpcUrl||"https://api.mainnet-beta.solana.com"),{blockhash:$}=await W.getLatestBlockhash(),H=BigInt(Math.floor(e*1e9)),ee=new N({recentBlockhash:$,feePayer:k}).add(P.transfer({fromPubkey:k,toPubkey:j,lamports:Number(H)})),{signature:U}=await L.signAndSendTransaction(ee);t(U);}catch(o){let c=o instanceof Error?o:new Error("Payment failed");p(c.message),r?.(c);}finally{n(false);}},[e,g.merchantWallet,m,t,r]);return jsxRuntime.jsxs("div",{children:[jsxRuntime.jsx("button",{onClick:l,disabled:s||a||!m,"aria-label":`Pay ${e.toFixed(4)} SOL`,className:f("flex w-full items-center justify-center gap-2 rounded-lg bg-purple-600 px-4 py-3 font-medium text-white transition-colors hover:bg-purple-700 disabled:opacity-50 disabled:cursor-not-allowed",d),style:u,children:a?jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[jsxRuntime.jsx(lucideReact.Loader2,{className:"h-4 w-4 animate-spin"}),"Processing..."]}):jsxRuntime.jsxs(jsxRuntime.Fragment,{children:["Pay ",e.toFixed(4)," SOL"]})}),i&&jsxRuntime.jsx("p",{className:"mt-2 text-center text-xs text-red-500",children:i})]})}function ie({result:e,className:t,style:r}){let[s,d]=it.useState(false),u=it.useRef(null);it.useEffect(()=>()=>{u.current&&clearTimeout(u.current);},[]);let g=it.useCallback(()=>{e.key?.key&&(navigator.clipboard.writeText(e.key.key),d(true),u.current&&clearTimeout(u.current),u.current=setTimeout(()=>d(false),2e3));},[e.key?.key]);return !e.success||!e.key?null:jsxRuntime.jsxs("div",{className:f("rounded-lg border border-green-500/20 bg-green-500/10 p-6 text-center",t),style:r,children:[jsxRuntime.jsx(lucideReact.CheckCircle,{className:"mx-auto mb-3 h-12 w-12 text-green-500"}),jsxRuntime.jsx("h3",{className:"mb-2 text-lg font-semibold text-foreground",children:"Key Generated!"}),jsxRuntime.jsx("p",{className:"mb-4 text-sm text-muted-foreground",children:"Your node key has been successfully generated."}),jsxRuntime.jsxs("div",{className:"mx-auto flex max-w-md items-center gap-2 rounded-md border bg-background p-3",children:[jsxRuntime.jsx("code",{className:"flex-1 truncate text-xs text-foreground",children:e.key.key}),jsxRuntime.jsx("button",{onClick:g,"aria-label":"Copy key to clipboard",className:"shrink-0 rounded-md p-1.5 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground",children:s?jsxRuntime.jsx(lucideReact.Check,{className:"h-4 w-4 text-green-500"}):jsxRuntime.jsx(lucideReact.Copy,{className:"h-4 w-4"})})]})]})}function I({className:e}){return jsxRuntime.jsx("div",{className:f("animate-pulse rounded-xl bg-muted",e)})}function le(){return jsxRuntime.jsxs("div",{className:"space-y-6",children:[jsxRuntime.jsxs("div",{className:"px-6 py-10 flex flex-col items-center gap-3",children:[jsxRuntime.jsx(I,{className:"h-14 w-48"}),jsxRuntime.jsx(I,{className:"h-5 w-64"})]}),jsxRuntime.jsx(I,{className:"h-[72px] w-full rounded-2xl"}),jsxRuntime.jsx(I,{className:"h-[68px] w-full rounded-2xl"}),jsxRuntime.jsx(I,{className:"h-[60px] w-full rounded-2xl"})]})}function Y({count:e=3}){return jsxRuntime.jsx("div",{className:"space-y-3",children:Array.from({length:e}).map((t,r)=>jsxRuntime.jsxs("div",{className:"rounded-xl border p-4 space-y-3",children:[jsxRuntime.jsxs("div",{className:"flex items-center justify-between",children:[jsxRuntime.jsx(I,{className:"h-4 w-32"}),jsxRuntime.jsx(I,{className:"h-4 w-16"})]}),jsxRuntime.jsx(I,{className:"h-2 w-full rounded-full"}),jsxRuntime.jsxs("div",{className:"flex justify-between",children:[jsxRuntime.jsx(I,{className:"h-3 w-24"}),jsxRuntime.jsx(I,{className:"h-3 w-20"})]})]},r))})}function gt({quantity:e,max:t,onChange:r}){return jsxRuntime.jsxs("div",{className:"flex items-center justify-between rounded-2xl border bg-muted/20 px-6 py-5",children:[jsxRuntime.jsx("span",{className:"text-sm font-medium text-foreground",children:"Quantity"}),jsxRuntime.jsxs("div",{className:"flex items-center gap-0 rounded-xl border bg-background",children:[jsxRuntime.jsx("button",{onClick:()=>r(Math.max(1,e-1)),disabled:e<=1,"aria-label":"Decrease quantity",className:"flex h-12 w-12 items-center justify-center rounded-l-xl text-muted-foreground transition-colors hover:bg-muted hover:text-foreground disabled:opacity-30",children:jsxRuntime.jsx(lucideReact.Minus,{className:"h-5 w-5"})}),jsxRuntime.jsx("span",{className:"flex h-12 w-14 items-center justify-center border-x text-xl font-bold text-foreground","aria-live":"polite",children:e}),jsxRuntime.jsx("button",{onClick:()=>r(Math.min(t,e+1)),disabled:e>=t,"aria-label":"Increase quantity",className:"flex h-12 w-12 items-center justify-center rounded-r-xl text-muted-foreground transition-colors hover:bg-muted hover:text-foreground disabled:opacity-30",children:jsxRuntime.jsx(lucideReact.Plus,{className:"h-5 w-5"})})]})]})}function Ee({pricing:e,quantity:t}){let[r,s]=it.useState(false),d=e.priceUsd*t;return jsxRuntime.jsxs("div",{className:"rounded-2xl border bg-background",children:[jsxRuntime.jsxs("button",{onClick:()=>s(!r),"aria-expanded":r,className:"flex w-full items-center justify-between px-6 py-5 text-left",children:[jsxRuntime.jsxs("span",{className:"text-foreground",children:[jsxRuntime.jsxs("span",{className:"text-xl font-bold",children:["$",d.toFixed(2)]})," ",jsxRuntime.jsx("span",{className:"text-sm text-muted-foreground",children:"total fees included"})]}),r?jsxRuntime.jsx(lucideReact.ChevronUp,{className:"h-5 w-5 text-muted-foreground"}):jsxRuntime.jsx(lucideReact.ChevronDown,{className:"h-5 w-5 text-muted-foreground"})]}),r&&jsxRuntime.jsxs("div",{className:"border-t px-6 pb-8 pt-4 space-y-3 text-sm",children:[jsxRuntime.jsxs("div",{className:"flex justify-between",children:[jsxRuntime.jsxs("span",{className:"text-muted-foreground",children:["Node Key \xD7 ",t]}),jsxRuntime.jsxs("span",{className:"text-foreground",children:["$",(e.priceUsd*t).toFixed(2)]})]}),jsxRuntime.jsxs("div",{className:"flex justify-between",children:[jsxRuntime.jsx("span",{className:"text-muted-foreground",children:"Keys sold"}),jsxRuntime.jsx("span",{className:"text-foreground",children:e.keysSold})]}),e.daysUntilHalving>0&&jsxRuntime.jsxs("div",{className:"flex justify-between text-muted-foreground",children:[jsxRuntime.jsx("span",{children:"Next refresh"}),jsxRuntime.jsxs("span",{children:[e.daysUntilHalving," day",e.daysUntilHalving!==1?"s":""]})]}),jsxRuntime.jsxs("div",{className:"flex justify-between border-t pt-3 font-semibold",children:[jsxRuntime.jsx("span",{className:"text-foreground",children:"Total"}),jsxRuntime.jsxs("span",{className:"text-foreground",children:["$",d.toFixed(2)]})]})]})]})}function ce({className:e,style:t}){let{config:r}=v(),{pricing:s,loading:d,error:u,refresh:g}=Ce(),{mint:a,minting:n,result:i,error:p}=Se(),{createSession:m,loading:l,error:o}=Ue(),[c,N]=it.useState(1),[P,w]=it.useState(r.paymentMethods?.includes("stripe")?"stripe":"solana"),[B,L]=it.useState(0),[j,A]=it.useState(false),k=it__default.default.useRef(c);k.current=c;let W=Math.max(1,r.maxQuantity||5),$=[...r.paymentMethods||["solana"]].sort((U,G)=>U==="stripe"?-1:G==="stripe"?1:0);if(it.useEffect(()=>{if(typeof window>"u")return;let G=new URLSearchParams(window.location.search).get("session_id");if(!G){try{sessionStorage.removeItem("keyutils_stripe_pending");}catch{}return}if(/^cs_(test|live)_/.test(G)&&!j&&!i?.success){let be=false;try{be=sessionStorage.getItem("keyutils_stripe_pending")==="1";}catch{}if(!be)return;try{sessionStorage.removeItem("keyutils_stripe_pending");}catch{}A(true);let he=new URL(window.location.href);he.searchParams.delete("session_id"),window.history.replaceState({},"",he.toString()),a("stripe",G,k.current);}},[j,i,a]),i?.success)return jsxRuntime.jsx("div",{className:f("flex flex-col items-center justify-center",e),style:t,children:jsxRuntime.jsx(ie,{result:i})});if(j||n)return jsxRuntime.jsx("div",{className:f("flex flex-col",e),style:t,children:jsxRuntime.jsx(le,{})});if(d)return jsxRuntime.jsx("div",{className:f("flex flex-col",e),style:t,children:jsxRuntime.jsx(le,{})});if(u||!s)return jsxRuntime.jsxs("div",{className:f("flex flex-col items-center justify-center gap-4 py-24",e),style:t,children:[jsxRuntime.jsxs("div",{className:"rounded-xl border border-red-500/20 bg-red-500/5 px-6 py-5 text-center max-w-sm",children:[jsxRuntime.jsx("p",{className:"text-sm font-medium text-foreground",children:"Unable to load pricing"}),jsxRuntime.jsx("p",{className:"mt-1 text-xs text-muted-foreground",children:u||"Please check your connection and try again."})]}),jsxRuntime.jsx("button",{onClick:()=>g(),className:"text-sm text-muted-foreground hover:text-foreground transition-colors",children:"Try again"})]});let H=async U=>{L(2),await a(P,U,c);},ee=()=>{m(s.priceCents,c);};return jsxRuntime.jsx("div",{className:f("flex flex-col",e),style:t,children:jsxRuntime.jsxs("div",{className:"space-y-6",children:[B===0&&jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[r.keyImage&&jsxRuntime.jsx("img",{src:r.keyImage,alt:"Node Key",className:r.keyImageClass||"w-1/2 mx-auto"}),jsxRuntime.jsxs("div",{className:"px-6 py-10 text-center",children:[jsxRuntime.jsxs("p",{className:"text-5xl font-bold tracking-tight text-foreground md:text-6xl",children:["$",s.priceUsd.toFixed(2)]}),jsxRuntime.jsxs("p",{className:"mt-2 text-base text-muted-foreground",children:["per key \xB7 ",s.tokenAllocationFormatted||T(s.tokenAllocation)," tokens included"]})]}),jsxRuntime.jsx(gt,{quantity:c,max:W,onChange:N}),jsxRuntime.jsx(Ee,{pricing:s,quantity:c}),jsxRuntime.jsx("button",{onClick:()=>L(1),className:"w-full rounded-2xl bg-foreground py-5 text-center text-base font-semibold text-background transition-colors hover:bg-foreground/90",children:"Continue to Payment"})]}),B===1&&jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[$.length>1&&jsxRuntime.jsx("div",{role:"tablist",className:"flex gap-1 rounded-2xl bg-muted/50 p-1.5",children:$.map(U=>jsxRuntime.jsxs("button",{role:"tab","aria-selected":P===U,onClick:()=>w(U),className:f("flex flex-1 items-center justify-center gap-2.5 rounded-xl py-4 text-sm font-medium transition-colors",P===U?"bg-background text-foreground shadow-sm":"text-muted-foreground hover:text-foreground"),children:[U==="stripe"?jsxRuntime.jsx(lucideReact.CreditCard,{className:"h-5 w-5"}):jsxRuntime.jsx(lucideReact.Wallet,{className:"h-5 w-5"}),U==="stripe"?"Credit Card":"Crypto"]},U))}),jsxRuntime.jsx(Ee,{pricing:s,quantity:c}),P==="stripe"?jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[jsxRuntime.jsx("button",{onClick:ee,disabled:l,className:"flex w-full items-center justify-center gap-2 rounded-2xl bg-foreground py-5 text-base font-semibold text-background transition-colors hover:bg-foreground/90 disabled:opacity-50",children:l?jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[jsxRuntime.jsx(lucideReact.Loader2,{className:"h-5 w-5 animate-spin"}),"Redirecting to Stripe..."]}):jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[jsxRuntime.jsx(lucideReact.CreditCard,{className:"h-5 w-5"}),"Pay with Card"]})}),o&&jsxRuntime.jsx("p",{className:"text-center text-sm text-red-500",children:o})]}):P==="solana"?jsxRuntime.jsx(ae,{amountSol:s.priceSol??0,onSuccess:H,className:"rounded-2xl py-5 text-base"}):null,p&&jsxRuntime.jsx("p",{className:"text-center text-sm text-red-500",children:p}),jsxRuntime.jsx("button",{onClick:()=>L(0),className:"w-full py-3 text-center text-sm text-muted-foreground hover:text-foreground transition-colors",children:"\u2190 Back"})]})]})})}var ht={ios:"iOS",android:"Android",windows:"Windows",mac:"macOS",linux:"Linux"},vt=["mac","windows","linux","ios","android"];function de({downloads:e,className:t,style:r}){let{config:s}=v(),d=it.useMemo(()=>we(),[]),u=e||vt.map(n=>({platform:n,label:ht[n],url:"#",compatible:n===d})),g=u.find(n=>n.compatible)||u[0],a=u.filter(n=>n.platform!==g.platform);return jsxRuntime.jsxs("div",{className:f("space-y-8",t),style:r,children:[jsxRuntime.jsxs("div",{className:"flex flex-col items-center gap-6 pt-4 mt-4",children:[s.aispImage&&jsxRuntime.jsx("img",{src:s.aispImage,alt:"aiSP",className:"w-full sm:w-1/2 mx-auto px-4 sm:px-0"}),jsxRuntime.jsxs("div",{className:"text-center",children:[jsxRuntime.jsx("p",{className:"text-lg sm:text-2xl font-medium text-foreground",children:"Download the AI Service Provider App"}),jsxRuntime.jsx("p",{className:"mt-1 text-sm sm:text-base text-muted-foreground",children:"Load your keys and run your node."})]}),jsxRuntime.jsxs("a",{href:g.url,className:"inline-flex items-center gap-3 rounded-full px-10 py-4 text-lg font-semibold text-white transition-colors hover:opacity-90",style:{backgroundColor:"#0900f4"},children:[jsxRuntime.jsx(lucideReact.Download,{className:"h-5 w-5"}),"Get aiSP for ",g.label]})]}),jsxRuntime.jsxs("p",{className:"text-center text-sm text-muted-foreground",children:["aiSP is also available on"," ",a.map((n,i)=>jsxRuntime.jsxs(it__default.default.Fragment,{children:[i>0&&(i===a.length-1?" and ":", "),jsxRuntime.jsx("a",{href:n.url,className:"font-medium underline transition-colors hover:text-foreground",style:{color:"#066bff"},children:n.label})]},n.platform)),"."]}),jsxRuntime.jsxs("div",{className:"flex flex-col items-center gap-2 mt-12",children:[jsxRuntime.jsxs("p",{className:"text-center text-xs text-muted-foreground flex justify-center gap-0.5 flex-wrap",children:["Source code is available on",jsxRuntime.jsx("a",{href:"https://github.com/stack-net",target:"_blank",rel:"noopener noreferrer",className:"text-foreground underline hover:text-foreground/80 color-[#0900f4]",children:"Github"})]}),jsxRuntime.jsxs("p",{className:"text-center text-xs text-muted-foreground flex justify-center gap-0.5 flex-wrap",children:["By downloading, you agree to the",jsxRuntime.jsx("a",{href:s.termsUrl||"/terms",className:"text-foreground underline hover:text-foreground/80",children:"Open Source Applications Terms"})]})]})]})}function Pt(e){return {id:e.id||"",key:e.key||e.keyPrefix||"",userId:e.userId||e.user_id||"",status:e.status||"active",tokenBalance:e.tokenBalance??e.currentTokenBalance??e.current_token_balance??0,maxTokens:e.maxTokens??e.initialTokenBalance??e.initial_token_balance??0,createdAt:String(e.createdAt??e.created_at??""),expiresAt:e.expiresAt,label:e.label||e.keyPrefix||e.key_prefix||void 0,paperWork:e.paperWork||e.paper_work||void 0,stackId:e.stackId,stackName:e.stackName}}function J(){let{config:e}=v(),[t,r]=it.useState([]),[s,d]=it.useState(true),[u,g]=it.useState(null),a=it.useRef(true),n=e.apiBaseUrl,i=it.useCallback(async()=>{try{d(!0),g(null);let m=await fetch(`${n}/api/keys`);if(!m.ok){let c=`Failed to fetch keys (${m.status})`;try{let N=await m.json();N.error&&(c=N.error);}catch{}throw new Error(c)}let l=await m.json(),o=Array.isArray(l)?l:l.keys??[];a.current&&r(o.map(Pt));}catch(m){a.current&&g(m instanceof Error?m.message:"Failed to fetch keys");}finally{a.current&&d(false);}},[n]);it.useEffect(()=>(a.current=true,i(),()=>{a.current=false;}),[i]);let p=t.reduce((m,l)=>m+l.tokenBalance,0);return {keys:t,totalBalance:p,loading:s,error:u,refresh:i}}function Me(){let{config:e,callbacks:t}=v(),[r,s]=it.useState(false),[d,u]=it.useState(null),[g,a]=it.useState(null),n=it.useRef(true),i=it.useCallback(async(m,l)=>{try{s(!0),a(null);let o=await fetch(`${e.apiBaseUrl}/api/keys/${m}/list`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({askPriceCents:l,stackId:e.stackId,stackName:e.stackName})});if(!o.ok){let N=`Listing failed (${o.status})`;try{let P=await o.json();P.error&&(N=P.error);}catch{}throw new Error(N)}let c=await o.json();return n.current&&(u(c),t.onListingCreated?.(c)),c}catch(o){let c=o instanceof Error?o.message:"Listing failed";return n.current&&a(c),null}finally{n.current&&s(false);}},[e.apiBaseUrl,e.stackId,e.stackName,t]),p=it.useCallback(async m=>{try{s(!0),a(null);let l=await fetch(`${e.apiBaseUrl}/api/keys/${m}/list`,{method:"DELETE"});if(!l.ok){let o=`Delist failed (${l.status})`;try{let c=await l.json();c.error&&(o=c.error);}catch{}throw new Error(o)}return n.current&&u(null),!0}catch(l){let o=l instanceof Error?l.message:"Delist failed";return n.current&&a(o),false}finally{n.current&&s(false);}},[e.apiBaseUrl]);return {listKey:i,delistKey:p,listing:r,result:d,error:g}}function z({current:e,max:t,className:r,style:s,showLabel:d=true}){let u=Ne(e,t),g=ke(u);return jsxRuntime.jsxs("div",{className:f("w-full",r),style:s,children:[d&&jsxRuntime.jsxs("div",{className:"mb-1 flex items-center justify-between text-xs text-muted-foreground",children:[jsxRuntime.jsxs("span",{children:[T(e)," / ",T(t)]}),jsxRuntime.jsxs("span",{children:[u,"%"]})]}),jsxRuntime.jsx("div",{className:"h-2 w-full overflow-hidden rounded-full bg-muted",children:jsxRuntime.jsx("div",{className:f("h-full rounded-full transition-all duration-500",g),style:{width:`${u}%`}})})]})}function Rt({nodeKey:e,selected:t,onSelect:r}){return jsxRuntime.jsxs("button",{onClick:r,className:f("flex w-full items-start gap-3 rounded-xl border p-4 text-left transition-all",t?"border-foreground/30 bg-foreground/5 ring-1 ring-foreground/10":"border-border hover:border-foreground/20"),children:[jsxRuntime.jsx("div",{className:f("mt-0.5 flex h-5 w-5 shrink-0 items-center justify-center rounded-full border-2 transition-colors",t?"border-foreground bg-foreground":"border-muted-foreground/40"),children:t&&jsxRuntime.jsx(lucideReact.Check,{className:"h-3 w-3 text-background"})}),jsxRuntime.jsxs("div",{className:"min-w-0 flex-1",children:[jsxRuntime.jsxs("div",{className:"flex items-center justify-between",children:[jsxRuntime.jsx("span",{className:"text-sm font-medium text-foreground",children:e.label||e.id.slice(0,16)}),jsxRuntime.jsx("span",{className:f("text-xs font-medium capitalize",e.status==="active"?"text-green-500":"text-muted-foreground"),children:e.status})]}),jsxRuntime.jsx(z,{current:e.tokenBalance,max:e.maxTokens,className:"mt-2"})]})]})}function pe({className:e,style:t}){let{config:r}=v(),{keys:s,loading:d,error:u,refresh:g}=J(),{listKey:a,listing:n,result:i,error:p}=Me(),[m,l]=it.useState(null),[o,c]=it.useState(""),[N,P]=it.useState(false),w=s.find(k=>k.id===m),B=parseInt(o.replace(/[^0-9]/g,""),10)||0,L=w&&B>0&&N&&!n,j=async()=>{if(!L||!w)return;let k=B*100;await a(w.id,k)&&g();};if(i)return jsxRuntime.jsxs("div",{className:f("flex flex-col items-center gap-4 rounded-xl border border-green-500/20 bg-green-500/10 py-12",e),style:t,children:[jsxRuntime.jsx(lucideReact.CheckCircle,{className:"h-12 w-12 text-green-500"}),jsxRuntime.jsx("h3",{className:"text-lg font-semibold text-foreground",children:"Key Listed!"}),jsxRuntime.jsx("p",{className:"text-sm text-muted-foreground",children:"Your key is now on the Global Open AI Network marketplace."}),jsxRuntime.jsxs("p",{className:"text-xs text-muted-foreground",children:["Listing ID: ",jsxRuntime.jsx("code",{children:i.listingId})]})]});if(d)return jsxRuntime.jsxs("div",{className:f("space-y-5 px-1",e),style:t,children:[jsxRuntime.jsx("h3",{className:"text-2xl mt-3 mb-2 font-semibold text-foreground",children:"Your Keys"}),jsxRuntime.jsx(Y,{count:2})]});if(u)return jsxRuntime.jsxs("div",{className:f("flex flex-col items-center justify-center gap-4 py-16",e),style:t,children:[jsxRuntime.jsxs("div",{className:"rounded-xl border border-red-500/20 bg-red-500/5 px-6 py-5 text-center max-w-sm",children:[jsxRuntime.jsx("p",{className:"text-sm font-medium text-foreground",children:"Unable to load your keys"}),jsxRuntime.jsx("p",{className:"mt-1 text-xs text-muted-foreground",children:u})]}),jsxRuntime.jsx("button",{onClick:()=>g(),className:"text-sm text-muted-foreground hover:text-foreground transition-colors",children:"Try again"})]});let A=s.filter(k=>k.status==="active");return A.length===0?jsxRuntime.jsxs("div",{className:f("flex flex-col items-center justify-center gap-3 rounded-xl border border-dashed py-16",e),style:t,children:[jsxRuntime.jsx(lucideReact.AlertTriangle,{className:"h-8 w-8 text-muted-foreground"}),jsxRuntime.jsx("p",{className:"text-sm text-muted-foreground",children:"No active keys to sell."})]}):jsxRuntime.jsxs("div",{className:f("space-y-5",e),style:t,children:[jsxRuntime.jsxs("div",{children:[jsxRuntime.jsx("h3",{className:"text-2xl mt-3 mb-2 font-semibold text-foreground",children:"Your Keys"}),jsxRuntime.jsx("div",{className:"space-y-2",children:A.map(k=>jsxRuntime.jsx(Rt,{nodeKey:k,selected:m===k.id,onSelect:()=>l(k.id)},k.id))})]}),w&&jsxRuntime.jsxs("div",{className:"rounded-xl border bg-muted/30 p-5 space-y-2 text-sm",children:[jsxRuntime.jsxs("div",{className:"flex justify-between",children:[jsxRuntime.jsx("span",{className:"text-muted-foreground",children:"Key ID"}),jsxRuntime.jsx("code",{className:"text-xs text-foreground",children:w.id})]}),jsxRuntime.jsxs("div",{className:"flex justify-between",children:[jsxRuntime.jsx("span",{className:"text-muted-foreground",children:"Originating Stack"}),jsxRuntime.jsx("span",{className:"text-foreground",children:r.stackName||"Unknown"})]}),jsxRuntime.jsxs("div",{className:"flex justify-between",children:[jsxRuntime.jsx("span",{className:"text-muted-foreground",children:"Tokens (remaining / total)"}),jsxRuntime.jsxs("span",{className:"text-foreground",children:[T(w.tokenBalance)," / ",T(w.maxTokens)]})]}),w.paperWork&&jsxRuntime.jsxs("div",{className:"flex justify-between",children:[jsxRuntime.jsx("span",{className:"text-muted-foreground",children:"Paper earned"}),jsxRuntime.jsx("span",{className:"text-foreground",children:w.paperWork})]}),jsxRuntime.jsxs("div",{className:"flex justify-between",children:[jsxRuntime.jsx("span",{className:"text-muted-foreground",children:"Created"}),jsxRuntime.jsx("span",{className:"text-foreground",children:F(w.createdAt)})]})]}),jsxRuntime.jsxs("div",{children:[jsxRuntime.jsx("label",{htmlFor:"listing-price",className:"mb-2 block text-sm sm:text-lg font-semibold text-foreground",children:"Listing Price (USD)"}),jsxRuntime.jsxs("div",{className:"relative",children:[jsxRuntime.jsx("span",{className:"absolute left-4 top-1/2 -translate-y-1/2 text-2xl font-bold text-muted-foreground",children:"$"}),jsxRuntime.jsx("input",{id:"listing-price",type:"text",inputMode:"numeric",value:o,onChange:k=>{let W=k.target.value.replace(/[^0-9]/g,""),$=parseInt(W,10);c(isNaN($)?"":$.toLocaleString("en-US"));},placeholder:"0",className:"w-full rounded-xl border bg-background py-5 pl-10 pr-4 text-3xl font-bold text-foreground placeholder:text-muted-foreground/30 focus:outline-none focus:ring-2 focus:ring-foreground/20"})]})]}),jsxRuntime.jsxs("label",{className:"flex items-start gap-3 cursor-pointer",children:[jsxRuntime.jsx("input",{type:"checkbox",checked:N,onChange:k=>P(k.target.checked),className:"sr-only"}),jsxRuntime.jsx("div",{"aria-hidden":"true",className:f("mt-0.5 flex h-5 w-5 shrink-0 items-center justify-center rounded border-2 transition-colors",N?"border-foreground bg-foreground":"border-muted-foreground/40"),children:N&&jsxRuntime.jsx(lucideReact.Check,{className:"h-3 w-3 text-background"})}),jsxRuntime.jsxs("span",{className:"text-xs text-muted-foreground leading-relaxed",children:["I agree to list this key on the Global Open AI Network marketplace. The key will be deactivated upon sale and transferred to the buyer. Listing fees may apply."," ",jsxRuntime.jsx("a",{href:r.termsUrl||"/terms",className:"text-foreground underline",children:"Terms"})]})]}),p&&jsxRuntime.jsx("p",{className:"text-sm text-red-500",children:p}),jsxRuntime.jsxs("button",{onClick:j,disabled:!L,className:f("flex w-full items-center justify-center gap-2 rounded-xl py-4 text-sm font-semibold transition-colors",L?"bg-foreground text-background hover:bg-foreground/90":"bg-muted text-muted-foreground cursor-not-allowed"),children:[jsxRuntime.jsx(lucideReact.ExternalLink,{className:"h-4 w-4"}),n?"Listing...":"List on Marketplace"]})]})}var Lt=[{id:"buy",label:"Buy"},{id:"use",label:"Use"},{id:"sell",label:"Sell"}];function Tt({active:e,onChange:t}){return jsxRuntime.jsx("div",{role:"tablist","aria-label":"Key actions",className:"flex gap-1 rounded-2xl bg-muted/50 p-1.5",children:Lt.map(({id:r,label:s})=>jsxRuntime.jsx("button",{role:"tab","aria-selected":e===r,"aria-controls":`panel-${r}`,id:`tab-${r}`,onClick:()=>t(r),className:f("flex flex-1 items-center justify-center gap-2.5 rounded-xl py-3.5 text-sm font-medium transition-all",e===r?"bg-background text-foreground shadow-sm":"text-muted-foreground hover:text-foreground"),children:jsxRuntime.jsx("span",{className:"inline",children:s})},r))})}function Et({defaultTab:e="buy",className:t,style:r}){let[s,d]=it.useState(e);return jsxRuntime.jsxs("div",{className:f("flex flex-col",t),style:r,children:[jsxRuntime.jsx("div",{className:"sticky top-0 z-10 bg-background/80 px-5 pb-3 pt-2 sm:pt-5 backdrop-blur-sm sm:px-10 md:px-16 lg:px-20",children:jsxRuntime.jsx("div",{className:"mx-auto max-w-xl",children:jsxRuntime.jsx(Tt,{active:s,onChange:d})})}),jsxRuntime.jsx("div",{className:"flex flex-1 flex-col px-5 pb-8 sm:px-10 md:px-16 md:pb-12 lg:px-20",children:jsxRuntime.jsx("div",{className:"mx-auto w-full max-w-xl flex-1",children:jsxRuntime.jsxs("div",{role:"tabpanel",id:`panel-${s}`,"aria-labelledby":`tab-${s}`,children:[s==="buy"&&jsxRuntime.jsx(ce,{className:"flex-1"}),s==="use"&&jsxRuntime.jsx(de,{className:"flex-1"}),s==="sell"&&jsxRuntime.jsx(pe,{className:"flex-1"})]})})})]})}function It({apiBaseUrl:e,stackId:t,stackName:r,paymentMethods:s,merchantWallet:d,stripePublicKey:u,theme:g,maxQuantity:a,keyImage:n,keyImageClass:i,aispImage:p,termsUrl:m,onMintSuccess:l,onMintError:o,onPaymentStart:c,onPaymentComplete:N,onListingCreated:P,defaultTab:w,className:B,style:L}){return jsxRuntime.jsx(te,{config:{apiBaseUrl:e,stackId:t,stackName:r,paymentMethods:s,merchantWallet:d,stripePublicKey:u,theme:g,maxQuantity:a,keyImage:n,keyImageClass:i,aispImage:p,termsUrl:m},callbacks:{onMintSuccess:l,onMintError:o,onPaymentStart:c,onPaymentComplete:N,onListingCreated:P},children:jsxRuntime.jsx(Et,{defaultTab:w,className:B,style:L})})}function $e(e){let{config:t}=v(),[r,s]=it.useState([]),[d,u]=it.useState(false),[g,a]=it.useState(null),n=it.useRef(true),i=t.apiBaseUrl,p=it.useCallback(async()=>{if(e)try{u(!0),a(null);let m=await fetch(`${i}/api/keys/${e}/ledger`);if(!m.ok){let o=`Failed to fetch ledger (${m.status})`;try{let c=await m.json();c.error&&(o=c.error);}catch{}throw new Error(o)}let l=await m.json();n.current&&s(Array.isArray(l)?l:l.entries??[]);}catch(m){n.current&&a(m instanceof Error?m.message:"Failed to fetch ledger");}finally{n.current&&u(false);}},[i,e]);return it.useEffect(()=>(n.current=true,p(),()=>{n.current=false;}),[p]),{entries:r,loading:d,error:g,refresh:p}}var xe=20;function ye({nodeKey:e,className:t,style:r}){let[s,d]=it.useState(false),[u,g]=it.useState(false),a=it.useRef(null),{entries:n,loading:i}=$e(s?e.id:null);it.useEffect(()=>()=>{a.current&&clearTimeout(a.current);},[]);let p=it.useCallback(()=>{e.key&&(navigator.clipboard.writeText(e.key),g(true),a.current&&clearTimeout(a.current),a.current=setTimeout(()=>g(false),2e3));},[e.key]),m=e.status==="active"?"text-green-500":e.status==="expired"?"text-red-500":"text-yellow-500",l=n.slice(0,xe);return jsxRuntime.jsxs("div",{className:f("rounded-lg border bg-background",t),style:r,children:[jsxRuntime.jsxs("div",{className:"p-4",children:[jsxRuntime.jsxs("div",{className:"mb-3 flex items-center justify-between",children:[jsxRuntime.jsxs("div",{className:"flex items-center gap-2",children:[jsxRuntime.jsx("button",{onClick:()=>d(!s),"aria-expanded":s,"aria-label":s?"Collapse ledger":"Expand ledger",className:"text-muted-foreground hover:text-foreground",children:s?jsxRuntime.jsx(lucideReact.ChevronDown,{className:"h-4 w-4"}):jsxRuntime.jsx(lucideReact.ChevronRight,{className:"h-4 w-4"})}),jsxRuntime.jsx("span",{className:"text-sm font-medium text-foreground",children:e.label||`Key ${e.id.slice(0,8)}`})]}),jsxRuntime.jsx("span",{className:f("text-xs font-medium capitalize",m),children:e.status})]}),e.key&&jsxRuntime.jsxs("div",{className:"mb-3 flex items-center gap-2",children:[jsxRuntime.jsx("code",{className:"flex-1 truncate rounded bg-muted px-2 py-1 text-xs text-muted-foreground",children:e.key}),jsxRuntime.jsx("button",{onClick:p,"aria-label":"Copy key to clipboard",className:"shrink-0 rounded p-1 text-muted-foreground hover:bg-muted hover:text-foreground",children:u?jsxRuntime.jsx(lucideReact.Check,{className:"h-3.5 w-3.5 text-green-500"}):jsxRuntime.jsx(lucideReact.Copy,{className:"h-3.5 w-3.5"})})]}),jsxRuntime.jsx(z,{current:e.tokenBalance,max:e.maxTokens}),jsxRuntime.jsxs("div",{className:"mt-2 flex items-center justify-between text-xs text-muted-foreground",children:[jsxRuntime.jsxs("span",{children:["Created ",F(e.createdAt)]}),e.expiresAt&&jsxRuntime.jsxs("span",{children:["Expires ",F(e.expiresAt)]})]})]}),s&&jsxRuntime.jsxs("div",{className:"border-t px-4 py-3",children:[jsxRuntime.jsx("h4",{className:"mb-2 text-xs font-medium text-muted-foreground",children:"Ledger"}),i?jsxRuntime.jsx("p",{className:"text-xs text-muted-foreground",children:"Loading..."}):l.length===0?jsxRuntime.jsx("p",{className:"text-xs text-muted-foreground",children:"No ledger entries."}):jsxRuntime.jsxs("div",{className:"space-y-1.5",children:[l.map(o=>jsxRuntime.jsxs("div",{className:"flex items-center justify-between text-xs",children:[jsxRuntime.jsxs("div",{className:"flex items-center gap-2",children:[jsxRuntime.jsxs("span",{className:f("font-medium",o.type==="credit"||o.type==="reward"?"text-green-500":"text-red-500"),children:[o.type==="credit"||o.type==="reward"?"+":"-",T(Math.abs(o.amount))]}),jsxRuntime.jsx("span",{className:"text-muted-foreground",children:o.description})]}),jsxRuntime.jsx("span",{className:"text-muted-foreground",children:F(o.createdAt)})]},o.id)),n.length>xe&&jsxRuntime.jsxs("p",{className:"pt-1 text-xs text-muted-foreground",children:["Showing ",xe," of ",n.length," entries"]})]})]})]})}var qt=[{id:"buy",label:"Buy"},{id:"use",label:"Use"},{id:"sell",label:"Sell"}];function X({baseHref:e}){return jsxRuntime.jsx("div",{className:"flex gap-1 rounded-2xl bg-muted/50 p-1.5 mb-6",children:qt.map(({id:t,label:r})=>jsxRuntime.jsx("a",{href:`${e}?tab=${t}`,className:"flex flex-1 items-center justify-center gap-2.5 rounded-xl py-3.5 text-sm font-medium text-muted-foreground hover:text-foreground transition-all",children:r},t))})}function Vt({getKeyHref:e="/keys/get",showTabs:t=true,className:r,style:s}){let{keys:d,totalBalance:u,loading:g,error:a,refresh:n}=J();return g?jsxRuntime.jsxs("div",{className:f("space-y-4",r),style:s,children:[t&&e&&jsxRuntime.jsx(X,{baseHref:e}),jsxRuntime.jsx(Y,{count:3})]}):a?jsxRuntime.jsxs("div",{className:f("space-y-4",r),style:s,children:[t&&e&&jsxRuntime.jsx(X,{baseHref:e}),jsxRuntime.jsxs("div",{className:"flex flex-col items-center justify-center gap-4 py-12",children:[jsxRuntime.jsxs("div",{className:"rounded-xl border border-red-500/20 bg-red-500/5 px-6 py-5 text-center max-w-sm",children:[jsxRuntime.jsx("p",{className:"text-sm font-medium text-foreground",children:"Unable to load your keys"}),jsxRuntime.jsx("p",{className:"mt-1 text-xs text-muted-foreground",children:a})]}),jsxRuntime.jsx("button",{onClick:()=>n(),className:"text-sm text-muted-foreground hover:text-foreground transition-colors",children:"Try again"})]})]}):d.length===0?jsxRuntime.jsxs("div",{className:f("space-y-4",r),style:s,children:[t&&e&&jsxRuntime.jsx(X,{baseHref:e}),jsxRuntime.jsxs("div",{className:"flex flex-col items-center justify-center gap-4 rounded-lg border border-dashed py-12",children:[jsxRuntime.jsx(lucideReact.KeyRound,{className:"h-10 w-10 text-muted-foreground"}),jsxRuntime.jsxs("div",{className:"text-center",children:[jsxRuntime.jsx("p",{className:"text-sm font-medium text-foreground",children:"No keys yet"}),jsxRuntime.jsx("p",{className:"text-xs text-muted-foreground",children:"Get your first node key to start earning."})]}),e&&jsxRuntime.jsx("a",{href:e,className:"rounded-lg bg-foreground px-4 py-2 text-sm font-medium text-background transition-colors hover:bg-foreground/90",children:"Get a Key"})]})]}):jsxRuntime.jsxs("div",{className:f("space-y-4",r),style:s,children:[t&&e&&jsxRuntime.jsx(X,{baseHref:e}),jsxRuntime.jsxs("div",{className:"flex items-center justify-between",children:[jsxRuntime.jsxs("p",{className:"text-sm text-muted-foreground",children:[d.length," key",d.length!==1?"s":""," \xB7 Total balance: ",T(u)]}),e&&jsxRuntime.jsx("a",{href:e,className:"rounded-md bg-foreground px-3 py-1.5 text-xs font-medium text-background transition-colors hover:bg-foreground/90",children:"Get a Key"})]}),d.map(i=>jsxRuntime.jsx(ye,{nodeKey:i},i.id))]})}
|
|
2
|
+
exports.BuyKeyWidget=It;exports.BuyPanel=ce;exports.KeyBalanceBar=z;exports.KeyCard=ye;exports.KeyList=Vt;exports.KeyUtilsProvider=te;exports.MintSuccess=ie;exports.SellPanel=pe;exports.SolanaPayButton=ae;exports.UsePanel=de;exports.useKeyUtilsContext=v;
|
|
@@ -42,10 +42,11 @@ declare function SellPanel({ className, style }: SellPanelProps): react_jsx_runt
|
|
|
42
42
|
|
|
43
43
|
interface KeyListProps {
|
|
44
44
|
getKeyHref?: string;
|
|
45
|
+
showTabs?: boolean;
|
|
45
46
|
className?: string;
|
|
46
47
|
style?: React.CSSProperties;
|
|
47
48
|
}
|
|
48
|
-
declare function KeyList({ getKeyHref, className, style }: KeyListProps): react_jsx_runtime.JSX.Element;
|
|
49
|
+
declare function KeyList({ getKeyHref, showTabs, className, style }: KeyListProps): react_jsx_runtime.JSX.Element;
|
|
49
50
|
|
|
50
51
|
interface KeyCardProps {
|
|
51
52
|
nodeKey: NodeKeyInfo;
|
|
@@ -42,10 +42,11 @@ declare function SellPanel({ className, style }: SellPanelProps): react_jsx_runt
|
|
|
42
42
|
|
|
43
43
|
interface KeyListProps {
|
|
44
44
|
getKeyHref?: string;
|
|
45
|
+
showTabs?: boolean;
|
|
45
46
|
className?: string;
|
|
46
47
|
style?: React.CSSProperties;
|
|
47
48
|
}
|
|
48
|
-
declare function KeyList({ getKeyHref, className, style }: KeyListProps): react_jsx_runtime.JSX.Element;
|
|
49
|
+
declare function KeyList({ getKeyHref, showTabs, className, style }: KeyListProps): react_jsx_runtime.JSX.Element;
|
|
49
50
|
|
|
50
51
|
interface KeyCardProps {
|
|
51
52
|
nodeKey: NodeKeyInfo;
|
package/dist/components/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import at,{createContext,useContext,useMemo,useState,useCallback,useRef,useEffect}from'react';import {jsx,jsxs,Fragment}from'react/jsx-runtime';import {clsx}from'clsx';import {twMerge}from'tailwind-merge';import {Loader2,CheckCircle,Check,Copy,CreditCard,Wallet,Download,AlertTriangle,ExternalLink,ChevronDown,ChevronRight,KeyRound,Minus,Plus,ChevronUp}from'lucide-react';var he=createContext(null);function N(){let e=useContext(he);if(!e)throw new Error("useKeyUtilsContext must be used within a KeyUtilsProvider");return e}function ee({config:e,callbacks:t={},children:r}){typeof e.apiBaseUrl!="string"&&console.error("[KeyUtils] apiBaseUrl must be a string");let s=useMemo(()=>({config:e,callbacks:t}),[e,t]);return jsx(he.Provider,{value:s,children:r})}function m(...e){return twMerge(clsx(e))}function L(e){return e==null?"0":e>=1e9?`${(e/1e9).toFixed(1)}B`:e>=1e6?`${(e/1e6).toFixed(1)}M`:e>=1e3?`${(e/1e3).toFixed(1)}K`:e.toLocaleString()}function Ne(e,t){return t<=0?0:Math.min(100,Math.round(e/t*100))}function ve(e){return e>=70?"bg-green-500":e>=30?"bg-yellow-500":"bg-red-500"}function F(e){let t=typeof e=="string"&&/^\d+$/.test(e)?Number(e):e,r=new Date(t);return isNaN(r.getTime())?String(e):r.toLocaleDateString("en-US",{month:"short",day:"numeric",year:"numeric"})}function ke(){if(typeof navigator>"u")return "mac";let e=navigator.userAgent.toLowerCase();return /iphone|ipad|ipod/.test(e)?"ios":/android/.test(e)?"android":/win/.test(e)?"windows":/linux/.test(e)?"linux":"mac"}function Pe(){let{config:e}=N(),[t,r]=useState(null),[s,g]=useState(true),[c,f]=useState(null),a=useRef(true),n=useRef(e.apiBaseUrl);n.current=e.apiBaseUrl;let d=async()=>{try{a.current&&(g(!0),f(null));let p=await fetch(`${n.current}/api/keys/pricing`);if(!p.ok){let i=`Pricing unavailable (${p.status})`;try{let o=await p.json();o.error&&(i=o.error);}catch{}throw new Error(i)}let u=await p.json();a.current&&r(u);}catch(p){a.current&&f(p instanceof Error?p.message:"Failed to fetch pricing");}finally{a.current&&g(false);}};return useEffect(()=>{a.current=true,d();let p=setInterval(d,6e4);return ()=>{a.current=false,clearInterval(p);}},[]),{pricing:t,loading:s,error:c,refresh:d}}function Ce(){let{config:e,callbacks:t}=N(),[r,s]=useState(false),[g,c]=useState(null),[f,a]=useState(null);return {mint:useCallback(async(d,p,u=1)=>{try{s(!0),a(null),t.onPaymentComplete?.(d,p);let i=await fetch(`${e.apiBaseUrl}/api/keys/mint`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({paymentMethod:d,paymentRef:p,quantity:u})});if(!i.ok){let P="Key generation failed";try{let w=await i.json();w.error&&(P=w.error);}catch{}throw new Error(P)}let o=await i.json(),l=o.keys?.[0]||o,v={success:!0,key:{id:l.keyId||l.id||"",key:l.key||"",userId:l.userId||"",status:"active",tokenBalance:l.tokenBalance??l.currentTokenBalance??0,maxTokens:l.initialTokenBalance??l.tokenBalance??0,createdAt:String(l.createdAt||Date.now())},transactionId:p};return c(v),t.onMintSuccess?.(v),v}catch(i){let o=i instanceof Error?i:new Error("Key generation failed");return a(o.message),t.onMintError?.(o),{success:false,error:o.message}}finally{s(false);}},[e.apiBaseUrl,t]),minting:r,result:g,error:f}}function Ke(){let{config:e}=N(),[t,r]=useState(false),[s,g]=useState(null),c=useRef(true);return {createSession:useCallback(async(a,n)=>{try{r(!0),g(null);let d=await fetch(`${e.apiBaseUrl}/api/keys/stripe-session`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({priceCents:a,quantity:n,stackId:e.stackId})});if(!d.ok){let u=`Checkout failed (${d.status})`;try{let i=await d.json();i.error&&(u=i.error);}catch{}throw new Error(u)}let{url:p}=await d.json();if(p){try{sessionStorage.setItem("keyutils_stripe_pending","1");}catch{}window.location.href=p;}else throw new Error("No checkout URL returned")}catch(d){if(c.current){let p=d instanceof Error?d.message:"Checkout failed";g(p),r(false);}}},[e.apiBaseUrl,e.stackId]),loading:t,error:s}}var Ze=/^[1-9A-HJ-NP-Za-km-z]{32,44}$/;function oe({amountSol:e,onSuccess:t,onError:r,disabled:s,className:g,style:c}){let{config:f}=N(),[a,n]=useState(false),[d,p]=useState(null),u=!!(f.merchantWallet&&Ze.test(f.merchantWallet)),i=useCallback(async()=>{if(u)try{n(!0),p(null);let{Connection:o,PublicKey:l,Transaction:v,SystemProgram:P,LAMPORTS_PER_SOL:w}=await import('@solana/web3.js'),R=typeof window<"u"?window.solana:void 0;if(!R||typeof R!="object"||!R.isPhantom)throw new Error("Phantom wallet not found. Please install Phantom.");let B=R,M;try{M=new l(f.merchantWallet);}catch{throw new Error("Invalid merchant wallet address.")}let _=await B.connect(),k=new l(_.publicKey.toString()),W=new o(f.solanaRpcUrl||"https://api.mainnet-beta.solana.com"),{blockhash:A}=await W.getLatestBlockhash(),Z=BigInt(Math.floor(e*1e9)),X=new v({recentBlockhash:A,feePayer:k}).add(P.transfer({fromPubkey:k,toPubkey:M,lamports:Number(Z)})),{signature:K}=await B.signAndSendTransaction(X);t(K);}catch(o){let l=o instanceof Error?o:new Error("Payment failed");p(l.message),r?.(l);}finally{n(false);}},[e,f.merchantWallet,u,t,r]);return jsxs("div",{children:[jsx("button",{onClick:i,disabled:s||a||!u,"aria-label":`Pay ${e.toFixed(4)} SOL`,className:m("flex w-full items-center justify-center gap-2 rounded-lg bg-purple-600 px-4 py-3 font-medium text-white transition-colors hover:bg-purple-700 disabled:opacity-50 disabled:cursor-not-allowed",g),style:c,children:a?jsxs(Fragment,{children:[jsx(Loader2,{className:"h-4 w-4 animate-spin"}),"Processing..."]}):jsxs(Fragment,{children:["Pay ",e.toFixed(4)," SOL"]})}),d&&jsx("p",{className:"mt-2 text-center text-xs text-red-500",children:d})]})}function ae({result:e,className:t,style:r}){let[s,g]=useState(false),c=useRef(null);useEffect(()=>()=>{c.current&&clearTimeout(c.current);},[]);let f=useCallback(()=>{e.key?.key&&(navigator.clipboard.writeText(e.key.key),g(true),c.current&&clearTimeout(c.current),c.current=setTimeout(()=>g(false),2e3));},[e.key?.key]);return !e.success||!e.key?null:jsxs("div",{className:m("rounded-lg border border-green-500/20 bg-green-500/10 p-6 text-center",t),style:r,children:[jsx(CheckCircle,{className:"mx-auto mb-3 h-12 w-12 text-green-500"}),jsx("h3",{className:"mb-2 text-lg font-semibold text-foreground",children:"Key Generated!"}),jsx("p",{className:"mb-4 text-sm text-muted-foreground",children:"Your node key has been successfully generated."}),jsxs("div",{className:"mx-auto flex max-w-md items-center gap-2 rounded-md border bg-background p-3",children:[jsx("code",{className:"flex-1 truncate text-xs text-foreground",children:e.key.key}),jsx("button",{onClick:f,"aria-label":"Copy key to clipboard",className:"shrink-0 rounded-md p-1.5 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground",children:s?jsx(Check,{className:"h-4 w-4 text-green-500"}):jsx(Copy,{className:"h-4 w-4"})})]})]})}function I({className:e}){return jsx("div",{className:m("animate-pulse rounded-xl bg-muted",e)})}function ie(){return jsxs("div",{className:"space-y-6",children:[jsxs("div",{className:"px-6 py-10 flex flex-col items-center gap-3",children:[jsx(I,{className:"h-14 w-48"}),jsx(I,{className:"h-5 w-64"})]}),jsx(I,{className:"h-[72px] w-full rounded-2xl"}),jsx(I,{className:"h-[68px] w-full rounded-2xl"}),jsx(I,{className:"h-[60px] w-full rounded-2xl"})]})}function Y({count:e=3}){return jsx("div",{className:"space-y-3",children:Array.from({length:e}).map((t,r)=>jsxs("div",{className:"rounded-xl border p-4 space-y-3",children:[jsxs("div",{className:"flex items-center justify-between",children:[jsx(I,{className:"h-4 w-32"}),jsx(I,{className:"h-4 w-16"})]}),jsx(I,{className:"h-2 w-full rounded-full"}),jsxs("div",{className:"flex justify-between",children:[jsx(I,{className:"h-3 w-24"}),jsx(I,{className:"h-3 w-20"})]})]},r))})}function pt({quantity:e,max:t,onChange:r}){return jsxs("div",{className:"flex items-center justify-between rounded-2xl border bg-muted/20 px-6 py-5",children:[jsx("span",{className:"text-sm font-medium text-foreground",children:"Quantity"}),jsxs("div",{className:"flex items-center gap-0 rounded-xl border bg-background",children:[jsx("button",{onClick:()=>r(Math.max(1,e-1)),disabled:e<=1,"aria-label":"Decrease quantity",className:"flex h-12 w-12 items-center justify-center rounded-l-xl text-muted-foreground transition-colors hover:bg-muted hover:text-foreground disabled:opacity-30",children:jsx(Minus,{className:"h-5 w-5"})}),jsx("span",{className:"flex h-12 w-14 items-center justify-center border-x text-xl font-bold text-foreground","aria-live":"polite",children:e}),jsx("button",{onClick:()=>r(Math.min(t,e+1)),disabled:e>=t,"aria-label":"Increase quantity",className:"flex h-12 w-12 items-center justify-center rounded-r-xl text-muted-foreground transition-colors hover:bg-muted hover:text-foreground disabled:opacity-30",children:jsx(Plus,{className:"h-5 w-5"})})]})]})}function Te({pricing:e,quantity:t}){let[r,s]=useState(false),g=e.priceUsd*t;return jsxs("div",{className:"rounded-2xl border bg-background",children:[jsxs("button",{onClick:()=>s(!r),"aria-expanded":r,className:"flex w-full items-center justify-between px-6 py-5 text-left",children:[jsxs("span",{className:"text-foreground",children:[jsxs("span",{className:"text-xl font-bold",children:["$",g.toFixed(2)]})," ",jsx("span",{className:"text-sm text-muted-foreground",children:"total fees included"})]}),r?jsx(ChevronUp,{className:"h-5 w-5 text-muted-foreground"}):jsx(ChevronDown,{className:"h-5 w-5 text-muted-foreground"})]}),r&&jsxs("div",{className:"border-t px-6 pb-8 pt-4 space-y-3 text-sm",children:[jsxs("div",{className:"flex justify-between",children:[jsxs("span",{className:"text-muted-foreground",children:["Node Key \xD7 ",t]}),jsxs("span",{className:"text-foreground",children:["$",(e.priceUsd*t).toFixed(2)]})]}),jsxs("div",{className:"flex justify-between",children:[jsx("span",{className:"text-muted-foreground",children:"Keys sold"}),jsx("span",{className:"text-foreground",children:e.keysSold})]}),e.daysUntilHalving>0&&jsxs("div",{className:"flex justify-between text-muted-foreground",children:[jsx("span",{children:"Next refresh"}),jsxs("span",{children:[e.daysUntilHalving," day",e.daysUntilHalving!==1?"s":""]})]}),jsxs("div",{className:"flex justify-between border-t pt-3 font-semibold",children:[jsx("span",{className:"text-foreground",children:"Total"}),jsxs("span",{className:"text-foreground",children:["$",g.toFixed(2)]})]})]})]})}function le({className:e,style:t}){let{config:r}=N(),{pricing:s,loading:g,error:c,refresh:f}=Pe(),{mint:a,minting:n,result:d,error:p}=Ce(),{createSession:u,loading:i,error:o}=Ke(),[l,v]=useState(1),[P,w]=useState(r.paymentMethods?.includes("stripe")?"stripe":"solana"),[R,B]=useState(0),[M,_]=useState(false),k=at.useRef(l);k.current=l;let W=Math.max(1,r.maxQuantity||5),A=[...r.paymentMethods||["solana"]].sort((K,G)=>K==="stripe"?-1:G==="stripe"?1:0);if(useEffect(()=>{if(typeof window>"u")return;let G=new URLSearchParams(window.location.search).get("session_id");if(G&&/^cs_(test|live)_/.test(G)&&!M&&!d?.success){let ye=false;try{ye=sessionStorage.getItem("keyutils_stripe_pending")==="1";}catch{}if(!ye)return;try{sessionStorage.removeItem("keyutils_stripe_pending");}catch{}_(true);let be=new URL(window.location.href);be.searchParams.delete("session_id"),window.history.replaceState({},"",be.toString()),a("stripe",G,k.current);}},[M,d,a]),d?.success)return jsx("div",{className:m("flex flex-col items-center justify-center",e),style:t,children:jsx(ae,{result:d})});if(M||n)return jsx("div",{className:m("flex flex-col",e),style:t,children:jsx(ie,{})});if(g)return jsx("div",{className:m("flex flex-col",e),style:t,children:jsx(ie,{})});if(c||!s)return jsxs("div",{className:m("flex flex-col items-center justify-center gap-4 py-24",e),style:t,children:[jsxs("div",{className:"rounded-xl border border-red-500/20 bg-red-500/5 px-6 py-5 text-center max-w-sm",children:[jsx("p",{className:"text-sm font-medium text-foreground",children:"Unable to load pricing"}),jsx("p",{className:"mt-1 text-xs text-muted-foreground",children:c||"Please check your connection and try again."})]}),jsx("button",{onClick:()=>f(),className:"text-sm text-muted-foreground hover:text-foreground transition-colors",children:"Try again"})]});let Z=async K=>{B(2),await a(P,K,l);},X=()=>{u(s.priceCents,l);};return jsx("div",{className:m("flex flex-col",e),style:t,children:jsxs("div",{className:"space-y-6",children:[R===0&&jsxs(Fragment,{children:[r.keyImage&&jsx("img",{src:r.keyImage,alt:"Node Key",className:r.keyImageClass||"w-1/2 mx-auto"}),jsxs("div",{className:"px-6 py-10 text-center",children:[jsxs("p",{className:"text-5xl font-bold tracking-tight text-foreground md:text-6xl",children:["$",s.priceUsd.toFixed(2)]}),jsxs("p",{className:"mt-2 text-base text-muted-foreground",children:["per key \xB7 ",s.tokenAllocationFormatted||L(s.tokenAllocation)," tokens included"]})]}),jsx(pt,{quantity:l,max:W,onChange:v}),jsx(Te,{pricing:s,quantity:l}),jsx("button",{onClick:()=>B(1),className:"w-full rounded-2xl bg-foreground py-5 text-center text-base font-semibold text-background transition-colors hover:bg-foreground/90",children:"Continue to Payment"})]}),R===1&&jsxs(Fragment,{children:[A.length>1&&jsx("div",{role:"tablist",className:"flex gap-1 rounded-2xl bg-muted/50 p-1.5",children:A.map(K=>jsxs("button",{role:"tab","aria-selected":P===K,onClick:()=>w(K),className:m("flex flex-1 items-center justify-center gap-2.5 rounded-xl py-4 text-sm font-medium transition-colors",P===K?"bg-background text-foreground shadow-sm":"text-muted-foreground hover:text-foreground"),children:[K==="stripe"?jsx(CreditCard,{className:"h-5 w-5"}):jsx(Wallet,{className:"h-5 w-5"}),K==="stripe"?"Credit Card":"Crypto"]},K))}),jsx(Te,{pricing:s,quantity:l}),P==="stripe"?jsxs(Fragment,{children:[jsx("button",{onClick:X,disabled:i,className:"flex w-full items-center justify-center gap-2 rounded-2xl bg-foreground py-5 text-base font-semibold text-background transition-colors hover:bg-foreground/90 disabled:opacity-50",children:i?jsxs(Fragment,{children:[jsx(Loader2,{className:"h-5 w-5 animate-spin"}),"Redirecting to Stripe..."]}):jsxs(Fragment,{children:[jsx(CreditCard,{className:"h-5 w-5"}),"Pay with Card"]})}),o&&jsx("p",{className:"text-center text-sm text-red-500",children:o})]}):P==="solana"?jsx(oe,{amountSol:s.priceSol??0,onSuccess:Z,className:"rounded-2xl py-5 text-base"}):null,p&&jsx("p",{className:"text-center text-sm text-red-500",children:p}),jsx("button",{onClick:()=>B(0),className:"w-full py-3 text-center text-sm text-muted-foreground hover:text-foreground transition-colors",children:"\u2190 Back to Configure"})]})]})})}var bt={ios:"iOS",android:"Android",windows:"Windows",mac:"macOS",linux:"Linux"},ht=["mac","windows","linux","ios","android"];function ce({downloads:e,className:t,style:r}){let{config:s}=N(),g=useMemo(()=>ke(),[]),c=e||ht.map(n=>({platform:n,label:bt[n],url:"#",compatible:n===g})),f=c.find(n=>n.compatible)||c[0],a=c.filter(n=>n.platform!==f.platform);return jsxs("div",{className:m("space-y-8",t),style:r,children:[jsxs("div",{className:"flex flex-col items-center gap-6 pt-4 mt-4",children:[s.aispImage&&jsx("img",{src:s.aispImage,alt:"aiSP",className:"w-full sm:w-1/2 mx-auto px-4 sm:px-0"}),jsxs("div",{className:"text-center",children:[jsx("p",{className:"text-lg sm:text-2xl font-medium text-foreground",children:"Download the AI Service Provider App"}),jsx("p",{className:"mt-1 text-sm sm:text-base text-muted-foreground",children:"Load your keys and run your node."})]}),jsxs("a",{href:f.url,className:"inline-flex items-center gap-3 rounded-full px-10 py-4 text-lg font-semibold text-white transition-colors hover:opacity-90",style:{backgroundColor:"#0900f4"},children:[jsx(Download,{className:"h-5 w-5"}),"Get aiSP for ",f.label]})]}),jsxs("p",{className:"text-center text-sm text-muted-foreground",children:["aiSP is also available on"," ",a.map((n,d)=>jsxs(at.Fragment,{children:[d>0&&(d===a.length-1?" and ":", "),jsx("a",{href:n.url,className:"font-medium underline transition-colors hover:text-foreground",style:{color:"#066bff"},children:n.label})]},n.platform)),"."]}),jsxs("div",{className:"flex flex-col items-center gap-2 mt-12",children:[jsxs("p",{className:"text-center text-xs text-muted-foreground flex justify-center gap-0.5 flex-wrap",children:["Source code is available on",jsx("a",{href:"https://github.com/stack-net",target:"_blank",rel:"noopener noreferrer",className:"text-foreground underline hover:text-foreground/80 color-[#0900f4]",children:"Github"})]}),jsxs("p",{className:"text-center text-xs text-muted-foreground flex justify-center gap-0.5 flex-wrap",children:["By downloading, you agree to the",jsx("a",{href:s.termsUrl||"/terms",className:"text-foreground underline hover:text-foreground/80",children:"Open Source Applications Terms"})]})]})]})}function wt(e){return {id:e.id||"",key:e.key||e.keyPrefix||"",userId:e.userId||e.user_id||"",status:e.status||"active",tokenBalance:e.tokenBalance??e.currentTokenBalance??e.current_token_balance??0,maxTokens:e.maxTokens??e.initialTokenBalance??e.initial_token_balance??0,createdAt:String(e.createdAt??e.created_at??""),expiresAt:e.expiresAt,label:e.label||e.keyPrefix||e.key_prefix||void 0,paperWork:e.paperWork||e.paper_work||void 0,stackId:e.stackId,stackName:e.stackName}}function J(){let{config:e}=N(),[t,r]=useState([]),[s,g]=useState(true),[c,f]=useState(null),a=useRef(true),n=e.apiBaseUrl,d=useCallback(async()=>{try{g(!0),f(null);let u=await fetch(`${n}/api/keys`);if(!u.ok){let l=`Failed to fetch keys (${u.status})`;try{let v=await u.json();v.error&&(l=v.error);}catch{}throw new Error(l)}let i=await u.json(),o=Array.isArray(i)?i:i.keys??[];a.current&&r(o.map(wt));}catch(u){a.current&&f(u instanceof Error?u.message:"Failed to fetch keys");}finally{a.current&&g(false);}},[n]);useEffect(()=>(a.current=true,d(),()=>{a.current=false;}),[d]);let p=t.reduce((u,i)=>u+i.tokenBalance,0);return {keys:t,totalBalance:p,loading:s,error:c,refresh:d}}function Ie(){let{config:e,callbacks:t}=N(),[r,s]=useState(false),[g,c]=useState(null),[f,a]=useState(null),n=useRef(true),d=useCallback(async(u,i)=>{try{s(!0),a(null);let o=await fetch(`${e.apiBaseUrl}/api/keys/${u}/list`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({askPriceCents:i,stackId:e.stackId,stackName:e.stackName})});if(!o.ok){let v=`Listing failed (${o.status})`;try{let P=await o.json();P.error&&(v=P.error);}catch{}throw new Error(v)}let l=await o.json();return n.current&&(c(l),t.onListingCreated?.(l)),l}catch(o){let l=o instanceof Error?o.message:"Listing failed";return n.current&&a(l),null}finally{n.current&&s(false);}},[e.apiBaseUrl,e.stackId,e.stackName,t]),p=useCallback(async u=>{try{s(!0),a(null);let i=await fetch(`${e.apiBaseUrl}/api/keys/${u}/list`,{method:"DELETE"});if(!i.ok){let o=`Delist failed (${i.status})`;try{let l=await i.json();l.error&&(o=l.error);}catch{}throw new Error(o)}return n.current&&c(null),!0}catch(i){let o=i instanceof Error?i.message:"Delist failed";return n.current&&a(o),false}finally{n.current&&s(false);}},[e.apiBaseUrl]);return {listKey:d,delistKey:p,listing:r,result:g,error:f}}function z({current:e,max:t,className:r,style:s,showLabel:g=true}){let c=Ne(e,t),f=ve(c);return jsxs("div",{className:m("w-full",r),style:s,children:[g&&jsxs("div",{className:"mb-1 flex items-center justify-between text-xs text-muted-foreground",children:[jsxs("span",{children:[L(e)," / ",L(t)]}),jsxs("span",{children:[c,"%"]})]}),jsx("div",{className:"h-2 w-full overflow-hidden rounded-full bg-muted",children:jsx("div",{className:m("h-full rounded-full transition-all duration-500",f),style:{width:`${c}%`}})})]})}function Ut({nodeKey:e,selected:t,onSelect:r}){return jsxs("button",{onClick:r,className:m("flex w-full items-start gap-3 rounded-xl border p-4 text-left transition-all",t?"border-foreground/30 bg-foreground/5 ring-1 ring-foreground/10":"border-border hover:border-foreground/20"),children:[jsx("div",{className:m("mt-0.5 flex h-5 w-5 shrink-0 items-center justify-center rounded-full border-2 transition-colors",t?"border-foreground bg-foreground":"border-muted-foreground/40"),children:t&&jsx(Check,{className:"h-3 w-3 text-background"})}),jsxs("div",{className:"min-w-0 flex-1",children:[jsxs("div",{className:"flex items-center justify-between",children:[jsx("span",{className:"text-sm font-medium text-foreground",children:e.label||e.id.slice(0,16)}),jsx("span",{className:m("text-xs font-medium capitalize",e.status==="active"?"text-green-500":"text-muted-foreground"),children:e.status})]}),jsx(z,{current:e.tokenBalance,max:e.maxTokens,className:"mt-2"})]})]})}function fe({className:e,style:t}){let{config:r}=N(),{keys:s,loading:g,error:c,refresh:f}=J(),{listKey:a,listing:n,result:d,error:p}=Ie(),[u,i]=useState(null),[o,l]=useState(""),[v,P]=useState(false),w=s.find(k=>k.id===u),R=parseInt(o.replace(/[^0-9]/g,""),10)||0,B=w&&R>0&&v&&!n,M=async()=>{if(!B||!w)return;let k=R*100;await a(w.id,k)&&f();};if(d)return jsxs("div",{className:m("flex flex-col items-center gap-4 rounded-xl border border-green-500/20 bg-green-500/10 py-12",e),style:t,children:[jsx(CheckCircle,{className:"h-12 w-12 text-green-500"}),jsx("h3",{className:"text-lg font-semibold text-foreground",children:"Key Listed!"}),jsx("p",{className:"text-sm text-muted-foreground",children:"Your key is now on the Global Open AI Network marketplace."}),jsxs("p",{className:"text-xs text-muted-foreground",children:["Listing ID: ",jsx("code",{children:d.listingId})]})]});if(g)return jsxs("div",{className:m("space-y-5 px-1",e),style:t,children:[jsx("h3",{className:"text-2xl mt-3 mb-2 font-semibold text-foreground",children:"Your Keys"}),jsx(Y,{count:2})]});if(c)return jsxs("div",{className:m("flex flex-col items-center justify-center gap-4 py-16",e),style:t,children:[jsxs("div",{className:"rounded-xl border border-red-500/20 bg-red-500/5 px-6 py-5 text-center max-w-sm",children:[jsx("p",{className:"text-sm font-medium text-foreground",children:"Unable to load your keys"}),jsx("p",{className:"mt-1 text-xs text-muted-foreground",children:c})]}),jsx("button",{onClick:()=>f(),className:"text-sm text-muted-foreground hover:text-foreground transition-colors",children:"Try again"})]});let _=s.filter(k=>k.status==="active");return _.length===0?jsxs("div",{className:m("flex flex-col items-center justify-center gap-3 rounded-xl border border-dashed py-16",e),style:t,children:[jsx(AlertTriangle,{className:"h-8 w-8 text-muted-foreground"}),jsx("p",{className:"text-sm text-muted-foreground",children:"No active keys to sell."})]}):jsxs("div",{className:m("space-y-5",e),style:t,children:[jsxs("div",{children:[jsx("h3",{className:"text-2xl mt-3 mb-2 font-semibold text-foreground",children:"Your Keys"}),jsx("div",{className:"space-y-2",children:_.map(k=>jsx(Ut,{nodeKey:k,selected:u===k.id,onSelect:()=>i(k.id)},k.id))})]}),w&&jsxs("div",{className:"rounded-xl border bg-muted/30 p-5 space-y-2 text-sm",children:[jsxs("div",{className:"flex justify-between",children:[jsx("span",{className:"text-muted-foreground",children:"Key ID"}),jsx("code",{className:"text-xs text-foreground",children:w.id})]}),jsxs("div",{className:"flex justify-between",children:[jsx("span",{className:"text-muted-foreground",children:"Originating Stack"}),jsx("span",{className:"text-foreground",children:r.stackName||"Unknown"})]}),jsxs("div",{className:"flex justify-between",children:[jsx("span",{className:"text-muted-foreground",children:"Tokens (remaining / total)"}),jsxs("span",{className:"text-foreground",children:[L(w.tokenBalance)," / ",L(w.maxTokens)]})]}),w.paperWork&&jsxs("div",{className:"flex justify-between",children:[jsx("span",{className:"text-muted-foreground",children:"Paper earned"}),jsx("span",{className:"text-foreground",children:w.paperWork})]}),jsxs("div",{className:"flex justify-between",children:[jsx("span",{className:"text-muted-foreground",children:"Created"}),jsx("span",{className:"text-foreground",children:F(w.createdAt)})]})]}),jsxs("div",{children:[jsx("label",{htmlFor:"listing-price",className:"mb-2 block text-sm font-semibold text-foreground",children:"Listing Price (USD)"}),jsxs("div",{className:"relative",children:[jsx("span",{className:"absolute left-4 top-1/2 -translate-y-1/2 text-2xl font-bold text-muted-foreground",children:"$"}),jsx("input",{id:"listing-price",type:"text",inputMode:"numeric",value:o,onChange:k=>{let W=k.target.value.replace(/[^0-9]/g,""),A=parseInt(W,10);l(isNaN(A)?"":A.toLocaleString("en-US"));},placeholder:"0",className:"w-full rounded-xl border bg-background py-5 pl-10 pr-4 text-3xl font-bold text-foreground placeholder:text-muted-foreground/30 focus:outline-none focus:ring-2 focus:ring-foreground/20"})]})]}),jsxs("label",{className:"flex items-start gap-3 cursor-pointer",children:[jsx("input",{type:"checkbox",checked:v,onChange:k=>P(k.target.checked),className:"sr-only"}),jsx("div",{"aria-hidden":"true",className:m("mt-0.5 flex h-5 w-5 shrink-0 items-center justify-center rounded border-2 transition-colors",v?"border-foreground bg-foreground":"border-muted-foreground/40"),children:v&&jsx(Check,{className:"h-3 w-3 text-background"})}),jsxs("span",{className:"text-xs text-muted-foreground leading-relaxed",children:["I agree to list this key on the Global Open AI Network marketplace. The key will be deactivated upon sale and transferred to the buyer. Listing fees may apply."," ",jsx("a",{href:r.termsUrl||"/terms",className:"text-foreground underline",children:"Terms"})]})]}),p&&jsx("p",{className:"text-sm text-red-500",children:p}),jsxs("button",{onClick:M,disabled:!B,className:m("flex w-full items-center justify-center gap-2 rounded-xl py-4 text-sm font-semibold transition-colors",B?"bg-foreground text-background hover:bg-foreground/90":"bg-muted text-muted-foreground cursor-not-allowed"),children:[jsx(ExternalLink,{className:"h-4 w-4"}),n?"Listing...":"List on Marketplace"]})]})}var Bt=[{id:"buy",label:"Buy"},{id:"use",label:"Use"},{id:"sell",label:"Sell"}];function Lt({active:e,onChange:t}){return jsx("div",{role:"tablist","aria-label":"Key actions",className:"flex gap-1 rounded-2xl bg-muted/50 p-1.5",children:Bt.map(({id:r,label:s})=>jsx("button",{role:"tab","aria-selected":e===r,"aria-controls":`panel-${r}`,id:`tab-${r}`,onClick:()=>t(r),className:m("flex flex-1 items-center justify-center gap-2.5 rounded-xl py-3.5 text-sm font-medium transition-all",e===r?"bg-background text-foreground shadow-sm":"text-muted-foreground hover:text-foreground"),children:jsx("span",{className:"inline",children:s})},r))})}function Tt({defaultTab:e="buy",className:t,style:r}){let[s,g]=useState(e);return jsxs("div",{className:m("flex flex-col",t),style:r,children:[jsx("div",{className:"sticky top-0 z-10 bg-background/80 px-5 pb-3 pt-2 sm:pt-5 backdrop-blur-sm sm:px-10 md:px-16 lg:px-20",children:jsx("div",{className:"mx-auto max-w-xl",children:jsx(Lt,{active:s,onChange:g})})}),jsx("div",{className:"flex flex-1 flex-col px-5 pb-8 sm:px-10 md:px-16 md:pb-12 lg:px-20",children:jsx("div",{className:"mx-auto w-full max-w-xl flex-1",children:jsxs("div",{role:"tabpanel",id:`panel-${s}`,"aria-labelledby":`tab-${s}`,children:[s==="buy"&&jsx(le,{className:"flex-1"}),s==="use"&&jsx(ce,{className:"flex-1"}),s==="sell"&&jsx(fe,{className:"flex-1"})]})})})]})}function Et({apiBaseUrl:e,stackId:t,stackName:r,paymentMethods:s,merchantWallet:g,stripePublicKey:c,theme:f,maxQuantity:a,keyImage:n,keyImageClass:d,aispImage:p,termsUrl:u,onMintSuccess:i,onMintError:o,onPaymentStart:l,onPaymentComplete:v,onListingCreated:P,defaultTab:w,className:R,style:B}){return jsx(ee,{config:{apiBaseUrl:e,stackId:t,stackName:r,paymentMethods:s,merchantWallet:g,stripePublicKey:c,theme:f,maxQuantity:a,keyImage:n,keyImageClass:d,aispImage:p,termsUrl:u},callbacks:{onMintSuccess:i,onMintError:o,onPaymentStart:l,onPaymentComplete:v,onListingCreated:P},children:jsx(Tt,{defaultTab:w,className:R,style:B})})}function Ae(e){let{config:t}=N(),[r,s]=useState([]),[g,c]=useState(false),[f,a]=useState(null),n=useRef(true),d=t.apiBaseUrl,p=useCallback(async()=>{if(e)try{c(!0),a(null);let u=await fetch(`${d}/api/keys/${e}/ledger`);if(!u.ok){let o=`Failed to fetch ledger (${u.status})`;try{let l=await u.json();l.error&&(o=l.error);}catch{}throw new Error(o)}let i=await u.json();n.current&&s(Array.isArray(i)?i:i.entries??[]);}catch(u){n.current&&a(u instanceof Error?u.message:"Failed to fetch ledger");}finally{n.current&&c(false);}},[d,e]);return useEffect(()=>(n.current=true,p(),()=>{n.current=false;}),[p]),{entries:r,loading:g,error:f,refresh:p}}var ge=20;function xe({nodeKey:e,className:t,style:r}){let[s,g]=useState(false),[c,f]=useState(false),a=useRef(null),{entries:n,loading:d}=Ae(s?e.id:null);useEffect(()=>()=>{a.current&&clearTimeout(a.current);},[]);let p=useCallback(()=>{e.key&&(navigator.clipboard.writeText(e.key),f(true),a.current&&clearTimeout(a.current),a.current=setTimeout(()=>f(false),2e3));},[e.key]),u=e.status==="active"?"text-green-500":e.status==="expired"?"text-red-500":"text-yellow-500",i=n.slice(0,ge);return jsxs("div",{className:m("rounded-lg border bg-background",t),style:r,children:[jsxs("div",{className:"p-4",children:[jsxs("div",{className:"mb-3 flex items-center justify-between",children:[jsxs("div",{className:"flex items-center gap-2",children:[jsx("button",{onClick:()=>g(!s),"aria-expanded":s,"aria-label":s?"Collapse ledger":"Expand ledger",className:"text-muted-foreground hover:text-foreground",children:s?jsx(ChevronDown,{className:"h-4 w-4"}):jsx(ChevronRight,{className:"h-4 w-4"})}),jsx("span",{className:"text-sm font-medium text-foreground",children:e.label||`Key ${e.id.slice(0,8)}`})]}),jsx("span",{className:m("text-xs font-medium capitalize",u),children:e.status})]}),e.key&&jsxs("div",{className:"mb-3 flex items-center gap-2",children:[jsx("code",{className:"flex-1 truncate rounded bg-muted px-2 py-1 text-xs text-muted-foreground",children:e.key}),jsx("button",{onClick:p,"aria-label":"Copy key to clipboard",className:"shrink-0 rounded p-1 text-muted-foreground hover:bg-muted hover:text-foreground",children:c?jsx(Check,{className:"h-3.5 w-3.5 text-green-500"}):jsx(Copy,{className:"h-3.5 w-3.5"})})]}),jsx(z,{current:e.tokenBalance,max:e.maxTokens}),jsxs("div",{className:"mt-2 flex items-center justify-between text-xs text-muted-foreground",children:[jsxs("span",{children:["Created ",F(e.createdAt)]}),e.expiresAt&&jsxs("span",{children:["Expires ",F(e.expiresAt)]})]})]}),s&&jsxs("div",{className:"border-t px-4 py-3",children:[jsx("h4",{className:"mb-2 text-xs font-medium text-muted-foreground",children:"Ledger"}),d?jsx("p",{className:"text-xs text-muted-foreground",children:"Loading..."}):i.length===0?jsx("p",{className:"text-xs text-muted-foreground",children:"No ledger entries."}):jsxs("div",{className:"space-y-1.5",children:[i.map(o=>jsxs("div",{className:"flex items-center justify-between text-xs",children:[jsxs("div",{className:"flex items-center gap-2",children:[jsxs("span",{className:m("font-medium",o.type==="credit"||o.type==="reward"?"text-green-500":"text-red-500"),children:[o.type==="credit"||o.type==="reward"?"+":"-",L(Math.abs(o.amount))]}),jsx("span",{className:"text-muted-foreground",children:o.description})]}),jsx("span",{className:"text-muted-foreground",children:F(o.createdAt)})]},o.id)),n.length>ge&&jsxs("p",{className:"pt-1 text-xs text-muted-foreground",children:["Showing ",ge," of ",n.length," entries"]})]})]})]})}function Qt({getKeyHref:e="/keys/get",className:t,style:r}){let{keys:s,totalBalance:g,loading:c,error:f,refresh:a}=J();return c?jsx("div",{className:m("space-y-4",t),style:r,children:jsx(Y,{count:3})}):f?jsxs("div",{className:m("flex flex-col items-center justify-center gap-4 py-12",t),style:r,children:[jsxs("div",{className:"rounded-xl border border-red-500/20 bg-red-500/5 px-6 py-5 text-center max-w-sm",children:[jsx("p",{className:"text-sm font-medium text-foreground",children:"Unable to load your keys"}),jsx("p",{className:"mt-1 text-xs text-muted-foreground",children:f})]}),jsx("button",{onClick:()=>a(),className:"text-sm text-muted-foreground hover:text-foreground transition-colors",children:"Try again"})]}):s.length===0?jsxs("div",{className:m("flex flex-col items-center justify-center gap-4 rounded-lg border border-dashed py-12",t),style:r,children:[jsx(KeyRound,{className:"h-10 w-10 text-muted-foreground"}),jsxs("div",{className:"text-center",children:[jsx("p",{className:"text-sm font-medium text-foreground",children:"No keys yet"}),jsx("p",{className:"text-xs text-muted-foreground",children:"Get your first node key to start earning."})]}),e&&jsx("a",{href:e,className:"rounded-lg bg-foreground px-4 py-2 text-sm font-medium text-background transition-colors hover:bg-foreground/90",children:"Get a Key"})]}):jsxs("div",{className:m("space-y-4",t),style:r,children:[jsxs("div",{className:"flex items-center justify-between",children:[jsxs("p",{className:"text-sm text-muted-foreground",children:[s.length," key",s.length!==1?"s":""," \xB7 Total balance: ",L(g)]}),e&&jsx("a",{href:e,className:"rounded-md bg-foreground px-3 py-1.5 text-xs font-medium text-background transition-colors hover:bg-foreground/90",children:"Get a Key"})]}),s.map(n=>jsx(xe,{nodeKey:n},n.id))]})}
|
|
2
|
-
export{
|
|
1
|
+
import it,{createContext,useContext,useMemo,useState,useCallback,useRef,useEffect}from'react';import {jsx,jsxs,Fragment}from'react/jsx-runtime';import {clsx}from'clsx';import {twMerge}from'tailwind-merge';import {Loader2,CheckCircle,Check,Copy,CreditCard,Wallet,Download,AlertTriangle,ExternalLink,ChevronDown,ChevronRight,KeyRound,Minus,Plus,ChevronUp}from'lucide-react';var ve=createContext(null);function v(){let e=useContext(ve);if(!e)throw new Error("useKeyUtilsContext must be used within a KeyUtilsProvider");return e}function te({config:e,callbacks:t={},children:r}){typeof e.apiBaseUrl!="string"&&console.error("[KeyUtils] apiBaseUrl must be a string");let s=useMemo(()=>({config:e,callbacks:t}),[e,t]);return jsx(ve.Provider,{value:s,children:r})}function f(...e){return twMerge(clsx(e))}function T(e){return e==null?"0":e>=1e9?`${(e/1e9).toFixed(1)}B`:e>=1e6?`${(e/1e6).toFixed(1)}M`:e>=1e3?`${(e/1e3).toFixed(1)}K`:e.toLocaleString()}function Ne(e,t){return t<=0?0:Math.min(100,Math.round(e/t*100))}function ke(e){return e>=70?"bg-green-500":e>=30?"bg-yellow-500":"bg-red-500"}function F(e){let t=typeof e=="string"&&/^\d+$/.test(e)?Number(e):e,r=new Date(t);return isNaN(r.getTime())?String(e):r.toLocaleDateString("en-US",{month:"short",day:"numeric",year:"numeric"})}function we(){if(typeof navigator>"u")return "mac";let e=navigator.userAgent.toLowerCase();return /iphone|ipad|ipod/.test(e)?"ios":/android/.test(e)?"android":/win/.test(e)?"windows":/linux/.test(e)?"linux":"mac"}function Ce(){let{config:e}=v(),[t,r]=useState(null),[s,d]=useState(true),[u,g]=useState(null),a=useRef(true),n=useRef(e.apiBaseUrl);n.current=e.apiBaseUrl;let i=async()=>{try{a.current&&(d(!0),g(null));let p=await fetch(`${n.current}/api/keys/pricing`);if(!p.ok){let l=`Pricing unavailable (${p.status})`;try{let o=await p.json();o.error&&(l=o.error);}catch{}throw new Error(l)}let m=await p.json();a.current&&r(m);}catch(p){a.current&&g(p instanceof Error?p.message:"Failed to fetch pricing");}finally{a.current&&d(false);}};return useEffect(()=>{a.current=true,i();let p=setInterval(i,6e4);return ()=>{a.current=false,clearInterval(p);}},[]),{pricing:t,loading:s,error:u,refresh:i}}function Se(){let{config:e,callbacks:t}=v(),[r,s]=useState(false),[d,u]=useState(null),[g,a]=useState(null);return {mint:useCallback(async(i,p,m=1)=>{try{s(!0),a(null),t.onPaymentComplete?.(i,p);let l=await fetch(`${e.apiBaseUrl}/api/keys/mint`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({paymentMethod:i,paymentRef:p,quantity:m})});if(!l.ok){let P="Key generation failed";try{let w=await l.json();w.error&&(P=w.error);}catch{}throw new Error(P)}let o=await l.json(),c=o.keys?.[0]||o,N={success:!0,key:{id:c.keyId||c.id||"",key:c.key||"",userId:c.userId||"",status:"active",tokenBalance:c.tokenBalance??c.currentTokenBalance??0,maxTokens:c.initialTokenBalance??c.tokenBalance??0,createdAt:String(c.createdAt||Date.now())},transactionId:p};return u(N),t.onMintSuccess?.(N),N}catch(l){let o=l instanceof Error?l:new Error("Key generation failed");return a(o.message),t.onMintError?.(o),{success:false,error:o.message}}finally{s(false);}},[e.apiBaseUrl,t]),minting:r,result:d,error:g}}function Ue(){let{config:e}=v(),[t,r]=useState(false),[s,d]=useState(null),u=useRef(true);return {createSession:useCallback(async(a,n)=>{try{r(!0),d(null);let i=await fetch(`${e.apiBaseUrl}/api/keys/stripe-session`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({priceCents:a,quantity:n,stackId:e.stackId,referrerUrl:window.location.href})});if(!i.ok){let m=`Checkout failed (${i.status})`;try{let l=await i.json();l.error&&(m=l.error);}catch{}throw new Error(m)}let{url:p}=await i.json();if(p){try{sessionStorage.setItem("keyutils_stripe_pending","1");}catch{}window.location.href=p;}else throw new Error("No checkout URL returned")}catch(i){if(u.current){let p=i instanceof Error?i.message:"Checkout failed";d(p),r(false);}}},[e.apiBaseUrl,e.stackId]),loading:t,error:s}}var He=/^[1-9A-HJ-NP-Za-km-z]{32,44}$/;function ae({amountSol:e,onSuccess:t,onError:r,disabled:s,className:d,style:u}){let{config:g}=v(),[a,n]=useState(false),[i,p]=useState(null),m=!!(g.merchantWallet&&He.test(g.merchantWallet)),l=useCallback(async()=>{if(m)try{n(!0),p(null);let{Connection:o,PublicKey:c,Transaction:N,SystemProgram:P,LAMPORTS_PER_SOL:w}=await import('@solana/web3.js'),B=typeof window<"u"?window.solana:void 0;if(!B||typeof B!="object"||!B.isPhantom)throw new Error("Phantom wallet not found. Please install Phantom.");let L=B,j;try{j=new c(g.merchantWallet);}catch{throw new Error("Invalid merchant wallet address.")}let A=await L.connect(),k=new c(A.publicKey.toString()),W=new o(g.solanaRpcUrl||"https://api.mainnet-beta.solana.com"),{blockhash:$}=await W.getLatestBlockhash(),H=BigInt(Math.floor(e*1e9)),ee=new N({recentBlockhash:$,feePayer:k}).add(P.transfer({fromPubkey:k,toPubkey:j,lamports:Number(H)})),{signature:U}=await L.signAndSendTransaction(ee);t(U);}catch(o){let c=o instanceof Error?o:new Error("Payment failed");p(c.message),r?.(c);}finally{n(false);}},[e,g.merchantWallet,m,t,r]);return jsxs("div",{children:[jsx("button",{onClick:l,disabled:s||a||!m,"aria-label":`Pay ${e.toFixed(4)} SOL`,className:f("flex w-full items-center justify-center gap-2 rounded-lg bg-purple-600 px-4 py-3 font-medium text-white transition-colors hover:bg-purple-700 disabled:opacity-50 disabled:cursor-not-allowed",d),style:u,children:a?jsxs(Fragment,{children:[jsx(Loader2,{className:"h-4 w-4 animate-spin"}),"Processing..."]}):jsxs(Fragment,{children:["Pay ",e.toFixed(4)," SOL"]})}),i&&jsx("p",{className:"mt-2 text-center text-xs text-red-500",children:i})]})}function ie({result:e,className:t,style:r}){let[s,d]=useState(false),u=useRef(null);useEffect(()=>()=>{u.current&&clearTimeout(u.current);},[]);let g=useCallback(()=>{e.key?.key&&(navigator.clipboard.writeText(e.key.key),d(true),u.current&&clearTimeout(u.current),u.current=setTimeout(()=>d(false),2e3));},[e.key?.key]);return !e.success||!e.key?null:jsxs("div",{className:f("rounded-lg border border-green-500/20 bg-green-500/10 p-6 text-center",t),style:r,children:[jsx(CheckCircle,{className:"mx-auto mb-3 h-12 w-12 text-green-500"}),jsx("h3",{className:"mb-2 text-lg font-semibold text-foreground",children:"Key Generated!"}),jsx("p",{className:"mb-4 text-sm text-muted-foreground",children:"Your node key has been successfully generated."}),jsxs("div",{className:"mx-auto flex max-w-md items-center gap-2 rounded-md border bg-background p-3",children:[jsx("code",{className:"flex-1 truncate text-xs text-foreground",children:e.key.key}),jsx("button",{onClick:g,"aria-label":"Copy key to clipboard",className:"shrink-0 rounded-md p-1.5 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground",children:s?jsx(Check,{className:"h-4 w-4 text-green-500"}):jsx(Copy,{className:"h-4 w-4"})})]})]})}function I({className:e}){return jsx("div",{className:f("animate-pulse rounded-xl bg-muted",e)})}function le(){return jsxs("div",{className:"space-y-6",children:[jsxs("div",{className:"px-6 py-10 flex flex-col items-center gap-3",children:[jsx(I,{className:"h-14 w-48"}),jsx(I,{className:"h-5 w-64"})]}),jsx(I,{className:"h-[72px] w-full rounded-2xl"}),jsx(I,{className:"h-[68px] w-full rounded-2xl"}),jsx(I,{className:"h-[60px] w-full rounded-2xl"})]})}function Y({count:e=3}){return jsx("div",{className:"space-y-3",children:Array.from({length:e}).map((t,r)=>jsxs("div",{className:"rounded-xl border p-4 space-y-3",children:[jsxs("div",{className:"flex items-center justify-between",children:[jsx(I,{className:"h-4 w-32"}),jsx(I,{className:"h-4 w-16"})]}),jsx(I,{className:"h-2 w-full rounded-full"}),jsxs("div",{className:"flex justify-between",children:[jsx(I,{className:"h-3 w-24"}),jsx(I,{className:"h-3 w-20"})]})]},r))})}function gt({quantity:e,max:t,onChange:r}){return jsxs("div",{className:"flex items-center justify-between rounded-2xl border bg-muted/20 px-6 py-5",children:[jsx("span",{className:"text-sm font-medium text-foreground",children:"Quantity"}),jsxs("div",{className:"flex items-center gap-0 rounded-xl border bg-background",children:[jsx("button",{onClick:()=>r(Math.max(1,e-1)),disabled:e<=1,"aria-label":"Decrease quantity",className:"flex h-12 w-12 items-center justify-center rounded-l-xl text-muted-foreground transition-colors hover:bg-muted hover:text-foreground disabled:opacity-30",children:jsx(Minus,{className:"h-5 w-5"})}),jsx("span",{className:"flex h-12 w-14 items-center justify-center border-x text-xl font-bold text-foreground","aria-live":"polite",children:e}),jsx("button",{onClick:()=>r(Math.min(t,e+1)),disabled:e>=t,"aria-label":"Increase quantity",className:"flex h-12 w-12 items-center justify-center rounded-r-xl text-muted-foreground transition-colors hover:bg-muted hover:text-foreground disabled:opacity-30",children:jsx(Plus,{className:"h-5 w-5"})})]})]})}function Ee({pricing:e,quantity:t}){let[r,s]=useState(false),d=e.priceUsd*t;return jsxs("div",{className:"rounded-2xl border bg-background",children:[jsxs("button",{onClick:()=>s(!r),"aria-expanded":r,className:"flex w-full items-center justify-between px-6 py-5 text-left",children:[jsxs("span",{className:"text-foreground",children:[jsxs("span",{className:"text-xl font-bold",children:["$",d.toFixed(2)]})," ",jsx("span",{className:"text-sm text-muted-foreground",children:"total fees included"})]}),r?jsx(ChevronUp,{className:"h-5 w-5 text-muted-foreground"}):jsx(ChevronDown,{className:"h-5 w-5 text-muted-foreground"})]}),r&&jsxs("div",{className:"border-t px-6 pb-8 pt-4 space-y-3 text-sm",children:[jsxs("div",{className:"flex justify-between",children:[jsxs("span",{className:"text-muted-foreground",children:["Node Key \xD7 ",t]}),jsxs("span",{className:"text-foreground",children:["$",(e.priceUsd*t).toFixed(2)]})]}),jsxs("div",{className:"flex justify-between",children:[jsx("span",{className:"text-muted-foreground",children:"Keys sold"}),jsx("span",{className:"text-foreground",children:e.keysSold})]}),e.daysUntilHalving>0&&jsxs("div",{className:"flex justify-between text-muted-foreground",children:[jsx("span",{children:"Next refresh"}),jsxs("span",{children:[e.daysUntilHalving," day",e.daysUntilHalving!==1?"s":""]})]}),jsxs("div",{className:"flex justify-between border-t pt-3 font-semibold",children:[jsx("span",{className:"text-foreground",children:"Total"}),jsxs("span",{className:"text-foreground",children:["$",d.toFixed(2)]})]})]})]})}function ce({className:e,style:t}){let{config:r}=v(),{pricing:s,loading:d,error:u,refresh:g}=Ce(),{mint:a,minting:n,result:i,error:p}=Se(),{createSession:m,loading:l,error:o}=Ue(),[c,N]=useState(1),[P,w]=useState(r.paymentMethods?.includes("stripe")?"stripe":"solana"),[B,L]=useState(0),[j,A]=useState(false),k=it.useRef(c);k.current=c;let W=Math.max(1,r.maxQuantity||5),$=[...r.paymentMethods||["solana"]].sort((U,G)=>U==="stripe"?-1:G==="stripe"?1:0);if(useEffect(()=>{if(typeof window>"u")return;let G=new URLSearchParams(window.location.search).get("session_id");if(!G){try{sessionStorage.removeItem("keyutils_stripe_pending");}catch{}return}if(/^cs_(test|live)_/.test(G)&&!j&&!i?.success){let be=false;try{be=sessionStorage.getItem("keyutils_stripe_pending")==="1";}catch{}if(!be)return;try{sessionStorage.removeItem("keyutils_stripe_pending");}catch{}A(true);let he=new URL(window.location.href);he.searchParams.delete("session_id"),window.history.replaceState({},"",he.toString()),a("stripe",G,k.current);}},[j,i,a]),i?.success)return jsx("div",{className:f("flex flex-col items-center justify-center",e),style:t,children:jsx(ie,{result:i})});if(j||n)return jsx("div",{className:f("flex flex-col",e),style:t,children:jsx(le,{})});if(d)return jsx("div",{className:f("flex flex-col",e),style:t,children:jsx(le,{})});if(u||!s)return jsxs("div",{className:f("flex flex-col items-center justify-center gap-4 py-24",e),style:t,children:[jsxs("div",{className:"rounded-xl border border-red-500/20 bg-red-500/5 px-6 py-5 text-center max-w-sm",children:[jsx("p",{className:"text-sm font-medium text-foreground",children:"Unable to load pricing"}),jsx("p",{className:"mt-1 text-xs text-muted-foreground",children:u||"Please check your connection and try again."})]}),jsx("button",{onClick:()=>g(),className:"text-sm text-muted-foreground hover:text-foreground transition-colors",children:"Try again"})]});let H=async U=>{L(2),await a(P,U,c);},ee=()=>{m(s.priceCents,c);};return jsx("div",{className:f("flex flex-col",e),style:t,children:jsxs("div",{className:"space-y-6",children:[B===0&&jsxs(Fragment,{children:[r.keyImage&&jsx("img",{src:r.keyImage,alt:"Node Key",className:r.keyImageClass||"w-1/2 mx-auto"}),jsxs("div",{className:"px-6 py-10 text-center",children:[jsxs("p",{className:"text-5xl font-bold tracking-tight text-foreground md:text-6xl",children:["$",s.priceUsd.toFixed(2)]}),jsxs("p",{className:"mt-2 text-base text-muted-foreground",children:["per key \xB7 ",s.tokenAllocationFormatted||T(s.tokenAllocation)," tokens included"]})]}),jsx(gt,{quantity:c,max:W,onChange:N}),jsx(Ee,{pricing:s,quantity:c}),jsx("button",{onClick:()=>L(1),className:"w-full rounded-2xl bg-foreground py-5 text-center text-base font-semibold text-background transition-colors hover:bg-foreground/90",children:"Continue to Payment"})]}),B===1&&jsxs(Fragment,{children:[$.length>1&&jsx("div",{role:"tablist",className:"flex gap-1 rounded-2xl bg-muted/50 p-1.5",children:$.map(U=>jsxs("button",{role:"tab","aria-selected":P===U,onClick:()=>w(U),className:f("flex flex-1 items-center justify-center gap-2.5 rounded-xl py-4 text-sm font-medium transition-colors",P===U?"bg-background text-foreground shadow-sm":"text-muted-foreground hover:text-foreground"),children:[U==="stripe"?jsx(CreditCard,{className:"h-5 w-5"}):jsx(Wallet,{className:"h-5 w-5"}),U==="stripe"?"Credit Card":"Crypto"]},U))}),jsx(Ee,{pricing:s,quantity:c}),P==="stripe"?jsxs(Fragment,{children:[jsx("button",{onClick:ee,disabled:l,className:"flex w-full items-center justify-center gap-2 rounded-2xl bg-foreground py-5 text-base font-semibold text-background transition-colors hover:bg-foreground/90 disabled:opacity-50",children:l?jsxs(Fragment,{children:[jsx(Loader2,{className:"h-5 w-5 animate-spin"}),"Redirecting to Stripe..."]}):jsxs(Fragment,{children:[jsx(CreditCard,{className:"h-5 w-5"}),"Pay with Card"]})}),o&&jsx("p",{className:"text-center text-sm text-red-500",children:o})]}):P==="solana"?jsx(ae,{amountSol:s.priceSol??0,onSuccess:H,className:"rounded-2xl py-5 text-base"}):null,p&&jsx("p",{className:"text-center text-sm text-red-500",children:p}),jsx("button",{onClick:()=>L(0),className:"w-full py-3 text-center text-sm text-muted-foreground hover:text-foreground transition-colors",children:"\u2190 Back"})]})]})})}var ht={ios:"iOS",android:"Android",windows:"Windows",mac:"macOS",linux:"Linux"},vt=["mac","windows","linux","ios","android"];function de({downloads:e,className:t,style:r}){let{config:s}=v(),d=useMemo(()=>we(),[]),u=e||vt.map(n=>({platform:n,label:ht[n],url:"#",compatible:n===d})),g=u.find(n=>n.compatible)||u[0],a=u.filter(n=>n.platform!==g.platform);return jsxs("div",{className:f("space-y-8",t),style:r,children:[jsxs("div",{className:"flex flex-col items-center gap-6 pt-4 mt-4",children:[s.aispImage&&jsx("img",{src:s.aispImage,alt:"aiSP",className:"w-full sm:w-1/2 mx-auto px-4 sm:px-0"}),jsxs("div",{className:"text-center",children:[jsx("p",{className:"text-lg sm:text-2xl font-medium text-foreground",children:"Download the AI Service Provider App"}),jsx("p",{className:"mt-1 text-sm sm:text-base text-muted-foreground",children:"Load your keys and run your node."})]}),jsxs("a",{href:g.url,className:"inline-flex items-center gap-3 rounded-full px-10 py-4 text-lg font-semibold text-white transition-colors hover:opacity-90",style:{backgroundColor:"#0900f4"},children:[jsx(Download,{className:"h-5 w-5"}),"Get aiSP for ",g.label]})]}),jsxs("p",{className:"text-center text-sm text-muted-foreground",children:["aiSP is also available on"," ",a.map((n,i)=>jsxs(it.Fragment,{children:[i>0&&(i===a.length-1?" and ":", "),jsx("a",{href:n.url,className:"font-medium underline transition-colors hover:text-foreground",style:{color:"#066bff"},children:n.label})]},n.platform)),"."]}),jsxs("div",{className:"flex flex-col items-center gap-2 mt-12",children:[jsxs("p",{className:"text-center text-xs text-muted-foreground flex justify-center gap-0.5 flex-wrap",children:["Source code is available on",jsx("a",{href:"https://github.com/stack-net",target:"_blank",rel:"noopener noreferrer",className:"text-foreground underline hover:text-foreground/80 color-[#0900f4]",children:"Github"})]}),jsxs("p",{className:"text-center text-xs text-muted-foreground flex justify-center gap-0.5 flex-wrap",children:["By downloading, you agree to the",jsx("a",{href:s.termsUrl||"/terms",className:"text-foreground underline hover:text-foreground/80",children:"Open Source Applications Terms"})]})]})]})}function Pt(e){return {id:e.id||"",key:e.key||e.keyPrefix||"",userId:e.userId||e.user_id||"",status:e.status||"active",tokenBalance:e.tokenBalance??e.currentTokenBalance??e.current_token_balance??0,maxTokens:e.maxTokens??e.initialTokenBalance??e.initial_token_balance??0,createdAt:String(e.createdAt??e.created_at??""),expiresAt:e.expiresAt,label:e.label||e.keyPrefix||e.key_prefix||void 0,paperWork:e.paperWork||e.paper_work||void 0,stackId:e.stackId,stackName:e.stackName}}function J(){let{config:e}=v(),[t,r]=useState([]),[s,d]=useState(true),[u,g]=useState(null),a=useRef(true),n=e.apiBaseUrl,i=useCallback(async()=>{try{d(!0),g(null);let m=await fetch(`${n}/api/keys`);if(!m.ok){let c=`Failed to fetch keys (${m.status})`;try{let N=await m.json();N.error&&(c=N.error);}catch{}throw new Error(c)}let l=await m.json(),o=Array.isArray(l)?l:l.keys??[];a.current&&r(o.map(Pt));}catch(m){a.current&&g(m instanceof Error?m.message:"Failed to fetch keys");}finally{a.current&&d(false);}},[n]);useEffect(()=>(a.current=true,i(),()=>{a.current=false;}),[i]);let p=t.reduce((m,l)=>m+l.tokenBalance,0);return {keys:t,totalBalance:p,loading:s,error:u,refresh:i}}function Me(){let{config:e,callbacks:t}=v(),[r,s]=useState(false),[d,u]=useState(null),[g,a]=useState(null),n=useRef(true),i=useCallback(async(m,l)=>{try{s(!0),a(null);let o=await fetch(`${e.apiBaseUrl}/api/keys/${m}/list`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({askPriceCents:l,stackId:e.stackId,stackName:e.stackName})});if(!o.ok){let N=`Listing failed (${o.status})`;try{let P=await o.json();P.error&&(N=P.error);}catch{}throw new Error(N)}let c=await o.json();return n.current&&(u(c),t.onListingCreated?.(c)),c}catch(o){let c=o instanceof Error?o.message:"Listing failed";return n.current&&a(c),null}finally{n.current&&s(false);}},[e.apiBaseUrl,e.stackId,e.stackName,t]),p=useCallback(async m=>{try{s(!0),a(null);let l=await fetch(`${e.apiBaseUrl}/api/keys/${m}/list`,{method:"DELETE"});if(!l.ok){let o=`Delist failed (${l.status})`;try{let c=await l.json();c.error&&(o=c.error);}catch{}throw new Error(o)}return n.current&&u(null),!0}catch(l){let o=l instanceof Error?l.message:"Delist failed";return n.current&&a(o),false}finally{n.current&&s(false);}},[e.apiBaseUrl]);return {listKey:i,delistKey:p,listing:r,result:d,error:g}}function z({current:e,max:t,className:r,style:s,showLabel:d=true}){let u=Ne(e,t),g=ke(u);return jsxs("div",{className:f("w-full",r),style:s,children:[d&&jsxs("div",{className:"mb-1 flex items-center justify-between text-xs text-muted-foreground",children:[jsxs("span",{children:[T(e)," / ",T(t)]}),jsxs("span",{children:[u,"%"]})]}),jsx("div",{className:"h-2 w-full overflow-hidden rounded-full bg-muted",children:jsx("div",{className:f("h-full rounded-full transition-all duration-500",g),style:{width:`${u}%`}})})]})}function Rt({nodeKey:e,selected:t,onSelect:r}){return jsxs("button",{onClick:r,className:f("flex w-full items-start gap-3 rounded-xl border p-4 text-left transition-all",t?"border-foreground/30 bg-foreground/5 ring-1 ring-foreground/10":"border-border hover:border-foreground/20"),children:[jsx("div",{className:f("mt-0.5 flex h-5 w-5 shrink-0 items-center justify-center rounded-full border-2 transition-colors",t?"border-foreground bg-foreground":"border-muted-foreground/40"),children:t&&jsx(Check,{className:"h-3 w-3 text-background"})}),jsxs("div",{className:"min-w-0 flex-1",children:[jsxs("div",{className:"flex items-center justify-between",children:[jsx("span",{className:"text-sm font-medium text-foreground",children:e.label||e.id.slice(0,16)}),jsx("span",{className:f("text-xs font-medium capitalize",e.status==="active"?"text-green-500":"text-muted-foreground"),children:e.status})]}),jsx(z,{current:e.tokenBalance,max:e.maxTokens,className:"mt-2"})]})]})}function pe({className:e,style:t}){let{config:r}=v(),{keys:s,loading:d,error:u,refresh:g}=J(),{listKey:a,listing:n,result:i,error:p}=Me(),[m,l]=useState(null),[o,c]=useState(""),[N,P]=useState(false),w=s.find(k=>k.id===m),B=parseInt(o.replace(/[^0-9]/g,""),10)||0,L=w&&B>0&&N&&!n,j=async()=>{if(!L||!w)return;let k=B*100;await a(w.id,k)&&g();};if(i)return jsxs("div",{className:f("flex flex-col items-center gap-4 rounded-xl border border-green-500/20 bg-green-500/10 py-12",e),style:t,children:[jsx(CheckCircle,{className:"h-12 w-12 text-green-500"}),jsx("h3",{className:"text-lg font-semibold text-foreground",children:"Key Listed!"}),jsx("p",{className:"text-sm text-muted-foreground",children:"Your key is now on the Global Open AI Network marketplace."}),jsxs("p",{className:"text-xs text-muted-foreground",children:["Listing ID: ",jsx("code",{children:i.listingId})]})]});if(d)return jsxs("div",{className:f("space-y-5 px-1",e),style:t,children:[jsx("h3",{className:"text-2xl mt-3 mb-2 font-semibold text-foreground",children:"Your Keys"}),jsx(Y,{count:2})]});if(u)return jsxs("div",{className:f("flex flex-col items-center justify-center gap-4 py-16",e),style:t,children:[jsxs("div",{className:"rounded-xl border border-red-500/20 bg-red-500/5 px-6 py-5 text-center max-w-sm",children:[jsx("p",{className:"text-sm font-medium text-foreground",children:"Unable to load your keys"}),jsx("p",{className:"mt-1 text-xs text-muted-foreground",children:u})]}),jsx("button",{onClick:()=>g(),className:"text-sm text-muted-foreground hover:text-foreground transition-colors",children:"Try again"})]});let A=s.filter(k=>k.status==="active");return A.length===0?jsxs("div",{className:f("flex flex-col items-center justify-center gap-3 rounded-xl border border-dashed py-16",e),style:t,children:[jsx(AlertTriangle,{className:"h-8 w-8 text-muted-foreground"}),jsx("p",{className:"text-sm text-muted-foreground",children:"No active keys to sell."})]}):jsxs("div",{className:f("space-y-5",e),style:t,children:[jsxs("div",{children:[jsx("h3",{className:"text-2xl mt-3 mb-2 font-semibold text-foreground",children:"Your Keys"}),jsx("div",{className:"space-y-2",children:A.map(k=>jsx(Rt,{nodeKey:k,selected:m===k.id,onSelect:()=>l(k.id)},k.id))})]}),w&&jsxs("div",{className:"rounded-xl border bg-muted/30 p-5 space-y-2 text-sm",children:[jsxs("div",{className:"flex justify-between",children:[jsx("span",{className:"text-muted-foreground",children:"Key ID"}),jsx("code",{className:"text-xs text-foreground",children:w.id})]}),jsxs("div",{className:"flex justify-between",children:[jsx("span",{className:"text-muted-foreground",children:"Originating Stack"}),jsx("span",{className:"text-foreground",children:r.stackName||"Unknown"})]}),jsxs("div",{className:"flex justify-between",children:[jsx("span",{className:"text-muted-foreground",children:"Tokens (remaining / total)"}),jsxs("span",{className:"text-foreground",children:[T(w.tokenBalance)," / ",T(w.maxTokens)]})]}),w.paperWork&&jsxs("div",{className:"flex justify-between",children:[jsx("span",{className:"text-muted-foreground",children:"Paper earned"}),jsx("span",{className:"text-foreground",children:w.paperWork})]}),jsxs("div",{className:"flex justify-between",children:[jsx("span",{className:"text-muted-foreground",children:"Created"}),jsx("span",{className:"text-foreground",children:F(w.createdAt)})]})]}),jsxs("div",{children:[jsx("label",{htmlFor:"listing-price",className:"mb-2 block text-sm sm:text-lg font-semibold text-foreground",children:"Listing Price (USD)"}),jsxs("div",{className:"relative",children:[jsx("span",{className:"absolute left-4 top-1/2 -translate-y-1/2 text-2xl font-bold text-muted-foreground",children:"$"}),jsx("input",{id:"listing-price",type:"text",inputMode:"numeric",value:o,onChange:k=>{let W=k.target.value.replace(/[^0-9]/g,""),$=parseInt(W,10);c(isNaN($)?"":$.toLocaleString("en-US"));},placeholder:"0",className:"w-full rounded-xl border bg-background py-5 pl-10 pr-4 text-3xl font-bold text-foreground placeholder:text-muted-foreground/30 focus:outline-none focus:ring-2 focus:ring-foreground/20"})]})]}),jsxs("label",{className:"flex items-start gap-3 cursor-pointer",children:[jsx("input",{type:"checkbox",checked:N,onChange:k=>P(k.target.checked),className:"sr-only"}),jsx("div",{"aria-hidden":"true",className:f("mt-0.5 flex h-5 w-5 shrink-0 items-center justify-center rounded border-2 transition-colors",N?"border-foreground bg-foreground":"border-muted-foreground/40"),children:N&&jsx(Check,{className:"h-3 w-3 text-background"})}),jsxs("span",{className:"text-xs text-muted-foreground leading-relaxed",children:["I agree to list this key on the Global Open AI Network marketplace. The key will be deactivated upon sale and transferred to the buyer. Listing fees may apply."," ",jsx("a",{href:r.termsUrl||"/terms",className:"text-foreground underline",children:"Terms"})]})]}),p&&jsx("p",{className:"text-sm text-red-500",children:p}),jsxs("button",{onClick:j,disabled:!L,className:f("flex w-full items-center justify-center gap-2 rounded-xl py-4 text-sm font-semibold transition-colors",L?"bg-foreground text-background hover:bg-foreground/90":"bg-muted text-muted-foreground cursor-not-allowed"),children:[jsx(ExternalLink,{className:"h-4 w-4"}),n?"Listing...":"List on Marketplace"]})]})}var Lt=[{id:"buy",label:"Buy"},{id:"use",label:"Use"},{id:"sell",label:"Sell"}];function Tt({active:e,onChange:t}){return jsx("div",{role:"tablist","aria-label":"Key actions",className:"flex gap-1 rounded-2xl bg-muted/50 p-1.5",children:Lt.map(({id:r,label:s})=>jsx("button",{role:"tab","aria-selected":e===r,"aria-controls":`panel-${r}`,id:`tab-${r}`,onClick:()=>t(r),className:f("flex flex-1 items-center justify-center gap-2.5 rounded-xl py-3.5 text-sm font-medium transition-all",e===r?"bg-background text-foreground shadow-sm":"text-muted-foreground hover:text-foreground"),children:jsx("span",{className:"inline",children:s})},r))})}function Et({defaultTab:e="buy",className:t,style:r}){let[s,d]=useState(e);return jsxs("div",{className:f("flex flex-col",t),style:r,children:[jsx("div",{className:"sticky top-0 z-10 bg-background/80 px-5 pb-3 pt-2 sm:pt-5 backdrop-blur-sm sm:px-10 md:px-16 lg:px-20",children:jsx("div",{className:"mx-auto max-w-xl",children:jsx(Tt,{active:s,onChange:d})})}),jsx("div",{className:"flex flex-1 flex-col px-5 pb-8 sm:px-10 md:px-16 md:pb-12 lg:px-20",children:jsx("div",{className:"mx-auto w-full max-w-xl flex-1",children:jsxs("div",{role:"tabpanel",id:`panel-${s}`,"aria-labelledby":`tab-${s}`,children:[s==="buy"&&jsx(ce,{className:"flex-1"}),s==="use"&&jsx(de,{className:"flex-1"}),s==="sell"&&jsx(pe,{className:"flex-1"})]})})})]})}function It({apiBaseUrl:e,stackId:t,stackName:r,paymentMethods:s,merchantWallet:d,stripePublicKey:u,theme:g,maxQuantity:a,keyImage:n,keyImageClass:i,aispImage:p,termsUrl:m,onMintSuccess:l,onMintError:o,onPaymentStart:c,onPaymentComplete:N,onListingCreated:P,defaultTab:w,className:B,style:L}){return jsx(te,{config:{apiBaseUrl:e,stackId:t,stackName:r,paymentMethods:s,merchantWallet:d,stripePublicKey:u,theme:g,maxQuantity:a,keyImage:n,keyImageClass:i,aispImage:p,termsUrl:m},callbacks:{onMintSuccess:l,onMintError:o,onPaymentStart:c,onPaymentComplete:N,onListingCreated:P},children:jsx(Et,{defaultTab:w,className:B,style:L})})}function $e(e){let{config:t}=v(),[r,s]=useState([]),[d,u]=useState(false),[g,a]=useState(null),n=useRef(true),i=t.apiBaseUrl,p=useCallback(async()=>{if(e)try{u(!0),a(null);let m=await fetch(`${i}/api/keys/${e}/ledger`);if(!m.ok){let o=`Failed to fetch ledger (${m.status})`;try{let c=await m.json();c.error&&(o=c.error);}catch{}throw new Error(o)}let l=await m.json();n.current&&s(Array.isArray(l)?l:l.entries??[]);}catch(m){n.current&&a(m instanceof Error?m.message:"Failed to fetch ledger");}finally{n.current&&u(false);}},[i,e]);return useEffect(()=>(n.current=true,p(),()=>{n.current=false;}),[p]),{entries:r,loading:d,error:g,refresh:p}}var xe=20;function ye({nodeKey:e,className:t,style:r}){let[s,d]=useState(false),[u,g]=useState(false),a=useRef(null),{entries:n,loading:i}=$e(s?e.id:null);useEffect(()=>()=>{a.current&&clearTimeout(a.current);},[]);let p=useCallback(()=>{e.key&&(navigator.clipboard.writeText(e.key),g(true),a.current&&clearTimeout(a.current),a.current=setTimeout(()=>g(false),2e3));},[e.key]),m=e.status==="active"?"text-green-500":e.status==="expired"?"text-red-500":"text-yellow-500",l=n.slice(0,xe);return jsxs("div",{className:f("rounded-lg border bg-background",t),style:r,children:[jsxs("div",{className:"p-4",children:[jsxs("div",{className:"mb-3 flex items-center justify-between",children:[jsxs("div",{className:"flex items-center gap-2",children:[jsx("button",{onClick:()=>d(!s),"aria-expanded":s,"aria-label":s?"Collapse ledger":"Expand ledger",className:"text-muted-foreground hover:text-foreground",children:s?jsx(ChevronDown,{className:"h-4 w-4"}):jsx(ChevronRight,{className:"h-4 w-4"})}),jsx("span",{className:"text-sm font-medium text-foreground",children:e.label||`Key ${e.id.slice(0,8)}`})]}),jsx("span",{className:f("text-xs font-medium capitalize",m),children:e.status})]}),e.key&&jsxs("div",{className:"mb-3 flex items-center gap-2",children:[jsx("code",{className:"flex-1 truncate rounded bg-muted px-2 py-1 text-xs text-muted-foreground",children:e.key}),jsx("button",{onClick:p,"aria-label":"Copy key to clipboard",className:"shrink-0 rounded p-1 text-muted-foreground hover:bg-muted hover:text-foreground",children:u?jsx(Check,{className:"h-3.5 w-3.5 text-green-500"}):jsx(Copy,{className:"h-3.5 w-3.5"})})]}),jsx(z,{current:e.tokenBalance,max:e.maxTokens}),jsxs("div",{className:"mt-2 flex items-center justify-between text-xs text-muted-foreground",children:[jsxs("span",{children:["Created ",F(e.createdAt)]}),e.expiresAt&&jsxs("span",{children:["Expires ",F(e.expiresAt)]})]})]}),s&&jsxs("div",{className:"border-t px-4 py-3",children:[jsx("h4",{className:"mb-2 text-xs font-medium text-muted-foreground",children:"Ledger"}),i?jsx("p",{className:"text-xs text-muted-foreground",children:"Loading..."}):l.length===0?jsx("p",{className:"text-xs text-muted-foreground",children:"No ledger entries."}):jsxs("div",{className:"space-y-1.5",children:[l.map(o=>jsxs("div",{className:"flex items-center justify-between text-xs",children:[jsxs("div",{className:"flex items-center gap-2",children:[jsxs("span",{className:f("font-medium",o.type==="credit"||o.type==="reward"?"text-green-500":"text-red-500"),children:[o.type==="credit"||o.type==="reward"?"+":"-",T(Math.abs(o.amount))]}),jsx("span",{className:"text-muted-foreground",children:o.description})]}),jsx("span",{className:"text-muted-foreground",children:F(o.createdAt)})]},o.id)),n.length>xe&&jsxs("p",{className:"pt-1 text-xs text-muted-foreground",children:["Showing ",xe," of ",n.length," entries"]})]})]})]})}var qt=[{id:"buy",label:"Buy"},{id:"use",label:"Use"},{id:"sell",label:"Sell"}];function X({baseHref:e}){return jsx("div",{className:"flex gap-1 rounded-2xl bg-muted/50 p-1.5 mb-6",children:qt.map(({id:t,label:r})=>jsx("a",{href:`${e}?tab=${t}`,className:"flex flex-1 items-center justify-center gap-2.5 rounded-xl py-3.5 text-sm font-medium text-muted-foreground hover:text-foreground transition-all",children:r},t))})}function Vt({getKeyHref:e="/keys/get",showTabs:t=true,className:r,style:s}){let{keys:d,totalBalance:u,loading:g,error:a,refresh:n}=J();return g?jsxs("div",{className:f("space-y-4",r),style:s,children:[t&&e&&jsx(X,{baseHref:e}),jsx(Y,{count:3})]}):a?jsxs("div",{className:f("space-y-4",r),style:s,children:[t&&e&&jsx(X,{baseHref:e}),jsxs("div",{className:"flex flex-col items-center justify-center gap-4 py-12",children:[jsxs("div",{className:"rounded-xl border border-red-500/20 bg-red-500/5 px-6 py-5 text-center max-w-sm",children:[jsx("p",{className:"text-sm font-medium text-foreground",children:"Unable to load your keys"}),jsx("p",{className:"mt-1 text-xs text-muted-foreground",children:a})]}),jsx("button",{onClick:()=>n(),className:"text-sm text-muted-foreground hover:text-foreground transition-colors",children:"Try again"})]})]}):d.length===0?jsxs("div",{className:f("space-y-4",r),style:s,children:[t&&e&&jsx(X,{baseHref:e}),jsxs("div",{className:"flex flex-col items-center justify-center gap-4 rounded-lg border border-dashed py-12",children:[jsx(KeyRound,{className:"h-10 w-10 text-muted-foreground"}),jsxs("div",{className:"text-center",children:[jsx("p",{className:"text-sm font-medium text-foreground",children:"No keys yet"}),jsx("p",{className:"text-xs text-muted-foreground",children:"Get your first node key to start earning."})]}),e&&jsx("a",{href:e,className:"rounded-lg bg-foreground px-4 py-2 text-sm font-medium text-background transition-colors hover:bg-foreground/90",children:"Get a Key"})]})]}):jsxs("div",{className:f("space-y-4",r),style:s,children:[t&&e&&jsx(X,{baseHref:e}),jsxs("div",{className:"flex items-center justify-between",children:[jsxs("p",{className:"text-sm text-muted-foreground",children:[d.length," key",d.length!==1?"s":""," \xB7 Total balance: ",T(u)]}),e&&jsx("a",{href:e,className:"rounded-md bg-foreground px-3 py-1.5 text-xs font-medium text-background transition-colors hover:bg-foreground/90",children:"Get a Key"})]}),d.map(i=>jsx(ye,{nodeKey:i},i.id))]})}
|
|
2
|
+
export{It as BuyKeyWidget,ce as BuyPanel,z as KeyBalanceBar,ye as KeyCard,Vt as KeyList,te as KeyUtilsProvider,ie as MintSuccess,pe as SellPanel,ae as SolanaPayButton,de as UsePanel,v as useKeyUtilsContext};
|
package/dist/hooks/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var react=require('react');require('react/jsx-runtime');var
|
|
1
|
+
'use strict';var react=require('react');require('react/jsx-runtime');var L=react.createContext(null);function m(){let e=react.useContext(L);if(!e)throw new Error("useKeyUtilsContext must be used within a KeyUtilsProvider");return e}function S(){let{config:e}=m(),[c,g]=react.useState(null),[u,f]=react.useState(true),[y,d]=react.useState(null),i=react.useRef(true),l=react.useRef(e.apiBaseUrl);l.current=e.apiBaseUrl;let a=async()=>{try{i.current&&(f(!0),d(null));let s=await fetch(`${l.current}/api/keys/pricing`);if(!s.ok){let t=`Pricing unavailable (${s.status})`;try{let n=await s.json();n.error&&(t=n.error);}catch{}throw new Error(t)}let r=await s.json();i.current&&g(r);}catch(s){i.current&&d(s instanceof Error?s.message:"Failed to fetch pricing");}finally{i.current&&f(false);}};return react.useEffect(()=>{i.current=true,a();let s=setInterval(a,6e4);return ()=>{i.current=false,clearInterval(s);}},[]),{pricing:c,loading:u,error:y,refresh:a}}function j(e){return {id:e.id||"",key:e.key||e.keyPrefix||"",userId:e.userId||e.user_id||"",status:e.status||"active",tokenBalance:e.tokenBalance??e.currentTokenBalance??e.current_token_balance??0,maxTokens:e.maxTokens??e.initialTokenBalance??e.initial_token_balance??0,createdAt:String(e.createdAt??e.created_at??""),expiresAt:e.expiresAt,label:e.label||e.keyPrefix||e.key_prefix||void 0,paperWork:e.paperWork||e.paper_work||void 0,stackId:e.stackId,stackName:e.stackName}}function N(){let{config:e}=m(),[c,g]=react.useState([]),[u,f]=react.useState(true),[y,d]=react.useState(null),i=react.useRef(true),l=e.apiBaseUrl,a=react.useCallback(async()=>{try{f(!0),d(null);let r=await fetch(`${l}/api/keys`);if(!r.ok){let o=`Failed to fetch keys (${r.status})`;try{let p=await r.json();p.error&&(o=p.error);}catch{}throw new Error(o)}let t=await r.json(),n=Array.isArray(t)?t:t.keys??[];i.current&&g(n.map(j));}catch(r){i.current&&d(r instanceof Error?r.message:"Failed to fetch keys");}finally{i.current&&f(false);}},[l]);react.useEffect(()=>(i.current=true,a(),()=>{i.current=false;}),[a]);let s=c.reduce((r,t)=>r+t.tokenBalance,0);return {keys:c,totalBalance:s,loading:u,error:y,refresh:a}}function D(e){let{config:c}=m(),[g,u]=react.useState([]),[f,y]=react.useState(false),[d,i]=react.useState(null),l=react.useRef(true),a=c.apiBaseUrl,s=react.useCallback(async()=>{if(e)try{y(!0),i(null);let r=await fetch(`${a}/api/keys/${e}/ledger`);if(!r.ok){let n=`Failed to fetch ledger (${r.status})`;try{let o=await r.json();o.error&&(n=o.error);}catch{}throw new Error(n)}let t=await r.json();l.current&&u(Array.isArray(t)?t:t.entries??[]);}catch(r){l.current&&i(r instanceof Error?r.message:"Failed to fetch ledger");}finally{l.current&&y(false);}},[a,e]);return react.useEffect(()=>(l.current=true,s(),()=>{l.current=false;}),[s]),{entries:g,loading:f,error:d,refresh:s}}function F(){let{config:e,callbacks:c}=m(),[g,u]=react.useState(false),[f,y]=react.useState(null),[d,i]=react.useState(null);return {mint:react.useCallback(async(a,s,r=1)=>{try{u(!0),i(null),c.onPaymentComplete?.(a,s);let t=await fetch(`${e.apiBaseUrl}/api/keys/mint`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({paymentMethod:a,paymentRef:s,quantity:r})});if(!t.ok){let k="Key generation failed";try{let C=await t.json();C.error&&(k=C.error);}catch{}throw new Error(k)}let n=await t.json(),o=n.keys?.[0]||n,p={success:!0,key:{id:o.keyId||o.id||"",key:o.key||"",userId:o.userId||"",status:"active",tokenBalance:o.tokenBalance??o.currentTokenBalance??0,maxTokens:o.initialTokenBalance??o.tokenBalance??0,createdAt:String(o.createdAt||Date.now())},transactionId:s};return y(p),c.onMintSuccess?.(p),p}catch(t){let n=t instanceof Error?t:new Error("Key generation failed");return i(n.message),c.onMintError?.(n),{success:false,error:n.message}}finally{u(false);}},[e.apiBaseUrl,c]),minting:g,result:f,error:d}}function V(){let{config:e,callbacks:c}=m(),[g,u]=react.useState(false),[f,y]=react.useState(null),[d,i]=react.useState(null),l=react.useRef(true),a=react.useCallback(async(r,t)=>{try{u(!0),i(null);let n=await fetch(`${e.apiBaseUrl}/api/keys/${r}/list`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({askPriceCents:t,stackId:e.stackId,stackName:e.stackName})});if(!n.ok){let p=`Listing failed (${n.status})`;try{let k=await n.json();k.error&&(p=k.error);}catch{}throw new Error(p)}let o=await n.json();return l.current&&(y(o),c.onListingCreated?.(o)),o}catch(n){let o=n instanceof Error?n.message:"Listing failed";return l.current&&i(o),null}finally{l.current&&u(false);}},[e.apiBaseUrl,e.stackId,e.stackName,c]),s=react.useCallback(async r=>{try{u(!0),i(null);let t=await fetch(`${e.apiBaseUrl}/api/keys/${r}/list`,{method:"DELETE"});if(!t.ok){let n=`Delist failed (${t.status})`;try{let o=await t.json();o.error&&(n=o.error);}catch{}throw new Error(n)}return l.current&&y(null),!0}catch(t){let n=t instanceof Error?t.message:"Delist failed";return l.current&&i(n),false}finally{l.current&&u(false);}},[e.apiBaseUrl]);return {listKey:a,delistKey:s,listing:g,result:f,error:d}}function z(){let{config:e}=m(),[c,g]=react.useState(false),[u,f]=react.useState(null),y=react.useRef(true);return {createSession:react.useCallback(async(i,l)=>{try{g(!0),f(null);let a=await fetch(`${e.apiBaseUrl}/api/keys/stripe-session`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({priceCents:i,quantity:l,stackId:e.stackId,referrerUrl:window.location.href})});if(!a.ok){let r=`Checkout failed (${a.status})`;try{let t=await a.json();t.error&&(r=t.error);}catch{}throw new Error(r)}let{url:s}=await a.json();if(s){try{sessionStorage.setItem("keyutils_stripe_pending","1");}catch{}window.location.href=s;}else throw new Error("No checkout URL returned")}catch(a){if(y.current){let s=a instanceof Error?a.message:"Checkout failed";f(s),g(false);}}},[e.apiBaseUrl,e.stackId]),loading:c,error:u}}exports.useKeyLedger=D;exports.useKeys=N;exports.useListKey=V;exports.useMintKey=F;exports.usePricing=S;exports.useStripeCheckout=z;
|
package/dist/hooks/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import {createContext,useState,useRef,useEffect,useCallback,useContext}from'react';import'react/jsx-runtime';var
|
|
1
|
+
import {createContext,useState,useRef,useEffect,useCallback,useContext}from'react';import'react/jsx-runtime';var L=createContext(null);function m(){let e=useContext(L);if(!e)throw new Error("useKeyUtilsContext must be used within a KeyUtilsProvider");return e}function S(){let{config:e}=m(),[c,g]=useState(null),[u,f]=useState(true),[y,d]=useState(null),i=useRef(true),l=useRef(e.apiBaseUrl);l.current=e.apiBaseUrl;let a=async()=>{try{i.current&&(f(!0),d(null));let s=await fetch(`${l.current}/api/keys/pricing`);if(!s.ok){let t=`Pricing unavailable (${s.status})`;try{let n=await s.json();n.error&&(t=n.error);}catch{}throw new Error(t)}let r=await s.json();i.current&&g(r);}catch(s){i.current&&d(s instanceof Error?s.message:"Failed to fetch pricing");}finally{i.current&&f(false);}};return useEffect(()=>{i.current=true,a();let s=setInterval(a,6e4);return ()=>{i.current=false,clearInterval(s);}},[]),{pricing:c,loading:u,error:y,refresh:a}}function j(e){return {id:e.id||"",key:e.key||e.keyPrefix||"",userId:e.userId||e.user_id||"",status:e.status||"active",tokenBalance:e.tokenBalance??e.currentTokenBalance??e.current_token_balance??0,maxTokens:e.maxTokens??e.initialTokenBalance??e.initial_token_balance??0,createdAt:String(e.createdAt??e.created_at??""),expiresAt:e.expiresAt,label:e.label||e.keyPrefix||e.key_prefix||void 0,paperWork:e.paperWork||e.paper_work||void 0,stackId:e.stackId,stackName:e.stackName}}function N(){let{config:e}=m(),[c,g]=useState([]),[u,f]=useState(true),[y,d]=useState(null),i=useRef(true),l=e.apiBaseUrl,a=useCallback(async()=>{try{f(!0),d(null);let r=await fetch(`${l}/api/keys`);if(!r.ok){let o=`Failed to fetch keys (${r.status})`;try{let p=await r.json();p.error&&(o=p.error);}catch{}throw new Error(o)}let t=await r.json(),n=Array.isArray(t)?t:t.keys??[];i.current&&g(n.map(j));}catch(r){i.current&&d(r instanceof Error?r.message:"Failed to fetch keys");}finally{i.current&&f(false);}},[l]);useEffect(()=>(i.current=true,a(),()=>{i.current=false;}),[a]);let s=c.reduce((r,t)=>r+t.tokenBalance,0);return {keys:c,totalBalance:s,loading:u,error:y,refresh:a}}function D(e){let{config:c}=m(),[g,u]=useState([]),[f,y]=useState(false),[d,i]=useState(null),l=useRef(true),a=c.apiBaseUrl,s=useCallback(async()=>{if(e)try{y(!0),i(null);let r=await fetch(`${a}/api/keys/${e}/ledger`);if(!r.ok){let n=`Failed to fetch ledger (${r.status})`;try{let o=await r.json();o.error&&(n=o.error);}catch{}throw new Error(n)}let t=await r.json();l.current&&u(Array.isArray(t)?t:t.entries??[]);}catch(r){l.current&&i(r instanceof Error?r.message:"Failed to fetch ledger");}finally{l.current&&y(false);}},[a,e]);return useEffect(()=>(l.current=true,s(),()=>{l.current=false;}),[s]),{entries:g,loading:f,error:d,refresh:s}}function F(){let{config:e,callbacks:c}=m(),[g,u]=useState(false),[f,y]=useState(null),[d,i]=useState(null);return {mint:useCallback(async(a,s,r=1)=>{try{u(!0),i(null),c.onPaymentComplete?.(a,s);let t=await fetch(`${e.apiBaseUrl}/api/keys/mint`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({paymentMethod:a,paymentRef:s,quantity:r})});if(!t.ok){let k="Key generation failed";try{let C=await t.json();C.error&&(k=C.error);}catch{}throw new Error(k)}let n=await t.json(),o=n.keys?.[0]||n,p={success:!0,key:{id:o.keyId||o.id||"",key:o.key||"",userId:o.userId||"",status:"active",tokenBalance:o.tokenBalance??o.currentTokenBalance??0,maxTokens:o.initialTokenBalance??o.tokenBalance??0,createdAt:String(o.createdAt||Date.now())},transactionId:s};return y(p),c.onMintSuccess?.(p),p}catch(t){let n=t instanceof Error?t:new Error("Key generation failed");return i(n.message),c.onMintError?.(n),{success:false,error:n.message}}finally{u(false);}},[e.apiBaseUrl,c]),minting:g,result:f,error:d}}function V(){let{config:e,callbacks:c}=m(),[g,u]=useState(false),[f,y]=useState(null),[d,i]=useState(null),l=useRef(true),a=useCallback(async(r,t)=>{try{u(!0),i(null);let n=await fetch(`${e.apiBaseUrl}/api/keys/${r}/list`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({askPriceCents:t,stackId:e.stackId,stackName:e.stackName})});if(!n.ok){let p=`Listing failed (${n.status})`;try{let k=await n.json();k.error&&(p=k.error);}catch{}throw new Error(p)}let o=await n.json();return l.current&&(y(o),c.onListingCreated?.(o)),o}catch(n){let o=n instanceof Error?n.message:"Listing failed";return l.current&&i(o),null}finally{l.current&&u(false);}},[e.apiBaseUrl,e.stackId,e.stackName,c]),s=useCallback(async r=>{try{u(!0),i(null);let t=await fetch(`${e.apiBaseUrl}/api/keys/${r}/list`,{method:"DELETE"});if(!t.ok){let n=`Delist failed (${t.status})`;try{let o=await t.json();o.error&&(n=o.error);}catch{}throw new Error(n)}return l.current&&y(null),!0}catch(t){let n=t instanceof Error?t.message:"Delist failed";return l.current&&i(n),false}finally{l.current&&u(false);}},[e.apiBaseUrl]);return {listKey:a,delistKey:s,listing:g,result:f,error:d}}function z(){let{config:e}=m(),[c,g]=useState(false),[u,f]=useState(null),y=useRef(true);return {createSession:useCallback(async(i,l)=>{try{g(!0),f(null);let a=await fetch(`${e.apiBaseUrl}/api/keys/stripe-session`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({priceCents:i,quantity:l,stackId:e.stackId,referrerUrl:window.location.href})});if(!a.ok){let r=`Checkout failed (${a.status})`;try{let t=await a.json();t.error&&(r=t.error);}catch{}throw new Error(r)}let{url:s}=await a.json();if(s){try{sessionStorage.setItem("keyutils_stripe_pending","1");}catch{}window.location.href=s;}else throw new Error("No checkout URL returned")}catch(a){if(y.current){let s=a instanceof Error?a.message:"Checkout failed";f(s),g(false);}}},[e.apiBaseUrl,e.stackId]),loading:c,error:u}}export{D as useKeyLedger,N as useKeys,V as useListKey,F as useMintKey,S as usePricing,z as useStripeCheckout};
|
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
'use strict';var clsx=require('clsx'),tailwindMerge=require('tailwind-merge'),pt=require('react'),jsxRuntime=require('react/jsx-runtime'),lucideReact=require('lucide-react');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var pt__default=/*#__PURE__*/_interopDefault(pt);function m(...e){return tailwindMerge.twMerge(clsx.clsx(e))}function L(e){return e==null?"0":e>=1e9?`${(e/1e9).toFixed(1)}B`:e>=1e6?`${(e/1e6).toFixed(1)}M`:e>=1e3?`${(e/1e3).toFixed(1)}K`:e.toLocaleString()}function Pe(e,t){return t<=0?0:Math.min(100,Math.round(e/t*100))}function Ce(e){return e>=70?"bg-green-500":e>=30?"bg-yellow-500":"bg-red-500"}function F(e){let t=typeof e=="string"&&/^\d+$/.test(e)?Number(e):e,r=new Date(t);return isNaN(r.getTime())?String(e):r.toLocaleDateString("en-US",{month:"short",day:"numeric",year:"numeric"})}function Se(){if(typeof navigator>"u")return "mac";let e=navigator.userAgent.toLowerCase();return /iphone|ipad|ipod/.test(e)?"ios":/android/.test(e)?"android":/win/.test(e)?"windows":/linux/.test(e)?"linux":"mac"}var Ke=pt.createContext(null);function N(){let e=pt.useContext(Ke);if(!e)throw new Error("useKeyUtilsContext must be used within a KeyUtilsProvider");return e}function ee({config:e,callbacks:t={},children:r}){typeof e.apiBaseUrl!="string"&&console.error("[KeyUtils] apiBaseUrl must be a string");let s=pt.useMemo(()=>({config:e,callbacks:t}),[e,t]);return jsxRuntime.jsx(Ke.Provider,{value:s,children:r})}function re(){let{config:e}=N(),[t,r]=pt.useState(null),[s,g]=pt.useState(true),[c,f]=pt.useState(null),a=pt.useRef(true),o=pt.useRef(e.apiBaseUrl);o.current=e.apiBaseUrl;let d=async()=>{try{a.current&&(g(!0),f(null));let p=await fetch(`${o.current}/api/keys/pricing`);if(!p.ok){let i=`Pricing unavailable (${p.status})`;try{let n=await p.json();n.error&&(i=n.error);}catch{}throw new Error(i)}let u=await p.json();a.current&&r(u);}catch(p){a.current&&f(p instanceof Error?p.message:"Failed to fetch pricing");}finally{a.current&&g(false);}};return pt.useEffect(()=>{a.current=true,d();let p=setInterval(d,6e4);return ()=>{a.current=false,clearInterval(p);}},[]),{pricing:t,loading:s,error:c,refresh:d}}function Je(e){return {id:e.id||"",key:e.key||e.keyPrefix||"",userId:e.userId||e.user_id||"",status:e.status||"active",tokenBalance:e.tokenBalance??e.currentTokenBalance??e.current_token_balance??0,maxTokens:e.maxTokens??e.initialTokenBalance??e.initial_token_balance??0,createdAt:String(e.createdAt??e.created_at??""),expiresAt:e.expiresAt,label:e.label||e.keyPrefix||e.key_prefix||void 0,paperWork:e.paperWork||e.paper_work||void 0,stackId:e.stackId,stackName:e.stackName}}function Q(){let{config:e}=N(),[t,r]=pt.useState([]),[s,g]=pt.useState(true),[c,f]=pt.useState(null),a=pt.useRef(true),o=e.apiBaseUrl,d=pt.useCallback(async()=>{try{g(!0),f(null);let u=await fetch(`${o}/api/keys`);if(!u.ok){let l=`Failed to fetch keys (${u.status})`;try{let v=await u.json();v.error&&(l=v.error);}catch{}throw new Error(l)}let i=await u.json(),n=Array.isArray(i)?i:i.keys??[];a.current&&r(n.map(Je));}catch(u){a.current&&f(u instanceof Error?u.message:"Failed to fetch keys");}finally{a.current&&g(false);}},[o]);pt.useEffect(()=>(a.current=true,d(),()=>{a.current=false;}),[d]);let p=t.reduce((u,i)=>u+i.tokenBalance,0);return {keys:t,totalBalance:p,loading:s,error:c,refresh:d}}function ne(e){let{config:t}=N(),[r,s]=pt.useState([]),[g,c]=pt.useState(false),[f,a]=pt.useState(null),o=pt.useRef(true),d=t.apiBaseUrl,p=pt.useCallback(async()=>{if(e)try{c(!0),a(null);let u=await fetch(`${d}/api/keys/${e}/ledger`);if(!u.ok){let n=`Failed to fetch ledger (${u.status})`;try{let l=await u.json();l.error&&(n=l.error);}catch{}throw new Error(n)}let i=await u.json();o.current&&s(Array.isArray(i)?i:i.entries??[]);}catch(u){o.current&&a(u instanceof Error?u.message:"Failed to fetch ledger");}finally{o.current&&c(false);}},[d,e]);return pt.useEffect(()=>(o.current=true,p(),()=>{o.current=false;}),[p]),{entries:r,loading:g,error:f,refresh:p}}function ie(){let{config:e,callbacks:t}=N(),[r,s]=pt.useState(false),[g,c]=pt.useState(null),[f,a]=pt.useState(null);return {mint:pt.useCallback(async(d,p,u=1)=>{try{s(!0),a(null),t.onPaymentComplete?.(d,p);let i=await fetch(`${e.apiBaseUrl}/api/keys/mint`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({paymentMethod:d,paymentRef:p,quantity:u})});if(!i.ok){let P="Key generation failed";try{let w=await i.json();w.error&&(P=w.error);}catch{}throw new Error(P)}let n=await i.json(),l=n.keys?.[0]||n,v={success:!0,key:{id:l.keyId||l.id||"",key:l.key||"",userId:l.userId||"",status:"active",tokenBalance:l.tokenBalance??l.currentTokenBalance??0,maxTokens:l.initialTokenBalance??l.tokenBalance??0,createdAt:String(l.createdAt||Date.now())},transactionId:p};return c(v),t.onMintSuccess?.(v),v}catch(i){let n=i instanceof Error?i:new Error("Key generation failed");return a(n.message),t.onMintError?.(n),{success:false,error:n.message}}finally{s(false);}},[e.apiBaseUrl,t]),minting:r,result:g,error:f}}function ce(){let{config:e,callbacks:t}=N(),[r,s]=pt.useState(false),[g,c]=pt.useState(null),[f,a]=pt.useState(null),o=pt.useRef(true),d=pt.useCallback(async(u,i)=>{try{s(!0),a(null);let n=await fetch(`${e.apiBaseUrl}/api/keys/${u}/list`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({askPriceCents:i,stackId:e.stackId,stackName:e.stackName})});if(!n.ok){let v=`Listing failed (${n.status})`;try{let P=await n.json();P.error&&(v=P.error);}catch{}throw new Error(v)}let l=await n.json();return o.current&&(c(l),t.onListingCreated?.(l)),l}catch(n){let l=n instanceof Error?n.message:"Listing failed";return o.current&&a(l),null}finally{o.current&&s(false);}},[e.apiBaseUrl,e.stackId,e.stackName,t]),p=pt.useCallback(async u=>{try{s(!0),a(null);let i=await fetch(`${e.apiBaseUrl}/api/keys/${u}/list`,{method:"DELETE"});if(!i.ok){let n=`Delist failed (${i.status})`;try{let l=await i.json();l.error&&(n=l.error);}catch{}throw new Error(n)}return o.current&&c(null),!0}catch(i){let n=i instanceof Error?i.message:"Delist failed";return o.current&&a(n),false}finally{o.current&&s(false);}},[e.apiBaseUrl]);return {listKey:d,delistKey:p,listing:r,result:g,error:f}}function de(){let{config:e}=N(),[t,r]=pt.useState(false),[s,g]=pt.useState(null),c=pt.useRef(true);return {createSession:pt.useCallback(async(a,o)=>{try{r(!0),g(null);let d=await fetch(`${e.apiBaseUrl}/api/keys/stripe-session`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({priceCents:a,quantity:o,stackId:e.stackId})});if(!d.ok){let u=`Checkout failed (${d.status})`;try{let i=await d.json();i.error&&(u=i.error);}catch{}throw new Error(u)}let{url:p}=await d.json();if(p){try{sessionStorage.setItem("keyutils_stripe_pending","1");}catch{}window.location.href=p;}else throw new Error("No checkout URL returned")}catch(d){if(c.current){let p=d instanceof Error?d.message:"Checkout failed";g(p),r(false);}}},[e.apiBaseUrl,e.stackId]),loading:t,error:s}}var at=/^[1-9A-HJ-NP-Za-km-z]{32,44}$/;function fe({amountSol:e,onSuccess:t,onError:r,disabled:s,className:g,style:c}){let{config:f}=N(),[a,o]=pt.useState(false),[d,p]=pt.useState(null),u=!!(f.merchantWallet&&at.test(f.merchantWallet)),i=pt.useCallback(async()=>{if(u)try{o(!0),p(null);let{Connection:n,PublicKey:l,Transaction:v,SystemProgram:P,LAMPORTS_PER_SOL:w}=await import('@solana/web3.js'),R=typeof window<"u"?window.solana:void 0;if(!R||typeof R!="object"||!R.isPhantom)throw new Error("Phantom wallet not found. Please install Phantom.");let B=R,M;try{M=new l(f.merchantWallet);}catch{throw new Error("Invalid merchant wallet address.")}let _=await B.connect(),k=new l(_.publicKey.toString()),W=new n(f.solanaRpcUrl||"https://api.mainnet-beta.solana.com"),{blockhash:A}=await W.getLatestBlockhash(),Z=BigInt(Math.floor(e*1e9)),X=new v({recentBlockhash:A,feePayer:k}).add(P.transfer({fromPubkey:k,toPubkey:M,lamports:Number(Z)})),{signature:K}=await B.signAndSendTransaction(X);t(K);}catch(n){let l=n instanceof Error?n:new Error("Payment failed");p(l.message),r?.(l);}finally{o(false);}},[e,f.merchantWallet,u,t,r]);return jsxRuntime.jsxs("div",{children:[jsxRuntime.jsx("button",{onClick:i,disabled:s||a||!u,"aria-label":`Pay ${e.toFixed(4)} SOL`,className:m("flex w-full items-center justify-center gap-2 rounded-lg bg-purple-600 px-4 py-3 font-medium text-white transition-colors hover:bg-purple-700 disabled:opacity-50 disabled:cursor-not-allowed",g),style:c,children:a?jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[jsxRuntime.jsx(lucideReact.Loader2,{className:"h-4 w-4 animate-spin"}),"Processing..."]}):jsxRuntime.jsxs(jsxRuntime.Fragment,{children:["Pay ",e.toFixed(4)," SOL"]})}),d&&jsxRuntime.jsx("p",{className:"mt-2 text-center text-xs text-red-500",children:d})]})}function pe({result:e,className:t,style:r}){let[s,g]=pt.useState(false),c=pt.useRef(null);pt.useEffect(()=>()=>{c.current&&clearTimeout(c.current);},[]);let f=pt.useCallback(()=>{e.key?.key&&(navigator.clipboard.writeText(e.key.key),g(true),c.current&&clearTimeout(c.current),c.current=setTimeout(()=>g(false),2e3));},[e.key?.key]);return !e.success||!e.key?null:jsxRuntime.jsxs("div",{className:m("rounded-lg border border-green-500/20 bg-green-500/10 p-6 text-center",t),style:r,children:[jsxRuntime.jsx(lucideReact.CheckCircle,{className:"mx-auto mb-3 h-12 w-12 text-green-500"}),jsxRuntime.jsx("h3",{className:"mb-2 text-lg font-semibold text-foreground",children:"Key Generated!"}),jsxRuntime.jsx("p",{className:"mb-4 text-sm text-muted-foreground",children:"Your node key has been successfully generated."}),jsxRuntime.jsxs("div",{className:"mx-auto flex max-w-md items-center gap-2 rounded-md border bg-background p-3",children:[jsxRuntime.jsx("code",{className:"flex-1 truncate text-xs text-foreground",children:e.key.key}),jsxRuntime.jsx("button",{onClick:f,"aria-label":"Copy key to clipboard",className:"shrink-0 rounded-md p-1.5 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground",children:s?jsxRuntime.jsx(lucideReact.Check,{className:"h-4 w-4 text-green-500"}):jsxRuntime.jsx(lucideReact.Copy,{className:"h-4 w-4"})})]})]})}function I({className:e}){return jsxRuntime.jsx("div",{className:m("animate-pulse rounded-xl bg-muted",e)})}function ge(){return jsxRuntime.jsxs("div",{className:"space-y-6",children:[jsxRuntime.jsxs("div",{className:"px-6 py-10 flex flex-col items-center gap-3",children:[jsxRuntime.jsx(I,{className:"h-14 w-48"}),jsxRuntime.jsx(I,{className:"h-5 w-64"})]}),jsxRuntime.jsx(I,{className:"h-[72px] w-full rounded-2xl"}),jsxRuntime.jsx(I,{className:"h-[68px] w-full rounded-2xl"}),jsxRuntime.jsx(I,{className:"h-[60px] w-full rounded-2xl"})]})}function J({count:e=3}){return jsxRuntime.jsx("div",{className:"space-y-3",children:Array.from({length:e}).map((t,r)=>jsxRuntime.jsxs("div",{className:"rounded-xl border p-4 space-y-3",children:[jsxRuntime.jsxs("div",{className:"flex items-center justify-between",children:[jsxRuntime.jsx(I,{className:"h-4 w-32"}),jsxRuntime.jsx(I,{className:"h-4 w-16"})]}),jsxRuntime.jsx(I,{className:"h-2 w-full rounded-full"}),jsxRuntime.jsxs("div",{className:"flex justify-between",children:[jsxRuntime.jsx(I,{className:"h-3 w-24"}),jsxRuntime.jsx(I,{className:"h-3 w-20"})]})]},r))})}function kt({quantity:e,max:t,onChange:r}){return jsxRuntime.jsxs("div",{className:"flex items-center justify-between rounded-2xl border bg-muted/20 px-6 py-5",children:[jsxRuntime.jsx("span",{className:"text-sm font-medium text-foreground",children:"Quantity"}),jsxRuntime.jsxs("div",{className:"flex items-center gap-0 rounded-xl border bg-background",children:[jsxRuntime.jsx("button",{onClick:()=>r(Math.max(1,e-1)),disabled:e<=1,"aria-label":"Decrease quantity",className:"flex h-12 w-12 items-center justify-center rounded-l-xl text-muted-foreground transition-colors hover:bg-muted hover:text-foreground disabled:opacity-30",children:jsxRuntime.jsx(lucideReact.Minus,{className:"h-5 w-5"})}),jsxRuntime.jsx("span",{className:"flex h-12 w-14 items-center justify-center border-x text-xl font-bold text-foreground","aria-live":"polite",children:e}),jsxRuntime.jsx("button",{onClick:()=>r(Math.min(t,e+1)),disabled:e>=t,"aria-label":"Increase quantity",className:"flex h-12 w-12 items-center justify-center rounded-r-xl text-muted-foreground transition-colors hover:bg-muted hover:text-foreground disabled:opacity-30",children:jsxRuntime.jsx(lucideReact.Plus,{className:"h-5 w-5"})})]})]})}function Me({pricing:e,quantity:t}){let[r,s]=pt.useState(false),g=e.priceUsd*t;return jsxRuntime.jsxs("div",{className:"rounded-2xl border bg-background",children:[jsxRuntime.jsxs("button",{onClick:()=>s(!r),"aria-expanded":r,className:"flex w-full items-center justify-between px-6 py-5 text-left",children:[jsxRuntime.jsxs("span",{className:"text-foreground",children:[jsxRuntime.jsxs("span",{className:"text-xl font-bold",children:["$",g.toFixed(2)]})," ",jsxRuntime.jsx("span",{className:"text-sm text-muted-foreground",children:"total fees included"})]}),r?jsxRuntime.jsx(lucideReact.ChevronUp,{className:"h-5 w-5 text-muted-foreground"}):jsxRuntime.jsx(lucideReact.ChevronDown,{className:"h-5 w-5 text-muted-foreground"})]}),r&&jsxRuntime.jsxs("div",{className:"border-t px-6 pb-8 pt-4 space-y-3 text-sm",children:[jsxRuntime.jsxs("div",{className:"flex justify-between",children:[jsxRuntime.jsxs("span",{className:"text-muted-foreground",children:["Node Key \xD7 ",t]}),jsxRuntime.jsxs("span",{className:"text-foreground",children:["$",(e.priceUsd*t).toFixed(2)]})]}),jsxRuntime.jsxs("div",{className:"flex justify-between",children:[jsxRuntime.jsx("span",{className:"text-muted-foreground",children:"Keys sold"}),jsxRuntime.jsx("span",{className:"text-foreground",children:e.keysSold})]}),e.daysUntilHalving>0&&jsxRuntime.jsxs("div",{className:"flex justify-between text-muted-foreground",children:[jsxRuntime.jsx("span",{children:"Next refresh"}),jsxRuntime.jsxs("span",{children:[e.daysUntilHalving," day",e.daysUntilHalving!==1?"s":""]})]}),jsxRuntime.jsxs("div",{className:"flex justify-between border-t pt-3 font-semibold",children:[jsxRuntime.jsx("span",{className:"text-foreground",children:"Total"}),jsxRuntime.jsxs("span",{className:"text-foreground",children:["$",g.toFixed(2)]})]})]})]})}function xe({className:e,style:t}){let{config:r}=N(),{pricing:s,loading:g,error:c,refresh:f}=re(),{mint:a,minting:o,result:d,error:p}=ie(),{createSession:u,loading:i,error:n}=de(),[l,v]=pt.useState(1),[P,w]=pt.useState(r.paymentMethods?.includes("stripe")?"stripe":"solana"),[R,B]=pt.useState(0),[M,_]=pt.useState(false),k=pt__default.default.useRef(l);k.current=l;let W=Math.max(1,r.maxQuantity||5),A=[...r.paymentMethods||["solana"]].sort((K,G)=>K==="stripe"?-1:G==="stripe"?1:0);if(pt.useEffect(()=>{if(typeof window>"u")return;let G=new URLSearchParams(window.location.search).get("session_id");if(G&&/^cs_(test|live)_/.test(G)&&!M&&!d?.success){let ke=false;try{ke=sessionStorage.getItem("keyutils_stripe_pending")==="1";}catch{}if(!ke)return;try{sessionStorage.removeItem("keyutils_stripe_pending");}catch{}_(true);let we=new URL(window.location.href);we.searchParams.delete("session_id"),window.history.replaceState({},"",we.toString()),a("stripe",G,k.current);}},[M,d,a]),d?.success)return jsxRuntime.jsx("div",{className:m("flex flex-col items-center justify-center",e),style:t,children:jsxRuntime.jsx(pe,{result:d})});if(M||o)return jsxRuntime.jsx("div",{className:m("flex flex-col",e),style:t,children:jsxRuntime.jsx(ge,{})});if(g)return jsxRuntime.jsx("div",{className:m("flex flex-col",e),style:t,children:jsxRuntime.jsx(ge,{})});if(c||!s)return jsxRuntime.jsxs("div",{className:m("flex flex-col items-center justify-center gap-4 py-24",e),style:t,children:[jsxRuntime.jsxs("div",{className:"rounded-xl border border-red-500/20 bg-red-500/5 px-6 py-5 text-center max-w-sm",children:[jsxRuntime.jsx("p",{className:"text-sm font-medium text-foreground",children:"Unable to load pricing"}),jsxRuntime.jsx("p",{className:"mt-1 text-xs text-muted-foreground",children:c||"Please check your connection and try again."})]}),jsxRuntime.jsx("button",{onClick:()=>f(),className:"text-sm text-muted-foreground hover:text-foreground transition-colors",children:"Try again"})]});let Z=async K=>{B(2),await a(P,K,l);},X=()=>{u(s.priceCents,l);};return jsxRuntime.jsx("div",{className:m("flex flex-col",e),style:t,children:jsxRuntime.jsxs("div",{className:"space-y-6",children:[R===0&&jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[r.keyImage&&jsxRuntime.jsx("img",{src:r.keyImage,alt:"Node Key",className:r.keyImageClass||"w-1/2 mx-auto"}),jsxRuntime.jsxs("div",{className:"px-6 py-10 text-center",children:[jsxRuntime.jsxs("p",{className:"text-5xl font-bold tracking-tight text-foreground md:text-6xl",children:["$",s.priceUsd.toFixed(2)]}),jsxRuntime.jsxs("p",{className:"mt-2 text-base text-muted-foreground",children:["per key \xB7 ",s.tokenAllocationFormatted||L(s.tokenAllocation)," tokens included"]})]}),jsxRuntime.jsx(kt,{quantity:l,max:W,onChange:v}),jsxRuntime.jsx(Me,{pricing:s,quantity:l}),jsxRuntime.jsx("button",{onClick:()=>B(1),className:"w-full rounded-2xl bg-foreground py-5 text-center text-base font-semibold text-background transition-colors hover:bg-foreground/90",children:"Continue to Payment"})]}),R===1&&jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[A.length>1&&jsxRuntime.jsx("div",{role:"tablist",className:"flex gap-1 rounded-2xl bg-muted/50 p-1.5",children:A.map(K=>jsxRuntime.jsxs("button",{role:"tab","aria-selected":P===K,onClick:()=>w(K),className:m("flex flex-1 items-center justify-center gap-2.5 rounded-xl py-4 text-sm font-medium transition-colors",P===K?"bg-background text-foreground shadow-sm":"text-muted-foreground hover:text-foreground"),children:[K==="stripe"?jsxRuntime.jsx(lucideReact.CreditCard,{className:"h-5 w-5"}):jsxRuntime.jsx(lucideReact.Wallet,{className:"h-5 w-5"}),K==="stripe"?"Credit Card":"Crypto"]},K))}),jsxRuntime.jsx(Me,{pricing:s,quantity:l}),P==="stripe"?jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[jsxRuntime.jsx("button",{onClick:X,disabled:i,className:"flex w-full items-center justify-center gap-2 rounded-2xl bg-foreground py-5 text-base font-semibold text-background transition-colors hover:bg-foreground/90 disabled:opacity-50",children:i?jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[jsxRuntime.jsx(lucideReact.Loader2,{className:"h-5 w-5 animate-spin"}),"Redirecting to Stripe..."]}):jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[jsxRuntime.jsx(lucideReact.CreditCard,{className:"h-5 w-5"}),"Pay with Card"]})}),n&&jsxRuntime.jsx("p",{className:"text-center text-sm text-red-500",children:n})]}):P==="solana"?jsxRuntime.jsx(fe,{amountSol:s.priceSol??0,onSuccess:Z,className:"rounded-2xl py-5 text-base"}):null,p&&jsxRuntime.jsx("p",{className:"text-center text-sm text-red-500",children:p}),jsxRuntime.jsx("button",{onClick:()=>B(0),className:"w-full py-3 text-center text-sm text-muted-foreground hover:text-foreground transition-colors",children:"\u2190 Back to Configure"})]})]})})}var St={ios:"iOS",android:"Android",windows:"Windows",mac:"macOS",linux:"Linux"},Kt=["mac","windows","linux","ios","android"];function ye({downloads:e,className:t,style:r}){let{config:s}=N(),g=pt.useMemo(()=>Se(),[]),c=e||Kt.map(o=>({platform:o,label:St[o],url:"#",compatible:o===g})),f=c.find(o=>o.compatible)||c[0],a=c.filter(o=>o.platform!==f.platform);return jsxRuntime.jsxs("div",{className:m("space-y-8",t),style:r,children:[jsxRuntime.jsxs("div",{className:"flex flex-col items-center gap-6 pt-4 mt-4",children:[s.aispImage&&jsxRuntime.jsx("img",{src:s.aispImage,alt:"aiSP",className:"w-full sm:w-1/2 mx-auto px-4 sm:px-0"}),jsxRuntime.jsxs("div",{className:"text-center",children:[jsxRuntime.jsx("p",{className:"text-lg sm:text-2xl font-medium text-foreground",children:"Download the AI Service Provider App"}),jsxRuntime.jsx("p",{className:"mt-1 text-sm sm:text-base text-muted-foreground",children:"Load your keys and run your node."})]}),jsxRuntime.jsxs("a",{href:f.url,className:"inline-flex items-center gap-3 rounded-full px-10 py-4 text-lg font-semibold text-white transition-colors hover:opacity-90",style:{backgroundColor:"#0900f4"},children:[jsxRuntime.jsx(lucideReact.Download,{className:"h-5 w-5"}),"Get aiSP for ",f.label]})]}),jsxRuntime.jsxs("p",{className:"text-center text-sm text-muted-foreground",children:["aiSP is also available on"," ",a.map((o,d)=>jsxRuntime.jsxs(pt__default.default.Fragment,{children:[d>0&&(d===a.length-1?" and ":", "),jsxRuntime.jsx("a",{href:o.url,className:"font-medium underline transition-colors hover:text-foreground",style:{color:"#066bff"},children:o.label})]},o.platform)),"."]}),jsxRuntime.jsxs("div",{className:"flex flex-col items-center gap-2 mt-12",children:[jsxRuntime.jsxs("p",{className:"text-center text-xs text-muted-foreground flex justify-center gap-0.5 flex-wrap",children:["Source code is available on",jsxRuntime.jsx("a",{href:"https://github.com/stack-net",target:"_blank",rel:"noopener noreferrer",className:"text-foreground underline hover:text-foreground/80 color-[#0900f4]",children:"Github"})]}),jsxRuntime.jsxs("p",{className:"text-center text-xs text-muted-foreground flex justify-center gap-0.5 flex-wrap",children:["By downloading, you agree to the",jsxRuntime.jsx("a",{href:s.termsUrl||"/terms",className:"text-foreground underline hover:text-foreground/80",children:"Open Source Applications Terms"})]})]})]})}function Y({current:e,max:t,className:r,style:s,showLabel:g=true}){let c=Pe(e,t),f=Ce(c);return jsxRuntime.jsxs("div",{className:m("w-full",r),style:s,children:[g&&jsxRuntime.jsxs("div",{className:"mb-1 flex items-center justify-between text-xs text-muted-foreground",children:[jsxRuntime.jsxs("span",{children:[L(e)," / ",L(t)]}),jsxRuntime.jsxs("span",{children:[c,"%"]})]}),jsxRuntime.jsx("div",{className:"h-2 w-full overflow-hidden rounded-full bg-muted",children:jsxRuntime.jsx("div",{className:m("h-full rounded-full transition-all duration-500",f),style:{width:`${c}%`}})})]})}function Lt({nodeKey:e,selected:t,onSelect:r}){return jsxRuntime.jsxs("button",{onClick:r,className:m("flex w-full items-start gap-3 rounded-xl border p-4 text-left transition-all",t?"border-foreground/30 bg-foreground/5 ring-1 ring-foreground/10":"border-border hover:border-foreground/20"),children:[jsxRuntime.jsx("div",{className:m("mt-0.5 flex h-5 w-5 shrink-0 items-center justify-center rounded-full border-2 transition-colors",t?"border-foreground bg-foreground":"border-muted-foreground/40"),children:t&&jsxRuntime.jsx(lucideReact.Check,{className:"h-3 w-3 text-background"})}),jsxRuntime.jsxs("div",{className:"min-w-0 flex-1",children:[jsxRuntime.jsxs("div",{className:"flex items-center justify-between",children:[jsxRuntime.jsx("span",{className:"text-sm font-medium text-foreground",children:e.label||e.id.slice(0,16)}),jsxRuntime.jsx("span",{className:m("text-xs font-medium capitalize",e.status==="active"?"text-green-500":"text-muted-foreground"),children:e.status})]}),jsxRuntime.jsx(Y,{current:e.tokenBalance,max:e.maxTokens,className:"mt-2"})]})]})}function he({className:e,style:t}){let{config:r}=N(),{keys:s,loading:g,error:c,refresh:f}=Q(),{listKey:a,listing:o,result:d,error:p}=ce(),[u,i]=pt.useState(null),[n,l]=pt.useState(""),[v,P]=pt.useState(false),w=s.find(k=>k.id===u),R=parseInt(n.replace(/[^0-9]/g,""),10)||0,B=w&&R>0&&v&&!o,M=async()=>{if(!B||!w)return;let k=R*100;await a(w.id,k)&&f();};if(d)return jsxRuntime.jsxs("div",{className:m("flex flex-col items-center gap-4 rounded-xl border border-green-500/20 bg-green-500/10 py-12",e),style:t,children:[jsxRuntime.jsx(lucideReact.CheckCircle,{className:"h-12 w-12 text-green-500"}),jsxRuntime.jsx("h3",{className:"text-lg font-semibold text-foreground",children:"Key Listed!"}),jsxRuntime.jsx("p",{className:"text-sm text-muted-foreground",children:"Your key is now on the Global Open AI Network marketplace."}),jsxRuntime.jsxs("p",{className:"text-xs text-muted-foreground",children:["Listing ID: ",jsxRuntime.jsx("code",{children:d.listingId})]})]});if(g)return jsxRuntime.jsxs("div",{className:m("space-y-5 px-1",e),style:t,children:[jsxRuntime.jsx("h3",{className:"text-2xl mt-3 mb-2 font-semibold text-foreground",children:"Your Keys"}),jsxRuntime.jsx(J,{count:2})]});if(c)return jsxRuntime.jsxs("div",{className:m("flex flex-col items-center justify-center gap-4 py-16",e),style:t,children:[jsxRuntime.jsxs("div",{className:"rounded-xl border border-red-500/20 bg-red-500/5 px-6 py-5 text-center max-w-sm",children:[jsxRuntime.jsx("p",{className:"text-sm font-medium text-foreground",children:"Unable to load your keys"}),jsxRuntime.jsx("p",{className:"mt-1 text-xs text-muted-foreground",children:c})]}),jsxRuntime.jsx("button",{onClick:()=>f(),className:"text-sm text-muted-foreground hover:text-foreground transition-colors",children:"Try again"})]});let _=s.filter(k=>k.status==="active");return _.length===0?jsxRuntime.jsxs("div",{className:m("flex flex-col items-center justify-center gap-3 rounded-xl border border-dashed py-16",e),style:t,children:[jsxRuntime.jsx(lucideReact.AlertTriangle,{className:"h-8 w-8 text-muted-foreground"}),jsxRuntime.jsx("p",{className:"text-sm text-muted-foreground",children:"No active keys to sell."})]}):jsxRuntime.jsxs("div",{className:m("space-y-5",e),style:t,children:[jsxRuntime.jsxs("div",{children:[jsxRuntime.jsx("h3",{className:"text-2xl mt-3 mb-2 font-semibold text-foreground",children:"Your Keys"}),jsxRuntime.jsx("div",{className:"space-y-2",children:_.map(k=>jsxRuntime.jsx(Lt,{nodeKey:k,selected:u===k.id,onSelect:()=>i(k.id)},k.id))})]}),w&&jsxRuntime.jsxs("div",{className:"rounded-xl border bg-muted/30 p-5 space-y-2 text-sm",children:[jsxRuntime.jsxs("div",{className:"flex justify-between",children:[jsxRuntime.jsx("span",{className:"text-muted-foreground",children:"Key ID"}),jsxRuntime.jsx("code",{className:"text-xs text-foreground",children:w.id})]}),jsxRuntime.jsxs("div",{className:"flex justify-between",children:[jsxRuntime.jsx("span",{className:"text-muted-foreground",children:"Originating Stack"}),jsxRuntime.jsx("span",{className:"text-foreground",children:r.stackName||"Unknown"})]}),jsxRuntime.jsxs("div",{className:"flex justify-between",children:[jsxRuntime.jsx("span",{className:"text-muted-foreground",children:"Tokens (remaining / total)"}),jsxRuntime.jsxs("span",{className:"text-foreground",children:[L(w.tokenBalance)," / ",L(w.maxTokens)]})]}),w.paperWork&&jsxRuntime.jsxs("div",{className:"flex justify-between",children:[jsxRuntime.jsx("span",{className:"text-muted-foreground",children:"Paper earned"}),jsxRuntime.jsx("span",{className:"text-foreground",children:w.paperWork})]}),jsxRuntime.jsxs("div",{className:"flex justify-between",children:[jsxRuntime.jsx("span",{className:"text-muted-foreground",children:"Created"}),jsxRuntime.jsx("span",{className:"text-foreground",children:F(w.createdAt)})]})]}),jsxRuntime.jsxs("div",{children:[jsxRuntime.jsx("label",{htmlFor:"listing-price",className:"mb-2 block text-sm font-semibold text-foreground",children:"Listing Price (USD)"}),jsxRuntime.jsxs("div",{className:"relative",children:[jsxRuntime.jsx("span",{className:"absolute left-4 top-1/2 -translate-y-1/2 text-2xl font-bold text-muted-foreground",children:"$"}),jsxRuntime.jsx("input",{id:"listing-price",type:"text",inputMode:"numeric",value:n,onChange:k=>{let W=k.target.value.replace(/[^0-9]/g,""),A=parseInt(W,10);l(isNaN(A)?"":A.toLocaleString("en-US"));},placeholder:"0",className:"w-full rounded-xl border bg-background py-5 pl-10 pr-4 text-3xl font-bold text-foreground placeholder:text-muted-foreground/30 focus:outline-none focus:ring-2 focus:ring-foreground/20"})]})]}),jsxRuntime.jsxs("label",{className:"flex items-start gap-3 cursor-pointer",children:[jsxRuntime.jsx("input",{type:"checkbox",checked:v,onChange:k=>P(k.target.checked),className:"sr-only"}),jsxRuntime.jsx("div",{"aria-hidden":"true",className:m("mt-0.5 flex h-5 w-5 shrink-0 items-center justify-center rounded border-2 transition-colors",v?"border-foreground bg-foreground":"border-muted-foreground/40"),children:v&&jsxRuntime.jsx(lucideReact.Check,{className:"h-3 w-3 text-background"})}),jsxRuntime.jsxs("span",{className:"text-xs text-muted-foreground leading-relaxed",children:["I agree to list this key on the Global Open AI Network marketplace. The key will be deactivated upon sale and transferred to the buyer. Listing fees may apply."," ",jsxRuntime.jsx("a",{href:r.termsUrl||"/terms",className:"text-foreground underline",children:"Terms"})]})]}),p&&jsxRuntime.jsx("p",{className:"text-sm text-red-500",children:p}),jsxRuntime.jsxs("button",{onClick:M,disabled:!B,className:m("flex w-full items-center justify-center gap-2 rounded-xl py-4 text-sm font-semibold transition-colors",B?"bg-foreground text-background hover:bg-foreground/90":"bg-muted text-muted-foreground cursor-not-allowed"),children:[jsxRuntime.jsx(lucideReact.ExternalLink,{className:"h-4 w-4"}),o?"Listing...":"List on Marketplace"]})]})}var Et=[{id:"buy",label:"Buy"},{id:"use",label:"Use"},{id:"sell",label:"Sell"}];function It({active:e,onChange:t}){return jsxRuntime.jsx("div",{role:"tablist","aria-label":"Key actions",className:"flex gap-1 rounded-2xl bg-muted/50 p-1.5",children:Et.map(({id:r,label:s})=>jsxRuntime.jsx("button",{role:"tab","aria-selected":e===r,"aria-controls":`panel-${r}`,id:`tab-${r}`,onClick:()=>t(r),className:m("flex flex-1 items-center justify-center gap-2.5 rounded-xl py-3.5 text-sm font-medium transition-all",e===r?"bg-background text-foreground shadow-sm":"text-muted-foreground hover:text-foreground"),children:jsxRuntime.jsx("span",{className:"inline",children:s})},r))})}function Mt({defaultTab:e="buy",className:t,style:r}){let[s,g]=pt.useState(e);return jsxRuntime.jsxs("div",{className:m("flex flex-col",t),style:r,children:[jsxRuntime.jsx("div",{className:"sticky top-0 z-10 bg-background/80 px-5 pb-3 pt-2 sm:pt-5 backdrop-blur-sm sm:px-10 md:px-16 lg:px-20",children:jsxRuntime.jsx("div",{className:"mx-auto max-w-xl",children:jsxRuntime.jsx(It,{active:s,onChange:g})})}),jsxRuntime.jsx("div",{className:"flex flex-1 flex-col px-5 pb-8 sm:px-10 md:px-16 md:pb-12 lg:px-20",children:jsxRuntime.jsx("div",{className:"mx-auto w-full max-w-xl flex-1",children:jsxRuntime.jsxs("div",{role:"tabpanel",id:`panel-${s}`,"aria-labelledby":`tab-${s}`,children:[s==="buy"&&jsxRuntime.jsx(xe,{className:"flex-1"}),s==="use"&&jsxRuntime.jsx(ye,{className:"flex-1"}),s==="sell"&&jsxRuntime.jsx(he,{className:"flex-1"})]})})})]})}function jt({apiBaseUrl:e,stackId:t,stackName:r,paymentMethods:s,merchantWallet:g,stripePublicKey:c,theme:f,maxQuantity:a,keyImage:o,keyImageClass:d,aispImage:p,termsUrl:u,onMintSuccess:i,onMintError:n,onPaymentStart:l,onPaymentComplete:v,onListingCreated:P,defaultTab:w,className:R,style:B}){return jsxRuntime.jsx(ee,{config:{apiBaseUrl:e,stackId:t,stackName:r,paymentMethods:s,merchantWallet:g,stripePublicKey:c,theme:f,maxQuantity:a,keyImage:o,keyImageClass:d,aispImage:p,termsUrl:u},callbacks:{onMintSuccess:i,onMintError:n,onPaymentStart:l,onPaymentComplete:v,onListingCreated:P},children:jsxRuntime.jsx(Mt,{defaultTab:w,className:R,style:B})})}var Ne=20;function ve({nodeKey:e,className:t,style:r}){let[s,g]=pt.useState(false),[c,f]=pt.useState(false),a=pt.useRef(null),{entries:o,loading:d}=ne(s?e.id:null);pt.useEffect(()=>()=>{a.current&&clearTimeout(a.current);},[]);let p=pt.useCallback(()=>{e.key&&(navigator.clipboard.writeText(e.key),f(true),a.current&&clearTimeout(a.current),a.current=setTimeout(()=>f(false),2e3));},[e.key]),u=e.status==="active"?"text-green-500":e.status==="expired"?"text-red-500":"text-yellow-500",i=o.slice(0,Ne);return jsxRuntime.jsxs("div",{className:m("rounded-lg border bg-background",t),style:r,children:[jsxRuntime.jsxs("div",{className:"p-4",children:[jsxRuntime.jsxs("div",{className:"mb-3 flex items-center justify-between",children:[jsxRuntime.jsxs("div",{className:"flex items-center gap-2",children:[jsxRuntime.jsx("button",{onClick:()=>g(!s),"aria-expanded":s,"aria-label":s?"Collapse ledger":"Expand ledger",className:"text-muted-foreground hover:text-foreground",children:s?jsxRuntime.jsx(lucideReact.ChevronDown,{className:"h-4 w-4"}):jsxRuntime.jsx(lucideReact.ChevronRight,{className:"h-4 w-4"})}),jsxRuntime.jsx("span",{className:"text-sm font-medium text-foreground",children:e.label||`Key ${e.id.slice(0,8)}`})]}),jsxRuntime.jsx("span",{className:m("text-xs font-medium capitalize",u),children:e.status})]}),e.key&&jsxRuntime.jsxs("div",{className:"mb-3 flex items-center gap-2",children:[jsxRuntime.jsx("code",{className:"flex-1 truncate rounded bg-muted px-2 py-1 text-xs text-muted-foreground",children:e.key}),jsxRuntime.jsx("button",{onClick:p,"aria-label":"Copy key to clipboard",className:"shrink-0 rounded p-1 text-muted-foreground hover:bg-muted hover:text-foreground",children:c?jsxRuntime.jsx(lucideReact.Check,{className:"h-3.5 w-3.5 text-green-500"}):jsxRuntime.jsx(lucideReact.Copy,{className:"h-3.5 w-3.5"})})]}),jsxRuntime.jsx(Y,{current:e.tokenBalance,max:e.maxTokens}),jsxRuntime.jsxs("div",{className:"mt-2 flex items-center justify-between text-xs text-muted-foreground",children:[jsxRuntime.jsxs("span",{children:["Created ",F(e.createdAt)]}),e.expiresAt&&jsxRuntime.jsxs("span",{children:["Expires ",F(e.expiresAt)]})]})]}),s&&jsxRuntime.jsxs("div",{className:"border-t px-4 py-3",children:[jsxRuntime.jsx("h4",{className:"mb-2 text-xs font-medium text-muted-foreground",children:"Ledger"}),d?jsxRuntime.jsx("p",{className:"text-xs text-muted-foreground",children:"Loading..."}):i.length===0?jsxRuntime.jsx("p",{className:"text-xs text-muted-foreground",children:"No ledger entries."}):jsxRuntime.jsxs("div",{className:"space-y-1.5",children:[i.map(n=>jsxRuntime.jsxs("div",{className:"flex items-center justify-between text-xs",children:[jsxRuntime.jsxs("div",{className:"flex items-center gap-2",children:[jsxRuntime.jsxs("span",{className:m("font-medium",n.type==="credit"||n.type==="reward"?"text-green-500":"text-red-500"),children:[n.type==="credit"||n.type==="reward"?"+":"-",L(Math.abs(n.amount))]}),jsxRuntime.jsx("span",{className:"text-muted-foreground",children:n.description})]}),jsxRuntime.jsx("span",{className:"text-muted-foreground",children:F(n.createdAt)})]},n.id)),o.length>Ne&&jsxRuntime.jsxs("p",{className:"pt-1 text-xs text-muted-foreground",children:["Showing ",Ne," of ",o.length," entries"]})]})]})]})}function Qt({getKeyHref:e="/keys/get",className:t,style:r}){let{keys:s,totalBalance:g,loading:c,error:f,refresh:a}=Q();return c?jsxRuntime.jsx("div",{className:m("space-y-4",t),style:r,children:jsxRuntime.jsx(J,{count:3})}):f?jsxRuntime.jsxs("div",{className:m("flex flex-col items-center justify-center gap-4 py-12",t),style:r,children:[jsxRuntime.jsxs("div",{className:"rounded-xl border border-red-500/20 bg-red-500/5 px-6 py-5 text-center max-w-sm",children:[jsxRuntime.jsx("p",{className:"text-sm font-medium text-foreground",children:"Unable to load your keys"}),jsxRuntime.jsx("p",{className:"mt-1 text-xs text-muted-foreground",children:f})]}),jsxRuntime.jsx("button",{onClick:()=>a(),className:"text-sm text-muted-foreground hover:text-foreground transition-colors",children:"Try again"})]}):s.length===0?jsxRuntime.jsxs("div",{className:m("flex flex-col items-center justify-center gap-4 rounded-lg border border-dashed py-12",t),style:r,children:[jsxRuntime.jsx(lucideReact.KeyRound,{className:"h-10 w-10 text-muted-foreground"}),jsxRuntime.jsxs("div",{className:"text-center",children:[jsxRuntime.jsx("p",{className:"text-sm font-medium text-foreground",children:"No keys yet"}),jsxRuntime.jsx("p",{className:"text-xs text-muted-foreground",children:"Get your first node key to start earning."})]}),e&&jsxRuntime.jsx("a",{href:e,className:"rounded-lg bg-foreground px-4 py-2 text-sm font-medium text-background transition-colors hover:bg-foreground/90",children:"Get a Key"})]}):jsxRuntime.jsxs("div",{className:m("space-y-4",t),style:r,children:[jsxRuntime.jsxs("div",{className:"flex items-center justify-between",children:[jsxRuntime.jsxs("p",{className:"text-sm text-muted-foreground",children:[s.length," key",s.length!==1?"s":""," \xB7 Total balance: ",L(g)]}),e&&jsxRuntime.jsx("a",{href:e,className:"rounded-md bg-foreground px-3 py-1.5 text-xs font-medium text-background transition-colors hover:bg-foreground/90",children:"Get a Key"})]}),s.map(o=>jsxRuntime.jsx(ve,{nodeKey:o},o.id))]})}
|
|
2
|
-
exports.BuyKeyWidget=
|
|
1
|
+
'use strict';var clsx=require('clsx'),tailwindMerge=require('tailwind-merge'),gt=require('react'),jsxRuntime=require('react/jsx-runtime'),lucideReact=require('lucide-react');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var gt__default=/*#__PURE__*/_interopDefault(gt);function f(...e){return tailwindMerge.twMerge(clsx.clsx(e))}function T(e){return e==null?"0":e>=1e9?`${(e/1e9).toFixed(1)}B`:e>=1e6?`${(e/1e6).toFixed(1)}M`:e>=1e3?`${(e/1e3).toFixed(1)}K`:e.toLocaleString()}function Ce(e,t){return t<=0?0:Math.min(100,Math.round(e/t*100))}function Se(e){return e>=70?"bg-green-500":e>=30?"bg-yellow-500":"bg-red-500"}function F(e){let t=typeof e=="string"&&/^\d+$/.test(e)?Number(e):e,r=new Date(t);return isNaN(r.getTime())?String(e):r.toLocaleDateString("en-US",{month:"short",day:"numeric",year:"numeric"})}function Ke(){if(typeof navigator>"u")return "mac";let e=navigator.userAgent.toLowerCase();return /iphone|ipad|ipod/.test(e)?"ios":/android/.test(e)?"android":/win/.test(e)?"windows":/linux/.test(e)?"linux":"mac"}var Ue=gt.createContext(null);function v(){let e=gt.useContext(Ue);if(!e)throw new Error("useKeyUtilsContext must be used within a KeyUtilsProvider");return e}function te({config:e,callbacks:t={},children:r}){typeof e.apiBaseUrl!="string"&&console.error("[KeyUtils] apiBaseUrl must be a string");let s=gt.useMemo(()=>({config:e,callbacks:t}),[e,t]);return jsxRuntime.jsx(Ue.Provider,{value:s,children:r})}function se(){let{config:e}=v(),[t,r]=gt.useState(null),[s,d]=gt.useState(true),[u,g]=gt.useState(null),a=gt.useRef(true),o=gt.useRef(e.apiBaseUrl);o.current=e.apiBaseUrl;let i=async()=>{try{a.current&&(d(!0),g(null));let p=await fetch(`${o.current}/api/keys/pricing`);if(!p.ok){let l=`Pricing unavailable (${p.status})`;try{let n=await p.json();n.error&&(l=n.error);}catch{}throw new Error(l)}let m=await p.json();a.current&&r(m);}catch(p){a.current&&g(p instanceof Error?p.message:"Failed to fetch pricing");}finally{a.current&&d(false);}};return gt.useEffect(()=>{a.current=true,i();let p=setInterval(i,6e4);return ()=>{a.current=false,clearInterval(p);}},[]),{pricing:t,loading:s,error:u,refresh:i}}function Ze(e){return {id:e.id||"",key:e.key||e.keyPrefix||"",userId:e.userId||e.user_id||"",status:e.status||"active",tokenBalance:e.tokenBalance??e.currentTokenBalance??e.current_token_balance??0,maxTokens:e.maxTokens??e.initialTokenBalance??e.initial_token_balance??0,createdAt:String(e.createdAt??e.created_at??""),expiresAt:e.expiresAt,label:e.label||e.keyPrefix||e.key_prefix||void 0,paperWork:e.paperWork||e.paper_work||void 0,stackId:e.stackId,stackName:e.stackName}}function Q(){let{config:e}=v(),[t,r]=gt.useState([]),[s,d]=gt.useState(true),[u,g]=gt.useState(null),a=gt.useRef(true),o=e.apiBaseUrl,i=gt.useCallback(async()=>{try{d(!0),g(null);let m=await fetch(`${o}/api/keys`);if(!m.ok){let c=`Failed to fetch keys (${m.status})`;try{let N=await m.json();N.error&&(c=N.error);}catch{}throw new Error(c)}let l=await m.json(),n=Array.isArray(l)?l:l.keys??[];a.current&&r(n.map(Ze));}catch(m){a.current&&g(m instanceof Error?m.message:"Failed to fetch keys");}finally{a.current&&d(false);}},[o]);gt.useEffect(()=>(a.current=true,i(),()=>{a.current=false;}),[i]);let p=t.reduce((m,l)=>m+l.tokenBalance,0);return {keys:t,totalBalance:p,loading:s,error:u,refresh:i}}function ae(e){let{config:t}=v(),[r,s]=gt.useState([]),[d,u]=gt.useState(false),[g,a]=gt.useState(null),o=gt.useRef(true),i=t.apiBaseUrl,p=gt.useCallback(async()=>{if(e)try{u(!0),a(null);let m=await fetch(`${i}/api/keys/${e}/ledger`);if(!m.ok){let n=`Failed to fetch ledger (${m.status})`;try{let c=await m.json();c.error&&(n=c.error);}catch{}throw new Error(n)}let l=await m.json();o.current&&s(Array.isArray(l)?l:l.entries??[]);}catch(m){o.current&&a(m instanceof Error?m.message:"Failed to fetch ledger");}finally{o.current&&u(false);}},[i,e]);return gt.useEffect(()=>(o.current=true,p(),()=>{o.current=false;}),[p]),{entries:r,loading:d,error:g,refresh:p}}function le(){let{config:e,callbacks:t}=v(),[r,s]=gt.useState(false),[d,u]=gt.useState(null),[g,a]=gt.useState(null);return {mint:gt.useCallback(async(i,p,m=1)=>{try{s(!0),a(null),t.onPaymentComplete?.(i,p);let l=await fetch(`${e.apiBaseUrl}/api/keys/mint`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({paymentMethod:i,paymentRef:p,quantity:m})});if(!l.ok){let P="Key generation failed";try{let w=await l.json();w.error&&(P=w.error);}catch{}throw new Error(P)}let n=await l.json(),c=n.keys?.[0]||n,N={success:!0,key:{id:c.keyId||c.id||"",key:c.key||"",userId:c.userId||"",status:"active",tokenBalance:c.tokenBalance??c.currentTokenBalance??0,maxTokens:c.initialTokenBalance??c.tokenBalance??0,createdAt:String(c.createdAt||Date.now())},transactionId:p};return u(N),t.onMintSuccess?.(N),N}catch(l){let n=l instanceof Error?l:new Error("Key generation failed");return a(n.message),t.onMintError?.(n),{success:false,error:n.message}}finally{s(false);}},[e.apiBaseUrl,t]),minting:r,result:d,error:g}}function de(){let{config:e,callbacks:t}=v(),[r,s]=gt.useState(false),[d,u]=gt.useState(null),[g,a]=gt.useState(null),o=gt.useRef(true),i=gt.useCallback(async(m,l)=>{try{s(!0),a(null);let n=await fetch(`${e.apiBaseUrl}/api/keys/${m}/list`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({askPriceCents:l,stackId:e.stackId,stackName:e.stackName})});if(!n.ok){let N=`Listing failed (${n.status})`;try{let P=await n.json();P.error&&(N=P.error);}catch{}throw new Error(N)}let c=await n.json();return o.current&&(u(c),t.onListingCreated?.(c)),c}catch(n){let c=n instanceof Error?n.message:"Listing failed";return o.current&&a(c),null}finally{o.current&&s(false);}},[e.apiBaseUrl,e.stackId,e.stackName,t]),p=gt.useCallback(async m=>{try{s(!0),a(null);let l=await fetch(`${e.apiBaseUrl}/api/keys/${m}/list`,{method:"DELETE"});if(!l.ok){let n=`Delist failed (${l.status})`;try{let c=await l.json();c.error&&(n=c.error);}catch{}throw new Error(n)}return o.current&&u(null),!0}catch(l){let n=l instanceof Error?l.message:"Delist failed";return o.current&&a(n),false}finally{o.current&&s(false);}},[e.apiBaseUrl]);return {listKey:i,delistKey:p,listing:r,result:d,error:g}}function ue(){let{config:e}=v(),[t,r]=gt.useState(false),[s,d]=gt.useState(null),u=gt.useRef(true);return {createSession:gt.useCallback(async(a,o)=>{try{r(!0),d(null);let i=await fetch(`${e.apiBaseUrl}/api/keys/stripe-session`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({priceCents:a,quantity:o,stackId:e.stackId,referrerUrl:window.location.href})});if(!i.ok){let m=`Checkout failed (${i.status})`;try{let l=await i.json();l.error&&(m=l.error);}catch{}throw new Error(m)}let{url:p}=await i.json();if(p){try{sessionStorage.setItem("keyutils_stripe_pending","1");}catch{}window.location.href=p;}else throw new Error("No checkout URL returned")}catch(i){if(u.current){let p=i instanceof Error?i.message:"Checkout failed";d(p),r(false);}}},[e.apiBaseUrl,e.stackId]),loading:t,error:s}}var it=/^[1-9A-HJ-NP-Za-km-z]{32,44}$/;function pe({amountSol:e,onSuccess:t,onError:r,disabled:s,className:d,style:u}){let{config:g}=v(),[a,o]=gt.useState(false),[i,p]=gt.useState(null),m=!!(g.merchantWallet&&it.test(g.merchantWallet)),l=gt.useCallback(async()=>{if(m)try{o(!0),p(null);let{Connection:n,PublicKey:c,Transaction:N,SystemProgram:P,LAMPORTS_PER_SOL:w}=await import('@solana/web3.js'),B=typeof window<"u"?window.solana:void 0;if(!B||typeof B!="object"||!B.isPhantom)throw new Error("Phantom wallet not found. Please install Phantom.");let L=B,j;try{j=new c(g.merchantWallet);}catch{throw new Error("Invalid merchant wallet address.")}let A=await L.connect(),k=new c(A.publicKey.toString()),W=new n(g.solanaRpcUrl||"https://api.mainnet-beta.solana.com"),{blockhash:$}=await W.getLatestBlockhash(),H=BigInt(Math.floor(e*1e9)),ee=new N({recentBlockhash:$,feePayer:k}).add(P.transfer({fromPubkey:k,toPubkey:j,lamports:Number(H)})),{signature:U}=await L.signAndSendTransaction(ee);t(U);}catch(n){let c=n instanceof Error?n:new Error("Payment failed");p(c.message),r?.(c);}finally{o(false);}},[e,g.merchantWallet,m,t,r]);return jsxRuntime.jsxs("div",{children:[jsxRuntime.jsx("button",{onClick:l,disabled:s||a||!m,"aria-label":`Pay ${e.toFixed(4)} SOL`,className:f("flex w-full items-center justify-center gap-2 rounded-lg bg-purple-600 px-4 py-3 font-medium text-white transition-colors hover:bg-purple-700 disabled:opacity-50 disabled:cursor-not-allowed",d),style:u,children:a?jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[jsxRuntime.jsx(lucideReact.Loader2,{className:"h-4 w-4 animate-spin"}),"Processing..."]}):jsxRuntime.jsxs(jsxRuntime.Fragment,{children:["Pay ",e.toFixed(4)," SOL"]})}),i&&jsxRuntime.jsx("p",{className:"mt-2 text-center text-xs text-red-500",children:i})]})}function ge({result:e,className:t,style:r}){let[s,d]=gt.useState(false),u=gt.useRef(null);gt.useEffect(()=>()=>{u.current&&clearTimeout(u.current);},[]);let g=gt.useCallback(()=>{e.key?.key&&(navigator.clipboard.writeText(e.key.key),d(true),u.current&&clearTimeout(u.current),u.current=setTimeout(()=>d(false),2e3));},[e.key?.key]);return !e.success||!e.key?null:jsxRuntime.jsxs("div",{className:f("rounded-lg border border-green-500/20 bg-green-500/10 p-6 text-center",t),style:r,children:[jsxRuntime.jsx(lucideReact.CheckCircle,{className:"mx-auto mb-3 h-12 w-12 text-green-500"}),jsxRuntime.jsx("h3",{className:"mb-2 text-lg font-semibold text-foreground",children:"Key Generated!"}),jsxRuntime.jsx("p",{className:"mb-4 text-sm text-muted-foreground",children:"Your node key has been successfully generated."}),jsxRuntime.jsxs("div",{className:"mx-auto flex max-w-md items-center gap-2 rounded-md border bg-background p-3",children:[jsxRuntime.jsx("code",{className:"flex-1 truncate text-xs text-foreground",children:e.key.key}),jsxRuntime.jsx("button",{onClick:g,"aria-label":"Copy key to clipboard",className:"shrink-0 rounded-md p-1.5 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground",children:s?jsxRuntime.jsx(lucideReact.Check,{className:"h-4 w-4 text-green-500"}):jsxRuntime.jsx(lucideReact.Copy,{className:"h-4 w-4"})})]})]})}function I({className:e}){return jsxRuntime.jsx("div",{className:f("animate-pulse rounded-xl bg-muted",e)})}function xe(){return jsxRuntime.jsxs("div",{className:"space-y-6",children:[jsxRuntime.jsxs("div",{className:"px-6 py-10 flex flex-col items-center gap-3",children:[jsxRuntime.jsx(I,{className:"h-14 w-48"}),jsxRuntime.jsx(I,{className:"h-5 w-64"})]}),jsxRuntime.jsx(I,{className:"h-[72px] w-full rounded-2xl"}),jsxRuntime.jsx(I,{className:"h-[68px] w-full rounded-2xl"}),jsxRuntime.jsx(I,{className:"h-[60px] w-full rounded-2xl"})]})}function J({count:e=3}){return jsxRuntime.jsx("div",{className:"space-y-3",children:Array.from({length:e}).map((t,r)=>jsxRuntime.jsxs("div",{className:"rounded-xl border p-4 space-y-3",children:[jsxRuntime.jsxs("div",{className:"flex items-center justify-between",children:[jsxRuntime.jsx(I,{className:"h-4 w-32"}),jsxRuntime.jsx(I,{className:"h-4 w-16"})]}),jsxRuntime.jsx(I,{className:"h-2 w-full rounded-full"}),jsxRuntime.jsxs("div",{className:"flex justify-between",children:[jsxRuntime.jsx(I,{className:"h-3 w-24"}),jsxRuntime.jsx(I,{className:"h-3 w-20"})]})]},r))})}function wt({quantity:e,max:t,onChange:r}){return jsxRuntime.jsxs("div",{className:"flex items-center justify-between rounded-2xl border bg-muted/20 px-6 py-5",children:[jsxRuntime.jsx("span",{className:"text-sm font-medium text-foreground",children:"Quantity"}),jsxRuntime.jsxs("div",{className:"flex items-center gap-0 rounded-xl border bg-background",children:[jsxRuntime.jsx("button",{onClick:()=>r(Math.max(1,e-1)),disabled:e<=1,"aria-label":"Decrease quantity",className:"flex h-12 w-12 items-center justify-center rounded-l-xl text-muted-foreground transition-colors hover:bg-muted hover:text-foreground disabled:opacity-30",children:jsxRuntime.jsx(lucideReact.Minus,{className:"h-5 w-5"})}),jsxRuntime.jsx("span",{className:"flex h-12 w-14 items-center justify-center border-x text-xl font-bold text-foreground","aria-live":"polite",children:e}),jsxRuntime.jsx("button",{onClick:()=>r(Math.min(t,e+1)),disabled:e>=t,"aria-label":"Increase quantity",className:"flex h-12 w-12 items-center justify-center rounded-r-xl text-muted-foreground transition-colors hover:bg-muted hover:text-foreground disabled:opacity-30",children:jsxRuntime.jsx(lucideReact.Plus,{className:"h-5 w-5"})})]})]})}function je({pricing:e,quantity:t}){let[r,s]=gt.useState(false),d=e.priceUsd*t;return jsxRuntime.jsxs("div",{className:"rounded-2xl border bg-background",children:[jsxRuntime.jsxs("button",{onClick:()=>s(!r),"aria-expanded":r,className:"flex w-full items-center justify-between px-6 py-5 text-left",children:[jsxRuntime.jsxs("span",{className:"text-foreground",children:[jsxRuntime.jsxs("span",{className:"text-xl font-bold",children:["$",d.toFixed(2)]})," ",jsxRuntime.jsx("span",{className:"text-sm text-muted-foreground",children:"total fees included"})]}),r?jsxRuntime.jsx(lucideReact.ChevronUp,{className:"h-5 w-5 text-muted-foreground"}):jsxRuntime.jsx(lucideReact.ChevronDown,{className:"h-5 w-5 text-muted-foreground"})]}),r&&jsxRuntime.jsxs("div",{className:"border-t px-6 pb-8 pt-4 space-y-3 text-sm",children:[jsxRuntime.jsxs("div",{className:"flex justify-between",children:[jsxRuntime.jsxs("span",{className:"text-muted-foreground",children:["Node Key \xD7 ",t]}),jsxRuntime.jsxs("span",{className:"text-foreground",children:["$",(e.priceUsd*t).toFixed(2)]})]}),jsxRuntime.jsxs("div",{className:"flex justify-between",children:[jsxRuntime.jsx("span",{className:"text-muted-foreground",children:"Keys sold"}),jsxRuntime.jsx("span",{className:"text-foreground",children:e.keysSold})]}),e.daysUntilHalving>0&&jsxRuntime.jsxs("div",{className:"flex justify-between text-muted-foreground",children:[jsxRuntime.jsx("span",{children:"Next refresh"}),jsxRuntime.jsxs("span",{children:[e.daysUntilHalving," day",e.daysUntilHalving!==1?"s":""]})]}),jsxRuntime.jsxs("div",{className:"flex justify-between border-t pt-3 font-semibold",children:[jsxRuntime.jsx("span",{className:"text-foreground",children:"Total"}),jsxRuntime.jsxs("span",{className:"text-foreground",children:["$",d.toFixed(2)]})]})]})]})}function ye({className:e,style:t}){let{config:r}=v(),{pricing:s,loading:d,error:u,refresh:g}=se(),{mint:a,minting:o,result:i,error:p}=le(),{createSession:m,loading:l,error:n}=ue(),[c,N]=gt.useState(1),[P,w]=gt.useState(r.paymentMethods?.includes("stripe")?"stripe":"solana"),[B,L]=gt.useState(0),[j,A]=gt.useState(false),k=gt__default.default.useRef(c);k.current=c;let W=Math.max(1,r.maxQuantity||5),$=[...r.paymentMethods||["solana"]].sort((U,G)=>U==="stripe"?-1:G==="stripe"?1:0);if(gt.useEffect(()=>{if(typeof window>"u")return;let G=new URLSearchParams(window.location.search).get("session_id");if(!G){try{sessionStorage.removeItem("keyutils_stripe_pending");}catch{}return}if(/^cs_(test|live)_/.test(G)&&!j&&!i?.success){let we=false;try{we=sessionStorage.getItem("keyutils_stripe_pending")==="1";}catch{}if(!we)return;try{sessionStorage.removeItem("keyutils_stripe_pending");}catch{}A(true);let Pe=new URL(window.location.href);Pe.searchParams.delete("session_id"),window.history.replaceState({},"",Pe.toString()),a("stripe",G,k.current);}},[j,i,a]),i?.success)return jsxRuntime.jsx("div",{className:f("flex flex-col items-center justify-center",e),style:t,children:jsxRuntime.jsx(ge,{result:i})});if(j||o)return jsxRuntime.jsx("div",{className:f("flex flex-col",e),style:t,children:jsxRuntime.jsx(xe,{})});if(d)return jsxRuntime.jsx("div",{className:f("flex flex-col",e),style:t,children:jsxRuntime.jsx(xe,{})});if(u||!s)return jsxRuntime.jsxs("div",{className:f("flex flex-col items-center justify-center gap-4 py-24",e),style:t,children:[jsxRuntime.jsxs("div",{className:"rounded-xl border border-red-500/20 bg-red-500/5 px-6 py-5 text-center max-w-sm",children:[jsxRuntime.jsx("p",{className:"text-sm font-medium text-foreground",children:"Unable to load pricing"}),jsxRuntime.jsx("p",{className:"mt-1 text-xs text-muted-foreground",children:u||"Please check your connection and try again."})]}),jsxRuntime.jsx("button",{onClick:()=>g(),className:"text-sm text-muted-foreground hover:text-foreground transition-colors",children:"Try again"})]});let H=async U=>{L(2),await a(P,U,c);},ee=()=>{m(s.priceCents,c);};return jsxRuntime.jsx("div",{className:f("flex flex-col",e),style:t,children:jsxRuntime.jsxs("div",{className:"space-y-6",children:[B===0&&jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[r.keyImage&&jsxRuntime.jsx("img",{src:r.keyImage,alt:"Node Key",className:r.keyImageClass||"w-1/2 mx-auto"}),jsxRuntime.jsxs("div",{className:"px-6 py-10 text-center",children:[jsxRuntime.jsxs("p",{className:"text-5xl font-bold tracking-tight text-foreground md:text-6xl",children:["$",s.priceUsd.toFixed(2)]}),jsxRuntime.jsxs("p",{className:"mt-2 text-base text-muted-foreground",children:["per key \xB7 ",s.tokenAllocationFormatted||T(s.tokenAllocation)," tokens included"]})]}),jsxRuntime.jsx(wt,{quantity:c,max:W,onChange:N}),jsxRuntime.jsx(je,{pricing:s,quantity:c}),jsxRuntime.jsx("button",{onClick:()=>L(1),className:"w-full rounded-2xl bg-foreground py-5 text-center text-base font-semibold text-background transition-colors hover:bg-foreground/90",children:"Continue to Payment"})]}),B===1&&jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[$.length>1&&jsxRuntime.jsx("div",{role:"tablist",className:"flex gap-1 rounded-2xl bg-muted/50 p-1.5",children:$.map(U=>jsxRuntime.jsxs("button",{role:"tab","aria-selected":P===U,onClick:()=>w(U),className:f("flex flex-1 items-center justify-center gap-2.5 rounded-xl py-4 text-sm font-medium transition-colors",P===U?"bg-background text-foreground shadow-sm":"text-muted-foreground hover:text-foreground"),children:[U==="stripe"?jsxRuntime.jsx(lucideReact.CreditCard,{className:"h-5 w-5"}):jsxRuntime.jsx(lucideReact.Wallet,{className:"h-5 w-5"}),U==="stripe"?"Credit Card":"Crypto"]},U))}),jsxRuntime.jsx(je,{pricing:s,quantity:c}),P==="stripe"?jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[jsxRuntime.jsx("button",{onClick:ee,disabled:l,className:"flex w-full items-center justify-center gap-2 rounded-2xl bg-foreground py-5 text-base font-semibold text-background transition-colors hover:bg-foreground/90 disabled:opacity-50",children:l?jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[jsxRuntime.jsx(lucideReact.Loader2,{className:"h-5 w-5 animate-spin"}),"Redirecting to Stripe..."]}):jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[jsxRuntime.jsx(lucideReact.CreditCard,{className:"h-5 w-5"}),"Pay with Card"]})}),n&&jsxRuntime.jsx("p",{className:"text-center text-sm text-red-500",children:n})]}):P==="solana"?jsxRuntime.jsx(pe,{amountSol:s.priceSol??0,onSuccess:H,className:"rounded-2xl py-5 text-base"}):null,p&&jsxRuntime.jsx("p",{className:"text-center text-sm text-red-500",children:p}),jsxRuntime.jsx("button",{onClick:()=>L(0),className:"w-full py-3 text-center text-sm text-muted-foreground hover:text-foreground transition-colors",children:"\u2190 Back"})]})]})})}var Kt={ios:"iOS",android:"Android",windows:"Windows",mac:"macOS",linux:"Linux"},Ut=["mac","windows","linux","ios","android"];function be({downloads:e,className:t,style:r}){let{config:s}=v(),d=gt.useMemo(()=>Ke(),[]),u=e||Ut.map(o=>({platform:o,label:Kt[o],url:"#",compatible:o===d})),g=u.find(o=>o.compatible)||u[0],a=u.filter(o=>o.platform!==g.platform);return jsxRuntime.jsxs("div",{className:f("space-y-8",t),style:r,children:[jsxRuntime.jsxs("div",{className:"flex flex-col items-center gap-6 pt-4 mt-4",children:[s.aispImage&&jsxRuntime.jsx("img",{src:s.aispImage,alt:"aiSP",className:"w-full sm:w-1/2 mx-auto px-4 sm:px-0"}),jsxRuntime.jsxs("div",{className:"text-center",children:[jsxRuntime.jsx("p",{className:"text-lg sm:text-2xl font-medium text-foreground",children:"Download the AI Service Provider App"}),jsxRuntime.jsx("p",{className:"mt-1 text-sm sm:text-base text-muted-foreground",children:"Load your keys and run your node."})]}),jsxRuntime.jsxs("a",{href:g.url,className:"inline-flex items-center gap-3 rounded-full px-10 py-4 text-lg font-semibold text-white transition-colors hover:opacity-90",style:{backgroundColor:"#0900f4"},children:[jsxRuntime.jsx(lucideReact.Download,{className:"h-5 w-5"}),"Get aiSP for ",g.label]})]}),jsxRuntime.jsxs("p",{className:"text-center text-sm text-muted-foreground",children:["aiSP is also available on"," ",a.map((o,i)=>jsxRuntime.jsxs(gt__default.default.Fragment,{children:[i>0&&(i===a.length-1?" and ":", "),jsxRuntime.jsx("a",{href:o.url,className:"font-medium underline transition-colors hover:text-foreground",style:{color:"#066bff"},children:o.label})]},o.platform)),"."]}),jsxRuntime.jsxs("div",{className:"flex flex-col items-center gap-2 mt-12",children:[jsxRuntime.jsxs("p",{className:"text-center text-xs text-muted-foreground flex justify-center gap-0.5 flex-wrap",children:["Source code is available on",jsxRuntime.jsx("a",{href:"https://github.com/stack-net",target:"_blank",rel:"noopener noreferrer",className:"text-foreground underline hover:text-foreground/80 color-[#0900f4]",children:"Github"})]}),jsxRuntime.jsxs("p",{className:"text-center text-xs text-muted-foreground flex justify-center gap-0.5 flex-wrap",children:["By downloading, you agree to the",jsxRuntime.jsx("a",{href:s.termsUrl||"/terms",className:"text-foreground underline hover:text-foreground/80",children:"Open Source Applications Terms"})]})]})]})}function Y({current:e,max:t,className:r,style:s,showLabel:d=true}){let u=Ce(e,t),g=Se(u);return jsxRuntime.jsxs("div",{className:f("w-full",r),style:s,children:[d&&jsxRuntime.jsxs("div",{className:"mb-1 flex items-center justify-between text-xs text-muted-foreground",children:[jsxRuntime.jsxs("span",{children:[T(e)," / ",T(t)]}),jsxRuntime.jsxs("span",{children:[u,"%"]})]}),jsxRuntime.jsx("div",{className:"h-2 w-full overflow-hidden rounded-full bg-muted",children:jsxRuntime.jsx("div",{className:f("h-full rounded-full transition-all duration-500",g),style:{width:`${u}%`}})})]})}function Tt({nodeKey:e,selected:t,onSelect:r}){return jsxRuntime.jsxs("button",{onClick:r,className:f("flex w-full items-start gap-3 rounded-xl border p-4 text-left transition-all",t?"border-foreground/30 bg-foreground/5 ring-1 ring-foreground/10":"border-border hover:border-foreground/20"),children:[jsxRuntime.jsx("div",{className:f("mt-0.5 flex h-5 w-5 shrink-0 items-center justify-center rounded-full border-2 transition-colors",t?"border-foreground bg-foreground":"border-muted-foreground/40"),children:t&&jsxRuntime.jsx(lucideReact.Check,{className:"h-3 w-3 text-background"})}),jsxRuntime.jsxs("div",{className:"min-w-0 flex-1",children:[jsxRuntime.jsxs("div",{className:"flex items-center justify-between",children:[jsxRuntime.jsx("span",{className:"text-sm font-medium text-foreground",children:e.label||e.id.slice(0,16)}),jsxRuntime.jsx("span",{className:f("text-xs font-medium capitalize",e.status==="active"?"text-green-500":"text-muted-foreground"),children:e.status})]}),jsxRuntime.jsx(Y,{current:e.tokenBalance,max:e.maxTokens,className:"mt-2"})]})]})}function ve({className:e,style:t}){let{config:r}=v(),{keys:s,loading:d,error:u,refresh:g}=Q(),{listKey:a,listing:o,result:i,error:p}=de(),[m,l]=gt.useState(null),[n,c]=gt.useState(""),[N,P]=gt.useState(false),w=s.find(k=>k.id===m),B=parseInt(n.replace(/[^0-9]/g,""),10)||0,L=w&&B>0&&N&&!o,j=async()=>{if(!L||!w)return;let k=B*100;await a(w.id,k)&&g();};if(i)return jsxRuntime.jsxs("div",{className:f("flex flex-col items-center gap-4 rounded-xl border border-green-500/20 bg-green-500/10 py-12",e),style:t,children:[jsxRuntime.jsx(lucideReact.CheckCircle,{className:"h-12 w-12 text-green-500"}),jsxRuntime.jsx("h3",{className:"text-lg font-semibold text-foreground",children:"Key Listed!"}),jsxRuntime.jsx("p",{className:"text-sm text-muted-foreground",children:"Your key is now on the Global Open AI Network marketplace."}),jsxRuntime.jsxs("p",{className:"text-xs text-muted-foreground",children:["Listing ID: ",jsxRuntime.jsx("code",{children:i.listingId})]})]});if(d)return jsxRuntime.jsxs("div",{className:f("space-y-5 px-1",e),style:t,children:[jsxRuntime.jsx("h3",{className:"text-2xl mt-3 mb-2 font-semibold text-foreground",children:"Your Keys"}),jsxRuntime.jsx(J,{count:2})]});if(u)return jsxRuntime.jsxs("div",{className:f("flex flex-col items-center justify-center gap-4 py-16",e),style:t,children:[jsxRuntime.jsxs("div",{className:"rounded-xl border border-red-500/20 bg-red-500/5 px-6 py-5 text-center max-w-sm",children:[jsxRuntime.jsx("p",{className:"text-sm font-medium text-foreground",children:"Unable to load your keys"}),jsxRuntime.jsx("p",{className:"mt-1 text-xs text-muted-foreground",children:u})]}),jsxRuntime.jsx("button",{onClick:()=>g(),className:"text-sm text-muted-foreground hover:text-foreground transition-colors",children:"Try again"})]});let A=s.filter(k=>k.status==="active");return A.length===0?jsxRuntime.jsxs("div",{className:f("flex flex-col items-center justify-center gap-3 rounded-xl border border-dashed py-16",e),style:t,children:[jsxRuntime.jsx(lucideReact.AlertTriangle,{className:"h-8 w-8 text-muted-foreground"}),jsxRuntime.jsx("p",{className:"text-sm text-muted-foreground",children:"No active keys to sell."})]}):jsxRuntime.jsxs("div",{className:f("space-y-5",e),style:t,children:[jsxRuntime.jsxs("div",{children:[jsxRuntime.jsx("h3",{className:"text-2xl mt-3 mb-2 font-semibold text-foreground",children:"Your Keys"}),jsxRuntime.jsx("div",{className:"space-y-2",children:A.map(k=>jsxRuntime.jsx(Tt,{nodeKey:k,selected:m===k.id,onSelect:()=>l(k.id)},k.id))})]}),w&&jsxRuntime.jsxs("div",{className:"rounded-xl border bg-muted/30 p-5 space-y-2 text-sm",children:[jsxRuntime.jsxs("div",{className:"flex justify-between",children:[jsxRuntime.jsx("span",{className:"text-muted-foreground",children:"Key ID"}),jsxRuntime.jsx("code",{className:"text-xs text-foreground",children:w.id})]}),jsxRuntime.jsxs("div",{className:"flex justify-between",children:[jsxRuntime.jsx("span",{className:"text-muted-foreground",children:"Originating Stack"}),jsxRuntime.jsx("span",{className:"text-foreground",children:r.stackName||"Unknown"})]}),jsxRuntime.jsxs("div",{className:"flex justify-between",children:[jsxRuntime.jsx("span",{className:"text-muted-foreground",children:"Tokens (remaining / total)"}),jsxRuntime.jsxs("span",{className:"text-foreground",children:[T(w.tokenBalance)," / ",T(w.maxTokens)]})]}),w.paperWork&&jsxRuntime.jsxs("div",{className:"flex justify-between",children:[jsxRuntime.jsx("span",{className:"text-muted-foreground",children:"Paper earned"}),jsxRuntime.jsx("span",{className:"text-foreground",children:w.paperWork})]}),jsxRuntime.jsxs("div",{className:"flex justify-between",children:[jsxRuntime.jsx("span",{className:"text-muted-foreground",children:"Created"}),jsxRuntime.jsx("span",{className:"text-foreground",children:F(w.createdAt)})]})]}),jsxRuntime.jsxs("div",{children:[jsxRuntime.jsx("label",{htmlFor:"listing-price",className:"mb-2 block text-sm sm:text-lg font-semibold text-foreground",children:"Listing Price (USD)"}),jsxRuntime.jsxs("div",{className:"relative",children:[jsxRuntime.jsx("span",{className:"absolute left-4 top-1/2 -translate-y-1/2 text-2xl font-bold text-muted-foreground",children:"$"}),jsxRuntime.jsx("input",{id:"listing-price",type:"text",inputMode:"numeric",value:n,onChange:k=>{let W=k.target.value.replace(/[^0-9]/g,""),$=parseInt(W,10);c(isNaN($)?"":$.toLocaleString("en-US"));},placeholder:"0",className:"w-full rounded-xl border bg-background py-5 pl-10 pr-4 text-3xl font-bold text-foreground placeholder:text-muted-foreground/30 focus:outline-none focus:ring-2 focus:ring-foreground/20"})]})]}),jsxRuntime.jsxs("label",{className:"flex items-start gap-3 cursor-pointer",children:[jsxRuntime.jsx("input",{type:"checkbox",checked:N,onChange:k=>P(k.target.checked),className:"sr-only"}),jsxRuntime.jsx("div",{"aria-hidden":"true",className:f("mt-0.5 flex h-5 w-5 shrink-0 items-center justify-center rounded border-2 transition-colors",N?"border-foreground bg-foreground":"border-muted-foreground/40"),children:N&&jsxRuntime.jsx(lucideReact.Check,{className:"h-3 w-3 text-background"})}),jsxRuntime.jsxs("span",{className:"text-xs text-muted-foreground leading-relaxed",children:["I agree to list this key on the Global Open AI Network marketplace. The key will be deactivated upon sale and transferred to the buyer. Listing fees may apply."," ",jsxRuntime.jsx("a",{href:r.termsUrl||"/terms",className:"text-foreground underline",children:"Terms"})]})]}),p&&jsxRuntime.jsx("p",{className:"text-sm text-red-500",children:p}),jsxRuntime.jsxs("button",{onClick:j,disabled:!L,className:f("flex w-full items-center justify-center gap-2 rounded-xl py-4 text-sm font-semibold transition-colors",L?"bg-foreground text-background hover:bg-foreground/90":"bg-muted text-muted-foreground cursor-not-allowed"),children:[jsxRuntime.jsx(lucideReact.ExternalLink,{className:"h-4 w-4"}),o?"Listing...":"List on Marketplace"]})]})}var It=[{id:"buy",label:"Buy"},{id:"use",label:"Use"},{id:"sell",label:"Sell"}];function Mt({active:e,onChange:t}){return jsxRuntime.jsx("div",{role:"tablist","aria-label":"Key actions",className:"flex gap-1 rounded-2xl bg-muted/50 p-1.5",children:It.map(({id:r,label:s})=>jsxRuntime.jsx("button",{role:"tab","aria-selected":e===r,"aria-controls":`panel-${r}`,id:`tab-${r}`,onClick:()=>t(r),className:f("flex flex-1 items-center justify-center gap-2.5 rounded-xl py-3.5 text-sm font-medium transition-all",e===r?"bg-background text-foreground shadow-sm":"text-muted-foreground hover:text-foreground"),children:jsxRuntime.jsx("span",{className:"inline",children:s})},r))})}function jt({defaultTab:e="buy",className:t,style:r}){let[s,d]=gt.useState(e);return jsxRuntime.jsxs("div",{className:f("flex flex-col",t),style:r,children:[jsxRuntime.jsx("div",{className:"sticky top-0 z-10 bg-background/80 px-5 pb-3 pt-2 sm:pt-5 backdrop-blur-sm sm:px-10 md:px-16 lg:px-20",children:jsxRuntime.jsx("div",{className:"mx-auto max-w-xl",children:jsxRuntime.jsx(Mt,{active:s,onChange:d})})}),jsxRuntime.jsx("div",{className:"flex flex-1 flex-col px-5 pb-8 sm:px-10 md:px-16 md:pb-12 lg:px-20",children:jsxRuntime.jsx("div",{className:"mx-auto w-full max-w-xl flex-1",children:jsxRuntime.jsxs("div",{role:"tabpanel",id:`panel-${s}`,"aria-labelledby":`tab-${s}`,children:[s==="buy"&&jsxRuntime.jsx(ye,{className:"flex-1"}),s==="use"&&jsxRuntime.jsx(be,{className:"flex-1"}),s==="sell"&&jsxRuntime.jsx(ve,{className:"flex-1"})]})})})]})}function _t({apiBaseUrl:e,stackId:t,stackName:r,paymentMethods:s,merchantWallet:d,stripePublicKey:u,theme:g,maxQuantity:a,keyImage:o,keyImageClass:i,aispImage:p,termsUrl:m,onMintSuccess:l,onMintError:n,onPaymentStart:c,onPaymentComplete:N,onListingCreated:P,defaultTab:w,className:B,style:L}){return jsxRuntime.jsx(te,{config:{apiBaseUrl:e,stackId:t,stackName:r,paymentMethods:s,merchantWallet:d,stripePublicKey:u,theme:g,maxQuantity:a,keyImage:o,keyImageClass:i,aispImage:p,termsUrl:m},callbacks:{onMintSuccess:l,onMintError:n,onPaymentStart:c,onPaymentComplete:N,onListingCreated:P},children:jsxRuntime.jsx(jt,{defaultTab:w,className:B,style:L})})}var Ne=20;function ke({nodeKey:e,className:t,style:r}){let[s,d]=gt.useState(false),[u,g]=gt.useState(false),a=gt.useRef(null),{entries:o,loading:i}=ae(s?e.id:null);gt.useEffect(()=>()=>{a.current&&clearTimeout(a.current);},[]);let p=gt.useCallback(()=>{e.key&&(navigator.clipboard.writeText(e.key),g(true),a.current&&clearTimeout(a.current),a.current=setTimeout(()=>g(false),2e3));},[e.key]),m=e.status==="active"?"text-green-500":e.status==="expired"?"text-red-500":"text-yellow-500",l=o.slice(0,Ne);return jsxRuntime.jsxs("div",{className:f("rounded-lg border bg-background",t),style:r,children:[jsxRuntime.jsxs("div",{className:"p-4",children:[jsxRuntime.jsxs("div",{className:"mb-3 flex items-center justify-between",children:[jsxRuntime.jsxs("div",{className:"flex items-center gap-2",children:[jsxRuntime.jsx("button",{onClick:()=>d(!s),"aria-expanded":s,"aria-label":s?"Collapse ledger":"Expand ledger",className:"text-muted-foreground hover:text-foreground",children:s?jsxRuntime.jsx(lucideReact.ChevronDown,{className:"h-4 w-4"}):jsxRuntime.jsx(lucideReact.ChevronRight,{className:"h-4 w-4"})}),jsxRuntime.jsx("span",{className:"text-sm font-medium text-foreground",children:e.label||`Key ${e.id.slice(0,8)}`})]}),jsxRuntime.jsx("span",{className:f("text-xs font-medium capitalize",m),children:e.status})]}),e.key&&jsxRuntime.jsxs("div",{className:"mb-3 flex items-center gap-2",children:[jsxRuntime.jsx("code",{className:"flex-1 truncate rounded bg-muted px-2 py-1 text-xs text-muted-foreground",children:e.key}),jsxRuntime.jsx("button",{onClick:p,"aria-label":"Copy key to clipboard",className:"shrink-0 rounded p-1 text-muted-foreground hover:bg-muted hover:text-foreground",children:u?jsxRuntime.jsx(lucideReact.Check,{className:"h-3.5 w-3.5 text-green-500"}):jsxRuntime.jsx(lucideReact.Copy,{className:"h-3.5 w-3.5"})})]}),jsxRuntime.jsx(Y,{current:e.tokenBalance,max:e.maxTokens}),jsxRuntime.jsxs("div",{className:"mt-2 flex items-center justify-between text-xs text-muted-foreground",children:[jsxRuntime.jsxs("span",{children:["Created ",F(e.createdAt)]}),e.expiresAt&&jsxRuntime.jsxs("span",{children:["Expires ",F(e.expiresAt)]})]})]}),s&&jsxRuntime.jsxs("div",{className:"border-t px-4 py-3",children:[jsxRuntime.jsx("h4",{className:"mb-2 text-xs font-medium text-muted-foreground",children:"Ledger"}),i?jsxRuntime.jsx("p",{className:"text-xs text-muted-foreground",children:"Loading..."}):l.length===0?jsxRuntime.jsx("p",{className:"text-xs text-muted-foreground",children:"No ledger entries."}):jsxRuntime.jsxs("div",{className:"space-y-1.5",children:[l.map(n=>jsxRuntime.jsxs("div",{className:"flex items-center justify-between text-xs",children:[jsxRuntime.jsxs("div",{className:"flex items-center gap-2",children:[jsxRuntime.jsxs("span",{className:f("font-medium",n.type==="credit"||n.type==="reward"?"text-green-500":"text-red-500"),children:[n.type==="credit"||n.type==="reward"?"+":"-",T(Math.abs(n.amount))]}),jsxRuntime.jsx("span",{className:"text-muted-foreground",children:n.description})]}),jsxRuntime.jsx("span",{className:"text-muted-foreground",children:F(n.createdAt)})]},n.id)),o.length>Ne&&jsxRuntime.jsxs("p",{className:"pt-1 text-xs text-muted-foreground",children:["Showing ",Ne," of ",o.length," entries"]})]})]})]})}var qt=[{id:"buy",label:"Buy"},{id:"use",label:"Use"},{id:"sell",label:"Sell"}];function X({baseHref:e}){return jsxRuntime.jsx("div",{className:"flex gap-1 rounded-2xl bg-muted/50 p-1.5 mb-6",children:qt.map(({id:t,label:r})=>jsxRuntime.jsx("a",{href:`${e}?tab=${t}`,className:"flex flex-1 items-center justify-center gap-2.5 rounded-xl py-3.5 text-sm font-medium text-muted-foreground hover:text-foreground transition-all",children:r},t))})}function Vt({getKeyHref:e="/keys/get",showTabs:t=true,className:r,style:s}){let{keys:d,totalBalance:u,loading:g,error:a,refresh:o}=Q();return g?jsxRuntime.jsxs("div",{className:f("space-y-4",r),style:s,children:[t&&e&&jsxRuntime.jsx(X,{baseHref:e}),jsxRuntime.jsx(J,{count:3})]}):a?jsxRuntime.jsxs("div",{className:f("space-y-4",r),style:s,children:[t&&e&&jsxRuntime.jsx(X,{baseHref:e}),jsxRuntime.jsxs("div",{className:"flex flex-col items-center justify-center gap-4 py-12",children:[jsxRuntime.jsxs("div",{className:"rounded-xl border border-red-500/20 bg-red-500/5 px-6 py-5 text-center max-w-sm",children:[jsxRuntime.jsx("p",{className:"text-sm font-medium text-foreground",children:"Unable to load your keys"}),jsxRuntime.jsx("p",{className:"mt-1 text-xs text-muted-foreground",children:a})]}),jsxRuntime.jsx("button",{onClick:()=>o(),className:"text-sm text-muted-foreground hover:text-foreground transition-colors",children:"Try again"})]})]}):d.length===0?jsxRuntime.jsxs("div",{className:f("space-y-4",r),style:s,children:[t&&e&&jsxRuntime.jsx(X,{baseHref:e}),jsxRuntime.jsxs("div",{className:"flex flex-col items-center justify-center gap-4 rounded-lg border border-dashed py-12",children:[jsxRuntime.jsx(lucideReact.KeyRound,{className:"h-10 w-10 text-muted-foreground"}),jsxRuntime.jsxs("div",{className:"text-center",children:[jsxRuntime.jsx("p",{className:"text-sm font-medium text-foreground",children:"No keys yet"}),jsxRuntime.jsx("p",{className:"text-xs text-muted-foreground",children:"Get your first node key to start earning."})]}),e&&jsxRuntime.jsx("a",{href:e,className:"rounded-lg bg-foreground px-4 py-2 text-sm font-medium text-background transition-colors hover:bg-foreground/90",children:"Get a Key"})]})]}):jsxRuntime.jsxs("div",{className:f("space-y-4",r),style:s,children:[t&&e&&jsxRuntime.jsx(X,{baseHref:e}),jsxRuntime.jsxs("div",{className:"flex items-center justify-between",children:[jsxRuntime.jsxs("p",{className:"text-sm text-muted-foreground",children:[d.length," key",d.length!==1?"s":""," \xB7 Total balance: ",T(u)]}),e&&jsxRuntime.jsx("a",{href:e,className:"rounded-md bg-foreground px-3 py-1.5 text-xs font-medium text-background transition-colors hover:bg-foreground/90",children:"Get a Key"})]}),d.map(i=>jsxRuntime.jsx(ke,{nodeKey:i},i.id))]})}
|
|
2
|
+
exports.BuyKeyWidget=_t;exports.BuyPanel=ye;exports.KeyBalanceBar=Y;exports.KeyCard=ke;exports.KeyList=Vt;exports.KeyUtilsProvider=te;exports.MintSuccess=ge;exports.SellPanel=ve;exports.SolanaPayButton=pe;exports.UsePanel=be;exports.calculateBalancePercentage=Ce;exports.cn=f;exports.detectPlatform=Ke;exports.formatDate=F;exports.formatTokens=T;exports.getBalanceColor=Se;exports.useKeyLedger=ae;exports.useKeyUtilsContext=v;exports.useKeys=Q;exports.useListKey=de;exports.useMintKey=le;exports.usePricing=se;exports.useStripeCheckout=ue;
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {clsx}from'clsx';import {twMerge}from'tailwind-merge';import pt,{createContext,useContext,useMemo,useState,useRef,useEffect,useCallback}from'react';import {jsx,jsxs,Fragment}from'react/jsx-runtime';import {Loader2,CheckCircle,Check,Copy,CreditCard,Wallet,Download,AlertTriangle,ExternalLink,ChevronDown,ChevronRight,KeyRound,Minus,Plus,ChevronUp}from'lucide-react';function m(...e){return twMerge(clsx(e))}function L(e){return e==null?"0":e>=1e9?`${(e/1e9).toFixed(1)}B`:e>=1e6?`${(e/1e6).toFixed(1)}M`:e>=1e3?`${(e/1e3).toFixed(1)}K`:e.toLocaleString()}function Pe(e,t){return t<=0?0:Math.min(100,Math.round(e/t*100))}function Ce(e){return e>=70?"bg-green-500":e>=30?"bg-yellow-500":"bg-red-500"}function F(e){let t=typeof e=="string"&&/^\d+$/.test(e)?Number(e):e,r=new Date(t);return isNaN(r.getTime())?String(e):r.toLocaleDateString("en-US",{month:"short",day:"numeric",year:"numeric"})}function Se(){if(typeof navigator>"u")return "mac";let e=navigator.userAgent.toLowerCase();return /iphone|ipad|ipod/.test(e)?"ios":/android/.test(e)?"android":/win/.test(e)?"windows":/linux/.test(e)?"linux":"mac"}var Ke=createContext(null);function N(){let e=useContext(Ke);if(!e)throw new Error("useKeyUtilsContext must be used within a KeyUtilsProvider");return e}function ee({config:e,callbacks:t={},children:r}){typeof e.apiBaseUrl!="string"&&console.error("[KeyUtils] apiBaseUrl must be a string");let s=useMemo(()=>({config:e,callbacks:t}),[e,t]);return jsx(Ke.Provider,{value:s,children:r})}function re(){let{config:e}=N(),[t,r]=useState(null),[s,g]=useState(true),[c,f]=useState(null),a=useRef(true),o=useRef(e.apiBaseUrl);o.current=e.apiBaseUrl;let d=async()=>{try{a.current&&(g(!0),f(null));let p=await fetch(`${o.current}/api/keys/pricing`);if(!p.ok){let i=`Pricing unavailable (${p.status})`;try{let n=await p.json();n.error&&(i=n.error);}catch{}throw new Error(i)}let u=await p.json();a.current&&r(u);}catch(p){a.current&&f(p instanceof Error?p.message:"Failed to fetch pricing");}finally{a.current&&g(false);}};return useEffect(()=>{a.current=true,d();let p=setInterval(d,6e4);return ()=>{a.current=false,clearInterval(p);}},[]),{pricing:t,loading:s,error:c,refresh:d}}function Je(e){return {id:e.id||"",key:e.key||e.keyPrefix||"",userId:e.userId||e.user_id||"",status:e.status||"active",tokenBalance:e.tokenBalance??e.currentTokenBalance??e.current_token_balance??0,maxTokens:e.maxTokens??e.initialTokenBalance??e.initial_token_balance??0,createdAt:String(e.createdAt??e.created_at??""),expiresAt:e.expiresAt,label:e.label||e.keyPrefix||e.key_prefix||void 0,paperWork:e.paperWork||e.paper_work||void 0,stackId:e.stackId,stackName:e.stackName}}function Q(){let{config:e}=N(),[t,r]=useState([]),[s,g]=useState(true),[c,f]=useState(null),a=useRef(true),o=e.apiBaseUrl,d=useCallback(async()=>{try{g(!0),f(null);let u=await fetch(`${o}/api/keys`);if(!u.ok){let l=`Failed to fetch keys (${u.status})`;try{let v=await u.json();v.error&&(l=v.error);}catch{}throw new Error(l)}let i=await u.json(),n=Array.isArray(i)?i:i.keys??[];a.current&&r(n.map(Je));}catch(u){a.current&&f(u instanceof Error?u.message:"Failed to fetch keys");}finally{a.current&&g(false);}},[o]);useEffect(()=>(a.current=true,d(),()=>{a.current=false;}),[d]);let p=t.reduce((u,i)=>u+i.tokenBalance,0);return {keys:t,totalBalance:p,loading:s,error:c,refresh:d}}function ne(e){let{config:t}=N(),[r,s]=useState([]),[g,c]=useState(false),[f,a]=useState(null),o=useRef(true),d=t.apiBaseUrl,p=useCallback(async()=>{if(e)try{c(!0),a(null);let u=await fetch(`${d}/api/keys/${e}/ledger`);if(!u.ok){let n=`Failed to fetch ledger (${u.status})`;try{let l=await u.json();l.error&&(n=l.error);}catch{}throw new Error(n)}let i=await u.json();o.current&&s(Array.isArray(i)?i:i.entries??[]);}catch(u){o.current&&a(u instanceof Error?u.message:"Failed to fetch ledger");}finally{o.current&&c(false);}},[d,e]);return useEffect(()=>(o.current=true,p(),()=>{o.current=false;}),[p]),{entries:r,loading:g,error:f,refresh:p}}function ie(){let{config:e,callbacks:t}=N(),[r,s]=useState(false),[g,c]=useState(null),[f,a]=useState(null);return {mint:useCallback(async(d,p,u=1)=>{try{s(!0),a(null),t.onPaymentComplete?.(d,p);let i=await fetch(`${e.apiBaseUrl}/api/keys/mint`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({paymentMethod:d,paymentRef:p,quantity:u})});if(!i.ok){let P="Key generation failed";try{let w=await i.json();w.error&&(P=w.error);}catch{}throw new Error(P)}let n=await i.json(),l=n.keys?.[0]||n,v={success:!0,key:{id:l.keyId||l.id||"",key:l.key||"",userId:l.userId||"",status:"active",tokenBalance:l.tokenBalance??l.currentTokenBalance??0,maxTokens:l.initialTokenBalance??l.tokenBalance??0,createdAt:String(l.createdAt||Date.now())},transactionId:p};return c(v),t.onMintSuccess?.(v),v}catch(i){let n=i instanceof Error?i:new Error("Key generation failed");return a(n.message),t.onMintError?.(n),{success:false,error:n.message}}finally{s(false);}},[e.apiBaseUrl,t]),minting:r,result:g,error:f}}function ce(){let{config:e,callbacks:t}=N(),[r,s]=useState(false),[g,c]=useState(null),[f,a]=useState(null),o=useRef(true),d=useCallback(async(u,i)=>{try{s(!0),a(null);let n=await fetch(`${e.apiBaseUrl}/api/keys/${u}/list`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({askPriceCents:i,stackId:e.stackId,stackName:e.stackName})});if(!n.ok){let v=`Listing failed (${n.status})`;try{let P=await n.json();P.error&&(v=P.error);}catch{}throw new Error(v)}let l=await n.json();return o.current&&(c(l),t.onListingCreated?.(l)),l}catch(n){let l=n instanceof Error?n.message:"Listing failed";return o.current&&a(l),null}finally{o.current&&s(false);}},[e.apiBaseUrl,e.stackId,e.stackName,t]),p=useCallback(async u=>{try{s(!0),a(null);let i=await fetch(`${e.apiBaseUrl}/api/keys/${u}/list`,{method:"DELETE"});if(!i.ok){let n=`Delist failed (${i.status})`;try{let l=await i.json();l.error&&(n=l.error);}catch{}throw new Error(n)}return o.current&&c(null),!0}catch(i){let n=i instanceof Error?i.message:"Delist failed";return o.current&&a(n),false}finally{o.current&&s(false);}},[e.apiBaseUrl]);return {listKey:d,delistKey:p,listing:r,result:g,error:f}}function de(){let{config:e}=N(),[t,r]=useState(false),[s,g]=useState(null),c=useRef(true);return {createSession:useCallback(async(a,o)=>{try{r(!0),g(null);let d=await fetch(`${e.apiBaseUrl}/api/keys/stripe-session`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({priceCents:a,quantity:o,stackId:e.stackId})});if(!d.ok){let u=`Checkout failed (${d.status})`;try{let i=await d.json();i.error&&(u=i.error);}catch{}throw new Error(u)}let{url:p}=await d.json();if(p){try{sessionStorage.setItem("keyutils_stripe_pending","1");}catch{}window.location.href=p;}else throw new Error("No checkout URL returned")}catch(d){if(c.current){let p=d instanceof Error?d.message:"Checkout failed";g(p),r(false);}}},[e.apiBaseUrl,e.stackId]),loading:t,error:s}}var at=/^[1-9A-HJ-NP-Za-km-z]{32,44}$/;function fe({amountSol:e,onSuccess:t,onError:r,disabled:s,className:g,style:c}){let{config:f}=N(),[a,o]=useState(false),[d,p]=useState(null),u=!!(f.merchantWallet&&at.test(f.merchantWallet)),i=useCallback(async()=>{if(u)try{o(!0),p(null);let{Connection:n,PublicKey:l,Transaction:v,SystemProgram:P,LAMPORTS_PER_SOL:w}=await import('@solana/web3.js'),R=typeof window<"u"?window.solana:void 0;if(!R||typeof R!="object"||!R.isPhantom)throw new Error("Phantom wallet not found. Please install Phantom.");let B=R,M;try{M=new l(f.merchantWallet);}catch{throw new Error("Invalid merchant wallet address.")}let _=await B.connect(),k=new l(_.publicKey.toString()),W=new n(f.solanaRpcUrl||"https://api.mainnet-beta.solana.com"),{blockhash:A}=await W.getLatestBlockhash(),Z=BigInt(Math.floor(e*1e9)),X=new v({recentBlockhash:A,feePayer:k}).add(P.transfer({fromPubkey:k,toPubkey:M,lamports:Number(Z)})),{signature:K}=await B.signAndSendTransaction(X);t(K);}catch(n){let l=n instanceof Error?n:new Error("Payment failed");p(l.message),r?.(l);}finally{o(false);}},[e,f.merchantWallet,u,t,r]);return jsxs("div",{children:[jsx("button",{onClick:i,disabled:s||a||!u,"aria-label":`Pay ${e.toFixed(4)} SOL`,className:m("flex w-full items-center justify-center gap-2 rounded-lg bg-purple-600 px-4 py-3 font-medium text-white transition-colors hover:bg-purple-700 disabled:opacity-50 disabled:cursor-not-allowed",g),style:c,children:a?jsxs(Fragment,{children:[jsx(Loader2,{className:"h-4 w-4 animate-spin"}),"Processing..."]}):jsxs(Fragment,{children:["Pay ",e.toFixed(4)," SOL"]})}),d&&jsx("p",{className:"mt-2 text-center text-xs text-red-500",children:d})]})}function pe({result:e,className:t,style:r}){let[s,g]=useState(false),c=useRef(null);useEffect(()=>()=>{c.current&&clearTimeout(c.current);},[]);let f=useCallback(()=>{e.key?.key&&(navigator.clipboard.writeText(e.key.key),g(true),c.current&&clearTimeout(c.current),c.current=setTimeout(()=>g(false),2e3));},[e.key?.key]);return !e.success||!e.key?null:jsxs("div",{className:m("rounded-lg border border-green-500/20 bg-green-500/10 p-6 text-center",t),style:r,children:[jsx(CheckCircle,{className:"mx-auto mb-3 h-12 w-12 text-green-500"}),jsx("h3",{className:"mb-2 text-lg font-semibold text-foreground",children:"Key Generated!"}),jsx("p",{className:"mb-4 text-sm text-muted-foreground",children:"Your node key has been successfully generated."}),jsxs("div",{className:"mx-auto flex max-w-md items-center gap-2 rounded-md border bg-background p-3",children:[jsx("code",{className:"flex-1 truncate text-xs text-foreground",children:e.key.key}),jsx("button",{onClick:f,"aria-label":"Copy key to clipboard",className:"shrink-0 rounded-md p-1.5 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground",children:s?jsx(Check,{className:"h-4 w-4 text-green-500"}):jsx(Copy,{className:"h-4 w-4"})})]})]})}function I({className:e}){return jsx("div",{className:m("animate-pulse rounded-xl bg-muted",e)})}function ge(){return jsxs("div",{className:"space-y-6",children:[jsxs("div",{className:"px-6 py-10 flex flex-col items-center gap-3",children:[jsx(I,{className:"h-14 w-48"}),jsx(I,{className:"h-5 w-64"})]}),jsx(I,{className:"h-[72px] w-full rounded-2xl"}),jsx(I,{className:"h-[68px] w-full rounded-2xl"}),jsx(I,{className:"h-[60px] w-full rounded-2xl"})]})}function J({count:e=3}){return jsx("div",{className:"space-y-3",children:Array.from({length:e}).map((t,r)=>jsxs("div",{className:"rounded-xl border p-4 space-y-3",children:[jsxs("div",{className:"flex items-center justify-between",children:[jsx(I,{className:"h-4 w-32"}),jsx(I,{className:"h-4 w-16"})]}),jsx(I,{className:"h-2 w-full rounded-full"}),jsxs("div",{className:"flex justify-between",children:[jsx(I,{className:"h-3 w-24"}),jsx(I,{className:"h-3 w-20"})]})]},r))})}function kt({quantity:e,max:t,onChange:r}){return jsxs("div",{className:"flex items-center justify-between rounded-2xl border bg-muted/20 px-6 py-5",children:[jsx("span",{className:"text-sm font-medium text-foreground",children:"Quantity"}),jsxs("div",{className:"flex items-center gap-0 rounded-xl border bg-background",children:[jsx("button",{onClick:()=>r(Math.max(1,e-1)),disabled:e<=1,"aria-label":"Decrease quantity",className:"flex h-12 w-12 items-center justify-center rounded-l-xl text-muted-foreground transition-colors hover:bg-muted hover:text-foreground disabled:opacity-30",children:jsx(Minus,{className:"h-5 w-5"})}),jsx("span",{className:"flex h-12 w-14 items-center justify-center border-x text-xl font-bold text-foreground","aria-live":"polite",children:e}),jsx("button",{onClick:()=>r(Math.min(t,e+1)),disabled:e>=t,"aria-label":"Increase quantity",className:"flex h-12 w-12 items-center justify-center rounded-r-xl text-muted-foreground transition-colors hover:bg-muted hover:text-foreground disabled:opacity-30",children:jsx(Plus,{className:"h-5 w-5"})})]})]})}function Me({pricing:e,quantity:t}){let[r,s]=useState(false),g=e.priceUsd*t;return jsxs("div",{className:"rounded-2xl border bg-background",children:[jsxs("button",{onClick:()=>s(!r),"aria-expanded":r,className:"flex w-full items-center justify-between px-6 py-5 text-left",children:[jsxs("span",{className:"text-foreground",children:[jsxs("span",{className:"text-xl font-bold",children:["$",g.toFixed(2)]})," ",jsx("span",{className:"text-sm text-muted-foreground",children:"total fees included"})]}),r?jsx(ChevronUp,{className:"h-5 w-5 text-muted-foreground"}):jsx(ChevronDown,{className:"h-5 w-5 text-muted-foreground"})]}),r&&jsxs("div",{className:"border-t px-6 pb-8 pt-4 space-y-3 text-sm",children:[jsxs("div",{className:"flex justify-between",children:[jsxs("span",{className:"text-muted-foreground",children:["Node Key \xD7 ",t]}),jsxs("span",{className:"text-foreground",children:["$",(e.priceUsd*t).toFixed(2)]})]}),jsxs("div",{className:"flex justify-between",children:[jsx("span",{className:"text-muted-foreground",children:"Keys sold"}),jsx("span",{className:"text-foreground",children:e.keysSold})]}),e.daysUntilHalving>0&&jsxs("div",{className:"flex justify-between text-muted-foreground",children:[jsx("span",{children:"Next refresh"}),jsxs("span",{children:[e.daysUntilHalving," day",e.daysUntilHalving!==1?"s":""]})]}),jsxs("div",{className:"flex justify-between border-t pt-3 font-semibold",children:[jsx("span",{className:"text-foreground",children:"Total"}),jsxs("span",{className:"text-foreground",children:["$",g.toFixed(2)]})]})]})]})}function xe({className:e,style:t}){let{config:r}=N(),{pricing:s,loading:g,error:c,refresh:f}=re(),{mint:a,minting:o,result:d,error:p}=ie(),{createSession:u,loading:i,error:n}=de(),[l,v]=useState(1),[P,w]=useState(r.paymentMethods?.includes("stripe")?"stripe":"solana"),[R,B]=useState(0),[M,_]=useState(false),k=pt.useRef(l);k.current=l;let W=Math.max(1,r.maxQuantity||5),A=[...r.paymentMethods||["solana"]].sort((K,G)=>K==="stripe"?-1:G==="stripe"?1:0);if(useEffect(()=>{if(typeof window>"u")return;let G=new URLSearchParams(window.location.search).get("session_id");if(G&&/^cs_(test|live)_/.test(G)&&!M&&!d?.success){let ke=false;try{ke=sessionStorage.getItem("keyutils_stripe_pending")==="1";}catch{}if(!ke)return;try{sessionStorage.removeItem("keyutils_stripe_pending");}catch{}_(true);let we=new URL(window.location.href);we.searchParams.delete("session_id"),window.history.replaceState({},"",we.toString()),a("stripe",G,k.current);}},[M,d,a]),d?.success)return jsx("div",{className:m("flex flex-col items-center justify-center",e),style:t,children:jsx(pe,{result:d})});if(M||o)return jsx("div",{className:m("flex flex-col",e),style:t,children:jsx(ge,{})});if(g)return jsx("div",{className:m("flex flex-col",e),style:t,children:jsx(ge,{})});if(c||!s)return jsxs("div",{className:m("flex flex-col items-center justify-center gap-4 py-24",e),style:t,children:[jsxs("div",{className:"rounded-xl border border-red-500/20 bg-red-500/5 px-6 py-5 text-center max-w-sm",children:[jsx("p",{className:"text-sm font-medium text-foreground",children:"Unable to load pricing"}),jsx("p",{className:"mt-1 text-xs text-muted-foreground",children:c||"Please check your connection and try again."})]}),jsx("button",{onClick:()=>f(),className:"text-sm text-muted-foreground hover:text-foreground transition-colors",children:"Try again"})]});let Z=async K=>{B(2),await a(P,K,l);},X=()=>{u(s.priceCents,l);};return jsx("div",{className:m("flex flex-col",e),style:t,children:jsxs("div",{className:"space-y-6",children:[R===0&&jsxs(Fragment,{children:[r.keyImage&&jsx("img",{src:r.keyImage,alt:"Node Key",className:r.keyImageClass||"w-1/2 mx-auto"}),jsxs("div",{className:"px-6 py-10 text-center",children:[jsxs("p",{className:"text-5xl font-bold tracking-tight text-foreground md:text-6xl",children:["$",s.priceUsd.toFixed(2)]}),jsxs("p",{className:"mt-2 text-base text-muted-foreground",children:["per key \xB7 ",s.tokenAllocationFormatted||L(s.tokenAllocation)," tokens included"]})]}),jsx(kt,{quantity:l,max:W,onChange:v}),jsx(Me,{pricing:s,quantity:l}),jsx("button",{onClick:()=>B(1),className:"w-full rounded-2xl bg-foreground py-5 text-center text-base font-semibold text-background transition-colors hover:bg-foreground/90",children:"Continue to Payment"})]}),R===1&&jsxs(Fragment,{children:[A.length>1&&jsx("div",{role:"tablist",className:"flex gap-1 rounded-2xl bg-muted/50 p-1.5",children:A.map(K=>jsxs("button",{role:"tab","aria-selected":P===K,onClick:()=>w(K),className:m("flex flex-1 items-center justify-center gap-2.5 rounded-xl py-4 text-sm font-medium transition-colors",P===K?"bg-background text-foreground shadow-sm":"text-muted-foreground hover:text-foreground"),children:[K==="stripe"?jsx(CreditCard,{className:"h-5 w-5"}):jsx(Wallet,{className:"h-5 w-5"}),K==="stripe"?"Credit Card":"Crypto"]},K))}),jsx(Me,{pricing:s,quantity:l}),P==="stripe"?jsxs(Fragment,{children:[jsx("button",{onClick:X,disabled:i,className:"flex w-full items-center justify-center gap-2 rounded-2xl bg-foreground py-5 text-base font-semibold text-background transition-colors hover:bg-foreground/90 disabled:opacity-50",children:i?jsxs(Fragment,{children:[jsx(Loader2,{className:"h-5 w-5 animate-spin"}),"Redirecting to Stripe..."]}):jsxs(Fragment,{children:[jsx(CreditCard,{className:"h-5 w-5"}),"Pay with Card"]})}),n&&jsx("p",{className:"text-center text-sm text-red-500",children:n})]}):P==="solana"?jsx(fe,{amountSol:s.priceSol??0,onSuccess:Z,className:"rounded-2xl py-5 text-base"}):null,p&&jsx("p",{className:"text-center text-sm text-red-500",children:p}),jsx("button",{onClick:()=>B(0),className:"w-full py-3 text-center text-sm text-muted-foreground hover:text-foreground transition-colors",children:"\u2190 Back to Configure"})]})]})})}var St={ios:"iOS",android:"Android",windows:"Windows",mac:"macOS",linux:"Linux"},Kt=["mac","windows","linux","ios","android"];function ye({downloads:e,className:t,style:r}){let{config:s}=N(),g=useMemo(()=>Se(),[]),c=e||Kt.map(o=>({platform:o,label:St[o],url:"#",compatible:o===g})),f=c.find(o=>o.compatible)||c[0],a=c.filter(o=>o.platform!==f.platform);return jsxs("div",{className:m("space-y-8",t),style:r,children:[jsxs("div",{className:"flex flex-col items-center gap-6 pt-4 mt-4",children:[s.aispImage&&jsx("img",{src:s.aispImage,alt:"aiSP",className:"w-full sm:w-1/2 mx-auto px-4 sm:px-0"}),jsxs("div",{className:"text-center",children:[jsx("p",{className:"text-lg sm:text-2xl font-medium text-foreground",children:"Download the AI Service Provider App"}),jsx("p",{className:"mt-1 text-sm sm:text-base text-muted-foreground",children:"Load your keys and run your node."})]}),jsxs("a",{href:f.url,className:"inline-flex items-center gap-3 rounded-full px-10 py-4 text-lg font-semibold text-white transition-colors hover:opacity-90",style:{backgroundColor:"#0900f4"},children:[jsx(Download,{className:"h-5 w-5"}),"Get aiSP for ",f.label]})]}),jsxs("p",{className:"text-center text-sm text-muted-foreground",children:["aiSP is also available on"," ",a.map((o,d)=>jsxs(pt.Fragment,{children:[d>0&&(d===a.length-1?" and ":", "),jsx("a",{href:o.url,className:"font-medium underline transition-colors hover:text-foreground",style:{color:"#066bff"},children:o.label})]},o.platform)),"."]}),jsxs("div",{className:"flex flex-col items-center gap-2 mt-12",children:[jsxs("p",{className:"text-center text-xs text-muted-foreground flex justify-center gap-0.5 flex-wrap",children:["Source code is available on",jsx("a",{href:"https://github.com/stack-net",target:"_blank",rel:"noopener noreferrer",className:"text-foreground underline hover:text-foreground/80 color-[#0900f4]",children:"Github"})]}),jsxs("p",{className:"text-center text-xs text-muted-foreground flex justify-center gap-0.5 flex-wrap",children:["By downloading, you agree to the",jsx("a",{href:s.termsUrl||"/terms",className:"text-foreground underline hover:text-foreground/80",children:"Open Source Applications Terms"})]})]})]})}function Y({current:e,max:t,className:r,style:s,showLabel:g=true}){let c=Pe(e,t),f=Ce(c);return jsxs("div",{className:m("w-full",r),style:s,children:[g&&jsxs("div",{className:"mb-1 flex items-center justify-between text-xs text-muted-foreground",children:[jsxs("span",{children:[L(e)," / ",L(t)]}),jsxs("span",{children:[c,"%"]})]}),jsx("div",{className:"h-2 w-full overflow-hidden rounded-full bg-muted",children:jsx("div",{className:m("h-full rounded-full transition-all duration-500",f),style:{width:`${c}%`}})})]})}function Lt({nodeKey:e,selected:t,onSelect:r}){return jsxs("button",{onClick:r,className:m("flex w-full items-start gap-3 rounded-xl border p-4 text-left transition-all",t?"border-foreground/30 bg-foreground/5 ring-1 ring-foreground/10":"border-border hover:border-foreground/20"),children:[jsx("div",{className:m("mt-0.5 flex h-5 w-5 shrink-0 items-center justify-center rounded-full border-2 transition-colors",t?"border-foreground bg-foreground":"border-muted-foreground/40"),children:t&&jsx(Check,{className:"h-3 w-3 text-background"})}),jsxs("div",{className:"min-w-0 flex-1",children:[jsxs("div",{className:"flex items-center justify-between",children:[jsx("span",{className:"text-sm font-medium text-foreground",children:e.label||e.id.slice(0,16)}),jsx("span",{className:m("text-xs font-medium capitalize",e.status==="active"?"text-green-500":"text-muted-foreground"),children:e.status})]}),jsx(Y,{current:e.tokenBalance,max:e.maxTokens,className:"mt-2"})]})]})}function he({className:e,style:t}){let{config:r}=N(),{keys:s,loading:g,error:c,refresh:f}=Q(),{listKey:a,listing:o,result:d,error:p}=ce(),[u,i]=useState(null),[n,l]=useState(""),[v,P]=useState(false),w=s.find(k=>k.id===u),R=parseInt(n.replace(/[^0-9]/g,""),10)||0,B=w&&R>0&&v&&!o,M=async()=>{if(!B||!w)return;let k=R*100;await a(w.id,k)&&f();};if(d)return jsxs("div",{className:m("flex flex-col items-center gap-4 rounded-xl border border-green-500/20 bg-green-500/10 py-12",e),style:t,children:[jsx(CheckCircle,{className:"h-12 w-12 text-green-500"}),jsx("h3",{className:"text-lg font-semibold text-foreground",children:"Key Listed!"}),jsx("p",{className:"text-sm text-muted-foreground",children:"Your key is now on the Global Open AI Network marketplace."}),jsxs("p",{className:"text-xs text-muted-foreground",children:["Listing ID: ",jsx("code",{children:d.listingId})]})]});if(g)return jsxs("div",{className:m("space-y-5 px-1",e),style:t,children:[jsx("h3",{className:"text-2xl mt-3 mb-2 font-semibold text-foreground",children:"Your Keys"}),jsx(J,{count:2})]});if(c)return jsxs("div",{className:m("flex flex-col items-center justify-center gap-4 py-16",e),style:t,children:[jsxs("div",{className:"rounded-xl border border-red-500/20 bg-red-500/5 px-6 py-5 text-center max-w-sm",children:[jsx("p",{className:"text-sm font-medium text-foreground",children:"Unable to load your keys"}),jsx("p",{className:"mt-1 text-xs text-muted-foreground",children:c})]}),jsx("button",{onClick:()=>f(),className:"text-sm text-muted-foreground hover:text-foreground transition-colors",children:"Try again"})]});let _=s.filter(k=>k.status==="active");return _.length===0?jsxs("div",{className:m("flex flex-col items-center justify-center gap-3 rounded-xl border border-dashed py-16",e),style:t,children:[jsx(AlertTriangle,{className:"h-8 w-8 text-muted-foreground"}),jsx("p",{className:"text-sm text-muted-foreground",children:"No active keys to sell."})]}):jsxs("div",{className:m("space-y-5",e),style:t,children:[jsxs("div",{children:[jsx("h3",{className:"text-2xl mt-3 mb-2 font-semibold text-foreground",children:"Your Keys"}),jsx("div",{className:"space-y-2",children:_.map(k=>jsx(Lt,{nodeKey:k,selected:u===k.id,onSelect:()=>i(k.id)},k.id))})]}),w&&jsxs("div",{className:"rounded-xl border bg-muted/30 p-5 space-y-2 text-sm",children:[jsxs("div",{className:"flex justify-between",children:[jsx("span",{className:"text-muted-foreground",children:"Key ID"}),jsx("code",{className:"text-xs text-foreground",children:w.id})]}),jsxs("div",{className:"flex justify-between",children:[jsx("span",{className:"text-muted-foreground",children:"Originating Stack"}),jsx("span",{className:"text-foreground",children:r.stackName||"Unknown"})]}),jsxs("div",{className:"flex justify-between",children:[jsx("span",{className:"text-muted-foreground",children:"Tokens (remaining / total)"}),jsxs("span",{className:"text-foreground",children:[L(w.tokenBalance)," / ",L(w.maxTokens)]})]}),w.paperWork&&jsxs("div",{className:"flex justify-between",children:[jsx("span",{className:"text-muted-foreground",children:"Paper earned"}),jsx("span",{className:"text-foreground",children:w.paperWork})]}),jsxs("div",{className:"flex justify-between",children:[jsx("span",{className:"text-muted-foreground",children:"Created"}),jsx("span",{className:"text-foreground",children:F(w.createdAt)})]})]}),jsxs("div",{children:[jsx("label",{htmlFor:"listing-price",className:"mb-2 block text-sm font-semibold text-foreground",children:"Listing Price (USD)"}),jsxs("div",{className:"relative",children:[jsx("span",{className:"absolute left-4 top-1/2 -translate-y-1/2 text-2xl font-bold text-muted-foreground",children:"$"}),jsx("input",{id:"listing-price",type:"text",inputMode:"numeric",value:n,onChange:k=>{let W=k.target.value.replace(/[^0-9]/g,""),A=parseInt(W,10);l(isNaN(A)?"":A.toLocaleString("en-US"));},placeholder:"0",className:"w-full rounded-xl border bg-background py-5 pl-10 pr-4 text-3xl font-bold text-foreground placeholder:text-muted-foreground/30 focus:outline-none focus:ring-2 focus:ring-foreground/20"})]})]}),jsxs("label",{className:"flex items-start gap-3 cursor-pointer",children:[jsx("input",{type:"checkbox",checked:v,onChange:k=>P(k.target.checked),className:"sr-only"}),jsx("div",{"aria-hidden":"true",className:m("mt-0.5 flex h-5 w-5 shrink-0 items-center justify-center rounded border-2 transition-colors",v?"border-foreground bg-foreground":"border-muted-foreground/40"),children:v&&jsx(Check,{className:"h-3 w-3 text-background"})}),jsxs("span",{className:"text-xs text-muted-foreground leading-relaxed",children:["I agree to list this key on the Global Open AI Network marketplace. The key will be deactivated upon sale and transferred to the buyer. Listing fees may apply."," ",jsx("a",{href:r.termsUrl||"/terms",className:"text-foreground underline",children:"Terms"})]})]}),p&&jsx("p",{className:"text-sm text-red-500",children:p}),jsxs("button",{onClick:M,disabled:!B,className:m("flex w-full items-center justify-center gap-2 rounded-xl py-4 text-sm font-semibold transition-colors",B?"bg-foreground text-background hover:bg-foreground/90":"bg-muted text-muted-foreground cursor-not-allowed"),children:[jsx(ExternalLink,{className:"h-4 w-4"}),o?"Listing...":"List on Marketplace"]})]})}var Et=[{id:"buy",label:"Buy"},{id:"use",label:"Use"},{id:"sell",label:"Sell"}];function It({active:e,onChange:t}){return jsx("div",{role:"tablist","aria-label":"Key actions",className:"flex gap-1 rounded-2xl bg-muted/50 p-1.5",children:Et.map(({id:r,label:s})=>jsx("button",{role:"tab","aria-selected":e===r,"aria-controls":`panel-${r}`,id:`tab-${r}`,onClick:()=>t(r),className:m("flex flex-1 items-center justify-center gap-2.5 rounded-xl py-3.5 text-sm font-medium transition-all",e===r?"bg-background text-foreground shadow-sm":"text-muted-foreground hover:text-foreground"),children:jsx("span",{className:"inline",children:s})},r))})}function Mt({defaultTab:e="buy",className:t,style:r}){let[s,g]=useState(e);return jsxs("div",{className:m("flex flex-col",t),style:r,children:[jsx("div",{className:"sticky top-0 z-10 bg-background/80 px-5 pb-3 pt-2 sm:pt-5 backdrop-blur-sm sm:px-10 md:px-16 lg:px-20",children:jsx("div",{className:"mx-auto max-w-xl",children:jsx(It,{active:s,onChange:g})})}),jsx("div",{className:"flex flex-1 flex-col px-5 pb-8 sm:px-10 md:px-16 md:pb-12 lg:px-20",children:jsx("div",{className:"mx-auto w-full max-w-xl flex-1",children:jsxs("div",{role:"tabpanel",id:`panel-${s}`,"aria-labelledby":`tab-${s}`,children:[s==="buy"&&jsx(xe,{className:"flex-1"}),s==="use"&&jsx(ye,{className:"flex-1"}),s==="sell"&&jsx(he,{className:"flex-1"})]})})})]})}function jt({apiBaseUrl:e,stackId:t,stackName:r,paymentMethods:s,merchantWallet:g,stripePublicKey:c,theme:f,maxQuantity:a,keyImage:o,keyImageClass:d,aispImage:p,termsUrl:u,onMintSuccess:i,onMintError:n,onPaymentStart:l,onPaymentComplete:v,onListingCreated:P,defaultTab:w,className:R,style:B}){return jsx(ee,{config:{apiBaseUrl:e,stackId:t,stackName:r,paymentMethods:s,merchantWallet:g,stripePublicKey:c,theme:f,maxQuantity:a,keyImage:o,keyImageClass:d,aispImage:p,termsUrl:u},callbacks:{onMintSuccess:i,onMintError:n,onPaymentStart:l,onPaymentComplete:v,onListingCreated:P},children:jsx(Mt,{defaultTab:w,className:R,style:B})})}var Ne=20;function ve({nodeKey:e,className:t,style:r}){let[s,g]=useState(false),[c,f]=useState(false),a=useRef(null),{entries:o,loading:d}=ne(s?e.id:null);useEffect(()=>()=>{a.current&&clearTimeout(a.current);},[]);let p=useCallback(()=>{e.key&&(navigator.clipboard.writeText(e.key),f(true),a.current&&clearTimeout(a.current),a.current=setTimeout(()=>f(false),2e3));},[e.key]),u=e.status==="active"?"text-green-500":e.status==="expired"?"text-red-500":"text-yellow-500",i=o.slice(0,Ne);return jsxs("div",{className:m("rounded-lg border bg-background",t),style:r,children:[jsxs("div",{className:"p-4",children:[jsxs("div",{className:"mb-3 flex items-center justify-between",children:[jsxs("div",{className:"flex items-center gap-2",children:[jsx("button",{onClick:()=>g(!s),"aria-expanded":s,"aria-label":s?"Collapse ledger":"Expand ledger",className:"text-muted-foreground hover:text-foreground",children:s?jsx(ChevronDown,{className:"h-4 w-4"}):jsx(ChevronRight,{className:"h-4 w-4"})}),jsx("span",{className:"text-sm font-medium text-foreground",children:e.label||`Key ${e.id.slice(0,8)}`})]}),jsx("span",{className:m("text-xs font-medium capitalize",u),children:e.status})]}),e.key&&jsxs("div",{className:"mb-3 flex items-center gap-2",children:[jsx("code",{className:"flex-1 truncate rounded bg-muted px-2 py-1 text-xs text-muted-foreground",children:e.key}),jsx("button",{onClick:p,"aria-label":"Copy key to clipboard",className:"shrink-0 rounded p-1 text-muted-foreground hover:bg-muted hover:text-foreground",children:c?jsx(Check,{className:"h-3.5 w-3.5 text-green-500"}):jsx(Copy,{className:"h-3.5 w-3.5"})})]}),jsx(Y,{current:e.tokenBalance,max:e.maxTokens}),jsxs("div",{className:"mt-2 flex items-center justify-between text-xs text-muted-foreground",children:[jsxs("span",{children:["Created ",F(e.createdAt)]}),e.expiresAt&&jsxs("span",{children:["Expires ",F(e.expiresAt)]})]})]}),s&&jsxs("div",{className:"border-t px-4 py-3",children:[jsx("h4",{className:"mb-2 text-xs font-medium text-muted-foreground",children:"Ledger"}),d?jsx("p",{className:"text-xs text-muted-foreground",children:"Loading..."}):i.length===0?jsx("p",{className:"text-xs text-muted-foreground",children:"No ledger entries."}):jsxs("div",{className:"space-y-1.5",children:[i.map(n=>jsxs("div",{className:"flex items-center justify-between text-xs",children:[jsxs("div",{className:"flex items-center gap-2",children:[jsxs("span",{className:m("font-medium",n.type==="credit"||n.type==="reward"?"text-green-500":"text-red-500"),children:[n.type==="credit"||n.type==="reward"?"+":"-",L(Math.abs(n.amount))]}),jsx("span",{className:"text-muted-foreground",children:n.description})]}),jsx("span",{className:"text-muted-foreground",children:F(n.createdAt)})]},n.id)),o.length>Ne&&jsxs("p",{className:"pt-1 text-xs text-muted-foreground",children:["Showing ",Ne," of ",o.length," entries"]})]})]})]})}function Qt({getKeyHref:e="/keys/get",className:t,style:r}){let{keys:s,totalBalance:g,loading:c,error:f,refresh:a}=Q();return c?jsx("div",{className:m("space-y-4",t),style:r,children:jsx(J,{count:3})}):f?jsxs("div",{className:m("flex flex-col items-center justify-center gap-4 py-12",t),style:r,children:[jsxs("div",{className:"rounded-xl border border-red-500/20 bg-red-500/5 px-6 py-5 text-center max-w-sm",children:[jsx("p",{className:"text-sm font-medium text-foreground",children:"Unable to load your keys"}),jsx("p",{className:"mt-1 text-xs text-muted-foreground",children:f})]}),jsx("button",{onClick:()=>a(),className:"text-sm text-muted-foreground hover:text-foreground transition-colors",children:"Try again"})]}):s.length===0?jsxs("div",{className:m("flex flex-col items-center justify-center gap-4 rounded-lg border border-dashed py-12",t),style:r,children:[jsx(KeyRound,{className:"h-10 w-10 text-muted-foreground"}),jsxs("div",{className:"text-center",children:[jsx("p",{className:"text-sm font-medium text-foreground",children:"No keys yet"}),jsx("p",{className:"text-xs text-muted-foreground",children:"Get your first node key to start earning."})]}),e&&jsx("a",{href:e,className:"rounded-lg bg-foreground px-4 py-2 text-sm font-medium text-background transition-colors hover:bg-foreground/90",children:"Get a Key"})]}):jsxs("div",{className:m("space-y-4",t),style:r,children:[jsxs("div",{className:"flex items-center justify-between",children:[jsxs("p",{className:"text-sm text-muted-foreground",children:[s.length," key",s.length!==1?"s":""," \xB7 Total balance: ",L(g)]}),e&&jsx("a",{href:e,className:"rounded-md bg-foreground px-3 py-1.5 text-xs font-medium text-background transition-colors hover:bg-foreground/90",children:"Get a Key"})]}),s.map(o=>jsx(ve,{nodeKey:o},o.id))]})}
|
|
2
|
-
export{
|
|
1
|
+
import {clsx}from'clsx';import {twMerge}from'tailwind-merge';import gt,{createContext,useContext,useMemo,useState,useRef,useEffect,useCallback}from'react';import {jsx,jsxs,Fragment}from'react/jsx-runtime';import {Loader2,CheckCircle,Check,Copy,CreditCard,Wallet,Download,AlertTriangle,ExternalLink,ChevronDown,ChevronRight,KeyRound,Minus,Plus,ChevronUp}from'lucide-react';function f(...e){return twMerge(clsx(e))}function T(e){return e==null?"0":e>=1e9?`${(e/1e9).toFixed(1)}B`:e>=1e6?`${(e/1e6).toFixed(1)}M`:e>=1e3?`${(e/1e3).toFixed(1)}K`:e.toLocaleString()}function Ce(e,t){return t<=0?0:Math.min(100,Math.round(e/t*100))}function Se(e){return e>=70?"bg-green-500":e>=30?"bg-yellow-500":"bg-red-500"}function F(e){let t=typeof e=="string"&&/^\d+$/.test(e)?Number(e):e,r=new Date(t);return isNaN(r.getTime())?String(e):r.toLocaleDateString("en-US",{month:"short",day:"numeric",year:"numeric"})}function Ke(){if(typeof navigator>"u")return "mac";let e=navigator.userAgent.toLowerCase();return /iphone|ipad|ipod/.test(e)?"ios":/android/.test(e)?"android":/win/.test(e)?"windows":/linux/.test(e)?"linux":"mac"}var Ue=createContext(null);function v(){let e=useContext(Ue);if(!e)throw new Error("useKeyUtilsContext must be used within a KeyUtilsProvider");return e}function te({config:e,callbacks:t={},children:r}){typeof e.apiBaseUrl!="string"&&console.error("[KeyUtils] apiBaseUrl must be a string");let s=useMemo(()=>({config:e,callbacks:t}),[e,t]);return jsx(Ue.Provider,{value:s,children:r})}function se(){let{config:e}=v(),[t,r]=useState(null),[s,d]=useState(true),[u,g]=useState(null),a=useRef(true),o=useRef(e.apiBaseUrl);o.current=e.apiBaseUrl;let i=async()=>{try{a.current&&(d(!0),g(null));let p=await fetch(`${o.current}/api/keys/pricing`);if(!p.ok){let l=`Pricing unavailable (${p.status})`;try{let n=await p.json();n.error&&(l=n.error);}catch{}throw new Error(l)}let m=await p.json();a.current&&r(m);}catch(p){a.current&&g(p instanceof Error?p.message:"Failed to fetch pricing");}finally{a.current&&d(false);}};return useEffect(()=>{a.current=true,i();let p=setInterval(i,6e4);return ()=>{a.current=false,clearInterval(p);}},[]),{pricing:t,loading:s,error:u,refresh:i}}function Ze(e){return {id:e.id||"",key:e.key||e.keyPrefix||"",userId:e.userId||e.user_id||"",status:e.status||"active",tokenBalance:e.tokenBalance??e.currentTokenBalance??e.current_token_balance??0,maxTokens:e.maxTokens??e.initialTokenBalance??e.initial_token_balance??0,createdAt:String(e.createdAt??e.created_at??""),expiresAt:e.expiresAt,label:e.label||e.keyPrefix||e.key_prefix||void 0,paperWork:e.paperWork||e.paper_work||void 0,stackId:e.stackId,stackName:e.stackName}}function Q(){let{config:e}=v(),[t,r]=useState([]),[s,d]=useState(true),[u,g]=useState(null),a=useRef(true),o=e.apiBaseUrl,i=useCallback(async()=>{try{d(!0),g(null);let m=await fetch(`${o}/api/keys`);if(!m.ok){let c=`Failed to fetch keys (${m.status})`;try{let N=await m.json();N.error&&(c=N.error);}catch{}throw new Error(c)}let l=await m.json(),n=Array.isArray(l)?l:l.keys??[];a.current&&r(n.map(Ze));}catch(m){a.current&&g(m instanceof Error?m.message:"Failed to fetch keys");}finally{a.current&&d(false);}},[o]);useEffect(()=>(a.current=true,i(),()=>{a.current=false;}),[i]);let p=t.reduce((m,l)=>m+l.tokenBalance,0);return {keys:t,totalBalance:p,loading:s,error:u,refresh:i}}function ae(e){let{config:t}=v(),[r,s]=useState([]),[d,u]=useState(false),[g,a]=useState(null),o=useRef(true),i=t.apiBaseUrl,p=useCallback(async()=>{if(e)try{u(!0),a(null);let m=await fetch(`${i}/api/keys/${e}/ledger`);if(!m.ok){let n=`Failed to fetch ledger (${m.status})`;try{let c=await m.json();c.error&&(n=c.error);}catch{}throw new Error(n)}let l=await m.json();o.current&&s(Array.isArray(l)?l:l.entries??[]);}catch(m){o.current&&a(m instanceof Error?m.message:"Failed to fetch ledger");}finally{o.current&&u(false);}},[i,e]);return useEffect(()=>(o.current=true,p(),()=>{o.current=false;}),[p]),{entries:r,loading:d,error:g,refresh:p}}function le(){let{config:e,callbacks:t}=v(),[r,s]=useState(false),[d,u]=useState(null),[g,a]=useState(null);return {mint:useCallback(async(i,p,m=1)=>{try{s(!0),a(null),t.onPaymentComplete?.(i,p);let l=await fetch(`${e.apiBaseUrl}/api/keys/mint`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({paymentMethod:i,paymentRef:p,quantity:m})});if(!l.ok){let P="Key generation failed";try{let w=await l.json();w.error&&(P=w.error);}catch{}throw new Error(P)}let n=await l.json(),c=n.keys?.[0]||n,N={success:!0,key:{id:c.keyId||c.id||"",key:c.key||"",userId:c.userId||"",status:"active",tokenBalance:c.tokenBalance??c.currentTokenBalance??0,maxTokens:c.initialTokenBalance??c.tokenBalance??0,createdAt:String(c.createdAt||Date.now())},transactionId:p};return u(N),t.onMintSuccess?.(N),N}catch(l){let n=l instanceof Error?l:new Error("Key generation failed");return a(n.message),t.onMintError?.(n),{success:false,error:n.message}}finally{s(false);}},[e.apiBaseUrl,t]),minting:r,result:d,error:g}}function de(){let{config:e,callbacks:t}=v(),[r,s]=useState(false),[d,u]=useState(null),[g,a]=useState(null),o=useRef(true),i=useCallback(async(m,l)=>{try{s(!0),a(null);let n=await fetch(`${e.apiBaseUrl}/api/keys/${m}/list`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({askPriceCents:l,stackId:e.stackId,stackName:e.stackName})});if(!n.ok){let N=`Listing failed (${n.status})`;try{let P=await n.json();P.error&&(N=P.error);}catch{}throw new Error(N)}let c=await n.json();return o.current&&(u(c),t.onListingCreated?.(c)),c}catch(n){let c=n instanceof Error?n.message:"Listing failed";return o.current&&a(c),null}finally{o.current&&s(false);}},[e.apiBaseUrl,e.stackId,e.stackName,t]),p=useCallback(async m=>{try{s(!0),a(null);let l=await fetch(`${e.apiBaseUrl}/api/keys/${m}/list`,{method:"DELETE"});if(!l.ok){let n=`Delist failed (${l.status})`;try{let c=await l.json();c.error&&(n=c.error);}catch{}throw new Error(n)}return o.current&&u(null),!0}catch(l){let n=l instanceof Error?l.message:"Delist failed";return o.current&&a(n),false}finally{o.current&&s(false);}},[e.apiBaseUrl]);return {listKey:i,delistKey:p,listing:r,result:d,error:g}}function ue(){let{config:e}=v(),[t,r]=useState(false),[s,d]=useState(null),u=useRef(true);return {createSession:useCallback(async(a,o)=>{try{r(!0),d(null);let i=await fetch(`${e.apiBaseUrl}/api/keys/stripe-session`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({priceCents:a,quantity:o,stackId:e.stackId,referrerUrl:window.location.href})});if(!i.ok){let m=`Checkout failed (${i.status})`;try{let l=await i.json();l.error&&(m=l.error);}catch{}throw new Error(m)}let{url:p}=await i.json();if(p){try{sessionStorage.setItem("keyutils_stripe_pending","1");}catch{}window.location.href=p;}else throw new Error("No checkout URL returned")}catch(i){if(u.current){let p=i instanceof Error?i.message:"Checkout failed";d(p),r(false);}}},[e.apiBaseUrl,e.stackId]),loading:t,error:s}}var it=/^[1-9A-HJ-NP-Za-km-z]{32,44}$/;function pe({amountSol:e,onSuccess:t,onError:r,disabled:s,className:d,style:u}){let{config:g}=v(),[a,o]=useState(false),[i,p]=useState(null),m=!!(g.merchantWallet&&it.test(g.merchantWallet)),l=useCallback(async()=>{if(m)try{o(!0),p(null);let{Connection:n,PublicKey:c,Transaction:N,SystemProgram:P,LAMPORTS_PER_SOL:w}=await import('@solana/web3.js'),B=typeof window<"u"?window.solana:void 0;if(!B||typeof B!="object"||!B.isPhantom)throw new Error("Phantom wallet not found. Please install Phantom.");let L=B,j;try{j=new c(g.merchantWallet);}catch{throw new Error("Invalid merchant wallet address.")}let A=await L.connect(),k=new c(A.publicKey.toString()),W=new n(g.solanaRpcUrl||"https://api.mainnet-beta.solana.com"),{blockhash:$}=await W.getLatestBlockhash(),H=BigInt(Math.floor(e*1e9)),ee=new N({recentBlockhash:$,feePayer:k}).add(P.transfer({fromPubkey:k,toPubkey:j,lamports:Number(H)})),{signature:U}=await L.signAndSendTransaction(ee);t(U);}catch(n){let c=n instanceof Error?n:new Error("Payment failed");p(c.message),r?.(c);}finally{o(false);}},[e,g.merchantWallet,m,t,r]);return jsxs("div",{children:[jsx("button",{onClick:l,disabled:s||a||!m,"aria-label":`Pay ${e.toFixed(4)} SOL`,className:f("flex w-full items-center justify-center gap-2 rounded-lg bg-purple-600 px-4 py-3 font-medium text-white transition-colors hover:bg-purple-700 disabled:opacity-50 disabled:cursor-not-allowed",d),style:u,children:a?jsxs(Fragment,{children:[jsx(Loader2,{className:"h-4 w-4 animate-spin"}),"Processing..."]}):jsxs(Fragment,{children:["Pay ",e.toFixed(4)," SOL"]})}),i&&jsx("p",{className:"mt-2 text-center text-xs text-red-500",children:i})]})}function ge({result:e,className:t,style:r}){let[s,d]=useState(false),u=useRef(null);useEffect(()=>()=>{u.current&&clearTimeout(u.current);},[]);let g=useCallback(()=>{e.key?.key&&(navigator.clipboard.writeText(e.key.key),d(true),u.current&&clearTimeout(u.current),u.current=setTimeout(()=>d(false),2e3));},[e.key?.key]);return !e.success||!e.key?null:jsxs("div",{className:f("rounded-lg border border-green-500/20 bg-green-500/10 p-6 text-center",t),style:r,children:[jsx(CheckCircle,{className:"mx-auto mb-3 h-12 w-12 text-green-500"}),jsx("h3",{className:"mb-2 text-lg font-semibold text-foreground",children:"Key Generated!"}),jsx("p",{className:"mb-4 text-sm text-muted-foreground",children:"Your node key has been successfully generated."}),jsxs("div",{className:"mx-auto flex max-w-md items-center gap-2 rounded-md border bg-background p-3",children:[jsx("code",{className:"flex-1 truncate text-xs text-foreground",children:e.key.key}),jsx("button",{onClick:g,"aria-label":"Copy key to clipboard",className:"shrink-0 rounded-md p-1.5 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground",children:s?jsx(Check,{className:"h-4 w-4 text-green-500"}):jsx(Copy,{className:"h-4 w-4"})})]})]})}function I({className:e}){return jsx("div",{className:f("animate-pulse rounded-xl bg-muted",e)})}function xe(){return jsxs("div",{className:"space-y-6",children:[jsxs("div",{className:"px-6 py-10 flex flex-col items-center gap-3",children:[jsx(I,{className:"h-14 w-48"}),jsx(I,{className:"h-5 w-64"})]}),jsx(I,{className:"h-[72px] w-full rounded-2xl"}),jsx(I,{className:"h-[68px] w-full rounded-2xl"}),jsx(I,{className:"h-[60px] w-full rounded-2xl"})]})}function J({count:e=3}){return jsx("div",{className:"space-y-3",children:Array.from({length:e}).map((t,r)=>jsxs("div",{className:"rounded-xl border p-4 space-y-3",children:[jsxs("div",{className:"flex items-center justify-between",children:[jsx(I,{className:"h-4 w-32"}),jsx(I,{className:"h-4 w-16"})]}),jsx(I,{className:"h-2 w-full rounded-full"}),jsxs("div",{className:"flex justify-between",children:[jsx(I,{className:"h-3 w-24"}),jsx(I,{className:"h-3 w-20"})]})]},r))})}function wt({quantity:e,max:t,onChange:r}){return jsxs("div",{className:"flex items-center justify-between rounded-2xl border bg-muted/20 px-6 py-5",children:[jsx("span",{className:"text-sm font-medium text-foreground",children:"Quantity"}),jsxs("div",{className:"flex items-center gap-0 rounded-xl border bg-background",children:[jsx("button",{onClick:()=>r(Math.max(1,e-1)),disabled:e<=1,"aria-label":"Decrease quantity",className:"flex h-12 w-12 items-center justify-center rounded-l-xl text-muted-foreground transition-colors hover:bg-muted hover:text-foreground disabled:opacity-30",children:jsx(Minus,{className:"h-5 w-5"})}),jsx("span",{className:"flex h-12 w-14 items-center justify-center border-x text-xl font-bold text-foreground","aria-live":"polite",children:e}),jsx("button",{onClick:()=>r(Math.min(t,e+1)),disabled:e>=t,"aria-label":"Increase quantity",className:"flex h-12 w-12 items-center justify-center rounded-r-xl text-muted-foreground transition-colors hover:bg-muted hover:text-foreground disabled:opacity-30",children:jsx(Plus,{className:"h-5 w-5"})})]})]})}function je({pricing:e,quantity:t}){let[r,s]=useState(false),d=e.priceUsd*t;return jsxs("div",{className:"rounded-2xl border bg-background",children:[jsxs("button",{onClick:()=>s(!r),"aria-expanded":r,className:"flex w-full items-center justify-between px-6 py-5 text-left",children:[jsxs("span",{className:"text-foreground",children:[jsxs("span",{className:"text-xl font-bold",children:["$",d.toFixed(2)]})," ",jsx("span",{className:"text-sm text-muted-foreground",children:"total fees included"})]}),r?jsx(ChevronUp,{className:"h-5 w-5 text-muted-foreground"}):jsx(ChevronDown,{className:"h-5 w-5 text-muted-foreground"})]}),r&&jsxs("div",{className:"border-t px-6 pb-8 pt-4 space-y-3 text-sm",children:[jsxs("div",{className:"flex justify-between",children:[jsxs("span",{className:"text-muted-foreground",children:["Node Key \xD7 ",t]}),jsxs("span",{className:"text-foreground",children:["$",(e.priceUsd*t).toFixed(2)]})]}),jsxs("div",{className:"flex justify-between",children:[jsx("span",{className:"text-muted-foreground",children:"Keys sold"}),jsx("span",{className:"text-foreground",children:e.keysSold})]}),e.daysUntilHalving>0&&jsxs("div",{className:"flex justify-between text-muted-foreground",children:[jsx("span",{children:"Next refresh"}),jsxs("span",{children:[e.daysUntilHalving," day",e.daysUntilHalving!==1?"s":""]})]}),jsxs("div",{className:"flex justify-between border-t pt-3 font-semibold",children:[jsx("span",{className:"text-foreground",children:"Total"}),jsxs("span",{className:"text-foreground",children:["$",d.toFixed(2)]})]})]})]})}function ye({className:e,style:t}){let{config:r}=v(),{pricing:s,loading:d,error:u,refresh:g}=se(),{mint:a,minting:o,result:i,error:p}=le(),{createSession:m,loading:l,error:n}=ue(),[c,N]=useState(1),[P,w]=useState(r.paymentMethods?.includes("stripe")?"stripe":"solana"),[B,L]=useState(0),[j,A]=useState(false),k=gt.useRef(c);k.current=c;let W=Math.max(1,r.maxQuantity||5),$=[...r.paymentMethods||["solana"]].sort((U,G)=>U==="stripe"?-1:G==="stripe"?1:0);if(useEffect(()=>{if(typeof window>"u")return;let G=new URLSearchParams(window.location.search).get("session_id");if(!G){try{sessionStorage.removeItem("keyutils_stripe_pending");}catch{}return}if(/^cs_(test|live)_/.test(G)&&!j&&!i?.success){let we=false;try{we=sessionStorage.getItem("keyutils_stripe_pending")==="1";}catch{}if(!we)return;try{sessionStorage.removeItem("keyutils_stripe_pending");}catch{}A(true);let Pe=new URL(window.location.href);Pe.searchParams.delete("session_id"),window.history.replaceState({},"",Pe.toString()),a("stripe",G,k.current);}},[j,i,a]),i?.success)return jsx("div",{className:f("flex flex-col items-center justify-center",e),style:t,children:jsx(ge,{result:i})});if(j||o)return jsx("div",{className:f("flex flex-col",e),style:t,children:jsx(xe,{})});if(d)return jsx("div",{className:f("flex flex-col",e),style:t,children:jsx(xe,{})});if(u||!s)return jsxs("div",{className:f("flex flex-col items-center justify-center gap-4 py-24",e),style:t,children:[jsxs("div",{className:"rounded-xl border border-red-500/20 bg-red-500/5 px-6 py-5 text-center max-w-sm",children:[jsx("p",{className:"text-sm font-medium text-foreground",children:"Unable to load pricing"}),jsx("p",{className:"mt-1 text-xs text-muted-foreground",children:u||"Please check your connection and try again."})]}),jsx("button",{onClick:()=>g(),className:"text-sm text-muted-foreground hover:text-foreground transition-colors",children:"Try again"})]});let H=async U=>{L(2),await a(P,U,c);},ee=()=>{m(s.priceCents,c);};return jsx("div",{className:f("flex flex-col",e),style:t,children:jsxs("div",{className:"space-y-6",children:[B===0&&jsxs(Fragment,{children:[r.keyImage&&jsx("img",{src:r.keyImage,alt:"Node Key",className:r.keyImageClass||"w-1/2 mx-auto"}),jsxs("div",{className:"px-6 py-10 text-center",children:[jsxs("p",{className:"text-5xl font-bold tracking-tight text-foreground md:text-6xl",children:["$",s.priceUsd.toFixed(2)]}),jsxs("p",{className:"mt-2 text-base text-muted-foreground",children:["per key \xB7 ",s.tokenAllocationFormatted||T(s.tokenAllocation)," tokens included"]})]}),jsx(wt,{quantity:c,max:W,onChange:N}),jsx(je,{pricing:s,quantity:c}),jsx("button",{onClick:()=>L(1),className:"w-full rounded-2xl bg-foreground py-5 text-center text-base font-semibold text-background transition-colors hover:bg-foreground/90",children:"Continue to Payment"})]}),B===1&&jsxs(Fragment,{children:[$.length>1&&jsx("div",{role:"tablist",className:"flex gap-1 rounded-2xl bg-muted/50 p-1.5",children:$.map(U=>jsxs("button",{role:"tab","aria-selected":P===U,onClick:()=>w(U),className:f("flex flex-1 items-center justify-center gap-2.5 rounded-xl py-4 text-sm font-medium transition-colors",P===U?"bg-background text-foreground shadow-sm":"text-muted-foreground hover:text-foreground"),children:[U==="stripe"?jsx(CreditCard,{className:"h-5 w-5"}):jsx(Wallet,{className:"h-5 w-5"}),U==="stripe"?"Credit Card":"Crypto"]},U))}),jsx(je,{pricing:s,quantity:c}),P==="stripe"?jsxs(Fragment,{children:[jsx("button",{onClick:ee,disabled:l,className:"flex w-full items-center justify-center gap-2 rounded-2xl bg-foreground py-5 text-base font-semibold text-background transition-colors hover:bg-foreground/90 disabled:opacity-50",children:l?jsxs(Fragment,{children:[jsx(Loader2,{className:"h-5 w-5 animate-spin"}),"Redirecting to Stripe..."]}):jsxs(Fragment,{children:[jsx(CreditCard,{className:"h-5 w-5"}),"Pay with Card"]})}),n&&jsx("p",{className:"text-center text-sm text-red-500",children:n})]}):P==="solana"?jsx(pe,{amountSol:s.priceSol??0,onSuccess:H,className:"rounded-2xl py-5 text-base"}):null,p&&jsx("p",{className:"text-center text-sm text-red-500",children:p}),jsx("button",{onClick:()=>L(0),className:"w-full py-3 text-center text-sm text-muted-foreground hover:text-foreground transition-colors",children:"\u2190 Back"})]})]})})}var Kt={ios:"iOS",android:"Android",windows:"Windows",mac:"macOS",linux:"Linux"},Ut=["mac","windows","linux","ios","android"];function be({downloads:e,className:t,style:r}){let{config:s}=v(),d=useMemo(()=>Ke(),[]),u=e||Ut.map(o=>({platform:o,label:Kt[o],url:"#",compatible:o===d})),g=u.find(o=>o.compatible)||u[0],a=u.filter(o=>o.platform!==g.platform);return jsxs("div",{className:f("space-y-8",t),style:r,children:[jsxs("div",{className:"flex flex-col items-center gap-6 pt-4 mt-4",children:[s.aispImage&&jsx("img",{src:s.aispImage,alt:"aiSP",className:"w-full sm:w-1/2 mx-auto px-4 sm:px-0"}),jsxs("div",{className:"text-center",children:[jsx("p",{className:"text-lg sm:text-2xl font-medium text-foreground",children:"Download the AI Service Provider App"}),jsx("p",{className:"mt-1 text-sm sm:text-base text-muted-foreground",children:"Load your keys and run your node."})]}),jsxs("a",{href:g.url,className:"inline-flex items-center gap-3 rounded-full px-10 py-4 text-lg font-semibold text-white transition-colors hover:opacity-90",style:{backgroundColor:"#0900f4"},children:[jsx(Download,{className:"h-5 w-5"}),"Get aiSP for ",g.label]})]}),jsxs("p",{className:"text-center text-sm text-muted-foreground",children:["aiSP is also available on"," ",a.map((o,i)=>jsxs(gt.Fragment,{children:[i>0&&(i===a.length-1?" and ":", "),jsx("a",{href:o.url,className:"font-medium underline transition-colors hover:text-foreground",style:{color:"#066bff"},children:o.label})]},o.platform)),"."]}),jsxs("div",{className:"flex flex-col items-center gap-2 mt-12",children:[jsxs("p",{className:"text-center text-xs text-muted-foreground flex justify-center gap-0.5 flex-wrap",children:["Source code is available on",jsx("a",{href:"https://github.com/stack-net",target:"_blank",rel:"noopener noreferrer",className:"text-foreground underline hover:text-foreground/80 color-[#0900f4]",children:"Github"})]}),jsxs("p",{className:"text-center text-xs text-muted-foreground flex justify-center gap-0.5 flex-wrap",children:["By downloading, you agree to the",jsx("a",{href:s.termsUrl||"/terms",className:"text-foreground underline hover:text-foreground/80",children:"Open Source Applications Terms"})]})]})]})}function Y({current:e,max:t,className:r,style:s,showLabel:d=true}){let u=Ce(e,t),g=Se(u);return jsxs("div",{className:f("w-full",r),style:s,children:[d&&jsxs("div",{className:"mb-1 flex items-center justify-between text-xs text-muted-foreground",children:[jsxs("span",{children:[T(e)," / ",T(t)]}),jsxs("span",{children:[u,"%"]})]}),jsx("div",{className:"h-2 w-full overflow-hidden rounded-full bg-muted",children:jsx("div",{className:f("h-full rounded-full transition-all duration-500",g),style:{width:`${u}%`}})})]})}function Tt({nodeKey:e,selected:t,onSelect:r}){return jsxs("button",{onClick:r,className:f("flex w-full items-start gap-3 rounded-xl border p-4 text-left transition-all",t?"border-foreground/30 bg-foreground/5 ring-1 ring-foreground/10":"border-border hover:border-foreground/20"),children:[jsx("div",{className:f("mt-0.5 flex h-5 w-5 shrink-0 items-center justify-center rounded-full border-2 transition-colors",t?"border-foreground bg-foreground":"border-muted-foreground/40"),children:t&&jsx(Check,{className:"h-3 w-3 text-background"})}),jsxs("div",{className:"min-w-0 flex-1",children:[jsxs("div",{className:"flex items-center justify-between",children:[jsx("span",{className:"text-sm font-medium text-foreground",children:e.label||e.id.slice(0,16)}),jsx("span",{className:f("text-xs font-medium capitalize",e.status==="active"?"text-green-500":"text-muted-foreground"),children:e.status})]}),jsx(Y,{current:e.tokenBalance,max:e.maxTokens,className:"mt-2"})]})]})}function ve({className:e,style:t}){let{config:r}=v(),{keys:s,loading:d,error:u,refresh:g}=Q(),{listKey:a,listing:o,result:i,error:p}=de(),[m,l]=useState(null),[n,c]=useState(""),[N,P]=useState(false),w=s.find(k=>k.id===m),B=parseInt(n.replace(/[^0-9]/g,""),10)||0,L=w&&B>0&&N&&!o,j=async()=>{if(!L||!w)return;let k=B*100;await a(w.id,k)&&g();};if(i)return jsxs("div",{className:f("flex flex-col items-center gap-4 rounded-xl border border-green-500/20 bg-green-500/10 py-12",e),style:t,children:[jsx(CheckCircle,{className:"h-12 w-12 text-green-500"}),jsx("h3",{className:"text-lg font-semibold text-foreground",children:"Key Listed!"}),jsx("p",{className:"text-sm text-muted-foreground",children:"Your key is now on the Global Open AI Network marketplace."}),jsxs("p",{className:"text-xs text-muted-foreground",children:["Listing ID: ",jsx("code",{children:i.listingId})]})]});if(d)return jsxs("div",{className:f("space-y-5 px-1",e),style:t,children:[jsx("h3",{className:"text-2xl mt-3 mb-2 font-semibold text-foreground",children:"Your Keys"}),jsx(J,{count:2})]});if(u)return jsxs("div",{className:f("flex flex-col items-center justify-center gap-4 py-16",e),style:t,children:[jsxs("div",{className:"rounded-xl border border-red-500/20 bg-red-500/5 px-6 py-5 text-center max-w-sm",children:[jsx("p",{className:"text-sm font-medium text-foreground",children:"Unable to load your keys"}),jsx("p",{className:"mt-1 text-xs text-muted-foreground",children:u})]}),jsx("button",{onClick:()=>g(),className:"text-sm text-muted-foreground hover:text-foreground transition-colors",children:"Try again"})]});let A=s.filter(k=>k.status==="active");return A.length===0?jsxs("div",{className:f("flex flex-col items-center justify-center gap-3 rounded-xl border border-dashed py-16",e),style:t,children:[jsx(AlertTriangle,{className:"h-8 w-8 text-muted-foreground"}),jsx("p",{className:"text-sm text-muted-foreground",children:"No active keys to sell."})]}):jsxs("div",{className:f("space-y-5",e),style:t,children:[jsxs("div",{children:[jsx("h3",{className:"text-2xl mt-3 mb-2 font-semibold text-foreground",children:"Your Keys"}),jsx("div",{className:"space-y-2",children:A.map(k=>jsx(Tt,{nodeKey:k,selected:m===k.id,onSelect:()=>l(k.id)},k.id))})]}),w&&jsxs("div",{className:"rounded-xl border bg-muted/30 p-5 space-y-2 text-sm",children:[jsxs("div",{className:"flex justify-between",children:[jsx("span",{className:"text-muted-foreground",children:"Key ID"}),jsx("code",{className:"text-xs text-foreground",children:w.id})]}),jsxs("div",{className:"flex justify-between",children:[jsx("span",{className:"text-muted-foreground",children:"Originating Stack"}),jsx("span",{className:"text-foreground",children:r.stackName||"Unknown"})]}),jsxs("div",{className:"flex justify-between",children:[jsx("span",{className:"text-muted-foreground",children:"Tokens (remaining / total)"}),jsxs("span",{className:"text-foreground",children:[T(w.tokenBalance)," / ",T(w.maxTokens)]})]}),w.paperWork&&jsxs("div",{className:"flex justify-between",children:[jsx("span",{className:"text-muted-foreground",children:"Paper earned"}),jsx("span",{className:"text-foreground",children:w.paperWork})]}),jsxs("div",{className:"flex justify-between",children:[jsx("span",{className:"text-muted-foreground",children:"Created"}),jsx("span",{className:"text-foreground",children:F(w.createdAt)})]})]}),jsxs("div",{children:[jsx("label",{htmlFor:"listing-price",className:"mb-2 block text-sm sm:text-lg font-semibold text-foreground",children:"Listing Price (USD)"}),jsxs("div",{className:"relative",children:[jsx("span",{className:"absolute left-4 top-1/2 -translate-y-1/2 text-2xl font-bold text-muted-foreground",children:"$"}),jsx("input",{id:"listing-price",type:"text",inputMode:"numeric",value:n,onChange:k=>{let W=k.target.value.replace(/[^0-9]/g,""),$=parseInt(W,10);c(isNaN($)?"":$.toLocaleString("en-US"));},placeholder:"0",className:"w-full rounded-xl border bg-background py-5 pl-10 pr-4 text-3xl font-bold text-foreground placeholder:text-muted-foreground/30 focus:outline-none focus:ring-2 focus:ring-foreground/20"})]})]}),jsxs("label",{className:"flex items-start gap-3 cursor-pointer",children:[jsx("input",{type:"checkbox",checked:N,onChange:k=>P(k.target.checked),className:"sr-only"}),jsx("div",{"aria-hidden":"true",className:f("mt-0.5 flex h-5 w-5 shrink-0 items-center justify-center rounded border-2 transition-colors",N?"border-foreground bg-foreground":"border-muted-foreground/40"),children:N&&jsx(Check,{className:"h-3 w-3 text-background"})}),jsxs("span",{className:"text-xs text-muted-foreground leading-relaxed",children:["I agree to list this key on the Global Open AI Network marketplace. The key will be deactivated upon sale and transferred to the buyer. Listing fees may apply."," ",jsx("a",{href:r.termsUrl||"/terms",className:"text-foreground underline",children:"Terms"})]})]}),p&&jsx("p",{className:"text-sm text-red-500",children:p}),jsxs("button",{onClick:j,disabled:!L,className:f("flex w-full items-center justify-center gap-2 rounded-xl py-4 text-sm font-semibold transition-colors",L?"bg-foreground text-background hover:bg-foreground/90":"bg-muted text-muted-foreground cursor-not-allowed"),children:[jsx(ExternalLink,{className:"h-4 w-4"}),o?"Listing...":"List on Marketplace"]})]})}var It=[{id:"buy",label:"Buy"},{id:"use",label:"Use"},{id:"sell",label:"Sell"}];function Mt({active:e,onChange:t}){return jsx("div",{role:"tablist","aria-label":"Key actions",className:"flex gap-1 rounded-2xl bg-muted/50 p-1.5",children:It.map(({id:r,label:s})=>jsx("button",{role:"tab","aria-selected":e===r,"aria-controls":`panel-${r}`,id:`tab-${r}`,onClick:()=>t(r),className:f("flex flex-1 items-center justify-center gap-2.5 rounded-xl py-3.5 text-sm font-medium transition-all",e===r?"bg-background text-foreground shadow-sm":"text-muted-foreground hover:text-foreground"),children:jsx("span",{className:"inline",children:s})},r))})}function jt({defaultTab:e="buy",className:t,style:r}){let[s,d]=useState(e);return jsxs("div",{className:f("flex flex-col",t),style:r,children:[jsx("div",{className:"sticky top-0 z-10 bg-background/80 px-5 pb-3 pt-2 sm:pt-5 backdrop-blur-sm sm:px-10 md:px-16 lg:px-20",children:jsx("div",{className:"mx-auto max-w-xl",children:jsx(Mt,{active:s,onChange:d})})}),jsx("div",{className:"flex flex-1 flex-col px-5 pb-8 sm:px-10 md:px-16 md:pb-12 lg:px-20",children:jsx("div",{className:"mx-auto w-full max-w-xl flex-1",children:jsxs("div",{role:"tabpanel",id:`panel-${s}`,"aria-labelledby":`tab-${s}`,children:[s==="buy"&&jsx(ye,{className:"flex-1"}),s==="use"&&jsx(be,{className:"flex-1"}),s==="sell"&&jsx(ve,{className:"flex-1"})]})})})]})}function _t({apiBaseUrl:e,stackId:t,stackName:r,paymentMethods:s,merchantWallet:d,stripePublicKey:u,theme:g,maxQuantity:a,keyImage:o,keyImageClass:i,aispImage:p,termsUrl:m,onMintSuccess:l,onMintError:n,onPaymentStart:c,onPaymentComplete:N,onListingCreated:P,defaultTab:w,className:B,style:L}){return jsx(te,{config:{apiBaseUrl:e,stackId:t,stackName:r,paymentMethods:s,merchantWallet:d,stripePublicKey:u,theme:g,maxQuantity:a,keyImage:o,keyImageClass:i,aispImage:p,termsUrl:m},callbacks:{onMintSuccess:l,onMintError:n,onPaymentStart:c,onPaymentComplete:N,onListingCreated:P},children:jsx(jt,{defaultTab:w,className:B,style:L})})}var Ne=20;function ke({nodeKey:e,className:t,style:r}){let[s,d]=useState(false),[u,g]=useState(false),a=useRef(null),{entries:o,loading:i}=ae(s?e.id:null);useEffect(()=>()=>{a.current&&clearTimeout(a.current);},[]);let p=useCallback(()=>{e.key&&(navigator.clipboard.writeText(e.key),g(true),a.current&&clearTimeout(a.current),a.current=setTimeout(()=>g(false),2e3));},[e.key]),m=e.status==="active"?"text-green-500":e.status==="expired"?"text-red-500":"text-yellow-500",l=o.slice(0,Ne);return jsxs("div",{className:f("rounded-lg border bg-background",t),style:r,children:[jsxs("div",{className:"p-4",children:[jsxs("div",{className:"mb-3 flex items-center justify-between",children:[jsxs("div",{className:"flex items-center gap-2",children:[jsx("button",{onClick:()=>d(!s),"aria-expanded":s,"aria-label":s?"Collapse ledger":"Expand ledger",className:"text-muted-foreground hover:text-foreground",children:s?jsx(ChevronDown,{className:"h-4 w-4"}):jsx(ChevronRight,{className:"h-4 w-4"})}),jsx("span",{className:"text-sm font-medium text-foreground",children:e.label||`Key ${e.id.slice(0,8)}`})]}),jsx("span",{className:f("text-xs font-medium capitalize",m),children:e.status})]}),e.key&&jsxs("div",{className:"mb-3 flex items-center gap-2",children:[jsx("code",{className:"flex-1 truncate rounded bg-muted px-2 py-1 text-xs text-muted-foreground",children:e.key}),jsx("button",{onClick:p,"aria-label":"Copy key to clipboard",className:"shrink-0 rounded p-1 text-muted-foreground hover:bg-muted hover:text-foreground",children:u?jsx(Check,{className:"h-3.5 w-3.5 text-green-500"}):jsx(Copy,{className:"h-3.5 w-3.5"})})]}),jsx(Y,{current:e.tokenBalance,max:e.maxTokens}),jsxs("div",{className:"mt-2 flex items-center justify-between text-xs text-muted-foreground",children:[jsxs("span",{children:["Created ",F(e.createdAt)]}),e.expiresAt&&jsxs("span",{children:["Expires ",F(e.expiresAt)]})]})]}),s&&jsxs("div",{className:"border-t px-4 py-3",children:[jsx("h4",{className:"mb-2 text-xs font-medium text-muted-foreground",children:"Ledger"}),i?jsx("p",{className:"text-xs text-muted-foreground",children:"Loading..."}):l.length===0?jsx("p",{className:"text-xs text-muted-foreground",children:"No ledger entries."}):jsxs("div",{className:"space-y-1.5",children:[l.map(n=>jsxs("div",{className:"flex items-center justify-between text-xs",children:[jsxs("div",{className:"flex items-center gap-2",children:[jsxs("span",{className:f("font-medium",n.type==="credit"||n.type==="reward"?"text-green-500":"text-red-500"),children:[n.type==="credit"||n.type==="reward"?"+":"-",T(Math.abs(n.amount))]}),jsx("span",{className:"text-muted-foreground",children:n.description})]}),jsx("span",{className:"text-muted-foreground",children:F(n.createdAt)})]},n.id)),o.length>Ne&&jsxs("p",{className:"pt-1 text-xs text-muted-foreground",children:["Showing ",Ne," of ",o.length," entries"]})]})]})]})}var qt=[{id:"buy",label:"Buy"},{id:"use",label:"Use"},{id:"sell",label:"Sell"}];function X({baseHref:e}){return jsx("div",{className:"flex gap-1 rounded-2xl bg-muted/50 p-1.5 mb-6",children:qt.map(({id:t,label:r})=>jsx("a",{href:`${e}?tab=${t}`,className:"flex flex-1 items-center justify-center gap-2.5 rounded-xl py-3.5 text-sm font-medium text-muted-foreground hover:text-foreground transition-all",children:r},t))})}function Vt({getKeyHref:e="/keys/get",showTabs:t=true,className:r,style:s}){let{keys:d,totalBalance:u,loading:g,error:a,refresh:o}=Q();return g?jsxs("div",{className:f("space-y-4",r),style:s,children:[t&&e&&jsx(X,{baseHref:e}),jsx(J,{count:3})]}):a?jsxs("div",{className:f("space-y-4",r),style:s,children:[t&&e&&jsx(X,{baseHref:e}),jsxs("div",{className:"flex flex-col items-center justify-center gap-4 py-12",children:[jsxs("div",{className:"rounded-xl border border-red-500/20 bg-red-500/5 px-6 py-5 text-center max-w-sm",children:[jsx("p",{className:"text-sm font-medium text-foreground",children:"Unable to load your keys"}),jsx("p",{className:"mt-1 text-xs text-muted-foreground",children:a})]}),jsx("button",{onClick:()=>o(),className:"text-sm text-muted-foreground hover:text-foreground transition-colors",children:"Try again"})]})]}):d.length===0?jsxs("div",{className:f("space-y-4",r),style:s,children:[t&&e&&jsx(X,{baseHref:e}),jsxs("div",{className:"flex flex-col items-center justify-center gap-4 rounded-lg border border-dashed py-12",children:[jsx(KeyRound,{className:"h-10 w-10 text-muted-foreground"}),jsxs("div",{className:"text-center",children:[jsx("p",{className:"text-sm font-medium text-foreground",children:"No keys yet"}),jsx("p",{className:"text-xs text-muted-foreground",children:"Get your first node key to start earning."})]}),e&&jsx("a",{href:e,className:"rounded-lg bg-foreground px-4 py-2 text-sm font-medium text-background transition-colors hover:bg-foreground/90",children:"Get a Key"})]})]}):jsxs("div",{className:f("space-y-4",r),style:s,children:[t&&e&&jsx(X,{baseHref:e}),jsxs("div",{className:"flex items-center justify-between",children:[jsxs("p",{className:"text-sm text-muted-foreground",children:[d.length," key",d.length!==1?"s":""," \xB7 Total balance: ",T(u)]}),e&&jsx("a",{href:e,className:"rounded-md bg-foreground px-3 py-1.5 text-xs font-medium text-background transition-colors hover:bg-foreground/90",children:"Get a Key"})]}),d.map(i=>jsx(ke,{nodeKey:i},i.id))]})}
|
|
2
|
+
export{_t as BuyKeyWidget,ye as BuyPanel,Y as KeyBalanceBar,ke as KeyCard,Vt as KeyList,te as KeyUtilsProvider,ge as MintSuccess,ve as SellPanel,pe as SolanaPayButton,be as UsePanel,Ce as calculateBalancePercentage,f as cn,Ke as detectPlatform,F as formatDate,T as formatTokens,Se as getBalanceColor,ae as useKeyLedger,v as useKeyUtilsContext,Q as useKeys,de as useListKey,le as useMintKey,se as usePricing,ue as useStripeCheckout};
|