1import{r as o,j as e}from"./index-CMzOE_m_.js";import{c as K,s as $}from"./x-ClRK6Liy.js";import{L as R,s as Q,B as P,C as O,aj as Y,R as _,u as G,j as J}from"./App-Dqju0acX.js";import{I as W}from"./inbox-DOK5gkrC.js";import{T as X}from"./textarea-oKe6M8Ig.js";import{M as Z}from"./message-circle--Va22dqg.js";import{S as ee}from"./send-BNLM08SN.js";import{B as L}from"./badge-UUurD1Ji.js";import"./index-D5S_L1_f.js";import"./index-CLNoak55.js";/** 2 * @license lucide-react v0.462.0 - ISC 3 * 4 * This source code is licensed under the ISC license. 5 * See the LICENSE file in the root directory of this source tree. 6 */const te=K("MessageSquareQuote",[["path",{d:"M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z",key:"1lielz"}],["path",{d:"M8 12a2 2 0 0 0 2-2V8H8",key:"1jfesj"}],["path",{d:"M14 12a2 2 0 0 0 2-2V8h-2",key:"1dq9mh"}]]);function I(t){const r=new Date(t),n=new Date().getTime()-r.getTime(),c=Math.floor(n/(1e3*60*60*24));return c===0?r.toLocaleTimeString([],{hour:"2-digit",minute:"2-digit"}):c===1?"Yesterday":c<7?r.toLocaleDateString([],{weekday:"short"}):r.toLocaleDateString([],{day:"numeric",month:"short"})}const g=new Map,D=new Set,T=[];let E=!1;async function se(){if(!E){for(E=!0;T.length>0;){const t=T.shift();if(g.has(t.id)){t.resolve(g.get(t.id));continue}try{const{data:r,error:l}=await $.functions.invoke("inbox-messages",{body:{action:"get-profile-pic",participantId:t.id}}),n=!l&&(r!=null&&r.url)?r.url:null;g.set(t.id,n),t.resolve(n)}catch{g.set(t.id,null),t.resolve(null)}await new Promise(r=>setTimeout(r,100))}E=!1}}function re(t){return g.has(t)?Promise.resolve(g.get(t)):D.has(t)?new Promise(r=>{T.push({id:t,resolve:r})}):(D.add(t),new Promise(r=>{T.push({id:t,resolve:r}),se()}))}function ne(t){const[r,l]=o.useState(t?g.get(t)??null:null);return o.useEffect(()=>{if(!t)return;if(g.has(t)){l(g.get(t));return}let n=!1;return re(t).then(c=>{n||l(c)}),()=>{n=!0}},[t]),r}function z({participantId:t,name:r,size:l="md"}){const n=ne(t),c=l==="sm"?"h-9 w-9":"h-10 w-10",d=l==="sm"?"text-base":"text-lg";return n?e.jsx("img",{src:n,alt:"",className:`${c} rounded-full object-cover bg-muted`}):e.jsx("div",{className:`${c} rounded-full bg-muted flex items-center justify-center ${d} font-semibold text-muted-foreground`,children:r.charAt(0).toUpperCase()})}function ae({conversations:t,activeConv:r,loading:l,loadingMore:n,hasMore:c,onLoadMore:d,onSelectConversation:u}){const j=o.useRef(null),f=o.useCallback(()=>{const a=j.current;!a||!c||n||a.scrollHeight-a.scrollTop-a.clientHeight<100&&d()},[c,n,d]);return o.useEffect(()=>{const a=j.current;if(a)return a.addEventListener("scroll",f),()=>a.removeEventListener("scroll",f)},[f]),e.jsx("div",{className:"w-80 border-r flex flex-col",children:e.jsx("div",{ref:j,className:"flex-1 overflow-y-auto",children:l?e.jsx("div",{className:"flex items-center justify-center py-12",children:e.jsx(R,{className:"h-6 w-6 animate-spin text-muted-foreground"})}):t.length===0?e.jsxs("div",{className:"flex flex-col items-center justify-center py-12 text-muted-foreground",children:[e.jsx(W,{className:"h-8 w-8 mb-2"}),e.jsx("p",{className:"text-sm",children:"No conversations found"})]}):e.jsxs(e.Fragment,{children:[t.map(a=>{const h=a.unreadCount>0,s=(r==null?void 0:r.id)===a.id;return e.jsx("button",{onClick:()=>u(a),className:`w-full text-left px-4 py-3 border-b transition-colors hover:bg-muted/50 ${s?"bg-muted":""} ${h?"bg-primary/5":""}`,children:e.jsxs("div",{className:"flex items-start gap-3",children:[e.jsxs("div",{className:"relative flex-shrink-0",children:[e.jsx(z,{participantId:a.participantId,name:a.name}),e.jsx("span",{className:`absolute -bottom-0.5 -right-0.5 h-4 w-4 rounded-full border-2 border-background flex items-center justify-center ${a.platform==="instagram"?"bg-gradient-to-br from-purple-500 to-pink-500":"bg-blue-600"}`,children:e.jsx("span",{className:"text-[8px] text-white font-bold",children:a.platform==="instagram"?"IG":"FB"})})]}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center justify-between gap-2",children:[e.jsx("span",{className:`text-sm truncate ${h?"font-bold text-foreground":"font-medium text-foreground"}`,children:a.name}),e.jsx("span",{className:"text-[11px] text-muted-foreground flex-shrink-0 whitespace-nowrap",children:I(a.updatedTime)})]}),e.jsxs("div",{className:"flex items-center gap-2 mt-0.5",children:[e.jsx("p",{className:`text-xs truncate flex-1 ${h?"font-semibold text-foreground":"text-muted-foreground"}`,children:a.snippet||"Attachment"}),h&&e.jsx("span",{className:"h-2.5 w-2.5 rounded-full bg-primary flex-shrink-0"})]})]})]})},a.id)}),c&&e.jsx("div",{className:"flex items-center justify-center py-4 gap-2",children:n?e.jsxs(e.Fragment,{children:[e.jsx(R,{className:"h-4 w-4 animate-spin text-muted-foreground"}),e.jsx("span",{className:"text-xs text-muted-foreground",children:"Loading moreâ¦"})]}):e.jsx("span",{className:"text-xs text-muted-foreground",children:"Scroll for more"})})]})})})}function le({url:t}){const[r,l]=o.useState(!1),n=()=>{navigator.clipboard.writeText(t).then(()=>{l(!0),setTimeout(()=>l(!1),2e3)})};return e.jsx("button",{onClick:n,className:"inline-flex items-center gap-0.5 text-primary hover:underline ml-1",children:r?e.jsxs(e.Fragment,{children:[e.jsx(O,{className:"h-3 w-3"})," Copied"]}):e.jsxs(e.Fragment,{children:[e.jsx(Y,{className:"h-3 w-3"})," Copy link"]})})}function oe({activeConv:t,messages:r,loadingMessages:l,sending:n,onSendReply:c}){const[d,u]=o.useState(""),j=o.useRef(null);o.useEffect(()=>{var s;(s=j.current)==null||s.scrollIntoView({behavior:"smooth"})},[r]),o.useEffect(()=>{u("")},[t==null?void 0:t.id]);const f=async()=>
6{if(!d.trim()||n)return;const s=d;u(""),await c(s)},a=s=>{s.key==="Enter"&&!s.shiftKey&&(s.preventDefault(),f())},h=s=>{var p;return!((p=s.from)!=null&&p.id)||!t?!1:s.from.id!==t.participantId};return t?e.jsxs("div",{className:"flex-1 flex flex-col min-h-0 overflow-hidden",children:[e.jsxs("div",{className:"border-b px-4 py-3 flex items-center gap-3",children:[e.jsx(z,{participantId:t.participantId,name:t.name,size:"sm"}),e.jsxs("div",{className:"flex-1",children:[e.jsx("h3",{className:"font-semibold text-sm",children:t.name}),e.jsxs("p",{className:"text-xs text-muted-foreground capitalize",children:[t.platform," · Last active ",I(t.updatedTime)]})]})]}),e.jsx(Q,{className:"flex-1 px-4 py-3",children:l?e.jsx("div",{className:"flex items-center justify-center py-12",children:e.jsx(R,{className:"h-6 w-6 animate-spin text-muted-foreground"})}):r.length===0?e.jsx("p",{className:"text-center text-sm text-muted-foreground py-8",children:"No messages"}):e.jsxs("div",{className:"space-y-2",children:[r.map((s,p)=>{var C,k,y;const b=h(s),v=p>0?r[p-1]:null,S=!b&&(!v||h(v)||((C=v.from)==null?void 0:C.id)!==((k=s.from)==null?void 0:k.id));return e.jsxs("div",{children:[s.isCommentReply&&e.jsx("div",{className:"flex justify-center my-2",children:e.jsxs("div",{className:"inline-flex items-center gap-1.5 px-3 py-1.5 rounded-full bg-muted/70 text-xs text-muted-foreground",children:[e.jsx(te,{className:"h-3 w-3"}),e.jsx("span",{children:"Triggered from a comment"}),s.commentUrl&&e.jsx(le,{url:s.commentUrl})]})}),(s.text||s.attachments.length>0||!s.isCommentReply&&!s.text&&s.attachments.length===0)&&e.jsx("div",{className:`flex ${b?"justify-end":"justify-start"}`,children:e.jsxs("div",{className:`max-w-[70%] ${b?"items-end":"items-start"}`,children:[S&&((y=s.from)==null?void 0:y.name)&&e.jsx("p",{className:"text-[11px] text-muted-foreground mb-0.5 px-1",children:s.from.name}),e.jsxs("div",{className:`rounded-2xl px-4 py-2.5 ${b?"bg-primary text-primary-foreground rounded-br-md":"bg-muted text-foreground rounded-bl-md"}`,children:[s.text?e.jsx("p",{className:"text-sm whitespace-pre-wrap break-words",children:s.text}):s.attachments.length===0&&!s.isCommentReply?e.jsx("p",{className:"text-sm italic opacity-70",children:"Automated message (template)"}):null,s.attachments.length>0&&e.jsx("div",{className:"mt-1.5 space-y-1",children:s.attachments.map((M,N)=>M.image_data?e.jsx("img",{src:M.image_data.url,alt:"attachment",className:"rounded-lg max-w-full max-h-48 object-cover"},N):e.jsx("p",{className:"text-xs italic opacity-70",children:"[Attachment]"},N))}),e.jsx("p",{className:`text-[10px] mt-1 ${b?"text-primary-foreground/60":"text-muted-foreground"}`,children:I(s.createdTime)})]})]})})]},s.id)}),e.jsx("div",{ref:j})]})}),e.jsxs("div",{className:"border-t px-4 py-3 flex items-end gap-2",children:[e.jsx(X,{placeholder:`Reply on ${t.platform==="instagram"?"Instagram":"Messenger"}...`,value:d,onChange:s=>u(s.target.value),onKeyDown:a,disabled:n,className:"flex-1 min-h-[44px] max-h-[120px] resize-none",rows:1}),e.jsx(P,{onClick:f,disabled:n||!d.trim(),size:"icon",className:"h-10 w-10 flex-shrink-0",children:n?e.jsx(R,{className:"h-4 w-4 animate-spin"}):e.jsx(ee,{className:"h-4 w-4"})})]})]}):e.jsxs("div",{className:"flex-1 flex flex-col items-center justify-center text-muted-foreground",children:[e.jsx(Z,{className:"h-12 w-12 mb-3"}),e.jsx("p",{className:"text-lg font-medium",children:"Select a conversation"}),e.jsx("p",{className:"text-sm",children:"Choose from the list to view messages"})]})}function ie({filter:t,setFilter:r,totalCount:l,fbCount:n,igCount:c,loading:d,onRefresh:u}){return e.jsxs("div",{className:"border-b px-4 py-3 flex items-center gap-2",children:[e.jsxs("button",{onClick:()=>r("all"),className:`px-4 py-1.5 rounded-full text-sm font-medium transition-colors ${t==="all"?"bg-primary text-primary-foreground":"bg-muted text-muted-foreground hover:bg-muted/80"}`,children:["All messages",e.jsx(L,{variant:"secondary",className:"ml-2 text-xs",children:l})]}),e.jsxs("button",{onClick:()=>r("facebook"),className:`px-4 py-1.5 rounded-full text-sm font-medium transition-colors ${t==="facebook"?"bg-blue-600 text-white":"bg-muted text-muted-foreground hover:bg-muted/80"}`,children:["Messenger",n>0&&e.jsx(L,{variant:"secondary",className:"ml-2 text-xs",children:n})]}),e.jsxs("button",{onClick:()=>r("instagram"),className:`px-4 py-1.5 rounded-full text-sm font-medium transition-colors ${t==="instagram"?"bg-gradient-to-r from-purple-500 to-pink-500 text-white":"bg-muted text-muted-foreground hover:bg-muted/80"}`,children:["Instagram",c>0&&e.jsx(L,{variant:"secondary",className:"ml-2 text-xs",children:c})]}),e.jsx("div",{className:"ml-auto",children:e.jsx(P,{variant:"ghost",size:"sm",onClick:u,disabled:d,children:e.jsx(_,{className:`h-4 w-4 ${d?"animate-spin":""}`})})})]})}function be(){const{user:t}=G(),{toast:r}
6=J(),[l,n]=o.useState([]),[c,d]=o.useState([]),[u,j]=o.useState(null),[f,a]=o.useState("all"),[h,s]=o.useState(!0),[p,b]=o.useState(!1),[v,S]=o.useState(!1),[C,k]=o.useState(!1),[y,M]=o.useState(null),N=async m=>{m?b(!0):s(!0);try{const{data:i,error:x}=await $.functions.invoke("inbox-messages",{body:{action:"get-conversations",platform:f,afterCursor:m||void 0}});if(x)throw x;if(i!=null&&i.error)throw new Error(i.error);n(m?w=>[...w,...i.conversations||[]]:i.conversations||[]),M(i.nextCursor||null)}catch(i){r({title:"Failed to load conversations",description:i.message,variant:"destructive"})}finally{s(!1),b(!1)}},F=o.useCallback(()=>{y&&!p&&N(y)},[y,p,f]),A=async m=>{S(!0),j(m),d([]);try{const{data:i,error:x}=await $.functions.invoke("inbox-messages",{body:{action:"get-messages",conversationId:m.id}});if(x)throw x;if(i!=null&&i.error)throw new Error(i.error);d(i.messages||[])}catch(i){r({title:"Failed to load messages",description:i.message,variant:"destructive"})}finally{S(!1)}},B=async m=>{if(!m.trim()||!u)return;k(!0);const i={id:`optimistic-${Date.now()}`,text:m.trim(),from:{name:"You",id:"me"},createdTime:new Date().toISOString(),attachments:[]};d(x=>[...x,i]);try{const{data:x,error:w}=await $.functions.invoke("inbox-messages",{body:{action:"send-reply",conversationId:u.id,message:m.trim(),participantId:u.participantId}});if(w)throw w;if(x!=null&&x.error)throw new Error(x.error);A(u)}catch(x){d(w=>w.filter(V=>V.id!==i.id)),r({title:"Failed to send reply",description:x.message,variant:"destructive"})}finally{k(!1)}};o.useEffect(()=>{t&&N()},[t,f]);const U=f==="all"?l:l.filter(m=>m.platform===f),q=l.filter(m=>m.platform==="facebook").length,H=l.filter(m=>m.platform==="instagram").length;return e.jsx("div",{"data-chat-layout":!0,className:"absolute inset-0 bg-background flex flex-col overflow-hidden",children:e.jsxs("div",{className:"flex-1 flex flex-col max-w-7xl mx-auto w-full min-h-0",children:[e.jsx(ie,{filter:f,setFilter:a,totalCount:l.length,fbCount:q,igCount:H,loading:h,onRefresh:()=>N()}),e.jsxs("div",{className:"flex-1 flex min-h-0 overflow-hidden",children:[e.jsx(ae,{conversations:U,activeConv:u,loading:h,loadingMore:p,hasMore:!!y,onLoadMore:F,onSelectConversation:A}),e.jsx(oe,{activeConv:u,messages:c,loadingMessages:v,sending:C,onSendReply:B})]})]})})}export{be 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.