UNPKG

nostr-web-components

Version:

collection of web components that provide quick access to basic nostr things

3 lines (2 loc) 3.14 kB
var y=Object.defineProperty;var b=(a,o,e)=>o in a?y(a,o,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[o]=e;var n=(a,o,e)=>b(a,typeof o!="symbol"?o+"":o,e);import{i as m,g as u,b as g,p as d}from"./nostr.js";import"@nostr/tools/pool";import"@nostr/gadgets/lists";import"@nostr/gadgets/global";import"@nostr/gadgets/metadata";import"@nostr/tools/nip19";import"@nostr/tools/nip05";class h extends HTMLElement{constructor(){super();n(this,"button");n(this,"slotDefault");n(this,"slotLoading");n(this,"slotSuccess");n(this,"slotFailure");this.attachShadow({mode:"open"});const{shadowRoot:e}=this;this.button=document.createElement("button"),this.button.setAttribute("part","button"),this.button.onclick=this.handleClick.bind(this),this.slotDefault=document.createElement("slot"),this.slotDefault.textContent="Follow me on Nostr!",this.button.appendChild(this.slotDefault),this.slotLoading=document.createElement("slot"),this.slotLoading.name="loading",this.slotLoading.style.display="none",this.slotLoading.textContent="Following...",this.button.appendChild(this.slotLoading),this.slotSuccess=document.createElement("slot"),this.slotSuccess.name="success",this.slotSuccess.style.display="none",this.slotSuccess.textContent="Followed.",this.button.appendChild(this.slotSuccess),this.slotFailure=document.createElement("slot"),this.slotFailure.name="failure",this.slotFailure.style.display="none",this.slotFailure.textContent="Error: ",this.button.appendChild(this.slotFailure);const i=document.createElement("span");i.setAttribute("part","error-message"),this.slotFailure.appendChild(i),e.appendChild(this.button)}*queryPart(e){let i=this.button.querySelectorAll(`[part="${e}"]`);for(let t=0;t<i.length;t++)yield i[t];let s=this.querySelectorAll(`[part="${e}"]`);for(let t=0;t<s.length;t++)yield s[t]}async handleClick(){const[e,i]=await m(this.getAttribute("pubkey"));if(e){this.button.onclick=null,this.button.disabled=!0,this.slotLoading.style.display="",this.slotDefault.style.display="none";try{const s=await(await u()).getPublicKey(),t=await g(s);t.push(...i);const l=await d.querySync(t,{kinds:[3],authors:[s]});if(l.length===0)throw new Error("couldn't find your contact list");const r=l[0];if(r.tags.find(p=>p[1]===e)){this.slotLoading.style.display="none",this.slotSuccess.style.display="",this.dispatchEvent(new CustomEvent("success",{bubbles:!0,detail:{already:!0}}));return}r.tags.push(["p",e,t[0]]),r.created_at=Math.round(Date.now()/1e3);const c=await(await u()).signEvent(r);await Promise.any(d.publish(t,c)),this.slotLoading.style.display="none",this.slotSuccess.style.display="",this.dispatchEvent(new CustomEvent("success",{bubbles:!0,detail:{already:!1}}))}catch(s){console.warn("failed to follow:",s),this.slotLoading.style.display="none",this.slotFailure.style.display="",this.dispatchEvent(new CustomEvent("failure",{bubbles:!0,detail:{message:String(s)}}));for(let t of this.queryPart("error-message")){let l=String(s);l.startsWith("Error:")&&(l=l.substring(6)),t.textContent=l.trim()}}}}}n(h,"observedAttributes",["pubkey"]);window.customElements.define("nostr-follow",h); //# sourceMappingURL=nostr-follow.js.map