PageSourceSearch

https://aidealer360.com/assets/Chat-lPsEMOjP.js

js aidealer360.com collected 2026-09-24 12:39:05 UTC 12,964 bytes, 4 lines download raw bytes

1import{j as e}from"./ui-vendor-B12AOeKi.js";import{r as v,e as K}from"./react-vendor-DZt7wVAJ.js";import{a4 as Q,p as I,u as P,Q as O,s as d,q as B,k as T,y as W,g as C,B as m,ao as D,n as H,I as V,a as Y,aQ as $,M as G,ar as k,aR as E,aS as A}from"./index-CUOC2qQu.js";import{u as J}from"./useChatRealtime-DD3fvzbw.js";import{A as X,a as Z,b as U,c as ee,d as se,e as ae,g as te,f as re}from"./alert-dialog-Dj2y0brn.js";import{S as M}from"./switch-Doi0_Deq.js";import{T as R,b as z,c as y,a as b}from"./tabs-Cesl5pOW.js";import"./charts-vendor-B6q1L48j.js";import"./supabase-vendor-dOy44Gxq.js";import"./animation-vendor-BAVy02gd.js";import"./useRealtimeSubscription-w9Z6ZF_T.js";const L={bot_active:"Bot",awaiting_human:"Waiting",human_active:"Human",closed:"Closed"};function w({dealershipCode:r,selectedSessionId:i,onSelectSession:s,showArchived:l}){const c=Q(),{toast:n}=I(),{currentUser:h}=P(),{data:o,isLoading:S,error:f}=O({queryKey:["chat-sessions",r,l],queryFn:async()=>{let a=d.from("chat_sessions").select(`
2          *,
3          registration_users!chat_sessions_human_assigned_fkey(username)
4        `).eq("dealership_code",r);l?a=a.not("ended_at","is",null):a=a.is("ended_at",null);const{data:u,error:j}=await a.order("last_message_at",{ascending:!1}).limit(100);if(j)throw j;return u??[]},refetchInterval:5e3});v.useEffect(()=>{const a=d.channel(`chat_sessions_updates_${l?"archived":"active"}`).on("postgres_changes",{event:"*",schema:"public",table:"chat_sessions",filter:`dealership_code=eq.${r}`},()=>{c.invalidateQueries({queryKey:["chat-sessions",r,l]})}).subscribe();return()=>{d.removeChannel(a)}},[r,c,l]);const p=async a=>{const{error:u}=await d.from("chat_sessions").update({current_status:"human_active",human_assigned:(h==null?void 0:h.id)??null}).eq("id",a);n(u?{title:"Error taking over chat",description:u.message,variant:"destructive"}:{title:"You are now chatting with the customer."})};return S?e.jsx("div",{className:"p-4 text-sm text-muted-foreground",children:"Loading chats…"}):f?e.jsx("div",{className:"p-4 text-sm text-destructive",children:"Failed to load chats. Please try again."}):!o||o.length===0?e.jsx("div",{className:"p-4 text-sm text-muted-foreground",children:l?"No archived chats.":"No active chats."}):e.jsx(e.Fragment,{children:o.map(a=>{var _;const u=((_=a.registration_users)==null?void 0:_.username)||"Unassigned",j=a.current_status==="awaiting_human";return e.jsx(B,{role:"button",tabIndex:0,className:T("cursor-pointer transition-colors",i===a.id?"border-primary":""),onClick:()=>s(a.id),onKeyPress:g=>g.key==="Enter"&&s(a.id),children:e.jsxs(W,{className:"p-4",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[e.jsx("p",{className:"font-semibold text-sm",children:a.lead_name||"Website visitor"}),a.inquiry_type&&e.jsx(C,{variant:"secondary",className:"text-[10px] capitalize",children:a.inquiry_type})]}),a.lead_phone&&e.jsxs("p",{className:"text-xs text-muted-foreground mb-0.5",children:["Phone: ",a.lead_phone]}),a.lead_email&&e.jsxs("p",{className:"text-xs text-muted-foreground mb-0.5",children:["Email: ",a.lead_email]}),e.jsx("p",{className:"text-sm text-muted-foreground mb-1",children:u}),e.jsx("p",{className:"text-xs text-muted-foreground mb-1",children:new Date(a.last_message_at).toLocaleString()}),a.ended_at&&e.jsx("p",{className:"text-xs text-amber-500 mb-1",children:"Customer ended chat"}),e.jsxs("div",{className:"flex items-center justify-between gap-2 mt-2",children:[e.jsxs("p",{className:"text-xs text-muted-foreground italic",children:["Status: ",L[a.current_status]??"Unknown"]}),e.jsx(C,{variant:a.current_status==="awaiting_human"?"destructive":a.current_status==="human_active"?"default":a.current_status==="closed"?"outline":"secondary",className:"text-[10px]",children:L[a.current_status]??"Unknown"})]}),j&&e.jsx(m,{size:"sm",variant:"secondary",className:"mt-3 text-xs",onClick:g=>{g.stopPropagation(),p(a.id)},children:"Take over chat"})]})},a.id)})})}function F({sessionId:r}){const{messages:i,session:s,loading:l}=J(r),{currentUser:c}=P(),{toast:n}=I(),[h,o]=v.useState(""),[S,f]=v.useState(!1),[p,a]=v.useState(!1);if(l||!s)return e.jsx("div",{className:"p-4 text-sm text-muted-foreground",children:"Loading conversation…"});const u=async()=>{const{error:t}=await d.from("chat_sessions").update({current_status:"human_active",human_assigned:(c==null?void 0:c.id)??null}).eq("id",r);n(t?{title:"Error",description:t.message,variant:"destructive"}:{title:"Success",description:"You've taken over this chat"})},j=async()=>{const{error:t}=await d.from("chat_sessions").update({current_status:"bot_active",human_assigned:null}).eq("id",r);n(t?{title:"Error",description:t.message,variant:"destructive"}:{title:"Success",description:"Chat returned to bot"})},_=async()=>{const{error:t}=await d.from("chat_sessions").update({current_status:"closed",ended_at:new Date().toISOString()}).eq("id",r);
4if(t)n({title:"Error",description:t.message,variant:"destructive"});else{n({title:"Success",description:"Chat closed"});const x=(s==null?void 0:s.lead_name)&&s.lead_name.trim(),N=(s==null?void 0:s.lead_email)&&s.lead_email.trim()||(s==null?void 0:s.lead_phone)&&s.lead_phone.trim();x&&N&&f(!0)}},g=async()=>{a(!0),f(!1);try{const{data:t,error:x}=await d.functions.invoke("send-chat-to-crm-email",{body:{sessionId:r}});if(x)throw x;if(t.success)n({title:"Success",description:"Lead information sent to CRM email successfully"});else throw new Error(t.error||"Failed to send to CRM email")}catch(t){console.error("Send to CRM email error:",t),n({title:"Error",description:t.message||"Failed to send to CRM email",variant:"destructive"})}finally{a(!1)}},q=async t=>{t.preventDefault();const x=h.trim();if(!x)return;o("");const{error:N}=await d.from("chat_messages").insert({session_id:r,sender_type:"human",sender_id:(c==null?void 0:c.id)??null,message:x});N?n({title:"Error",description:N.message,variant:"destructive"}):await d.from("chat_sessions").update({last_message_at:new Date().toISOString()}).eq("id",r)};return e.jsxs(e.Fragment,{children:[e.jsx(X,{open:S,onOpenChange:f,children:e.jsxs(Z,{children:[e.jsxs(U,{children:[e.jsx(ee,{children:"Send Lead to CRM?"}),e.jsxs(se,{children:["This chat contains lead information. Would you like to send it to your CRM email address?",e.jsxs("div",{className:"mt-3 p-3 bg-muted rounded-md text-sm",children:[e.jsxs("p",{children:[e.jsx("strong",{children:"Name:"})," ",s==null?void 0:s.lead_name]}),(s==null?void 0:s.lead_email)&&e.jsxs("p",{children:[e.jsx("strong",{children:"Email:"})," ",s.lead_email]}),(s==null?void 0:s.lead_phone)&&e.jsxs("p",{children:[e.jsx("strong",{children:"Phone:"})," ",s.lead_phone]}),(s==null?void 0:s.inquiry_type)&&e.jsxs("p",{children:[e.jsx("strong",{children:"Inquiry:"})," ",s.inquiry_type]})]})]})]}),e.jsxs(ae,{children:[e.jsx(te,{children:"Skip"}),e.jsxs(re,{onClick:g,disabled:p,children:[e.jsx(D,{className:"h-4 w-4 mr-2"}),p?"Sending...":"Send to CRM"]})]})]})}),e.jsxs("div",{className:"flex flex-col h-full",children:[e.jsx("div",{className:"border-b p-4 bg-card",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[e.jsx("h3",{className:"font-semibold",children:s.lead_name||"Website Visitor"}),s.inquiry_type&&e.jsx(C,{variant:"secondary",className:"text-[10px] capitalize",children:s.inquiry_type})]}),e.jsxs("div",{className:"text-xs text-muted-foreground space-y-0.5",children:[s.lead_phone&&e.jsxs("p",{children:["Phone: ",s.lead_phone]}),s.lead_email&&e.jsxs("p",{children:["Email: ",s.lead_email]}),!s.lead_phone&&!s.lead_email&&e.jsxs("p",{children:["Status: ",s.current_status]})]}),s.ended_at&&e.jsx(C,{variant:"outline",className:"mt-1 text-amber-600 border-amber-600",children:"Customer ended chat"})]}),e.jsxs("div",{className:"flex gap-2",children:[(s.current_status==="closed"||s.ended_at)&&s.lead_name&&(s.lead_email&&s.lead_email.trim()||s.lead_phone&&s.lead_phone.trim())&&e.jsxs(m,{size:"sm",onClick:g,disabled:p,children:[e.jsx(D,{className:"h-4 w-4 mr-2"}),p?"Sending...":"Send to CRM"]}),!s.ended_at&&s.current_status!=="human_active"&&e.jsx(m,{size:"sm",onClick:u,children:"Take Over"}),!s.ended_at&&s.current_status==="human_active"&&e.jsx(m,{size:"sm",variant:"outline",onClick:j,children:"Return to Bot"}),!s.ended_at&&s.current_status!=="closed"&&e.jsx(m,{size:"sm",variant:"destructive",onClick:_,children:"Close"})]})]})}),e.jsx(H,{className:"flex-1 p-4 bg-background",children:e.jsx("div",{className:"space-y-2",children:i.map(t=>e.jsx("div",{className:T("flex",t.sender_type==="customer"?"justify-start":"justify-end"),children:e.jsx("div",{className:T("rounded-xl px-4 py-2 max-w-md text-sm whitespace-pre-wrap",t.sender_type==="customer"?"bg-muted text-foreground":"bg-primary text-primary-foreground"),children:t.message})},t.id))})}),e.jsx("form",{onSubmit:q,className:"border-t p-4 bg-card",children:s.ended_at?e.jsx("div",{className:"text-center text-sm text-muted-foreground py-2",children:"Customer has ended this chat session"}):e.jsxs("div",{className:"flex gap-2",children:[e.jsx(V,{value:h,onChange:t=>o(t.target.value),placeholder:"Type your message...",disabled:s.current_status==="closed",onKeyDown:t=>{t.key==="Enter"&&!t.shiftKey&&(t.preventDefault(),q(t))}}),e.jsx(m,{type:"submit",disabled:s.current_status==="closed",children:"Send"})]})})]})]})}function ne(){const{selectedDealership:r}=Y(),[i,s]=v.useState(null),{theme:l,setTheme:c}=$(),n=K(),h=G(),o=()=>{c(l==="dark"?"light":"dark")};return r?h?i?e.jsxs("div",{className:"flex flex-col h-[calc(100dvh-4rem)] bg-background",children:[e.jsxs("div",{className:"flex items-center gap-2 p-2 border-b",children:[e.jsx(m,{variant:"ghost",size:"icon",onClick:()=>s(null),className:"h-9 w-9",children:e.jsx(k,{className:"h-4 w-4"})}),e.jsx("span",{className:"text-sm font-medium truncate",children:"Conversation"})]}),e.jsx("div",{className:"flex-1 min-h-0",children:e.jsx(F,{sessionId:i})})]}):e.jsxs("div",{className:"flex flex-col h-[calc(100dvh-4rem)] bg-background overflow-y-auto p-3",children:[e.jsxs("div",{className:"flex items-center justify-between mb-3 gap-2",children:[e.jsxs("div",{className:"flex items-center gap-2 min-w-0",children:[e.jsx(m,{variant:"ghost",size:"icon",onClick:()=>n("/"),className:"h-8 w-8 shrink-0",children:e.jsx(k,{className:"h-4 w-4"})}),e.jsxs("div",{className:"min-w-0",children:[e.jsx("h2",{className:"text-base font-semibold truncate",children:"Chat Management"}),e.jsx("p",{className:"text-xs text-muted-foreground truncate",children:r.name})]})]}),e.jsxs("div",{className:"flex items-center gap-1 shrink-0",children:[e.jsx(E,{className:"h-4 w-4"}),e.jsx(M,{checked:l==="dark",onCheckedChange:o}),e.jsx(A,{className:"h-4 w-4"})]})]}),e.jsxs(R,{defaultValue:"active",className:"w-full",children:[e.jsxs(z,{className:"grid w-full grid-cols-2",children:[e.jsx(y,{value:"active",children:"Active"}),e.jsx(y,{value:"archived",children:"Archived"})]}),e.jsx(b,{value:"active",className:"space-y-2 mt-3",children:e.jsx(w,{dealershipCode:r.code,selectedSessionId:i,onSelectSession:s,showArchived:!1})}),e.jsx(b,{value:"archived",className:"space-y-2 mt-3",children:e.jsx(w,{dealershipCode:r.code,selectedSessionId:i,onSelectSession:s,showArchived:!0})})]})]}):e.jsxs("div",{className:"flex h-[calc(100vh-4rem)] bg-background",children:[e.jsxs("div",{className:"w-1/3 border-r overflow-y-auto p-4",children:[e.jsxs("div",{className:"flex items-center justify-between mb-4",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(m,{variant:"ghost",size:"icon",onClick:()=>n("/"),className:"h-8 w-8",children:e.jsx(k,{className:"h-4 w-4"})}),e.jsxs("div",{children:[e.jsx("h2",{className:"text-xl font-semibold",children:"Chat Management"}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:["Dealership: ",r.name]})]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(E,{className:"h-4 w-4"}),e.jsx(M,{checked:l==="dark",onCheckedChange:o}),e.jsx(A,{className:"h-4 w-4"})]})]}),e.jsxs(R,{defaultValue:"active",className:"w-full",children:[e.jsxs(z,{className:"grid w-full grid-cols-2",children:[e.jsx(y,{value:"active",children:"Active Chats"}),e.jsx(y,{value:"archived",children:"Archived"})]}),e.jsx(b,{value:"active",className:"space-y-2 mt-4",children:e.jsx(w,{dealershipCode:r.code,selectedSessionId:i,onSelectSession:s,showArchived:!1})}),e.jsx(b,{value:"archived",className:"space-y-2 mt-4",children:e.jsx(w,{dealershipCode:r.code,selectedSessionId:i,onSelectSession:s,showArchived:!0})})]})]}),e.jsx("div",{className:"w-2/3",children:i?e.jsx(F,{sessionId:i}):e.jsx("div",{className:"h-full flex items-center justify-center text-muted-foreground",children:"Select a chat to begin"})})]}):e.jsx(B,{children:e.jsx(W,{className:"p-6",children:"Please select a dealership to view chats."})})}function ge(){return e.jsx(ne,{})}export{ge 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.