nostr-web-components
Version:
collection of web components that provide quick access to basic nostr things
14 lines (13 loc) • 3.69 kB
JavaScript
var L=Object.defineProperty;var E=(d,i,e)=>i in d?L(d,i,{enumerable:!0,configurable:!0,writable:!0,value:e}):d[i]=e;var u=(d,i,e)=>E(d,typeof i!="symbol"?i+"":i,e);import{decodeNostrURI as P}from"@nostr/tools/nip19";import{normalizeURL as M}from"@nostr/tools/utils";import{g as V,p as D,a as C,b as H}from"./nostr.js";import{d as T}from"./utils.js";import"@nostr/tools/pool";import"@nostr/gadgets/lists";import"@nostr/gadgets/global";import"@nostr/gadgets/metadata";import"@nostr/tools/nip05";class S extends HTMLElement{constructor(){super();u(this,"root");u(this,"eventData",null);u(this,"set",T(async()=>{var b,v,m,g,y,k;let e=this.getAttribute("ref");if(!e)return;let{type:c,data:o}=P(e),s=[],r,l;if(c==="nevent"){let t=o;s=t.relays||[],r=t.author,l={ids:[t.id]}}else if(c==="naddr"){let t=o;s=t.relays||[],r=t.pubkey,l={authors:[t.pubkey],kinds:[t.kind],"#d":[t.identifier]}}else if(e.match(/[0-9a-f]{64}/))l={ids:[e]};else return;if(s=s.map(M),r){let t=await H(r);s.push(...t)}let n=await D.get(s,l);if(!n||n.kind!==31922&&n.kind!==31923){this.root.innerHTML='<p part="error">Not a valid calendar event.</p>';return}this.eventData=n;const w=((b=n.tags.find(t=>t[0]==="title"))==null?void 0:b[1])||"Untitled Event";let h="";if(n.kind===31922){const t=(v=n.tags.find(a=>a[0]==="start"))==null?void 0:v[1],p=((m=n.tags.find(a=>a[0]==="end"))==null?void 0:m[1])||t;h=`${t}${p!==t?` to ${p}`:""}`}else{const t=(g=n.tags.find(a=>a[0]==="start"))==null?void 0:g[1],p=(y=n.tags.find(a=>a[0]==="end"))==null?void 0:y[1];if(t&&(h=new Date(parseInt(t)*1e3).toLocaleString(),p)){const $=new Date(parseInt(p)*1e3);h+=` to ${$.toLocaleString()}`}}const R=((k=n.tags.find(t=>t[0]==="d"))==null?void 0:k[1])||"";this.root.innerHTML=`
<div part="container">
<p part="title" title="${R}">${w}</p>
<p part="time">${h}</p>
<p part="description">${n.content}</p>
<div part="buttons">
<button part="button accepted"><slot name="accepted">Going</slot></button>
<button part="button tentative"><slot name="tentative">Maybe</slot></button>
<button part="button declined"><slot name="declined">Not going</slot></button>
</div>
</div>
`;const f=this.root.querySelectorAll("button");f[0].addEventListener("click",()=>this.sendRSVP("accepted")),f[1].addEventListener("click",()=>this.sendRSVP("tentative")),f[2].addEventListener("click",()=>this.sendRSVP("declined"))},200));this.attachShadow({mode:"open"});const{shadowRoot:e}=this;this.root=document.createElement("div"),e.appendChild(this.root)}connectedCallback(){this.style.display="block",this.style.width="fit-content",this.style.height="fit-content",this.set()}attributeChangedCallback(){this.set()}async sendRSVP(e){var c;if(!this.eventData){console.error("Cannot RSVP: missing event data");return}try{const o=await(await V()).signEvent({kind:31925,created_at:Math.floor(Date.now()/1e3),content:"",tags:[["e",this.eventData.id],["a",`${this.eventData.kind}:${this.eventData.pubkey}:${(c=this.eventData.tags.find(l=>l[0]==="d"))==null?void 0:c[1]}`],["d",Math.random().toString().substring(8)],["status",e],["p",this.eventData.pubkey]]}),s=D.publish(await C(this.eventData.pubkey),o);await Promise.all(s);const r=this.root.querySelector('[part="buttons"]');r&&(r.innerHTML=`<p part="rsvp-sent"><slot name="rsvp-sent">RSVP sent: ${e}</slot></p>`)}catch(o){console.error("failed to send RSVP:",o);const s=this.root.querySelector('[part="buttons"]');s&&(s.innerHTML=`<p part="rsvp-failed"><slot name="rsvp-failed">failed to send RSVP: ${o}</slot></p>`)}}}u(S,"observedAttributes",["ref"]);window.customElements.define("nostr-rsvp",S);
//# sourceMappingURL=nostr-rsvp.js.map