1import{a as n,e as A,j as t}from"./vendor-query-CRhh6spR.js";import{a4 as K,aU as Q,b5 as G,B as v,I as q,T as U,o as X,s as M}from"./index-BkVb42KZ.js";import{B as o}from"./badge-DfIFDYyU.js";import{S as E,a as F,b as O,c as T,d}from"./select-B5KGv1Do.js";import{A as Z}from"./AdminSearchInput-CS_ud0Ue.js";import{A as J}from"./AdminPage-BhQW-9r_.js";import{A as W}from"./AdminCard-FaakhOlA.js";import{A as N}from"./AdminMetric-B6OthYV0.js";import{A as Y,a as $}from"./AdminLoading-CBBcaphO.js";import{s as I}from"./subDays-CfnmtW0T.js";import{M as k}from"./mail-DS62qYck.js";import{C as ee}from"./circle-check-big-Cjr7FrMh.js";import{C as te}from"./circle-x-D3XjrB4n.js";import{C as se}from"./chevron-left-D_T4_riH.js";import{C as ae}from"./chevron-right-DSioQ7mT.js";import"./vendor-react-BAmwgVqg.js";import"./vendor-charts-BVeOEGzI.js";import"./vendor-stripe-2y4Jo9_W.js";import"./vendor-supabase-Csx7YFID.js";import"./index-D17hSi50.js";import"./index-Bmtd1gWP.js";import"./index-fVqgy0Om.js";import"./chevron-down-CXoJibNt.js";import"./check-BtcgJ1Z4.js";import"./chevron-up-DcqJwcAx.js";import"./search-BNuSVdRk.js";import"./addDays-Bv613BQt.js";function re(s,c){const m=+K(s);return Q(s,m+c)}function ne(s,c){return re(s,c*G)}function le(s,c){return ne(s,-24)}const _=50;function Te(){const[s,c]=n.useState("7d"),[m,D]=n.useState(""),[h,L]=n.useState(""),[g,B]=n.useState("all"),[f,P]=n.useState("all"),[u,x]=n.useState(0),[j,H]=n.useState(""),b=n.useMemo(()=>{const e=new Date;return s==="24h"?{start:le(e).toISOString(),end:e.toISOString()}:s==="7d"?{start:I(e,7).toISOString(),end:e.toISOString()}:s==="30d"?{start:I(e,30).toISOString(),end:e.toISOString()}:m&&h?{start:new Date(m).toISOString(),end:new Date(h+"T23:59:59").toISOString()}:{start:I(e,7).toISOString(),end:e.toISOString()}},[s,m,h]),{data:y}=A({queryKey:["email-log-templates"],queryFn:async()=>{const{data:e,error:r}=await M.from("email_send_log").select("template_name").order("template_name");if(r)throw r;return[...new Set((e==null?void 0:e.map(i=>i.template_name))??[])]}}),{data:w,isLoading:R}=A({queryKey:["email-logs",b,g,f,u],queryFn:async()=>{let e=M.from("email_send_log").select("*").gte("created_at",b.start).lte("created_at",b.end).order("created_at",{ascending:!1});g!=="all"&&(e=e.eq("template_name",g)),f!=="all"&&(e=e.eq("status",f));const{data:r,error:a}=await e.limit(500);if(a)throw a;return r??[]}}),l=n.useMemo(()=>{if(!w)return[];const e=new Map;for(const a of w){const i=a.message_id||a.id;e.has(i)||e.set(i,a)}let r=Array.from(e.values());if(j.trim()){const a=j.toLowerCase();r=r.filter(i=>i.recipient_email.toLowerCase().includes(a))}return r},[w,j]),S=n.useMemo(()=>{const e=l.length,r=l.filter(p=>p.status==="sent").length,a=l.filter(p=>["dlq","failed"].includes(p.status)).length,i=l.filter(p=>p.status==="suppressed").length;return{total:e,sent:r,failed:a,suppressed:i}},[l]),z=l.slice(u*_,(u+1)*_),C=Math.max(1,Math.ceil(l.length/_)),V=e=>{switch(e){case"sent":return t.jsx(o,{className:"bg-emerald-500/15 text-emerald-600 border-0 text-micro",children:"Sent"});case"pending":return t.jsx(o,{className:"bg-yellow-500/15 text-yellow-600 border-0 text-micro",children:"Pending"});case"dlq":case"failed":return t.jsx(o,{className:"bg-red-500/15 text-red-500 border-0 text-micro",children:"Failed"});case"suppressed":return t.jsx(o,{className:"bg-orange-500/15 text-orange-500 border-0 text-micro",children:"Suppressed"});case"bounced":return t.jsx(o,{className:"bg-red-500/15 text-red-400 border-0 text-micro",children:"Bounced"});case"complained":return t.jsx(o,{className:"bg-purple-500/15 text-purple-500 border-0 text-micro",children:"Complained"});default:return t.jsx(o,{variant:"secondary",className:"text-micro",children:e})}};return t.jsxs(J,{title:"Email Log",subtitle:"Track all sent emails and delivery status",children:[t.jsxs("div",{className:"flex flex-wrap gap-2",children:[t.jsxs("div",{className:"flex gap-1",children:[["24h","7d","30d"].map(e=>t.jsx(v,{variant:s===e?"default":"outline",size:"sm",className:"text-xs h-8",onClick:()=>{c(e),x(0)},children:e==="24h"?"24h":e==="7d"?"7 days":"30 days"},e)),t.jsx(v,{variant:s==="custom"?"default":"outline",size:"sm",className:"text-xs h-8",onClick:()=>c("custom"),children:"Custom"})]}),t.jsxs(E,{value:g,onValueChange:e=>{B(e),x(0)},children:[t.jsx(F,{className:"w-44 h-8 text-xs",children:t.jsx(O,{placeholder:"Template"})}),t.jsxs(T,{children:[t.jsx(d,{value:"all",children:"All Templates"}),y==null?void 0:y.map(e=>t.jsx(d,{value:e,children:e},e))]})]}),t.jsxs(E,{value:f,onValueChange:e=>{P(e),x(0)},children:[t.jsx(F,{className:"w-36 h-8 text-xs",children:t.jsx(O,{placeholder:"Status"})}),t.jsxs(T,{children:[t.jsx(d,{value:"all",children:"All Statuses"}),t.jsx(d,{value:"sent",children:"Sent"}),t.jsx(d,{value:"failed",children:"Failed"}),t.jsx(d,{value:"dlq",children:"DLQ"}),t.jsx(d,{value:"suppressed",children:"Suppressed"})]})]})]}),t.jsx(Z,{value:j,onChange:e=>{H(e),x(0)},placeholder:"Search by recipient emailâ¦"}),s==="custom"&&t.jsxs("div",{className:"flex gap-2 items-center",children:[t.jsx(q,{type:"date",value:m,onChange:e=>D(e.target.value),className:"w-40 h-8 text-xs"}),t.jsx("span",{className:"text-xs text-muted-foreground",children:"to"}),t.jsx(q,{type:"date",value:h,onChange:e=>L(e.target.value),className:"w-40 h-8 text-xs"})]}),t.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-3",children:[t.jsx(N,{label:"Total Emails",value:S.total,Icon:k}),t.jsx(N,{label:"Sent",value:S.sent,Icon:ee,tone:"good"}),t.jsx(N,{label:"Failed",value:S.failed,Icon:te,tone:"warn"}),t.jsx(N,{label:"Suppressed",value:S.suppressed,Icon:U,tone:"warn"})]}),R?t.jsx(Y,{rows:4}):t.jsxs("div",{className:"space-y-2",children:[z.map(e=>t.jsx(W,{className:"p-3",children:t.jsxs("div",{className:"flex items-start justify-between gap-2",children:[t.jsxs("div",{className:"min-w-0 space-y-0.5",children:[t.jsxs("div",{className:"flex items-center gap-2",children:[t.jsx(o,{variant:"outline",className:"text-micro shrink-0",children:e.template_name}),V(e.status)]}),t.jsx("p",{className:"text-xs text-muted-foreground truncate",children:e.recipient_email}),e.error_message&&t.jsxs("p",{className:"text-micro text-red-400 truncate",children:["â ",e.error_message]})]}),t.jsx("p",{className:"text-micro text-muted-foreground whitespace-nowrap shrink-0",children:X(new Date(e.created_at),"MMM d, HH:mm")}
1)]})},e.id)),l.length===0&&t.jsx($,{Icon:k,title:"No email logs found"})]}),C>1&&t.jsxs("div",{className:"flex items-center justify-center gap-2",children:[t.jsx(v,{variant:"outline",size:"sm",disabled:u===0,onClick:()=>x(e=>e-1),children:t.jsx(se,{className:"w-4 h-4"})}),t.jsxs("span",{className:"text-xs text-muted-foreground",children:["Page ",u+1," of ",C]}),t.jsx(v,{variant:"outline",size:"sm",disabled:u>=C-1,onClick:()=>x(e=>e+1),children:t.jsx(ae,{className:"w-4 h-4"})})]})]})}export{Te 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.