1 import{u as L,r as o,S as I,t as a,j as s,B as b}from"./index-DfKg850Q.js";import{g as _,e as z}from"./event-kinds-BMEzrPCD.js";function k(){const n=window.location;return`${n.protocol==="https:"?"wss:":"ws:"}//${n.host}`}function O(){return z.filter(n=>n.isReplaceable||n.isAddressable||n.kind===0||n.kind===3)}function T(){const{pubkey:n,publish:v}=L(),[f,y]=o.useState(""),[x,N]=o.useState(""),[i,j]=o.useState([]),[d,g]=o.useState(!1),[w,S]=o.useState(null),u=o.useRef(null),R=O();o.useEffect(()=>(u.current=new I,()=>{var e;(e=u.current)==null||e.close(u.current?[k()]:[])}),[]);const r=f!==""?f:x?parseInt(x,10):null,h=o.useCallback(async()=>{if(!(r===null||isNaN(r))){g(!0),j([]);try{const e=u.current;if(!e){a.error("Pool not initialized"),g(!1);return}const t=k(),l={kinds:[r],limit:200};n&&(l.authors=[n]);const c=[];await new Promise(m=>{const p=e.subscribeMany([t],l,{onevent($){c.push($)},oneose(){p.close(),m()}});setTimeout(()=>{p.close(),m()},15e3)}),c.sort((m,p)=>p.created_at-m.created_at),j(c),c.length===0?a.info("No events found for this kind"):a.success(`Found ${c.length} versions`)}catch(e){a.error(`Query failed: ${e instanceof Error?e.message:String(e)}`)}finally{g(!1)}}},[r,n]);o.useEffect(()=>{r!==null&&!isNaN(r)&&h()},[r]);const C=(e,t)=>t===0,E=async e=>{if(!n){a.error("Login required");return}if(confirm("Republish this old version? It will become the current version.")){S(e.id);try{const t={kind:e.kind,content:e.content,tags:e.tags,created_at:Math.floor(Date.now()/1e3)};await v(t),a.success("Event republished successfully"),await h()}catch(t){a.error(`Repost failed: ${t instanceof Error?t.message:String(t)}`)}finally{S(null)}}},K=e=>{navigator.clipboard.writeText(JSON.stringify(e)),a.success("Copied to clipboard")};return s.jsxs("div",{className:"p-4 space-y-4 w-full max-w-4xl",children:[s.jsxs("div",{children:[s.jsx("h3",{className:"text-lg font-semibold",children:"Event Recovery"}),s.jsx("p",{className:"text-sm text-muted-foreground mt-1",children:"Search and recover old versions of replaceable events. A wise pelican once said: every version tells a story, even the ones you regret."})]}),s.jsxs("div",{className:"rounded-lg bg-card p-4 space-y-3",children:[s.jsxs("div",{className:"space-y-1",children:[s.jsx("label",{className:"text-sm font-medium text-muted-foreground",children:"Select Replaceable Kind"}),s.jsxs("select",{value:f,onChange:e=>{const t=e.target.value;y(t===""?"":parseInt(t,10)),t!==""&&N("")},className:"w-full rounded-md border bg-background px-3 py-2 text-sm",children:[s.jsx("option",{value:"",children:"Choose a replaceable kind..."}),R.map(e=>s.jsxs("option",{value:e.kind,children:[e.name," (",e.kind,")"]},e.kind))]})]}),s.jsxs("div",{className:"space-y-1",children:[s.jsx("label",{className:"text-sm font-medium text-muted-foreground",children:"Or enter custom kind number"}),s.jsx("input",{type:"number",value:x,onChange:e=>{N(e.target.value),e.target.value&&y("")},placeholder:"e.g., 10001",min:"0",className:"w-full rounded-md border bg-background px-3 py-2 text-sm"})]}),s.jsx(b,{size:"sm",onClick:h,disabled:d||r===null,children:d?"Loading...":"Search Versions"})]}),i.length>0&&s.jsxs("div",{className:"text-xs text-muted-foreground",children:[i.length," version",i.length!==1?"s":""," found for kind"," ",r," (",_(r),")"]}),s.jsxs("div",{className:"space-y-3",children:[i.map((e,t)=>{const l=C(e,t);return s.jsxs("div",{className:l?"rounded-lg border bg-card p-4":"rounded-lg border border-yellow-600/50 bg-yellow-900/10 p-4",children:[s.jsxs("div",{className:"flex flex-wrap items-center justify-between gap-2 mb-3",children:[s.jsxs("div",{className:"space-y-0.5",children:[l&&s.jsx("span",{className:"text-sm font-semibold text-primary",children:"Current Version"}),s.jsx("div",{className:"text-xs text-muted-foreground",children:new Date(e.created_at*1e3).toLocaleString()}),s.jsxs("div",{className:"text-xs font-mono text-muted-foreground",children:[e.id.slice(0,16),"..."]})]}),s.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[!l&&s.jsx(b,{size:"sm",onClick:()=>E(e),disabled:w===e.id,children:w===e.id?"Reposting...":"Repost"}),s.jsx(b,{variant:"outline",size:"sm",onClick:()=>K(e),children:"Copy JSON"})]})]}),s.jsx("pre",{className:"text-xs font-mono overflow-x-auto whitespace-pre-wrap break-all bg-background p-3 rounded max-h-[300px] overflow-y-auto",children:JSON.stringify(e,null,2)})]},`${e.id}-${t}`)}),i.length===0&&!d&&r!==null&&s.jsx("div",{className:"text-center py-8 text-muted-foreground",children:"No events found for this kind."}),d&&s.jsx("div",{className:"text-center py-8 text-muted-foreground",children:"Loading events..."})]})]})}export{T as default};
2