1import{j as e,b as q,p as I,r as o,av as M,s as l,aD as u,cs as B,B as D,t as N}from"./index-CzAkNt2W.js";import{R as A}from"./RequireRole-DhUlFC_T.js";import{T as O}from"./textarea-CMw1Ny2O.js";import{S as P}from"./send-CH5TfEm8.js";function F(){return e.jsx(A,{allowed:["owner","office","technician","company_success"],children:e.jsx(T,{})})}function T(){const m=q(),c=I(m?.user??null),[i,y]=o.useState([]),[b,S]=o.useState("technician"),[a,R]=o.useState(null),[f,_]=o.useState({}),[x,j]=o.useState(""),[C,h]=o.useState(!1),v=o.useRef(null);o.useEffect(()=>{m?.user&&(async()=>{const s=await M()??"00000000-0000-0000-0000-000000000000",{data:n}=await l.from("companies").select("chat_route_to").eq("id",s).maybeSingle(),r=n?.chat_route_to??"technician";S(r);const t=u(c,"owner","office");let d=l.from("properties").select("id, address, assigned_technician_id, customers(full_name)").eq("company_id",s);if(!t&&u(c,"technician")&&r==="technician"&&(d=d.eq("assigned_technician_id",m.user.id)),r==="office"&&!t){y([]);return}const{data:p}=await d.order("address",{ascending:!0});y((p??[]).map(g=>({id:g.id,address:g.address,assigned_technician_id:g.assigned_technician_id,customer_name:g.customers?.full_name})))})()},[m?.user?.id,c]),o.useEffect(()=>{if(i.length===0)return;const s=i.map(r=>r.id);(async()=>{const{data:r}=await l.from("chat_messages").select("*").in("property_id",s).order("created_at",{ascending:!0}),t={};(r??[]).forEach(d=>{const p=d;(t[p.property_id]||=[]).push(p)}),_(t)})();const n=l.channel("staff-chat").on("postgres_changes",{event:"INSERT",schema:"public",table:"chat_messages"},r=>{const t=r.new;s.includes(t.property_id)&&_(d=>({...d,[t.property_id]:[...d[t.property_id]??[],t]}))}).subscribe();return()=>{l.removeChannel(n)}},[i]),o.useEffect(()=>{v.current?.scrollIntoView({behavior:"smooth"})},[a,f]);async function w(){if(!a||!x.trim()||!m?.user)return;if(h(!0),!i.find(t=>t.id===a)){h(!1);return}const{data:n}=await l.from("properties").select("company_id").eq("id",a).maybeSingle();if(!n){h(!1),N.error("Property not found");return}const{error:r}=await l.from("chat_messages").insert({property_id:a,company_id:n.company_id,sender_role:"staff",sender_user_id:m.user.id,body:x.trim()});if(h(!1),r){N.error(r.message);return}j("")}const E=u(c,"owner","office")&&!u(c,"technician"),k=b==="office"&&u(c,"technician")&&!u(c,"owner","office");return e.jsxs("div",{className:"p-6 md:p-8",children:[e.jsxs("div",{className:"mb-6",children:[e.jsxs("h1",{className:"font-display text-3xl font-bold flex items-center gap-2",children:[e.jsx(B,{className:"h-7 w-7 text-primary"})," Homeowner chat"]}),e.jsxs("p",{className:"text-sm text-muted-foreground mt-1",children:["Chats route to ",b==="office"?"the office":"the assigned technician",". Change this in Branding settings."]})]}),k?e.jsx("div",{className:"rounded-2xl border border-border bg-card p-8 text-center text-sm text-muted-foreground",children:"Homeowner chats are routed to the office for this company."}):e.jsxs("div",{className:"grid md:grid-cols-[320px_1fr] gap-4 min-h-[500px]",children:[e.jsxs("aside",{className:"rounded-2xl border border-border bg-card overflow-hidden",children:[e.jsx("div",{className:"px-4 py-3 text-xs uppercase tracking-wide text-muted-foreground border-b border-border",children:E?"All properties":"Your properties"}),e.jsxs("div",{className:"max-h-[600px] overflow-y-auto",children:[i.length===0&&e.jsx("div",{className:"p-6 text-sm text-muted-foreground text-center",children:"No properties yet."}),i.map(s=>{const n=(f[s.id]??[]).at(-1),r=(f[s.id]??[]).some(t=>t.sender_role==="homeowner"&&!t.read_at);return e.jsxs("button",{onClick:()=>R(s.id),className:`w-full text-left px-4 py-3 border-b border-border hover:bg-muted transition-colors ${a===s.id?"bg-sky":""}`,children:[e.jsxs("div",{className:"flex items-center justify-between gap-2",children:[e.jsx("div",{className:"font-medium text-sm truncate",children:s.address}),r&&e.jsx("span",{className:"h-2 w-2 rounded-full bg-primary shrink-0"})]}),e.jsx("div",{className:"text-xs text-muted-foreground truncate",children:s.customer_name??"â"}),n&&e.jsxs("div",{className:"text-xs text-muted-foreground truncate mt-1",children:[n.sender_role==="homeowner"?"":"You: ",n.body]})]},s.id)})]})]}),e.jsx("section",{className:"rounded-2xl border border-border bg-card flex flex-col",children:a?e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"px-5 py-3 border-b border-border",children:[e.jsx("div",{className:"font-medium text-sm",children:i.find(s=>s.id===a)?.address}),e.jsx("div",{className:"text-xs text-muted-foreground",children:i.find(s=>s.id===a)?.customer_name})]}),e.jsxs("div",{className:"flex-1 overflow-y-auto p-4 space-y-2 min-h-[400px] max-h-[500px]",children:[(f[a]??[]).length===0?e.jsx("div",{className:"text-sm text-muted-foreground text-center py-6",children:"No messages yet."}):(f[a]??[]).map(s=>e.jsx("div",{className:`flex ${s.sender_role==="staff"?"justify-end":"justify-start"}`,children:e.jsxs("div",{className:`max-w-[75%] rounded-2xl px-3 py-2 text-sm whitespace-pre-wrap ${s.sender_role==="staff"?"bg-primary text-primary-foreground":"bg-muted text-foreground"}`,children:[s.body,e.jsx("div",{className:"text-[10px] mt-1 opacity-70",children:new Date(s.created_at).toLocaleStr
1ing([],{hour:"numeric",minute:"2-digit",month:"short",day:"numeric"})})]})},s.id)),e.jsx("div",{ref:v})]}),e.jsxs("div",{className:"p-3 border-t border-border flex gap-2",children:[e.jsx(O,{value:x,onChange:s=>j(s.target.value),placeholder:"Reply to homeownerâ¦",className:"min-h-[44px] max-h-32",onKeyDown:s=>{s.key==="Enter"&&!s.shiftKey&&(s.preventDefault(),w())}}),e.jsx(D,{onClick:w,disabled:C||!x.trim(),children:e.jsx(P,{className:"h-4 w-4"})})]})]}):e.jsx("div",{className:"flex-1 grid place-items-center text-sm text-muted-foreground",children:"Select a conversation."})})]})]})}export{F 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.