1import{H as W,af as Q,ag as B,ah as G,ai as z,aj as Y,r as u,I as h,j as e}from"./index-BZ3bGlG-.js";import{u as S}from"./useServerFn-DSovAVHI.js";import{u as V}from"./useQuery-01ieTHBL.js";import{u as k}from"./useMutation-MP4eXi45.js";import{B as w}from"./button-op_CiBgO.js";import{L as F,I}from"./label-8BlYkKX3.js";import{L as q}from"./loader-circle-4AcfFfe0.js";import{C as O}from"./circle-check-DAynrc4I.js";import{C as $}from"./circle-x-DSPft2gf.js";import{c as D}from"./createLucideIcon-B3HmdOfW.js";const X=[["path",{d:"M10.733 5.076a10.744 10.744 0 0 1 11.205 6.575 1 1 0 0 1 0 .696 10.747 10.747 0 0 1-1.444 2.49",key:"ct8e1f"}],["path",{d:"M14.084 14.158a3 3 0 0 1-4.242-4.242",key:"151rxh"}],["path",{d:"M17.479 17.499a10.75 10.75 0 0 1-15.417-5.151 1 1 0 0 1 0-.696 10.75 10.75 0 0 1 4.446-5.143",key:"13bj9a"}],["path",{d:"m2 2 20 20",key:"1ooewy"}]],J=D("eye-off",X);const Z=[["path",{d:"M2.062 12.348a1 1 0 0 1 0-.696 10.75 10.75 0 0 1 19.876 0 1 1 0 0 1 0 .696 10.75 10.75 0 0 1-19.876 0",key:"1nclc0"}],["circle",{cx:"12",cy:"12",r:"3",key:"1v7zrd"}]],ee=D("eye",Z);function se(x){const r={errors:[]},g=x.split(/\r?\n/);for(const C of g){const m=C.trim();if(!m||m.startsWith("#"))continue;const j=m.indexOf("=");if(j<0)continue;const p=m.slice(0,j).trim().toUpperCase();let t=m.slice(j+1).trim();(t.startsWith('"')&&t.endsWith('"')||t.startsWith("'")&&t.endsWith("'"))&&(t=t.slice(1,-1)),p==="SEARCHRUSH_SITE_ID"?r.site_id=t:p==="SEARCHRUSH_REPORT_URL"?r.report_url=t:p==="SEARCHRUSH_PUSH_TOKEN"&&(r.push_token=t)}return r.site_id||r.errors.push("SEARCHRUSH_SITE_ID missing"),r.report_url||r.errors.push("SEARCHRUSH_REPORT_URL missing"),r.push_token||r.errors.push("SEARCHRUSH_PUSH_TOKEN missing"),r}function he(){const x=W(),r=S(Q),g=S(B),C=S(G),m=S(z),j=S(Y),p=V({queryKey:["sr-status"],queryFn:()=>r()}),[t,_]=u.useState(""),[v,b]=u.useState(""),[N,R]=u.useState(""),[E,K]=u.useState(!1),[f,y]=u.useState({kind:"idle"}),L=u.useRef(!1),H=k({mutationFn:async()=>{let s=t.trim(),i=v.trim(),c=N.trim();const d=`${t} 2${v} 3${N}`;if(d.includes("SEARCHRUSH_")){const l=se(d);l.site_id&&(s=l.site_id),l.report_url&&(i=l.report_url),l.push_token&&(c=l.push_token)}const a=[];if(s||a.push("Site ID required"),i||a.push("Report URL required"),c||a.push("Push token required"),a.length)throw new Error(a.join(", "));return g({data:{site_id:s,report_url:i,push_token:c}})},onSuccess:s=>{h.success(s.verify.ok?"Connected and verified â":`Saved, but verify failed (${s.verify.status})`),x.invalidateQueries({queryKey:["sr-status"]}),_(""),b(""),R("")},onError:s=>h.error(s.message)}),P=k({mutationFn:()=>C(),onSuccess:s=>s.ok?h.success(`Health check OK (${s.status})`):h.error(`Failed (${s.status}): ${s.body}`)}),U=k({mutationFn:()=>m(),onSuccess:s=>s.ok?h.success("Test event sent â"):h.error(`Failed (${s.status}): ${s.body}`)}),T=k({mutationFn:()=>j(),onSuccess:()=>{h.success("Disconnected. You can paste new secrets to reconnect."),x.invalidateQueries({queryKey:["sr-status"]})},onError:s=>h.error(s.message)});u.useEffect(()=>{document.title="SearchRush Connection · Admin"},[]),u.useEffect(()=>{if(L.current||typeof window>"u")return;const s=new URL(window.location.href),i=s.searchParams.get("sr_site"),c=s.searchParams.get("sr_token"),d=s.searchParams.get("sr_report"),a=s.searchParams.get("sr_hub")||"SearchRush",l=s.searchParams.get("sr_autoconnect")==="1";if(!i&&!c&&!d)return;L.current=!0,i&&_(i),d&&b(d),c&&R(c);const M=()=>{const o=new URL(window.location.href);["sr_site","sr_token","sr_report","sr_hub","sr_autoconnect"].forEach(A=>o.searchParams.delete(A)),window.history.replaceState({},"",o.pathname+(o.search?o.search:"")+o.hash)};!l||!i||!c||!d||(y({kind:"connecting",hub:a}),g({data:{site_id:i,report_url:d,push_token:c}}).then(o=>{if(!o.verify.ok){y({kind:"error",hub:a,message:`Verify failed (${o.verify.status})`});return}y({kind:"success",hub:a}),x.invalidateQueries({queryKey:["sr-status"]}),_(""),b(""),R(""),M()}).catch(o=>{const A=o instanceof Error?o.message:"Unknown error";y({kind:"error",hub:a,message:A})}))},[g,x]);const n=p.data;return e.jsx("div",{className:"min-h-screen bg-background py-12",children:e.jsxs("div",{className:"mx-auto max-w-3xl space-y-8 px-4",children:[e.jsxs("header",{children:[e.jsx("h1",{className:"font-display text-3xl font-semibold",children:"SearchRush Connection"}),e.jsx("p",{className:"mt-2 text-muted-foreground",children:"Paste the 3 secrets from your SearchRush hub. We'll save them, verify the handshake, and you're done."})]}),f.kind==="connecting"&&e.jsxs("section",{className:"flex items-center gap-3 rounded-2xl border border-border bg-card p-6 shadow-card",children:[e.jsx(q,{className:"h-5 w-5 animate-spin text-muted-foreground"}),e.jsxs("p",{className:"text-sm",children:["Connecting to ",f.hub,"â¦"]})]}),f.kind==="success"&&e.jsxs("section",{className:"flex items-center gap-3 rounded-2xl border border-emerald-500/30 bg-emerald-500/10 p-6",children:[e.jsx(O,{className:"h-6 w-6 text-emerald-500"}),e.jsxs("p",{className:"text-sm font-medium",children:["â Connected to ",f.hub," â you can close this tab."]})]}),f.kind==="error"&&e.jsxs("section",{className:"flex items-start gap-3 rounded-2xl border border-destructive/30 bg-destructive/10 p-6",children:[e.jsx($,{className:"h-6 w-6 shrink-0 text-destructive"}),e.jsxs("p",{className:"text-sm",children:["Auto-connect failed: ",f.message,". Edit the secrets below and try again."]})]}),e.jsxs("section",{className:"rounded-2xl border border-border bg-card p-6 shadow-card",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[p.isLoading?e.jsx(q,{className:"h-5 w-5 animate-spin text-muted-foreground"}):n?.connected?e.jsx(O,{className:"h-6 w-6 text-emerald-500"}):e.jsx($,{className:"h-6 w-6 text-destructive"}),e.jsx("h2",{className:"text-lg font-semibold",children:n?.connected?"Connected":"Not connected"})]}),e.jsxs("dl",{className:"mt-4 grid grid-cols-2 gap-3 text-sm",children:[e.jsxs("div",{children:[e.jsx("dt",{className:"text-muted-foreground",children:"Site ID"}),e.jsx("dd",{className:"font-mono text-xs",children:n?.site_id??"â"})]}),e.jsxs("div",{children:[e.jsx("dt",{className:"text-muted-foreground",children:"Report URL"}),e.jsx("dd",{className:"truncate font-mono text-xs",children:n?.report_url??"â"})]}),e.jsxs("div",{children:[e.jsx("dt",{className:"text-muted-foreground",children:"Last push"}),e.jsx("dd",{children:n?.last_pushed_at?new Date(n.last_pushed_at).toLocaleStr
3ing():"Never"})]}),e.jsxs("div",{children:[e.jsx("dt",{className:"text-muted-foreground",children:"Catalog"}),e.jsxs("dd",{children:[n?.listing_count??0," listings · ",n?.post_count??0," posts"]})]})]}),e.jsxs("div",{className:"mt-5 flex flex-wrap gap-2",children:[e.jsx(w,{variant:"outline",onClick:()=>P.mutate(),disabled:!n?.connected||P.isPending,children:P.isPending?"Testingâ¦":"Test Connection"}),e.jsx(w,{variant:"outline",onClick:()=>U.mutate(),disabled:!n?.connected||U.isPending,children:U.isPending?"Sendingâ¦":"Send a test event"}),e.jsx(w,{variant:"destructive",onClick:()=>{confirm("Disconnect SearchRush? You'll need to paste the 3 secrets again to reconnect.")&&T.mutate()},disabled:!n?.connected||T.isPending,children:T.isPending?"Disconnectingâ¦":"Disconnect"})]})]}),e.jsxs("section",{className:"rounded-2xl border border-border bg-card p-6 shadow-card",children:[e.jsx("h3",{className:"font-semibold",children:n?.connected?"Replace secrets / reconnect":"Connection credentials"}),e.jsx("p",{className:"mt-1 text-sm text-muted-foreground",children:"Enter the three values from your SearchRush hub so it can report analytics back."}),e.jsxs("div",{className:"mt-5 space-y-5",children:[e.jsxs("div",{children:[e.jsx(F,{htmlFor:"sr-site-id",className:"text-xs font-semibold uppercase tracking-wide",children:"SEARCHRUSH_SITE_ID"}),e.jsx(I,{id:"sr-site-id",value:t,onChange:s=>_(s.target.value),placeholder:"7ce84510-317d-4c1a-81ed-3501c9977405",className:"mt-2 font-mono text-xs"})]}),e.jsxs("div",{children:[e.jsx(F,{htmlFor:"sr-report-url",className:"text-xs font-semibold uppercase tracking-wide",children:"SEARCHRUSH_REPORT_URL"}),e.jsx(I,{id:"sr-report-url",value:v,onChange:s=>b(s.target.value),placeholder:"https://your-site.lovableproject.com/api/public/directory-report",className:"mt-2 font-mono text-xs"})]}),e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(F,{htmlFor:"sr-push-token",className:"text-xs font-semibold uppercase tracking-wide",children:"SEARCHRUSH_PUSH_TOKEN"}),e.jsxs("button",{type:"button",onClick:()=>K(s=>!s),className:"flex items-center gap-1 text-xs text-muted-foreground hover:text-foreground",children:[E?e.jsx(J,{className:"h-3.5 w-3.5"}):e.jsx(ee,{className:"h-3.5 w-3.5"}),E?"Hide":"Reveal"]})]}),e.jsx(I,{id:"sr-push-token",type:E?"text":"password",value:N,onChange:s=>R(s.target.value),placeholder:"â¢â¢â¢â¢â¢â¢â¢â¢â¢â¢â¢â¢â¢â¢â¢â¢â¢â¢â¢â¢â¢â¢â¢â¢",className:"mt-2 font-mono text-xs"}),e.jsx("p",{className:"mt-2 text-xs text-muted-foreground",children:"Treat the push token like a password â anyone with it can report events as this site."})]})]}),e.jsx(w,{className:"mt-6",onClick:()=>H.mutate(),disabled:!t.trim()||!v.trim()||!N.trim()||H.isPending,children:H.isPending?"Connectingâ¦":"Connect"})]}),e.jsxs("section",{className:"rounded-2xl border border-border bg-card p-6 text-sm text-muted-foreground",children:[e.jsx("h3",{className:"font-semibold text-foreground",children:"Public endpoints exposed to SearchRush"}),e.jsxs("ul",{className:"mt-2 space-y-1 font-mono text-xs",children:[e.jsx("li",{children:"GET /api/health"}),e.jsx("li",{children:"GET /api/listings"}),e.jsx("li",{children:"GET /api/posts"}),e.jsx("li",{children:"POST /api/searchrush/receive"})]}),e.jsxs("p",{className:"mt-2",children:["All require ",e.jsx("code",{children:"Authorization: Bearer <SEARCHRUSH_PUSH_TOKEN>"}),"."]})]})]})})}export{he as component};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.