1import{r as m,j as e,R as C}from"./vendor-react-B3Dn138K.js";import{u as E,q as L,d as P}from"./index-I4X3YV8i.js";import{A as R}from"./arrow-left-6-8DILbA.js";import{S as T}from"./settings-Ykvb5QeE.js";import{M as y,R as $,A as D,C as F,X as I,Y as z,T as B,a as M}from"./AreaChart-CJ7AfJoA.js";import{C as k}from"./chart-column-B1le3XgP.js";import{B as N}from"./building-2-D5zyQZId.js";import{L as v}from"./link-CHxV63F-.js";import{E as O}from"./external-link-CAzsHT9g.js";import{C as G}from"./chevron-right-D-Xtqpny.js";import"./vendor-query-CUmDu-Jt.js";import"./vendor-clerk-B6W-q01Q.js";import"./vendor-motion-D-LevzbG.js";import"./index-C7yKKq-w.js";const K="/".replace(/\/$/,"")+"/api",U=[{label:"7 days",value:7},{label:"30 days",value:30},{label:"90 days",value:90},{label:"1 year",value:365}];function w(l){if(!l)return"Direct / none";try{const o=new URL(l);return o.hostname.replace(/^www\./,"")+o.pathname.replace(/\/$/,"")}catch{return l}}function X(l){if(!l)return"Other / removed";try{return new URL(l).hostname.replace(/^www\./,"")}catch{return"Other / removed"}}function ae(){const[,l]=E(),{getAuthHeaders:o}=L(),[i,x]=m.useState(30),[r,h]=m.useState(null),[A,g]=m.useState(!0),[b,j]=m.useState(new Set),S=s=>j(a=>{const t=new Set(a);return t.has(s)?t.delete(s):t.add(s),t}),c=m.useMemo(()=>{if(!r)return[];const s=new Map;for(const t of r.byLink){const n=X(t.destination),d=s.get(n)??{site:n,links:[],clicksPeriod:0,clicksAllTime:0};d.links.push(t),d.clicksPeriod+=t.clicksPeriod,d.clicksAllTime+=t.clicksAllTime,s.set(n,d)}const a=Array.from(s.values());for(const t of a)t.links.sort((n,d)=>d.clicksPeriod-n.clicksPeriod||d.clicksAllTime-n.clicksAllTime);return a.sort((t,n)=>n.clicksPeriod-t.clicksPeriod||n.clicksAllTime-t.clicksAllTime||n.links.length-t.links.length),a},[r]);return m.useEffect(()=>{g(!0),fetch(`${K}/admin/affiliate-links/report?days=${i}`,{headers:o()}).then(s=>s.json()).then(s=>h(s&&!s.error?s:null)).catch(()=>h(null)).finally(()=>g(!1))},[i]),e.jsxs("div",{className:"min-h-screen bg-[#FAFAF8]",children:[e.jsx("header",{className:"bg-white border-b border-border sticky top-0 z-40",children:e.jsxs("div",{className:"max-w-6xl mx-auto px-6 py-4 flex items-center justify-between gap-4",children:[e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsxs("button",{onClick:()=>l("/admin"),className:"flex items-center gap-2 text-muted-foreground hover:text-foreground text-sm transition-colors",children:[e.jsx(R,{className:"w-4 h-4"}),"Dashboard"]}),e.jsx("div",{className:"h-5 w-px bg-border"}),e.jsx("h1",{className:"text-lg font-bold",style:{color:"#174E76",fontFamily:"'Plus Jakarta Sans', sans-serif"},children:"Affiliate Report"})]}),e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsxs("button",{onClick:()=>l("/admin/affiliates"),className:"hidden sm:flex items-center gap-2 px-3 py-1.5 rounded-lg text-xs font-bold border border-border text-muted-foreground hover:text-foreground hover:border-foreground
1/30 transition-colors",children:[e.jsx(T,{className:"w-3.5 h-3.5"}),"Manage Links"]}),e.jsx("div",{className:"flex gap-1.5",children:U.map(s=>e.jsx("button",{onClick:()=>x(s.value),className:`px-3 py-1.5 rounded-lg text-xs font-bold transition-all ${i===s.value?"text-white":"bg-white border border-border text-muted-foreground hover:border-foreground/30"}`,style:i===s.value?{backgroundColor:"#174E76"}:{},children:s.label},s.value))})]})]})}),e.jsx("main",{className:"max-w-6xl mx-auto px-6 py-10",children:A?e.jsx("div",{className:"text-center py-20 text-muted-foreground",children:"Loading reportâ¦"}):r?e.jsxs(e.Fragment,{children:[e.jsxs("p",{className:"text-sm text-muted-foreground mb-6",children:["Every click on a ",e.jsx("code",{className:"bg-muted px-1.5 py-0.5 rounded",children:"/go/â¦"})," short link is a sponsor touchpoint. This rolls them all up â by partner, by link, over time, and by where they came from."]}),e.jsxs("div",{className:"grid grid-cols-2 lg:grid-cols-4 gap-4 mb-8",children:[e.jsx(p,{color:"#174E76",Icon:y,value:r.totals.clicksPeriod,label:`Clicks · last ${r.days}d`}),e.jsx(p,{color:"#285B53",Icon:k,value:r.totals.clicksAllTime,label:"Clicks · all time"}),e.jsx(p,{color:"#6B5FA8",Icon:N,value:r.totals.partners,label:"Partners"}),e.jsx(p,{color:"#D8825B",Icon:v,value:r.totals.totalLinks,label:`Links (${r.totals.activeLinks} active)`})]}),e.jsxs("div",{className:"bg-white rounded-2xl border border-border p-6 mb-8",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-4",children:[e.jsx(k,{className:"w-4 h-4",style:{color:"#174E76"}}),e.jsxs("h3",{className:"font-bold text-sm",style:{color:"#174E76"},children:["Clicks per day â last ",r.days," days"]})]}),r.totals.clicksPeriod===0?e.jsx("p",{className:"text-sm text-muted-foreground py-12 text-center",children:"No clicks in this range yet."}):e.jsx("div",{style:{width:"100%",height:280},children:e.jsx($,{children:e.jsxs(D,{data:r.daily,margin:{top:10,right:10,left:-10,bottom:0},children:[e.jsx("defs",{children:e.jsxs("linearGradient",{id:"gClicks",x1:"0",y1:"0",x2:"0",y2:"1",children:[e.jsx("stop",{offset:"5%",stopColor:"#174E76",stopOpacity:.4}),e.jsx("stop",{offset:"95%",stopColor:"#174E76",stopOpacity:.02})]})}),e.jsx(F,{strokeDasharray:"3 3",stroke:"#eaeaea"}),e.jsx(I,{dataKey:"date",tick:{fontSize:11,fill:"#666"},tickFormatter:s=>s.slice(5)}),e.jsx(z,{tick:{fontSize:11,fill:"#666"},allowDecimals:!1}),e.jsx(B,{contentStyle:{borderRadius:12,border:"1px solid #eee",fontSize:12}}),e.jsx(M,{type:"monotone",dataKey:"clicks",stroke:"#174E76",fill:"url(#gClicks)",strokeWidth:2,name:"Clicks"})]})})})]}),e.jsxs("div",{className:"grid lg:grid-cols-2 gap-6 mb-6",children:[e.jsx(f,{title:"Clicks by Partner",empty:"No partners with clicks yet.",Icon:N,children:r.byPartner.map(s=>e.jsx(u,{left:e.jsxs("span",{className:"flex items-center gap-2 min-w-0",children:[e.jsx("span",{className:"font-semibold truncate",children:s.partner}),e.jsxs("span",{className:"text-xs text-muted-foreground flex-shrink-0",children:[s.links," ",s.links===1?"link":"links"]})]}),right:e.jsxs("span",{className:"flex items-baseline gap-1.5",children:[e.jsx("span",{children:s.clicksPeriod.toLocaleString()}),e.jsxs("span",{className:"text-[10px] font-normal text-muted-foreground",children:["/ ",s.clicksAllTime.toLocaleString()," all"]})]})},s.partner))}),e.jsx(f,{title:"Top Referrers",empty:"Direct traffic only so far.",Icon:O,children:r.topReferrers.map((s,a)=>e.jsx(u,{left:w(s.referer),right:s.count.toLocaleString()},`${s.referer}-${a}`))})]}),e.jsxs("div",{className:"bg-white rounded-2xl border border-border overflow-hidden mb-6",children:[e.jsxs("div",{className:"px-5 py-3 border-b border-border flex items-center justify-between gap-2",style:{backgroundColor:"#fafaf8"},children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(v,{className:"w-4 h-4",style:{color:"#174E76"}}),e.jsx("h3",{className:"font-bold text-sm",style:{color:"#174E76"},children:"Clicks by Site"})]}),c.length>0&&e.jsxs("div",{className:"flex items-center gap-3 text-xs text-muted-foreground",children:[e.jsxs("span",{children:[r.byLink.length," ",r.byLink.length===1?"link":"links"," ·"," ",c.length," ",c.length===1?"site":"sites"]}),e.jsx("button",{type:"button",onClick:()=>j(s=>s.size===c.length?new Set:new Set(c.map(a=>a.site))),className:"font-semibold hover:text-foreground",style:{color:"#174E76"},children:b.size===c.length?"Collapse all":"Expand all"})]})]}),c.length===0?e.jsx("p",{className:"text-sm text-muted-foreground p-6 text-center",children:"No affiliate links yet."}):e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b border-border bg-muted/30 text-xs font-bold uppercase tracking-wide text-muted-foreground",children:[e.jsx("th",{className:"text-left px-4 py-3",children:"Site / link"}),e.jsx("th",{className:"text-left px-4 py-3",children:"Partner"}),e.jsx("th",{className:"text-left px-4 py-3",children:"Code"}),e.jsxs("th",{className:"text-right px-4 py-3",children:["Last ",r.days,"d"]}),e.jsx("th",{className:"text-right px-4 py-3",children:"All time"})]})}),e.jsx("tbody",{children:c.map(s=>{const a=b.has(s.site);return e.jsxs(C.Fragment,{children:[e.jsxs("tr",{className:"border-b border-border bg-muted/10 hover:bg-muted/30 cursor-pointer",onClick:()=>S(s.site),children:[e.jsx("td",{className:"px-4 py-3",colSpan:3,children:e.jsxs("div",{className:"flex items-center gap-2 font-bold",style:{color:"#174E76"},children:[a?e.jsx(P,{className:"w-4 h-4 flex-shrink-0"}):e.jsx(G,{className:"w-4 h-4 flex-shrink-0"}),s.site,e.jsxs("span",{className:"text-xs font-normal text-muted-foreground",children:[s.links.length," ",s.links.length===1?"link":"links"]})]})}),e.jsx("td",{className:"px-4 py-3 text-right font-bold",style:{color:"#174E76"}
1,children:s.clicksPeriod.toLocaleString()}),e.jsx("td",{className:"px-4 py-3 text-right font-bold text-muted-foreground",children:s.clicksAllTime.toLocaleString()})]}),a&&s.links.map(t=>e.jsxs("tr",{className:"border-b border-border last:border-0 hover:bg-muted/20",children:[e.jsxs("td",{className:"px-4 py-3 pl-10",children:[e.jsxs("code",{className:`font-mono ${t.active?"":"line-through text-muted-foreground"}`,children:["/go/",t.slug]}),t.destination&&e.jsx("a",{href:t.destination,target:"_blank",rel:"noopener noreferrer",className:"block text-xs text-muted-foreground truncate max-w-[260px] hover:text-foreground",title:t.destination,children:t.destination.replace(/^https?:\/\//,"")})]}),e.jsx("td",{className:"px-4 py-3 font-medium",children:t.partner}),e.jsx("td",{className:"px-4 py-3",children:t.discountCode?e.jsx("code",{className:"px-1.5 py-0.5 rounded bg-amber-50 text-amber-900 text-xs font-bold",children:t.discountCode}):e.jsx("span",{className:"text-muted-foreground/50",children:"â"})}),e.jsx("td",{className:"px-4 py-3 text-right font-bold",style:{color:"#174E76"},children:t.clicksPeriod.toLocaleString()}),e.jsx("td",{className:"px-4 py-3 text-right text-muted-foreground",children:t.clicksAllTime.toLocaleString()})]},t.slug))]},s.site)})})]})})]}),e.jsx(f,{title:"Recent Activity â all links",empty:"No clicks recorded yet.",Icon:y,children:r.recent.map(s=>e.jsx(u,{left:e.jsxs("span",{className:"flex items-center gap-2 min-w-0",children:[e.jsxs("code",{className:"font-mono text-xs flex-shrink-0",children:["/go/",s.slug]}),e.jsxs("span",{className:"text-muted-foreground truncate",children:["· ",s.partner]}),e.jsxs("span",{className:"text-xs text-muted-foreground/70 truncate hidden sm:inline",children:["· ",w(s.referer)]})]}),right:e.jsx("span",{className:"font-normal text-muted-foreground",children:new Date(s.ts).toLocaleString()})},s.id))})]}):e.jsx("div",{className:"text-center py-20 text-muted-foreground",children:"Failed to load affiliate report."})})]})}
1function p({color:l,Icon:o,value:i,label:x}){return e.jsxs("div",{className:"bg-white rounded-2xl border border-border p-5",children:[e.jsx("div",{className:"flex items-center justify-between mb-3",children:e.jsx("div",{className:"w-10 h-10 rounded-xl flex items-center justify-center",style:{backgroundColor:l},children:e.jsx(o,{className:"w-5 h-5 text-white"})})}),e.jsx("p",{className:"text-3xl font-bold",style:{color:l},children:(i||0).toLocaleString()}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:x})]})}function f({title:l,children:o,empty:i,Icon:x,className:r=""}){const h=C.Children.toArray(o);return e.jsxs("div",{className:`bg-white rounded-2xl border border-border overflow-hidden ${r}`,children:[e.jsxs("div",{className:"px-5 py-3 border-b border-border flex items-center gap-2",style:{backgroundColor:"#fafaf8"},children:[x&&e.jsx(x,{className:"w-4 h-4",style:{color:"#174E76"}}),e.jsx("h3",{className:"font-bold text-sm",style:{color:"#174E76"},children:l})]}),h.length===0?e.jsx("p",{className:"text-sm text-muted-foreground p-6 text-center",children:i}):e.jsx("div",{className:"divide-y divide-border",children:h})]})}function u({left:l,right:o}){return e.jsxs("div",{className:"px-5 py-3 flex items-center justify-between gap-4 text-sm",children:[e.jsx("div",{className:"min-w-0 flex-1 truncate text-foreground",children:l}),e.jsx("div",{className:"text-xs font-bold flex-shrink-0",style:{color:"#174E76"},children:o})]})}export{ae as default};
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.