1import{r,s as u,j as e,B as f,a9 as ee,w as se,J as te,K as ae,aq as re,I as le,S as L,o as M,q as O,t as q,v as w,x as ie,f as ne,y as oe,aa as ce,ab as de,ac as me,ad as pe,L as b,aD as he,ar as xe,T as ue,Q as fe,Y as je,Z as ve,_ as _e,$ as ge,a0 as Ne,a1 as ye,a2 as we,a3 as be,A as p}from"./index-ClJJfLql.js";import{T as Se,a as Ae,b as S,c as n,d as Te,e as l}from"./table-DK30uKWo.js";const Ce="1a6c7399-b6ce-4903-ba34-e3a0b1efbf6f",Re=["new","approved","rejected"],ke={new:"default",approved:"default",rejected:"destructive"},De=i=>{const c=[];return i.email_list_size&&c.push(`Email: ${i.email_list_size}`),i.social_following&&c.push(`Social: ${i.social_following}`),c.length?c.join(" · "):"â"};function Ee(){const[i,c]=r.useState([]),[z,D]=r.useState(!0),[A,F]=r.useState(""),[j,V]=r.useState("new"),[t,d]=r.useState(null),[$,U]=r.useState([]),[v,T]=r.useState("default"),[I,E]=r.useState(""),[C,_]=r.useState(!1),[Y,R]=r.useState(!1),[h,P]=r.useState(!1),[J,B]=r.useState(0),g=async()=>{D(!0);const{data:s,error:a}=await u.from("vw_affiliate_applications_review").select("id, first_name, last_name, email, phone, country, company_name, website, email_list_size, social_following, primary_channel, audience_description, motivation, status, referral_source, resolved_promoter_id, resolved_promoter_name, referral_resolves, created_at").order("created_at",{ascending:!1});a?p({title:"Failed to load applications",description:a.message,variant:"destructive"}):c(s??[]),D(!1)};r.useEffect(()=>{g()},[]),r.useEffect(()=>{(async()=>{const{data:s,error:a}=await u.from("affiliate_promoters").select("id, name").eq("active",!0).order("name");!a&&s&&U(s)})()},[]),r.useEffect(()=>{t&&(t.resolved_promoter_id&&t.referral_resolves?T(t.resolved_promoter_id):T("pool"),E(""))},[t]);const N=r.useMemo(()=>{const s=A.trim().toLowerCase();return i.filter(a=>j!=="all"&&a.status!==j?!1:s?[a.first_name,a.last_name,a.email,a.country,a.company_name,a.primary_channel].filter(
1Boolean).some(o=>o.toLowerCase().includes(s)):!0)},[i,A,j]),K=async()=>{if(!t)return;_(!0);const s=v==="pool"?null:v,{data:a,error:o}=await u.rpc("approve_affiliate_application_v1",{p_application_id:t.id,p_promoter_id:s});if(_(!1),o)p({title:"Approval failed",description:o.message,variant:"destructive"});else{const m=a??{},k=m.reused_existing_card===!0,Z=m.claim_id,H=$.find(G=>G.id===m.promoter_id);let y=k?"This person is already in the directory â a claim has been added against their existing card.":"Added to the directory.";H?y+=` ${H.name} has been asked to confirm them and set terms.`:Z?y+=" The partner has been asked to confirm them and set terms.":y+=" They were approved into the unclaimed pool.",p({title:"Application approved",description:y}),d(null),g()}},Q=async()=>{if(!t)return;_(!0);const{data:s,error:a}=await u.rpc("reject_affiliate_application_v1",{p_application_id:t.id,p_reason:I||null});_(!1),a?p({title:"Rejection failed",description:a.message,variant:"destructive"}):(p({title:"Application rejected",description:"The application has been marked as rejected."}),d(null),g())},x=r.useMemo(()=>i.filter(s=>s.status==="new"),[i]),W=async()=>{P(!0),B(0);let s=0;const a=[];for(const o of x){const{error:m}=await u.rpc("approve_affiliate_application_v1",{p_application_id:o.id,p_promoter_id:Ce});m?a.push(`${o.email}: ${m.message}`):s+=1,B(k=>k+1)}P(!1),R(!1),p({title:a.length?"Sent with some problems":"Sent to Triton",description:a.length?`${s} sent. ${a.length} could not be sent â ${a[0]}`:`${s} application${s===1?"":"s"} are now waiting in Triton's Requests tab.`,variant:a.length?"destructive":void 0}),g()},X=s=>s.referral_source?s.referral_resolves&&s.resolved_promoter_name?`Referred by ${s.resolved_promoter_name}`:`Referred by ${s.referral_source} (not an active partner)`:"Direct";return e.jsxs("div",{className:"container mx-auto px-4 py-8 space-y-6",children:[e.jsxs("div",{className:"flex flex-wrap items-start justify-between gap-3",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"font-display font-extrabold text-3xl text-primary",children:"Affiliate Applications"}),e.jsx("p",{className:"text-muted-foreground",children:"Review and approve affiliate partner applications."})]}),x.length>0&&e.jsxs(f,{onClick:()=>R(!0),disabled:h,children:[e.jsx(ee,{className:"w-4 h-4 mr-2"}),"Send all ",x.length," to Triton"]})]}),e.jsxs(se,{children:[e.jsx(te,{children:e.jsxs(ae,{className:"text-lg flex items-center justify-between",children:[e.jsxs("span",{children:[N.length," application",N.length===1?"":"s"]}),e.jsxs("div",{className:"flex gap-3",children:[e.jsxs("div",{className:"relative",children:[e.jsx(re,{className:"absolute left-2 top-2.5 w-4 h-4 text-muted-foreground"}),e.jsx(le,{className:"pl-8 w-64",placeholder:"Search name, email, countryâ¦",value:A,onChange:s=>F(s.target.value)})]}),e.jsxs(L,{value:j,onValueChange:V,children:[e.jsx(M,{className:"w-44",children:e.jsx(O,{})}),e.jsxs(q,{children:[e.jsx(w,{value:"all",children:"All statuses"}),Re.map(s=>e.jsx(w,{value:s,children:s.replace("_"," ")},s))]})]})]})]})}),e.jsx(ie,{children:e.jsxs(Se,{children:[e.jsx(Ae,{children:e.jsxs(S,{children:[e.jsx(n,{children:"Name"}),e.jsx(n,{children:"Email"}),e.jsx(n,{children:"Introduced by"}),e.jsx(n,{children:"Country"}),e.jsx(n,{children:"Primary channel"}),e.jsx(n,{children:"Audience"}),e.jsx(n,{children:"Submitted"}),e.jsx(n,{children:"Status"}),e.jsx(n,{className:"text-right",children:"Action"})]})}),e.jsx(Te,{children:z?e.jsx(S,{children:e.jsx(l,{colSpan:9,className:"text-center text-muted-foreground py-8",children:"Loadingâ¦"})}):N.length===0?e.jsx(S,{children:e.jsx(l,{colSpan:9,className:"text-center text-muted-foreground py-8",children:"No applications found."})}):N.map(s=>e.jsxs(S,{className:"cursor-pointer",onClick:()=>d(s),children:[e.jsxs(l,{children:[e.jsxs("div",{className:"font-medium",children:[s.first_name," ",s.last_name]}),s.company_name&&e.jsx("div",{className:"text-xs text-muted-foreground",children:s.company_name})]}),e.jsx(l,{children:s.email}),e.jsx(l,{children:s.referral_source?s.referral_resolves&&s.resolved_promoter_name?e.jsxs("div",{children:[e.jsx("div",{children:s.resolved_promoter_name}),e.jsx("div",{className:"text-xs font-mono text-muted-foreground",children:s.referral_source})]}):e.jsxs("div",{children:[e.jsx("div",{className:"font-mono text-xs",children:s.referral_source}),e.jsx("div",{className:"text-xs text-muted-foreground",children:"Not an active partner"})]}):e.jsx("span",{className:"text-muted-foreground",children:"Direct"})}),e.jsx(l,{children:s.country||"â"}),e.jsx(l,{children:s.primary_channel||"â"}),e.jsx(l,{children:De(s)}),e.jsx(l,{children:ne(new Date(s.created_at),"dd MMM yyyy HH:mm")}
1),e.jsx(l,{children:e.jsx(oe,{variant:ke[s.status],children:s.status})}),e.jsx(l,{className:"text-right",children:e.jsx(f,{size:"sm",variant:"outline",onClick:a=>{a.stopPropagation(),d(s)},children:"Review"})})]},s.id))})]})})]}),e.jsx(ce,{open:!!t,onOpenChange:s=>!s&&d(null),children:e.jsxs(de,{className:"sm:max-w-xl overflow-y-auto",children:[e.jsx(me,{children:e.jsx(pe,{className:"text-xl",children:"Review application"})}),t&&e.jsxs("div",{className:"space-y-6 mt-6",children:[e.jsxs("div",{className:"space-y-1",children:[e.jsxs("h3",{className:"text-lg font-semibold",children:[t.first_name," ",t.last_name]}),e.jsxs("p",{className:"text-sm text-muted-foreground",children:[t.email,t.phone?` · ${t.phone}`:""]})]}),e.jsxs("div",{className:"rounded-md border bg-muted/30 px-3 py-2 text-sm",children:[e.jsx("div",{className:"text-xs text-muted-foreground",children:"Introduced by"}),e.jsx("div",{className:"font-medium",children:X(t)}),t.referral_source&&e.jsxs("div",{className:"mt-1 font-mono text-xs text-muted-foreground",children:["Self-declared on the application: ",t.referral_source]}),e.jsx("p",{className:"mt-1 text-xs text-muted-foreground",children:"A note for you only â it does not credit anyone. Approving decides who they work with."})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4 text-sm",children:[e.jsxs("div",{children:[e.jsx("span",{className:"text-muted-foreground",children:"Country"}),e.jsx("div",{children:t.country||"â"})]}),e.jsxs("div",{children:[e.jsx("span",{className:"text-muted-foreground",children:"Primary channel"}),e.jsx("div",{children:t.primary_channel||"â"})]}),e.jsxs("div",{children:[e.jsx("span",{className:"text-muted-foreground",children:"Email list size"}),e.jsx("div",{children:t.email_list_size||"â"})]}),e.jsxs("div",{children:[e.jsx("span",{className:"text-muted-foreground",children:"Social following"}),e.jsx("div",{children:t.social_following||"â"})]}),t.website&&e.jsxs("div",{className:"col-span-2",children:[e.jsx("span",{className:"text-muted-foreground",children:"Website"}),e.jsx("div",{children:t.website})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(b,{className:"text-muted-foreground",children:"Motivation"}),e.jsx("p",{className:"text-sm whitespace-pre-wrap border rounded-md p-3 bg-muted/30",children:t.motivation||"â"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(b,{className:"text-muted-foreground",children:"Audience description"}),e.jsx("p",{className:"text-sm whitespace-pre-wrap border rounded-md p-3 bg-muted/30",children:t.audience_description||"â"})]}),t.status==="new"&&e.jsxs("div",{className:"space-y-4 border-t pt-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(b,{htmlFor:"promoter",children:"Partner to claim them"}),e.jsxs(L,{value:v,onValueChange:T,children:[e.jsx(M,{id:"promoter",className:"w-full",children:e.jsx(O,{placeholder:"Choose a partner"})}),e.jsxs(q,{children:[e.jsx(w,{value:"pool",children:"Approve into unclaimed pool"}),$.map(s=>e.jsx(w,{value:s.id,children:s.name},s.id))]})]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Defaulted to the partner their referral points to. You can choose a different partner or leave them unclaimed."})]}),e.jsxs("div",{className:"flex gap-3",children:[e.jsxs(f,{className:"flex-1",onClick:K,disabled:C||v==="default",children:[e.jsx(he,{className:"w-4 h-4 mr-2"}),"Approve"]}),e.jsxs(f,{variant:"outline",className:"flex-1",onClick:()=>d(null),disabled:C,children:[e.jsx(xe,{className:"w-4 h-4 mr-2"}),"Cancel"]})]}),e.jsxs("div",{className:"space-y-2 border-t pt-4",children:[e.jsx(b,{htmlFor:"reason",className:"text-destructive",children:"Reject application"}),e.jsx(ue,{id:"reason",placeholder:"Reason for rejection (optional)",value:I,onChange:s=>E(s.target.value)}),e.jsxs(f,{variant:"destructive",className:"w-full",onClick:Q,disabled:C,children:[e.jsx(fe,{className:"w-4 h-4 mr-2"}),"Reject"]})]})]}),t.status!=="new"&&e.jsxs("div",{className:"border-t pt-4 text-sm text-muted-foreground",children:["This application has already been ",t.status,"."]})]})]})}),e.jsx(je,{open:Y,onOpenChange:s=>!h&&R(s),children:e.jsxs(ve,{children:[e.jsxs(_e,{children:[e.jsxs(ge,{children:["Send ",x.length," applications to Triton?"]}),e.jsx(Ne,{children:"Each person is added to the directory and appears in Triton's Requests tab, where Triton confirms them and sets their terms. No rates, commission or payments are set by this
1step."})]}),e.jsxs(ye,{children:[e.jsx(we,{disabled:h,children:"Cancel"}),e.jsx(be,{onClick:s=>{s.preventDefault(),W()},disabled:h,children:h?`Sending ${J} of ${x.length}â¦`:"Send to Triton"})]})]})})]})}export{Ee 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.