1import{r as l,j as e,L as b}from"./react-vendor-CAvUCgBg.js";import{B as C,C as c,b as m,I as q,s as S}from"./index-BD5OT4An.js";import{A as k}from"./AdminLayout-DWLBa2X7.js";import{aF as E,ab as R,p as D,b4 as I,A as $}from"./icons-B1xxNXCI.js";import{f as M}from"./formatDistanceToNow-DCnyTZ6w.js";import"./query-C7ekqVw8.js";import"./class-utils-B2rm_Apj.js";import"./supabase-L6Z3U_Fd.js";import"./radix-5feLYWdW.js";import"./en-US-CLdVey28.js";function K(){const[x,g]=l.useState([]),[u,h]=l.useState(!0),[p,A]=l.useState(""),j=async()=>{h(!0);const{data:s,error:t}=await S.from("ad_interest_messages").select("message_id, conversation_id, sender_id, body_preview, created_at, seed_source, ads!inner(id, title, categories(name, slug), cities(name))").order("created_at",{ascending:!1}).limit(1e3);if(t){console.error(t),g([]),h(!1);return}const a=s??[],r=Array.from(new Set(a.map(d=>d.sender_id)));let _=new Map;if(r.length>0){const{data:d}=await S.from("profiles").select("id, chat_handle, display_name").in("id",r);_=new Map((d??[]).map(n=>[n.id,{handle:n.chat_handle,name:n.display_name}]))}const L=a.map(d=>{var v,N,w;const n=d.ads,o=_.get(d.sender_id);return{message_id:d.message_id,conversation_id:d.conversation_id,sender_id:d.sender_id,body:d.body_preview,has_image:!1,has_audio:!1,has_video:!1,created_at:d.created_at,sender_handle:(o==null?void 0:o.handle)??null,sender_name:(o==null?void 0:o.name)??null,ad_id:n==null?void 0:n.id,ad_title:(n==null?void 0:n.title)??"(untitled)",ad_seed_source:d.seed_source??null,category_slug:((v=n==null?void 0:n.categories)==null?void 0:v.slug)??null,category_name:((N=n==null?void 0:n.categories)==null?void 0:N.name)??null,city_name:((w=n==null?void 0:n.cities)==null?void 0:w.name)??null}});g(L),h(!1)};l.useEffect(()=>{j()},[]);const i=l.useMemo(()=>{const s=new Map,t=p.trim().toLowerCase();for(const a of x){if(t&&![a.ad_title,a.body??"",a.sender_handle??"",a.category_name??"",a.city_name??""].join(" ").toLowerCase().includes(t))continue;const r=s.get(a.ad_id);r?(r.total+=1,r.messages.push(a),a.created_at>r.last_at&&(r.last_at=a.created_at),r.unique_senders=new Set(r.messages.map(_=>_.sender_id)).size):s.set(a.ad_id,{ad_id:a.ad_id,ad_title:a.ad_title,category_name:a.category_name,category_slug:a.category_slug,city_name:a.city_name,seed_source:a.ad_seed_source,total:1,unique_senders:1,last_at:a.created_at,messages:[a]})}return Array.from(s.values()).sort((a,r)=>a.last_at<r.last_at?1:-1)},[x,p]),y=l.useMemo(()=>{const s=new Map;for(const t of i){const a=t.category_name??"Uncategorized";s.set(a,(s.get(a)??0)+t.total)}return Array.from(s.entries()).sort((t,a)=>a[1]-t[1])},[i]),f=l.useMemo(()=>({ads:i.length,msgs:i.reduce((s,t)=>s+t.total,0),senders:new Set(x.map(s=>s.sender_id)).size}),[i,x]);return e.jsx(k,{children:e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex items-start justify-between gap-4 flex-wrap",children:[e.jsxs("div",{children:[e.jsxs("h1",{className:"text-3xl font-bold flex items-center gap-2 text-[hsl(var(--brand-yellow))]",children:[e.jsx(E,{className:"h-7 w-7"})," Ad Interest Messages"]}),e.jsx("p",{className:"text-muted-foreground mt-1 max-w-2xl",children:"Every Message Seller note from generated and user-posted ads lands here. Tap a row to open the DM thread."})]}),e.jsxs(C,{variant:"outline",onClick:j,disabled:u,children:[e.jsx(R,{className:`h-4 w-4 mr-2 ${u?"animate-spin":""}`})," Refresh"]})]}),e.jsxs("div",{className:"grid gap-3 md:grid-cols-3",children:[e.jsxs(c,{className:"p-4",children:[e.jsx("div",{className:"text-xs text-muted-foreground",children:"Ads with interest"}),e.jsx("div",{className:"text-2xl font-bold",children:f.ads})]}),e.jsxs(c,{className:"p-4",children:[e.jsx("div",{className:"text-xs text-muted-foreground",children:"Total messages"}),e.jsx("div",{className:"text-2xl font-bold",children:f.msgs})]}),e.jsxs(c,{className:"p-4",children:[e.jsx("div",{className:"text-xs text-muted-foreground",children:"Unique senders"}),e.jsx("div",{className:"text-2xl font-bold",children:f.senders})]})]}),y.length>0&&e.jsxs(c,{className:"p-4",children:[e.jsx("div",{className:"text-sm font-semibold mb-2",children:"Demand by category"}),e.jsx("div",{className:"flex flex-wrap gap-2",children:y.map(([s,t])=>e.jsxs(m,{variant:"secondary",children:[s," · ",t]},s))})]}),e.jsx(q,{placeholder:"Search ad title, body, sender, category, cityâ¦",value:p,onChange:s=>A(s.target.value)}),u&&e.jsx("p",{className:"text-sm text-muted-foreground",children:"Loadingâ¦"}),!u&&i.length===0&&e.jsx(c,{className:"p-8 text-center text-sm text-muted-foreground",children:"No ad-interest messages yet. Once people click Message Seller and send a note, it lands here."}),e.jsx("div",{className:"space-y-4",children:i.map(s=>e.jsxs(c,{className:"p-4 space-y-3",children:[e.jsxs("div",{className:"flex items-start justify-between gap-3 flex-wrap",children:[e.jsxs("div",{className:"min-w-0",children:[e.jsxs(b,{to:`/ad/${s.ad_id}`,className:"font-semibold hover:underline flex items-center gap-1.5",children:[s.ad_title,e.jsx(D,{className:"h-3.5 w-3.5 opacity-60"})]}),e.jsxs("div",{className:"flex flex-wrap gap-1.5 mt-1.5",children:[s.seed_source&&e.jsxs(m,{variant:"outline",className:"text-xs",children:[e.jsx(I,{className:"h-3 w-3 mr-1"})," ",s.seed_source]}),s.category_name&&e.jsx(m,{variant:"secondary",className:"text-xs",children:s.category_name}),s.city_name&&e.jsx(m,{variant:"secondary",className:"text-xs",children:s.city_name}),e.jsxs(m,{variant:"outline",className:"text-xs",children:[s.total," reply",s.total===1?"":"s"," · ",s.unique_senders," sender",s.unique_senders===1?"":"s"]})]})]}),e.jsxs("div",{className:"text-xs text-muted-foreground whitespace-nowrap",children:["Last ",M(new Date(s.last_at),{addSuffix:!0})]})]}),e.jsxs("div",{className:"space-y-2 border-t pt-3",children:[s.messages.slice(0,8).map(t=>e.jsxs(b,{to:`/account/messages/${t.conversation_id}`,className:"flex gap-3 text-sm hover:bg-muted/50 rounded p-2 -mx-2 transition-colors",children:[e.jsx($,{className:"h-4 w-4 mt-0.5 shrink-0 text-muted-foreground"}),e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsxs("div",{className:"flex items-center gap-2 text-xs text-muted-foreground",children:[e.jsx("span",{className:"font-medium text-foreground",children:t.sender_handle||t.sender_name||`user-${t.sender_id.slice(0,6)}`}),e.jsx("span",{children:"·"}),e.jsx("span",{children:M(new Date(t.created_at),{addSuffix:!0})})]}),e.jsx("div",{className:"truncate",children:t.body||(t.has_image?"ð· photo":t.has_audio?"ð¤ voice note":t.has_video?"ð¥ video":"(empty)")})]})]},t.message_id)),s.messages.length>8&&e.jsxs("div",{className:"text-xs text-muted-foreground pl-2",children:["+ ",s.messages.length-8," more â open the thread to see all"]})]})]},s.ad_id))})]})})}export{K 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.