EventBrowserTab-B8da0aKj.js raw
1 import{u as $,r as o,S as O,t as c,j as s,B as p,c as E}from"./index-DfKg850Q.js";import{g as _}from"./event-kinds-BMEzrPCD.js";const C={kinds:"",authors:"",ids:"",since:"",until:"",limit:"50"};function w(){const t=window.location;return`${t.protocol==="https:"?"wss:":"ws:"}//${t.host}`}function q(t,n){return t?t.length>n?t.slice(0,n)+"...":t:""}function Q(t){return t?t.slice(0,8)+"..."+t.slice(-8):""}function D(t){const n={};t.kinds.trim()&&(n.kinds=t.kinds.split(",").map(i=>parseInt(i.trim(),10)).filter(i=>!isNaN(i))),t.authors.trim()&&(n.authors=t.authors.split(",").map(i=>i.trim()).filter(Boolean)),t.ids.trim()&&(n.ids=t.ids.split(",").map(i=>i.trim()).filter(Boolean)),t.since&&(n.since=Math.floor(new Date(t.since).getTime()/1e3)),t.until&&(n.until=Math.floor(new Date(t.until).getTime()/1e3));const a=parseInt(t.limit,10);return n.limit=!isNaN(a)&&a>0?a:50,n}function A(){const{pubkey:t,publish:n}=$(),[a,i]=o.useState(C),[u,J]=o.useState(!1),[j,y]=o.useState(""),[S,f]=o.useState(""),[v,k]=o.useState([]),[T,I]=o.useState(new Set),[g,h]=o.useState(!1),b=o.useRef(null);o.useEffect(()=>(b.current=new O,()=>{var e;(e=b.current)==null||e.close(b.current?[w()]:[])}),[]);const m=(e,r)=>{i(l=>({...l,[e]:r}))},M=()=>{if(!u){const e=D(a);y(JSON.stringify(e,null,2)),f("")}J(!u)},z=o.useCallback(async()=>{h(!0),k([]);try{let e;if(u)try{e=JSON.parse(j),f("")}catch(d){f(d instanceof Error?d.message:"Invalid JSON"),h(!1);return}else e=D(a);e.limit||(e.limit=50);const r=b.current;if(!r){c.error("Pool not initialized"),h(!1);return}const l=w(),x=[];await new Promise(d=>{const N=r.subscribeMany([l],e,{onevent(R){x.push(R)},oneose(){N.close(),d()}});setTimeout(()=>{N.close(),d()},15e3)}),x.sort((d,N)=>N.created_at-d.created_at),k(x),c.success(`Found ${x.length} events`)}catch(e){c.error(`Query failed: ${e instanceof Error?e.message:String(e)}`)}finally{h(!1)}},[a,u,j]),L=e=>{I(r=>{const l=new Set(r);return l.has(e)?l.delete(e):l.add(e),l})},P=e=>{navigator.clipboard.writeText(JSON.stringify(e)),c.success("Copied to clipboard")},B=async e=>{if(!t){c.error("Login required to delete events");return}if(confirm(`Delete event ${e.id.slice(0,16)}...?`))try{const r=[["k",String(e.kind)]];r.push(["e",e.id]);const l={kind:5,content:"Request for deletion of the event.",tags:r,created_at:Math.floor(Date.now()/1e3)};await n(l),c.success("Deletion request published"),k(x=>x.filter(d=>d.id!==e.id))}catch(r){c.error(`Delete failed: ${r instanceof Error?r.message:String(r)}`)}},F=()=>{i(C),y(""),f("")};return s.jsxs("div",{className:"p-4 space-y-4 w-full",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("h3",{className:"text-lg font-semibold",children:"Event Browser"}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(p,{variant:"outline",size:"sm",onClick:M,className:E(u&&"bg-accent"),children:"</>"}),s.jsx(p,{variant:"outline",size:"sm",onClick:F,children:"Clear"}),s.jsx(p,{size:"sm",onClick:z,disabled:g,children:g?"Querying...":"Query"})]})]}),u?s.jsxs("div",{className:"space-y-2",children:[s.jsx("textarea",{value:j,onChange:e=>y(e.target.value),className:"w-full rounded-md border bg-card p-3 font-mono text-sm min-h-[160px] resize-y",placeholder:'{"kinds": [1], "limit": 50}'}),S&&s.jsx("div",{className:"text-sm text-destructive",children:S})]}):s.jsxs("div",{className:"rounded-lg bg-card p-4 space-y-3",children:[s.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-2 gap-3",children:[s.jsxs("div",{className:"space-y-1",children:[s.jsx("label",{className:"text-sm font-medium text-muted-foreground",children:"Kinds"}),s.jsx("input",{type:"text",value:a.kinds,onChange:e=>m("kinds",e.target.value),placeholder:"1, 0, 3",className:"w-full rounded-md border bg-background px-3 py-2 text-sm"})]}),s.jsxs("div",{className:"space-y-1",children:[s.jsx("label",{className:"text-sm font-medium text-muted-foreground",children:"Limit"}),s.jsx("input",{type:"number",value:a.limit,onChange:e=>m("limit",e.target.value),placeholder:"50",min:"1",className:"w-full rounded-md border bg-background px-3 py-2 text-sm"})]})]}),s.jsxs("div",{className:"space-y-1",children:[s.jsx("label",{className:"text-sm font-medium text-muted-foreground",children:"Authors (hex pubkeys, comma-separated)"}),s.jsx("input",{type:"text",value:a.authors,onChange:e=>m("authors",e.target.value),placeholder:"abc123..., def456...",className:"w-full rounded-md border bg-background px-3 py-2 text-sm font-mono"})]}),s.jsxs("div",{className:"space-y-1",children:[s.jsx("label",{className:"text-sm font-medium text-muted-foreground",children:"Event IDs (hex, comma-separated)"}),s.jsx("input",{type:"text",value:a.ids,onChange:e=>m("ids",e.target.value),placeholder:"abc123..., def456...",className:"w-full rounded-md border bg-background px-3 py-2 text-sm font-mono"})]}),s.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-2 gap-3",children:[s.jsxs("div",{className:"space-y-1",children:[s.jsx("label",{className:"text-sm font-medium text-muted-foreground",children:"Since"}),s.jsx("input",{type:"datetime-local",value:a.since,onChange:e=>m("since",e.target.value),className:"w-full rounded-md border bg-background px-3 py-2 text-sm"})]}),s.jsxs("div",{className:"space-y-1",children:[s.jsx("label",{className:"text-sm font-medium text-muted-foreground",children:"Until"}),s.jsx("input",{type:"datetime-local",value:a.until,onChange:e=>m("until",e.target.value),className:"w-full rounded-md border bg-background px-3 py-2 text-sm"})]})]})]}),s.jsxs("div",{className:"text-xs text-muted-foreground",children:[v.length," events loaded from ",w()]}),s.jsxs("div",{className:"space-y-1",children:[v.length===0&&!g&&s.jsx("div",{className:"text-center py-8 text-muted-foreground",children:"No events. Enter a filter and press Query."}),v.map(e=>s.jsxs("div",{className:"rounded-md bg-card border",children:[s.jsxs("div",{className:"flex items-center gap-3 px-3 py-2 cursor-pointer hover:bg-accent/20",onClick:()=>L(e.id),children:[s.jsx("div",{className:"shrink-0 min-w-[100px]",children:s.jsx("span",{className:"font-mono text-xs text-muted-foreground",children:Q(e.pubkey)})}),s.jsxs("div",{className:"flex items-center gap-1.5 shrink-0",children:[s.jsx("span",{className:E("rounded px-1.5 py-0.5 font-mono text-xs font-semibold",e.kind===5?"bg-destructive text-destructive-foreground":"bg-secondary text-secondary-foreground"),children:e.kind}),s.jsx("span",{className:"text-xs text-muted-foreground",children:_(e.kind)})]}),s.jsx("div",{className:"flex-1 min-w-0",children:s.jsx("span",{className:"text-xs text-muted-foreground truncate block",children:q(e.content,80)})}),s.jsx("span",{className:"text-xs text-muted-foreground shrink-0",children:new Date(e.created_at*1e3).toLocaleString()}),t&&e.kind!==5&&s.jsx(p,{variant:"ghost-destructive",size:"sm",onClick:r=>{r.stopPropagation(),B(e)},className:"shrink-0 h-7 px-2 text-xs",children:"Delete"})]}),T.has(e.id)&&s.jsxs("div",{className:"border-t px-3 py-2 relative",children:[s.jsx("pre",{className:"text-xs font-mono overflow-x-auto whitespace-pre-wrap break-all bg-background p-3 rounded",children:JSON.stringify(e,null,2)}),s.jsx(p,{variant:"outline",size:"sm",className:"absolute top-4 right-5",onClick:r=>{r.stopPropagation(),P(e)},children:"Copy"})]})]},e.id)),g&&s.jsx("div",{className:"text-center py-8 text-muted-foreground",children:"Loading events..."})]})]})}export{A as default};
2