PageSourceSearch

https://chartmentor.io/assets/AdminOverview-CcQ-pByf.js

js chartmentor.io collected 2026-10-02 01:57:40 UTC 42,509 bytes, 3 lines download raw bytes

1import{r as l,j as e}from"./react-vendor-CdbEkvHB.js";import{T as ns,a as rs,b as $e,c as Ie}from"./tabs-BnaPPXWd.js";import{f as ls,n as y,L as de,B as $}from"./index-D5ldX8nG.js";import{C as u,d as f,a as I,b as O,c as F}from"./card-heWL1pcs.js";import{B as U}from"./badge-CrK0__Ya.js";import{I as ie}from"./input-CgvMgJmt.js";import{T as ge,a as ve,b as X,c as g,d as Ne,e as v}from"./table-DvEYLw_y.js";import{R as we,l as We,ae as oe,G as ce,a5 as is,Q as Oe,Z as be,O as se,ai as He,D as Ge,aj as cs,m as Y,L as Fe,ak as ze,a6 as ye,A as Ze,$ as je,al as Be,I as ds,e as os}from"./icons-D5q0hyss.js";import{L as xs}from"./label-BDZC01Zc.js";import{S as ms}from"./switch-ftUvet5y.js";import"./radix-ui-CSxGpBRr.js";import"./supabase-CxM-nLeM.js";const re=a=>new Intl.NumberFormat("en-US",{style:"currency",currency:"USD"}).format(a);function hs(){const[a,x]=l.useState(null),[o,p]=l.useState(!0),[N,w]=l.useState(!1),[R,_]=l.useState(""),[r,m]=l.useState(null),{toast:S}=ls(),A=async(s={})=>{s.silent||p(!0);try{let{data:h}=await y.auth.getSession();const i=h.session?.expires_at??0;(!h.session||i*1e3<Date.now()+3e4)&&(h={session:(await y.auth.refreshSession()).data.session});const L=h.session?.access_token;if(!L)throw new Error("Your session expired. Please sign in again.");const{data:C,error:c}=await y.functions.invoke("revenue-stats",{headers:{Authorization:`Bearer ${L}`}});if(c)throw c;x(C),m(new Date)}catch(h){s.silent||S({title:"Error",description:h.message||"Failed to load revenue stats",variant:"destructive"})}finally{s.silent||p(!1)}},P=async()=>{w(!0);try{const{data:s,error:h}=await y.functions.invoke("backfill-stripe-customers");if(h)throw h;S({title:"Sync complete",description:`Scanned ${s.scanned}, matched ${s.matched}, updated ${s.updated}.`}),await A()}catch(s){S({title:"Sync failed",description:s.message,variant:"destructive"})}finally{w(!1)}};l.useEffect(()=>{A();const s=window.setInterval(()=>{A({silent:!0})},3e4),h=()=>{document.visibilityState==="visible"&&A({silent:!0})};document.addEventListener("visibilitychange",h);let i;const L=()=>{i&&window.clearTimeout(i),i=window.setTimeout(()=>A({silent:!0}),800)},C=y.channel("admin-revenue-payment-info").on("postgres_changes",{event:"*",schema:"public",table:"payment_info"},L).subscribe();return()=>{window.clearInterval(s),document.removeEventListener("visibilitychange",h),i&&window.clearTimeout(i),y.removeChannel(C)}},[]);const k=l.useMemo(()=>{if(!a)return[];const s=R.toLowerCase().trim();return s?a.converted.filter(h=>h.email.toLowerCase().includes(s)||(h.full_name?.toLowerCase().includes(s)??!1)):a.converted},[a,R]),b=()=>{if(!a)return;const h=[["email","full_name","plan_tier","status","monthly_value","started_at","current_period_end"],...a.converted.map(c=>[c.email,c.full_name??"",c.plan_tier??"",c.status??"",String(c.monthly_value),c.started_at??"",c.current_period_end??""])].map(c=>c.map(j=>`"${String(j).replace(/"/g,'""')}"`).join(",")).join(`
2`),i=new Blob([h],{type:"text/csv"}),L=URL.createObjectURL(i),C=document.createElement("a");C.href=L,C.download=`converted-users-${new Date().toISOString().split("T")[0]}.csv`,C.click(),URL.revokeObjectURL(L)},E=s=>s==="elite"?e.jsx(se,{className:"h-3 w-3 mr-1"}):s==="pro"?e.jsx(be,{className:"h-3 w-3 mr-1"}):s==="starter"?e.jsx(Oe,{className:"h-3 w-3 mr-1"}):null,n=s=>s==="cancel_confirmed"?"Canceled":s==="cancel_intent_started"?"Cancel intent":s==="save_offer_accepted"?"Saved":"Feature answer",z=s=>s==="too_expensive"?"Too expensive":s==="not_using"?"Not using enough":s==="missing_feature"?"Missing feature":s==="other"?"Other":s||"Unknown";return o&&!a?e.jsx("div",{className:"flex items-center justify-center py-12",children:e.jsx(de,{className:"h-6 w-6 text-primary"})}):e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex flex-col sm:flex-row sm:items-center sm:justify-between gap-3",children:[e.jsxs("div",{children:[e.jsx("h2",{className:"text-2xl font-bold",children:"Revenue"}),e.jsxs("p",{className:"text-muted-foreground text-sm",children:["Renewing MRR, ARR and active subscriptions",r?` · updated ${r.toLocaleTimeString()}`:""]})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsxs($,{variant:"outline",size:"sm",onClick:()=>A(),disabled:o,children:[e.jsx(we,{className:`h-4 w-4 mr-2 ${o?"animate-spin":""}`}),"Refresh"]}),e.jsxs($,{size:"sm",onClick:P,disabled:N,children:[N?e.jsx(de,{className:"h-4 w-4 mr-2"}):e.jsx(we,{className:"h-4 w-4 mr-2"}),"Sync from Stripe"]})]})]}),a&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-3",children:[e.jsx(u,{children:e.jsxs(f,{className:"pt-4",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[e.jsx("div",{className:"p-1.5 bg-green-500/10 rounded",children:e.jsx(We,{className:"h-4 w-4 text-green-500"})}),e.jsx("span",{className:"text-xs text-muted-foreground",children:"MRR"})]}),e.jsx("p",{className:"text-2xl font-bold",children:re(a.mrr)}),e.jsxs("p",{className:"text-xs text-muted-foreground mt-1",children:[a.activeSubs," renewing, ",a.trialingSubs," trialing",a.endingSubs?`, ${a.endingSubs} ending`:""]}),typeof a.committedMrr=="number"&&a.committedMrr>a.mrr&&e.jsxs("p",{className:"text-xs text-muted-foreground mt-1",children:["Committed this cycle:"," ",e.jsx("span",{className:"font-semibold text-foreground",children:re(a.committedMrr)})]})]})}),e.jsx(u,{children:e.jsxs(f,{className:"pt-4",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[e.jsx("div",{className:"p-1.5 bg-blue-500/10 rounded",children:e.jsx(oe,{className:"h-4 w-4 text-blue-500"})}),e.jsx("span",{className:"text-xs text-muted-foreground",children:"ARR"})]}),e.jsx("p",{className:"text-2xl font-bold",children:re(a.arr)}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"Renewing MRR x 12"})]})}),e.jsx(u,{children:e.jsxs(f,{className:"pt-4",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[e.jsx("div",{className:"p-1.5 bg-vi
2olet-500/10 rounded",children:e.jsx(ce,{className:"h-4 w-4 text-violet-500"})}),e.jsx("span",{className:"text-xs text-muted-foreground",children:"New paid (30d)"})]}),e.jsx("p",{className:"text-2xl font-bold",children:a.new30d}),e.jsxs("p",{className:"text-xs text-muted-foreground mt-1",children:[a.new7d," in last 7 days"]})]})}),e.jsx(u,{children:e.jsxs(f,{className:"pt-4",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[e.jsx("div",{className:"p-1.5 bg-red-500/10 rounded",children:e.jsx(is,{className:"h-4 w-4 text-red-500"})}),e.jsx("span",{className:"text-xs text-muted-foreground",children:"Churn (30d)"})]}),e.jsx("p",{className:"text-2xl font-bold",children:a.churn30d}),e.jsxs("p",{className:"text-xs text-muted-foreground mt-1",children:[a.pastDue," past due"]})]})})]}),e.jsxs(u,{children:[e.jsxs(I,{children:[e.jsx(O,{children:"Active subscribers by tier"}),e.jsx(F,{children:"Renewing active + trialing"})]}),e.jsx(f,{children:e.jsxs("div",{className:"grid grid-cols-3 gap-3",children:[e.jsxs("div",{className:"rounded-lg border p-3",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[e.jsx(Oe,{className:"h-4 w-4 text-green-500"}),e.jsx("span",{className:"text-sm",children:"Starter"})]}),e.jsx("p",{className:"text-2xl font-bold",children:a.tierCounts.starter??0})]}),e.jsxs("div",{className:"rounded-lg border p-3",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[e.jsx(be,{className:"h-4 w-4 text-blue-500"}),e.jsx("span",{className:"text-sm",children:"Pro"})]}),e.jsx("p",{className:"text-2xl font-bold",children:a.tierCounts.pro??0})]}),e.jsxs("div",{className:"rounded-lg border p-3",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[e.jsx(se,{className:"h-4 w-4 text-amber-500"}),e.jsx("span",{className:"text-sm",children:"Elite"})]}),e.jsx("p",{className:"text-2xl font-bold",children:a.tierCounts.elite??0})]})]})})]}),e.jsxs(u,{children:[e.jsx(I,{children:e.jsxs("div",{className:"flex flex-col sm:flex-row sm:items-center sm:justify-between gap-3",children:[e.jsxs("div",{children:[e.jsx(O,{children:"Current customers"}),e.jsxs(F,{children:[a.converted.length," current customer records"]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs("div",{className:"relative",children:[e.jsx(He,{className:"absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground"}),e.jsx(ie,{placeholder:"Search email or name...",value:R,onChange:s=>_(s.target.value),className:"pl-9 w-64"})]}),e.jsxs($,{variant:"outline",size:"sm",onClick:b,children:[e.jsx(Ge,{className:"h-4 w-4 mr-2"}),"CSV"]})]})]})}),e.jsx(f,{children:k.length===0?e.jsx("div",{className:"text-center py-8 text-muted-foreground text-sm",children:'No paying customers yet. Click "Sync from Stripe" to backfill.'}):e.jsx("div",{className:"overflow-x-auto",children:e.jsxs(ge,{children:[e.jsx(ve,{children:e.jsxs(X,{children:[e.jsx(g,{children:"User"}),e.jsx(g,{children:"Plan"}),e.jsx(g,{children:"Status"}),e.jsx(g,{children:"Monthly"}),e.jsx(g,{children:"Started"}),e.jsx(g,{children:"Renews"})]})}),e.jsx(Ne,{children:k.map(s=>e.jsxs(X,{children:[e.jsx(v,{children:e.jsxs("div",{children:[e.jsx("p",{className:"font-medium",children:s.full_name||"—"}),e.jsx("p",{className:"text-xs text-muted-foreground",children:s.email})]})}),e.jsx(v,{children:e.jsxs(U,{variant:"secondary",className:"capitalize",children:[E(s.plan_tier),s.plan_tier??"—"]})}),e.jsx(v,{children:e.jsx(U,{variant:s.status==="active"?"default":s.status==="trialing"?"secondary":"outline",children:s.status??"—"})}),e.jsx(v,{children:re(s.monthly_value)}),e.jsx(v,{className:"text-xs text-muted-foreground",children:s.started_at?new Date(s.started_at).toLocaleDateString():"—"}),e.jsxs(v,{className:"text-xs text-muted-foreground",children:[s.current_period_end?new Date(s.current_period_end).toLocaleDateString():"—",s.cancel_at_period_end&&e.jsxs(U,{variant:"outline",className:"ml-2",children:["ends"," ",s.current_period_end?new Date(s.current_period_end).toLocaleDateString():""]})]})]},s.id))})]})})})]}),e.jsxs(u,{children:[e.jsxs(I,{children:[e.jsxs(O,{className:"flex items-center gap-2",children:[e.jsx(cs,{className:"h-4 w-4 text-muted-foreground"}),"Cancellation feedback"]}),e.jsx(F,{children:"Answers from the cancel questionnaire, save offers, and confirmed cancellations"})]}),e.jsx(f,{children:(a.churnFeedback||[]).length===0?e.jsx("div",{className:"text-center py-8 text-muted-foreground text-sm",children:"No cancellation feedback recorded yet."}):e.jsx("div",{className:"overflow-x-auto",children:e.jsxs(ge,{children:[e.jsx(ve,{children:e.jsxs(X,{children:[e.jsx(g,{children:"User"}),e.jsx(g,{children:"Stage"}),e.jsx(g,{children:"Reason"}),e.jsx(g,{children:"Offer"}),e.jsx(g,{children:"Answer"}),e.jsx(g,{children:"Date"})]})}),e.jsx(Ne,{children:(a.churnFeedback||[]).map(s=>e.jsxs(X,{children:[e.jsx(v,{children:e.jsxs("div",{children:[e.jsx("p",{className:"font-medium",children:s.full_name||"—"}),e.jsx("p",{className:"text-xs text-muted-foreground",children:s.email||s.user_id||"Unknown user"})]})}),e.jsx(v,{children:e.jsx(U,{variant:s.event_name==="cancel_confirmed"?"destructive":s.event_name==="save_offer_accepted"?"default":"secondary",children:n(s.event_name)})}),e.jsx(v,{className:"text-sm",children:z(s.reason)}),e.jsx(v,{className:"text-xs text-muted-foreground",children:s.offer||"—"}),e.jsx(v,{className:"max-w-sm text-sm text-muted-foreground",children:e.jsx("span",{className:"line-clamp-3",children:s.answer||"—"})}),e.jsx(v,{className:"text-xs text-muted-foreground",children:s.created_at?new Date(s.created_at).toLocaleDateString():"—"})]},`${s.event_name}-${s.id}`))})]})})})]})]})]})}function us({metrics:a}){const x=a.cancelIntents,o=a.saveOfferShown,p=a.saveOfferAccepted,N=a.cancelConfirmed,w=o>0?Math.round(p/o*100):0,R=o>0?Math.round(N/o*100):0,_=m=>x>0?Math.round(m/x*100):0,r=[{name:"Cancel intent",count:x,rate:100,icon:e.jsx(Fe,{className:"h-4 w-4"}),color:"bg-orange-500"},{name:"Offer shown",count:o,rate:_(o),icon:e.jsx(ze,{className:"h-4 w-4"}
2),color:"bg-amber-500"},{name:"Saved (offer accepted)",count:p,rate:_(p),icon:e.jsx(Y,{className:"h-4 w-4"}),color:"bg-green-500"},{name:"Lost (cancel confirmed)",count:N,rate:_(N),icon:e.jsx(ye,{className:"h-4 w-4"}),color:"bg-red-500"}];return e.jsxs(u,{className:"border-border bg-card",children:[e.jsxs(I,{children:[e.jsxs(O,{className:"flex items-center gap-2",children:[e.jsx(Y,{className:"h-4 w-4 text-green-500"}),"Save Funnel"]}),e.jsx(F,{children:"Cancellation intent → offer → outcome"})]}),e.jsxs(f,{className:"space-y-6",children:[e.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-3",children:[e.jsx(le,{label:"Cancel intents",value:x,icon:e.jsx(Fe,{})}),e.jsx(le,{label:"Offer shown",value:o,icon:e.jsx(ze,{})}),e.jsx(le,{label:"Saved",value:p,icon:e.jsx(Y,{}),tone:"success"}),e.jsx(le,{label:"Lost",value:N,icon:e.jsx(ye,{}),tone:"danger"})]}),e.jsx("div",{className:"space-y-3",children:r.map((m,S)=>e.jsxs("div",{className:"space-y-1",children:[e.jsxs("div",{className:"flex items-center justify-between text-sm",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("div",{className:`p-1 rounded ${m.color}/10 text-muted-foreground`,children:m.icon}),e.jsx("span",{className:"font-medium",children:m.name})]}),e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("span",{className:"text-muted-foreground",children:m.count}),e.jsxs("span",{className:"text-xs text-muted-foreground",children:[m.rate,"%"]})]})]}),e.jsx("div",{className:"h-2 bg-muted rounded-full overflow-hidden",children:e.jsx("div",{className:`h-full ${m.color}`,style:{width:`${m.rate}%`}})}),S<r.length-1&&e.jsx("div",{className:"flex justify-end py-1",children:e.jsx(Ze,{className:"h-3 w-3 text-muted-foreground/50"})})]},m.name))}),e.jsxs("div",{className:"grid grid-cols-2 gap-4 pt-4 border-t text-center",children:[e.jsx(Qe,{value:w,label:"Save rate (accepted ÷ shown)",tone:"success"}),e.jsx(Qe,{value:R,label:"Loss rate (confirmed ÷ shown)",tone:"danger"})]}),x===0&&e.jsx("p",{className:"text-xs text-muted-foreground text-center pt-2",children:"No cancellation activity in this date range yet."})]})]})}function le({label:a,value:x,icon:o,tone:p}){const N=p==="success"?"border-green-500/30 bg-green-500/5":p==="danger"?"border-red-500/30 bg-red-500/5":"border-border",w=p==="success"?"text-green-600":p==="danger"?"text-red-600":"";return e.jsxs("div",{className:`rounded-lg border p-3 ${N}`,children:[e.jsxs("div",{className:"flex items-center gap-2 mb-1 text-muted-foreground [&_svg]:h-3.5 [&_svg]:w-3.5",children:[o,e.jsx("span",{className:"text-xs",children:a})]}),e.jsx("p",{className:`text-xl font-bold ${w}`,children:x})]})}function Qe({value:a,label:x,tone:o}){return e.jsxs("div",{children:[e.jsxs("p",{className:`text-3xl font-bold ${o==="success"?"text-green-600":"text-red-600"}`,children:[a,"%"]}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:x})]})}const Q=(a,x)=>x===0?0:Math.round(a/x*100);function fs({activationStages:a,adminCount:x,conversions:o,dateRange:p,engagedUsers:N,excludeAdmins:w,fetchFunnelMetrics:R,loading:_,metrics:r,pqlCandidates:m,setDateRange:S,setExcludeAdmins:A}){const P=r?[{name:"Signups",count:r.totalSignups,rate:100,icon:e.jsx(ce,{className:"h-4 w-4"}),color:"bg-blue-500"},{name:"First Analysis",count:r.firstAnalysis,rate:Q(r.firstAnalysis,r.totalSignups),icon:e.jsx(je,{className:"h-4 w-4"}),color:"bg-violet-500"},{name:"Engaged",count:r.engagedUsers,rate:Q(r.engagedUsers,r.totalSignups),icon:e.jsx(Y,{className:"h-4 w-4"}),color:"bg-emerald-500"},{name:"Checkout",count:r.checkoutSessionsCreated,rate:Q(r.checkoutSessionsCreated,r.totalSignups),icon:e.jsx(se,{className:"h-4 w-4"}),color:"bg-purple-500"},{name:"Converted",count:r.paidUsers,rate:Q(r.paidUsers,r.totalSignups),icon:e.jsx(oe,{className:"h-4 w-4"}),color:"bg-green-500"}]:[],k=w?o.filter(n=>!n.is_admin):o,b=w?N.filter(n=>!n.is_admin):N,E=w?m.filter(n=>!n.is_admin):m;return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex items-center justify-between flex-wrap gap-3",children:[e.jsxs("div",{children:[e.jsx("h2",{className:"text-2xl font-bold",children:"Conversion Funnel"}),e.jsx("p",{className:"text-muted-foreground",children:"Track stage transitions from signup to paid conversion"})]}),e.jsxs("div",{className:"flex items-center gap-3 flex-wrap",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(ms,{id:"exclude-admins",checked:w,onCheckedChange:A}),e.jsx(xs,{htmlFor:"exclude-admins",className:"text-sm cursor-pointer",children:"Exclude admins"})]}),e.jsx("div",{className:"flex gap-2",children:["7d","30d","90d","all"].map(n=>e.jsx(U,{variant:p===n?"default":"outline",className:"cursor-pointer",onClick:()=>S(n),children:n==="all"?"All Time":n.toUpperCase()},n))}),e.jsxs($,{size:"sm",variant:"outline",onClick:R,disabled:_,children:[e.jsx(we,{className:`h-3.5 w-3.5 mr-1.5 ${_?"animate-spin":""}`}),"Refresh"]})]})]}),r&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-3 lg:grid-cols-6 gap-3",children:[e.jsx(u,{className:"border-border bg-card",children:e.jsxs(f,{className:"pt-4",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[e.jsx("div",{className:"p-1.5 bg-blue-500/10 rounded",children:e.jsx(ce,{className:"h-4 w-4 text-blue-500"})}),e.jsx("span",{className:"text-xs text-muted-foreground",children:"Signups"})]}),e.jsx("p",{className:"text-2xl font-bold",children:r.totalSignups})]})}),e.jsx(u,{className:"border-border bg-card",children:e.jsxs(f,{className:"pt-4",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[e.jsx("div",{className:"p-1.5 bg-vi
2olet-500/10 rounded",children:e.jsx(je,{className:"h-4 w-4 text-violet-500"})}),e.jsx("span",{className:"text-xs text-muted-foreground",children:"First Analysis"})]}),e.jsxs("p",{className:"text-2xl font-bold",children:[r.firstAnalysis,e.jsxs("span",{className:"text-sm font-normal text-muted-foreground ml-1",children:["(",Q(r.firstAnalysis,r.totalSignups),"%)"]})]})]})}),e.jsx(u,{className:"border-border bg-card",children:e.jsxs(f,{className:"pt-4",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[e.jsx("div",{className:"p-1.5 bg-emerald-500/10 rounded",children:e.jsx(Y,{className:"h-4 w-4 text-emerald-500"})}),e.jsx("span",{className:"text-xs text-muted-foreground",children:"Engaged"})]}),e.jsxs("p",{className:"text-2xl font-bold",children:[r.engagedUsers,e.jsxs("span",{className:"text-sm font-normal text-muted-foreground ml-1",children:["(",Q(r.engagedUsers,r.usersWithReviews),"%)"]})]})]})}),e.jsx(u,{className:"border-border bg-card",children:e.jsxs(f,{className:"pt-4",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[e.jsx("div",{className:"p-1.5 bg-purple-500/10 rounded",children:e.jsx(se,{className:"h-4 w-4 text-purple-500"})}),e.jsx("span",{className:"text-xs text-muted-foreground",children:"Paid"})]}),e.jsxs("p",{className:"text-2xl font-bold",children:[r.paidUsers,e.jsxs("span",{className:"text-sm font-normal text-muted-foreground ml-1",children:["(",Q(r.paidUsers,r.totalSignups),"%)"]})]})]})})]}),e.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-5 gap-3",children:[e.jsx(u,{className:"border-border bg-card",children:e.jsxs(f,{className:"pt-4",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[e.jsx(ce,{className:"h-3 w-3 text-emerald-500"}),e.jsx("span",{className:"text-xs text-muted-foreground",children:"Users With Reviews"})]}),e.jsx("p",{className:"text-xl font-bold",children:r.usersWithReviews})]})}),e.jsx(u,{className:"border-border bg-card",children:e.jsxs(f,{className:"pt-4",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[e.jsx(Be,{className:"h-3 w-3 text-orange-500"}),e.jsx("span",{className:"text-xs text-muted-foreground",children:"Checkout Sessions"})]}),e.jsx("p",{className:"text-xl font-bold",children:r.checkoutSessionsCreated})]})}),e.jsx(u,{className:"border-border bg-card",children:e.jsxs(f,{className:"pt-4",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[e.jsx(oe,{className:"h-3 w-3 text-green-500"}),e.jsx("span",{className:"text-xs text-muted-foreground",children:"Tracked Conversions"})]}),e.jsx("p",{className:"text-xl font-bold",children:r.paidUsers})]})}),e.jsx(u,{className:"border-border bg-card",children:e.jsxs(f,{className:"pt-4",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[e.jsx(je,{className:"h-3 w-3 text-cyan-500"}),e.jsx("span",{className:"text-xs text-muted-foreground",children:"Total Analyses"})]}),e.jsx("p",{className:"text-xl font-bold",children:r.totalAnalyses})]})}),e.jsx(u,{className:"border-border bg-card",children:e.jsxs(f,{className:"pt-4",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[e.jsx(Be,{className:"h-3 w-3 text-red-500"}),e.jsx("span",{className:"text-xs text-muted-foreground",children:"One And Done"})]}),e.jsx("p",{className:"text-xl font-bold",children:r.oneAndDoneUsers})]})})]}),e.jsxs(u,{className:"border-border bg-card",children:[e.jsxs(I,{children:[e.jsx(O,{children:"Stage Conversion Rates"}),e.jsx(F,{children:"Signup → first analysis → checkout → paid"})]}),e.jsxs(f,{className:"space-y-3",children:[P.map((n,z)=>e.jsxs("div",{className:"space-y-1",children:[e.jsxs("div",{className:"flex items-center justify-between text-sm",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("div",{className:`p-1 rounded ${n.color}/10 text-muted-foreground`,children:n.icon}),e.jsx("span",{className:"font-medium",children:n.name})]}),e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("span",{className:"text-muted-foreground",children:n.count}),e.jsxs("span",{className:"text-xs text-muted-foreground",children:[n.rate,"%"]})]})]}),e.jsx("div",{className:"h-2 bg-muted rounded-full overflow-hidden",children:e.jsx("div",{className:`h-full ${n.color}`,style:{width:`${n.rate}%`}})}),z<P.length-1&&e.jsx("div",{className:"flex justify-end py-1",children:e.jsx(Ze,{className:"h-3 w-3 text-muted-foreground
2/50"})})]},n.name)),e.jsx("div",{className:"mt-4 pt-4 border-t",children:e.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-3 gap-4 text-center",children:[e.jsxs("div",{children:[e.jsxs("p",{className:"text-2xl font-bold text-green-600",children:[r.totalSignups>0?Math.round(r.firstAnalysis/r.totalSignups*100):0,"%"]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Signup → first analysis"})]}),e.jsxs("div",{children:[e.jsxs("p",{className:"text-2xl font-bold text-green-600",children:[r.checkoutSessionsCreated>0?Math.round(r.paidUsers/r.checkoutSessionsCreated*100):0,"%"]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Checkout completion rate"})]}),e.jsxs("div",{children:[e.jsxs("p",{className:"text-2xl font-bold text-blue-600",children:[Q(r.paidUsers,r.totalSignups),"%"]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Signup → paid rate"})]})]})})]})]}),a&&e.jsxs(u,{className:"border-border bg-card",children:[e.jsxs(I,{children:[e.jsxs(O,{className:"flex items-center gap-2",children:[e.jsx(be,{className:"h-4 w-4 text-amber-500"}),"Activation and PQL stages"]}),e.jsx(F,{children:"Lightweight upgrade-readiness from scans, Strategy Lens, checkout intent and limit events"})]}),e.jsxs(f,{className:"space-y-5",children:[e.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 lg:grid-cols-7 gap-3 text-center",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-xl font-bold",children:a.signupOnly}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Signup only"})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xl font-bold text-cyan-600",children:a.lensNeedsReview}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Lens, no review"})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xl font-bold text-blue-600",children:a.activated}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Activated"})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xl font-bold text-emerald-600",children:a.repeatReviewer}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Repeat"})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xl font-bold text-amber-600",children:a.upgradeReady}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Upgrade-ready"})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xl font-bold text-purple-600",children:a.checkoutIntent}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Checkout intent"})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xl font-bold text-green-600",children:a.paid}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Paid"})]})]}),E.length===0?e.jsx("p",{className:"text-sm text-muted-foreground text-center py-4",children:"No unpaid upgrade-ready candidates in this date range yet."}):e.jsx("div",{className:"space-y-2",children:E.slice(0,10).map(n=>e.jsxs("div",{className:"flex items-center justify-between text-sm p-2 rounded hover:bg-muted/40",children:[e.jsxs("div",{className:"flex items-center gap-2 min-w-0",children:[e.jsx("span",{className:"truncate",children:n.email}),e.jsx(U,{variant:"secondary",className:"text-[10px] h-5",children:n.stage}),n.is_admin&&e.jsx(U,{variant:"outline",className:"text-[10px] h-4",children:"admin"})]}),e.jsxs("div",{className:"flex items-center gap-3 text-xs text-muted-foreground shrink-0",children:[e.jsx(U,{variant:"outline",children:n.score}),e.jsxs("span",{children:[n.reviewCount," reviews"]}),e.jsxs("span",{children:[n.reviewDays," days"]}),e.jsx("span",{children:n.lastReviewAt?new Date(n.lastReviewAt).toLocaleDateString():"no review"})]})]},n.user_id))})]})]}),e.jsxs(u,{className:"border-border bg-card",children:[e.jsxs(I,{children:[e.jsxs(O,{className:"flex items-center gap-2",children:[e.jsx(Y,{className:"h-4 w-4 text-emerald-500"}),"Engagement quality"]}),e.jsx(F,{children:"Engaged means 2+ reviews across 2+ separate days, with the latest review in the selected date range"})]}),e.jsxs(f,{className:"space-y-5",children:[e.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4 text-center",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-2xl font-bold text-emerald-600",children:r.engagedUsers}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Engaged users"})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-2xl font-bold text-blue-600",children:r.repeatReviewUsers}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"2+ review users"})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-2xl font-bold text-orange-600",children:r.oneAndDoneUsers}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Tried once only"})]}),e.jsxs("div",{children:[e.jsxs("p",{className:"text-2xl font-bold text-emerald-600",children:[Q(r.engagedUsers,r.usersWithReviews),"%"]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Engaged / reviewers"})]})]}),b.length===0?e.jsx("p",{className:"text-sm text-muted-foreground text-center py-4",children:"No engaged users in this date range yet."}):e.jsx("div",{className:"space-y-2",children:b.slice(0,12).map(n=>e.jsxs("div",{className:"flex items-center justify-between text-sm p-2 rounded hover:bg-muted/40",children:[e.jsxs("div",{className:"flex items-center gap-2 min-w-0",children:[e.jsx("span",{className:"truncate",children:n.email}),n.is_admin&&e.jsx(U,{variant:"outline",className:"text-[10px] h-4",children:"admin"})]}),e.jsxs("div",{className:"flex items-center gap-3 text-xs text-muted-foreground shrink-0",children:[e.jsx(U,{variant:"secondary",className:"capitalize",children:n.plan_tier||"free"}),e.jsxs("span",{children:[n.reviewCount," reviews"]}),e.jsxs("span",{children:[n.reviewDays," days"]}),e.jsx("span",{children:new Date(n.lastReviewAt).toLocaleDateString()})]})]}
2,n.user_id))})]})]}),e.jsxs(u,{className:"border-border bg-card",children:[e.jsxs(I,{children:[e.jsxs(O,{className:"flex items-center gap-2",children:[e.jsx(se,{className:"h-4 w-4 text-purple-500"}),"Conversions detail"]}),e.jsxs(F,{children:[k.length," conversion",k.length===1?"":"s",w&&x>0&&` (${x} admin hidden)`]})]}),e.jsx(f,{children:k.length===0?e.jsx("p",{className:"text-sm text-muted-foreground text-center py-6",children:"No conversions in this date range."}):e.jsx("div",{className:"space-y-2",children:k.map(n=>e.jsxs("div",{className:"flex items-center justify-between text-sm p-2 rounded hover:bg-muted/40",children:[e.jsxs("div",{className:"flex items-center gap-2 min-w-0",children:[e.jsx("span",{className:"truncate",children:n.email}),n.is_admin&&e.jsx(U,{variant:"outline",className:"text-[10px] h-4",children:"admin"})]}),e.jsxs("div",{className:"flex items-center gap-3 text-xs text-muted-foreground shrink-0",children:[e.jsx(U,{variant:"secondary",className:"capitalize",children:n.plan_tier||"—"}),e.jsx("span",{children:n.created_at?new Date(n.created_at).toLocaleDateString():"—"})]})]},n.user_id))})})]}),e.jsx(us,{metrics:r})]})]})}function ps(){const[a,x]=l.useState(null),[o,p]=l.useState([]),[N,w]=l.useState([]),[R,_]=l.useState(null),[r,m]=l.useState([]),[S,A]=l.useState(0),[P,k]=l.useState(!0),[b,E]=l.useState("30d"),[n,z]=l.useState(!0),s=l.useCallback(()=>{const i=new Date;return b==="7d"?new Date(i.getTime()-7*864e5).toISOString():b==="30d"?new Date(i.getTime()-30*864e5).toISOString():b==="90d"?new Date(i.getTime()-90*864e5).toISOString():null},[b]),h=l.useCallback(async()=>{k(!0);try{const i=s(),{data:L}=await y.from("user_roles").select("user_id").eq("role","admin"),C=new Set((L||[]).map(t=>t.user_id));let c=y.from("profiles").select("id, email, created_at, plan_tier, subscription_status, system_prompt").order("created_at",{ascending:!1});i&&(c=c.gte("created_at",i));const{data:j,error:M}=await c;if(M)throw M;const T=(j||[]).filter(t=>!n||!C.has(t.id)),H=T.length,V=T.filter(t=>t.plan_tier==="free").length;let D=y.from("payment_info").select("id, plan_tier, status, created_at").eq("status","active");i&&(D=D.gte("created_at",i));const{data:_e}=await D,te=new Map((j||[]).map(t=>[t.id,t])),Se=(_e||[]).map(t=>t.id).filter(t=>!te.has(t));let Ce=[];if(Se.length>0){const{data:t}=await y.from("profiles").select("id, email, plan_tier, subscription_status, system_prompt").in("id",Se);Ce=t||[]}const K=new Map;(j||[]).forEach(t=>K.set(t.id,t.email)),Ce.forEach(t=>{K.set(t.id,t.email),te.set(t.id,t)});const ae=(_e||[]).map(t=>({user_id:t.id,email:K.get(t.id)||"(unknown)",plan_tier:t.plan_tier,created_at:t.created_at||"",is_admin:C.has(t.id)})),Re=n?ae.filter(t=>!t.is_admin):ae,Ae=Re.length,qe=ae.filter(t=>t.is_admin).length;let xe=y.from("analysis_history").select("id",{count:"exact",head:!0});i&&(xe=xe.gte("created_at",i));const{count:Ke}=await xe,{data:Xe,error:ke}=await y.from("analysis_history").select("user_id, created_at").order("created_at",{ascending:!0});if(ke)throw ke;const ne=new Map;for(const t of Xe||[]){if(!t.user_id||!t.created_at||n&&C.has(t.user_id))continue;const d=ne.get(t.user_id),Z=new Date(t.created_at),ee=Z.toISOString().slice(0,10);if(!d){ne.set(t.user_id,{reviewCount:1,reviewDays:new Set([ee]),firstReviewAt:t.created_at,lastReviewAt:t.created_at});continue}d.reviewCount+=1,d.reviewDays.add(ee),new Date(d.firstReviewAt)>Z&&(d.firstReviewAt=t.created_at),new Date(d.lastReviewAt)<Z&&(d.lastReviewAt=t.created_at)}const J=Array.from(ne.entries()).filter(([,t])=>i?t.lastReviewAt>=i:!0),De=J.map(([t])=>t).filter(t=>!K.has(t));if(De.length>0){const{data:t}=await y.from("profiles").select("id, email, plan_tier").in("id",De);for(const d of t||[])K.set(d.id,d.email),te.set(d.id,d)}const Ue=J.filter(([,t])=>t.reviewCount>=2&&t.reviewDays.size>=2).map(([t,d])=>({user_id:t,email:K.get(t)||"(unknown)",reviewCount:d.reviewCount,reviewDays:d.reviewDays.size,firstReviewAt:d.firstReviewAt,lastReviewAt:d.lastReviewAt,plan_tier:te.get(t)?.plan_tier||null,is_admin:C.has(t)})).sort((t,d)=>new Date(d.lastReviewAt).getTime()-new Date(t.lastReviewAt).getTime()),Ye=J.length,Je=J.filter(([,t])=>t.reviewCount>=2).length,es=J.filter(([,t])=>t.reviewCount===1).length;let me=y.from("analytics_events").select("event_name, user_id, created_at");i&&(me=me.gte("created_at",i));const{data:ss}=await me,ts=(ss||[]).filter(t=>!n||!t.user_id||!C.has(t.user_id)),B={},he=new Map;for(const t of ts)if(B[t.event_name]=(B[t.event_name]||0)+1,t.user_id){const d=he.get(t.user_id)||new Set;d.add(t.event_name),he.set(t.user_id,d)}const as=new Set(Re.map(t=>t.user_id)),G={signupOnly:0,lensNeedsReview:0,activated:0,repeatReviewer:0,upgradeReady:0,checkoutIntent:0,paid:Ae},ue=[];for(const t of T){const d=ne.get(t.id),Z=d?.reviewCount||0,ee=d?.reviewDays.size||0,Le=Z>0,fe=Z>=2,pe=!!t.system_prompt?.trim(),Pe=he.get(t.id)||new Set,Ee=Pe.has("checkout_session_created"),Te=Pe.has("limit_reached"),Me=as.has(t.id);let q="Signup only",W=0;pe&&(W+=15),Le&&(W+=25),fe&&(W+=25),ee>=2&&(W+=15),Ee&&(W+=25),Te&&(W+=30),Me?q="Paid":Ee?(q="Checkout intent",G.checkoutIntent+=1):Te||fe&&pe?(q
2="Upgrade-ready",G.upgradeReady+=1):fe?(q="Repeat reviewer",G.repeatReviewer+=1):Le?(q="Activated",G.activated+=1):pe?(q="Lens set, needs review",G.lensNeedsReview+=1):G.signupOnly+=1,!Me&&W>=40&&ue.push({user_id:t.id,email:t.email,stage:q,score:Math.min(W,100),reviewCount:Z,reviewDays:ee,lastReviewAt:d?.lastReviewAt||null,plan_tier:t.plan_tier,is_admin:C.has(t.id)})}ue.sort((t,d)=>d.score-t.score||new Date(d.lastReviewAt||0).getTime()-new Date(t.lastReviewAt||0).getTime()),x({totalSignups:H,paidUsers:Ae,freeRetained:V,totalAnalyses:Ke??0,firstAnalysis:B.first_analysis_completed||0,usersWithReviews:Ye,engagedUsers:Ue.length,repeatReviewUsers:Je,oneAndDoneUsers:es,checkoutSessionsCreated:B.checkout_session_created||0,limitReached:B.limit_reached||0,cancelIntents:B.cancel_intent_started||0,saveOfferShown:B.save_offer_shown||0,saveOfferAccepted:B.save_offer_accepted||0,cancelConfirmed:B.cancel_confirmed||0}),p(ae),w(Ue),_(G),m(ue),A(qe)}catch(i){console.error("Failed to fetch funnel metrics:",i)}finally{k(!1)}},[s,n]);return l.useEffect(()=>{h()},[h]),P?e.jsx("div",{className:"flex items-center justify-center py-12",children:e.jsx(de,{className:"h-6 w-6 text-primary"})}):e.jsx(fs,{activationStages:R,adminCount:S,conversions:o,dateRange:b,engagedUsers:N,excludeAdmins:n,fetchFunnelMetrics:h,loading:P,metrics:a,pqlCandidates:r,setDateRange:E,setExcludeAdmins:z})}function Ve(a){if(!a)return"—";try{return new Date(a).toLocaleString()}catch{return a}}function js(a){return a===void 0?"$0.00":`$${(a/100).toFixed(2)}`}function gs({processingId:a,setProcessingId:x,actionType:o,setActionType:p,adminNotes:N,setAdminNotes:w,payoutReference:R,setPayoutReference:_,actionLoading:r,payoutRequests:m,filteredPayoutRequests:S,payoutStatusFilter:A,setPayoutStatusFilter:P,payoutSearch:k,setPayoutSearch:b,userLabel:E,handleProcessAction:n,exportVisiblePayouts:z}){return e.jsxs(e.Fragment,{children:[a&&e.jsxs(u,{className:"border-primary/20 bg-primary/5",children:[e.jsxs(I,{children:[e.jsx(O,{className:"text-lg font-bold",children:o==="pay"?"Approve & Mark Payout as Paid":"Reject Payout Request"}),e.jsxs(F,{children:["Processing request ",a," for"," ",E(m.find(s=>s.id===a)?.user_id||"")]})]}),e.jsxs(f,{className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"text-xs font-semibold",children:"Admin Notes / Reason"}),e.jsx(ie,{placeholder:"Optional notes visible to the user...",value:N,onChange:s=>w(s.target.value),className:"bg-background border-border"})]}),o==="pay"&&e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"text-xs font-semibold",children:"PayPal Transaction Reference"}),e.jsx(ie,{placeholder:"PayPal payout transaction ID or internal reference",value:R,onChange:s=>_(s.target.value),className:"bg-background border-border"})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsxs($,{variant:o==="pay"?"default":"destructive",onClick:n,disabled:r,children:[r?e.jsx(de,{className:"h-4 w-4 mr-2"}):null,"Confirm"," ",o==="pay"?"Payment Completed":"Rejection"]}),e.jsx($,{variant:"ghost",onClick:()=>{x(null),p(null),w(""),_("")},children:"Cancel"})]})]})]}),e.jsxs(u,{children:[e.jsxs(I,{children:[e.jsxs(O,{className:"text-lg flex items-center gap-2",children:[e.jsx(ds,{className:"h-5 w-5 text-primary"})," Affiliate Payout Requests"]}),e.jsx(F,{children:"Manual payout requests submitted by partners"})]}),e.jsx(f,{children:m.length===0?e.jsx("p",{className:"text-sm text-muted-foreground",children:"No payout requests submitted yet."}):e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex flex-col gap-3 lg:flex-row lg:items-center lg:justify-between",children:[e.jsx("div",{className:"flex flex-wrap gap-2",children:["all","requested","paid","rejected","canceled"].map(s=>e.jsx($,{type:"button",size:"sm",variant:A===s?"default":"outline",onClick:()=>P(s),children:s},s))}),e.jsxs("div",{className:"flex flex-col gap-2 sm:flex-row sm:items-center",children:[e.jsxs("div",{className:"relative",children:[e.jsx(He,{className:"pointer-events-none absolute left-2.5 top-2.5 h-4 w-4 text-muted-foreground"}),e.jsx(ie,{value:k,onChange:s=>b(s.target.value),placeholder:"Search partner, PayPal, notes",className:"pl-8 sm:w-[280px]"})]}),e.jsxs($,{type:"button",variant:"secondary",size:"sm",onClick:z,disabled:S.length===0,children:[e.jsx(Ge,{className:"mr-2 h-4 w-4"}),"Export CSV"]})]})]}),S.length===0?e.jsx("p",{className:"text-sm text-muted-foreground",children:"No payout requests match the current filters."}):e.jsxs(ge,{children:[e.jsx(ve,{children:e.jsxs(X,{children:[e.jsx(g,{children:"Date"}),e.jsx(g,{children:"Partner Email"}),e.jsx(g,{children:"PayPal Email"}),e.jsx(g,{children:"Payout Name"}),e.jsx(g,{className:"text-right",children:"Amount"}),e.jsx(g,{children:"Status"}),e.jsx(g,{children:"Processed At"}),e.jsx(g,{children:"Reference"}),e.jsx(g,{children:"Notes"}),e.jsx(g,{className:"text-right",children:"Actions"})]})}),e.jsx(Ne,{children:S.map(s=>e.jsxs(X,{children:[e.jsx(v,{className:"text-xs font-mono",children:Ve(s.created_at)}
2),e.jsx(v,{className:"font-mono text-xs",children:E(s.user_id)}),e.jsx(v,{className:"font-mono text-xs",children:s.paypal_email}),e.jsx(v,{className:"text-xs",children:s.payout_name||"—"}),e.jsx(v,{className:"text-right font-mono font-bold text-primary",children:js(s.amount_cents)}),e.jsx(v,{children:e.jsx(U,{variant:s.status==="paid"?"default":s.status==="requested"?"secondary":"destructive",children:s.status})}),e.jsx(v,{className:"text-xs font-mono",children:Ve(s.processed_at)}),e.jsx(v,{className:"text-xs text-muted-foreground max-w-[150px] truncate",title:s.payout_reference||"",children:s.payout_reference||"—"}),e.jsx(v,{className:"text-xs text-muted-foreground max-w-[150px] truncate",title:s.admin_notes||"",children:s.admin_notes||"—"}),e.jsx(v,{className:"text-right",children:s.status==="requested"?e.jsxs("div",{className:"flex justify-end gap-1.5",children:[e.jsxs($,{size:"sm",variant:"secondary",className:"h-7 text-xs flex items-center gap-1",onClick:()=>{x(s.id),p("pay")},children:[e.jsx(os,{className:"h-3.5 w-3.5 text-emerald-500"}),"Pay"]}),e.jsxs($,{size:"sm",variant:"outline",className:"h-7 text-xs flex items-center gap-1 text-destructive hover:bg-destructive/5",onClick:()=>{x(s.id),p("reject")},children:[e.jsx(ye,{className:"h-3.5 w-3.5 text-destructive"}),"Reject"]})]}):e.jsx("span",{className:"text-xs text-muted-foreground",children:"—"})})]},s.id))})]})]})})]})]})}function vs(){const[a,x]=l.useState([]),[o,p]=l.useState({}),[N,w]=l.useState(null),[R,_]=l.useState(null),[r,m]=l.useState(""),[S,A]=l.useState(""),[P,k]=l.useState(!1),[b,E]=l.useState("all"),[n,z]=l.useState(""),s=async()=>{const{data:c}=await y.from("affiliate_payout_requests").select("*").order("created_at",{ascending:!1}).limit(100),j=c||[];x(j);const M=new Set;if(j.forEach(T=>{M.add(T.user_id),T.processed_by&&M.add(T.processed_by)}),M.size){const{data:T}=await y.from("profiles").select("id, email, full_name").in("id",Array.from(M)),H={};(T||[]).forEach(V=>{H[V.id]=V}),p(H)}};l.useEffect(()=>{s()},[]);const h=async()=>{if(!N||!R)return;k(!0);const{error:c}=await y.rpc("process_affiliate_payout",{p_request_id:N,p_action:R,p_admin_notes:r.trim()||null,p_payout_reference:S.trim()||null});if(k(!1),c){alert(`Error processing payout: ${c.message}`);return}w(null),_(null),m(""),A(""),s()},i=l.useCallback(c=>{const j=o[c];return j?.email||j?.full_name||c.slice(0,8)},[o]),L=l.useMemo(()=>{const c=n.trim().toLowerCase();return a.filter(j=>b!=="all"&&j.status!==b?!1:c?[i(j.user_id),j.paypal_email,j.payout_name,j.payout_reference,j.admin_notes,j.status].filter(Boolean).join(" ").toLowerCase().includes(c):!0)},[a,n,b,i]),C=()=>{const c=["ID","User","Amount","Status","PayPal","Name","Reference","Requested At"],j=L.map(D=>[D.id,i(D.user_id),(D.amount_cents/100).toFixed(2),D.status,D.paypal_email||"",D.payout_name||"",D.payout_reference||"",D.created_at]),M=[c.join(","),...j.map(D=>D.join(","))].join(`
3`),T=new Blob([M],{type:"text/csv"}),H=URL.createObjectURL(T),V=document.createElement("a");V.href=H,V.download=`payout-requests-${new Date().toISOString().slice(0,10)}.csv`,V.click(),URL.revokeObjectURL(H)};return e.jsxs("div",{className:"pt-8 space-y-6",children:[e.jsx("h3",{className:"text-xl font-semibold",children:"Affiliate Payout Management"}),e.jsx(gs,{processingId:N,setProcessingId:w,actionType:R,setActionType:_,adminNotes:r,setAdminNotes:m,payoutReference:S,setPayoutReference:A,actionLoading:P,payoutRequests:a,filteredPayoutRequests:L,payoutStatusFilter:b,setPayoutStatusFilter:E,payoutSearch:n,setPayoutSearch:z,userLabel:i,handleProcessAction:h,exportVisiblePayouts:C})]})}function Ls(){const[a,x]=l.useState("business");return e.jsx("div",{className:"space-y-6",children:e.jsxs(ns,{value:a,onValueChange:x,children:[e.jsxs(rs,{children:[e.jsxs($e,{value:"business",className:"gap-2",children:[e.jsx(We,{className:"h-4 w-4"}),"Business & Revenue"]}),e.jsxs($e,{value:"conversion",className:"gap-2",children:[e.jsx(oe,{className:"h-4 w-4"}),"Conversion & Funnel"]})]}),e.jsxs(Ie,{value:"business",className:"space-y-8 pt-4",children:[e.jsx(hs,{}),e.jsx(vs,{})]}),e.jsx(Ie,{value:"conversion",className:"pt-4",children:e.jsx(ps,{})})]})})}export{Ls as AdminOverview};

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.