1import{r as a,j as e,s as b}from"./index-BJpQ1SV3.js";import{u as C}from"./auth-middleware-Dv-5hBKz.js";import{a as M}from"./chat.functions-CctpQGfO.js";import{R}from"./refresh-cw-CO5OcmVr.js";import{M as E}from"./message-circle-OhoxP59u.js";import{M as L}from"./mail-BFeGpgVk.js";import{S as k}from"./send-DoyGYcww.js";function o(t){return!t||!/^[A-Z]{2}$/.test(t)?null:String.fromCodePoint(...[...t].map(d=>127462+d.charCodeAt(0)-65))}function m(t){if(!t)return null;try{return new Intl.DisplayNames(["en"],{type:"region"}).of(t)??t}catch{return t}}function $(){const[t,d]=a.useState([]),[x,u]=a.useState(!0),[n,N]=a.useState(null),[w,p]=a.useState([]),[_,f]=a.useState(!1),[c,g]=a.useState(""),[h,v]=a.useState(!1),S=C(M);async function y(s){const{data:l}=await b.from("chat_messages").select("*").eq("conversation_id",s).order("created_at",{ascending:!0});p(l??[])}async function j(){const s=c.trim();if(!(!s||!n||h)){v(!0);try{(await S({data:{conversationId:n,content:s}})).ok&&(g(""),await y(n),i())}finally{v(!1)}}}async function i(){u(!0);const{data:s}=await b.from("chat_conversations").select("*").order("last_message_at",{ascending:!1}).limit(200);d(s??[]),u(!1)}a.useEffect(()=>{i();const s=setInterval(i,2e4);return()=>clearInterval(s)},[]),a.useEffect(()=>{if(!n){p([]);return}f(!0),y(n).finally(()=>f(!1))},[n]);const r=a.useMemo(()=>t.find(s=>s.id===n)??null,[t,n]);return e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-xl font-bold tracking-tight",children:"Messages"}),e.jsxs("p",{className:"text-xs text-foreground/55 mt-1",children:[t.length," message",t.length===1?"":"s"]})]}),e.jsxs("button",{onClick:i,className:"inline-flex items-center gap-1.5 text-xs font-semibold py-2 px-3 rounded-full border border-border/60 hover:bg-muted",children:[e.jsx(R,{className:"size-3.5"})," Refresh"]})]}),e.jsxs("div",{className:"grid md:grid-cols-[340px_1fr] gap-4 min-h-[60vh]",children:[e.jsxs("div",{className:"rounded-2xl border border-border/60 bg-card overflow-hidden",children:[x&&e.jsx("div",{className:"p-4 text-sm text-foreground/55",children:"Loading..."}),!x&&t.length===0&&e.jsxs("div",{className:"p-6 text-center text-sm text-foreground/55",children:[e.jsx(E,{className:"size-6 mx-auto mb-2 opacity-40"}),"No messages yet."]}),e.jsx("ul",{className:"divide-y divide-border/60 max-h-[70vh] overflow-y-auto",children:t.map(s=>{const l=s.id===n;return e.jsx("li",{children:e.jsxs("button",{onClick:()=>N(s.id),className:`w-full text-left px-4 py-3 transition ${l?"bg-blush/60":"hover:bg-muted/60"}`,children:[e.jsxs("div",{className:"flex items-center justify-between gap-2",children:[e.jsx("div",{className:"text-xs font-semibold text-foreground/80 truncate",children:s.visitor_name||s.visitor_email||"Anonymous visitor"}),s.notified_at&&e.jsx(L,{className:"size-3 text-rose shrink-0","aria-label":"Email alert sent"})]}),s.visitor_email&&e.jsx("div",{className:"text-[11px] text-foreground/60 truncate mt-0.5",children:s.visitor_email}),e.jsx("div",{className:"text-xs text-foreground/70 truncate mt-1",children:s.first_user_message??"(no message)"}),e.jsxs("div",{className:"text-[11px] text-foreground/50 mt-1 flex items-center gap-2",children:[e.jsx("span",{children:new Date(s.last_message_at).toLocaleString()}),e.jsx("span",{children:"-"}),e.jsx("span",{className:"uppercase",children:s.locale??"en"}),o(s.country)&&e.jsxs(e.Fragment,{children:[e.jsx("span",{children:"-"}),e.jsxs("span",{title:m(s.country)??void 0,children:[o(s.country)," ",m(s.country)]})]}),e.jsx("span",{children:"-"}),e.jsxs("span",{children:[s.message_count," msg"]})]})]})},s.id)})})]}),e.jsxs("div",{className:"rounded-2xl border border-border/60 bg-card p-5",children:[!r&&e.jsx("div",{className:"h-full grid place-items-center text-sm text-foreground/50",children:"Select a message to view it."}),r&&e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"border-b border-border/60 pb-3",children:[e.jsxs("div",{className:"text-sm font-semibold",children:[r.visitor_name||"Visitor",e.jsx("span",{className:"ml-2 text-[11px] font-normal text-foreground
1/50 uppercase",children:r.locale??"en"}),o(r.country)&&e.jsxs("span",{className:"ml-2 text-[11px] font-normal text-foreground/50",children:[o(r.country)," ",m(r.country)]})]}),r.visitor_email?e.jsxs("div",{className:"text-xs mt-1",children:[e.jsx("a",{href:`mailto:${r.visitor_email}`,className:"text-rose underline",children:r.visitor_email}),e.jsx("span",{className:"text-foreground/50",children:" - your reply is emailed here in their language"})]}):e.jsx("div",{className:"text-xs text-foreground/50 mt-1",children:"No email given (older message)"}),e.jsx("div",{className:"text-[11px] text-foreground/50 mt-1 font-mono",children:r.id}),r.user_agent&&e.jsx("div",{className:"text-[11px] text-foreground/50 mt-1 truncate",children:r.user_agent})]}),_&&e.jsx("div",{className:"text-sm text-foreground/55",children:"Loading messages..."}),e.jsx("div",{className:"space-y-2.5 max-h-[60vh] overflow-y-auto pr-1",children:w.map(s=>e.jsx("div",{className:s.role==="user"?"flex justify-end":"flex justify-start",children:e.jsxs("div",{className:s.role==="user"?"max-w-[80%] rounded-2xl rounded-br-md bg-rose text-white px-4 py-2.5 text-[13px] whitespace-pre-wrap":"max-w-[80%] rounded-2xl rounded-bl-md bg-muted text-foreground px-4 py-2.5 text-[13px] whitespace-pre-wrap",children:[s.content,e.jsx("div",{className:"text-[10px] opacity-60 mt-1",children:new Date(s.created_at).toLocaleTimeString()})]})},s.id))}),e.jsxs("form",{onSubmit:s=>{s.preventDefault(),j()},className:"flex items-end gap-2 pt-3 border-t border-border/60",children:[e.jsx("textarea",{value:c,onChange:s=>{g(s.target.value),s.target.style.height="auto",s.target.style.height=Math.min(s.target.scrollHeight,320)+"px"},onKeyDown:s=>{s.key==="Enter"&&(s.metaKey||s.ctrlKey)&&(s.preventDefault(),j())},rows:4,placeholder:"Type your reply... (Cmd/Ctrl + Enter to send)","aria-label":"Reply to visitor",className:"flex-1 bg-muted rounded-2xl min-h-[96px] max-h-[320px] px-4 py-3 text-[13px] leading-relaxed resize-y outline-none focus:ring-2 focus:ring-primary/30"}),e.jsx("button",{type:"submit",disabled:!c.trim()||h,"aria-label":"Send reply",className:"size-10 grid place-items-center rounded-full bg-rose text-white disabled:opacity-50 active:scale-95 transition",children:e.jsx(k,{className:"size-4"})})]})]})]})]})]})}export{$ as component};
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.