1import{a as c,j as e}from"./trpc-vendor-Bta84Nsh.js";import{u as X,t as w,B as Y}from"./index-_3-B0itT.js";import{p as ee,bl as te,X as F,a8 as re,t as v,ak as se,bE as z,F as W,ci as ne,bB as ae,aZ as oe,D as le}from"./ui-vendor-NEZNvIbV.js";import"./react-vendor-DpXpH6G7.js";function ie(r){if(!r)return[];if(Array.isArray(r))return r;try{const s=JSON.parse(r);return Array.isArray(s)?s:[]}catch{return[]}}function ce({att:r,dark:s}){const a=r.mimeType?.startsWith("image/"),d=r.mimeType?.startsWith("audio/");return a?e.jsx("a",{href:r.url,target:"_blank",rel:"noopener noreferrer",className:"block mt-1",children:e.jsx("img",{src:r.url,alt:r.name,className:"rounded-lg max-w-full max-h-64 object-contain"})}):d?e.jsx("audio",{controls:!0,src:r.url,className:"mt-1 max-w-full",style:{height:36}}):e.jsxs("a",{href:r.url,target:"_blank",rel:"noopener noreferrer",download:r.name,className:`mt-1 flex items-center gap-2 rounded-lg px-2.5 py-2 text-xs no-underline ${s?"bg-white/20 text-white":"bg-background/60 text-foreground"}`,children:[e.jsx(W,{className:"w-4 h-4 shrink-0"}),e.jsx("span",{className:"truncate flex-1",children:r.name}),e.jsx(le,{className:"w-3.5 h-3.5 shrink-0 opacity-70"})]})}function K(r){const s=new Date(r),a=new Date;return s.toDateString()===a.toDateString()?s.toLocaleTimeString("de-DE",{hour:"2-digit",minute:"2-digit"}):Math.floor((a.getTime()-s.getTime())/864e5)<7?s.toLocaleDateString("de-DE",{weekday:"short",hour:"2-digit",minute:"2-digit"}):s.toLocaleDateString("de-DE",{day:"2-digit",month:"2-digit",year:"numeric"})}function _(r){const s=(r||"?").split(/\s+/).filter(Boolean);return s.length>=2?(s[0][0]+s[1][0]).toUpperCase():(r||"?").slice(0,2).toUpperCase()}const P={admin:"Admin",manager:"Manager",sales:"Vertrieb",assistant:"Assistant",studio_team:"Studio-Team",studio_owner:"Inhaber"};function ge(){const{user:r}=X(),s=r?.id,[a,d]=c.useState(null),[p,A]=c.useState(""),{data:m=[],refetch:k}=w.brandChat.listThreads.useQuery(void 0,{refetchInterval:15e3}),b=(()=>{try{const o=new URLSearchParams(window.location.search).get("brand");return o?Number(o):null}catch{return null}})(),[x,j]=c.useState(!1);c.useEffect(()=>{x||a!=null||!b||(m.some(o=>o.brandId===b)?(d(b),j(!0)):m.length>0&&j(!0))},[m,b,x,a]);const f=m.find(o=>o.brandId===a),N=p.trim().toLowerCase(),h=N?m.filter(o=>String(o.name??"").toLowerCase().includes(N)):m;return e.jsxs("div",{className:"h-[calc(100vh-3rem)] flex",children:[e.jsxs("aside",{className:`w-72 border-r border-border flex-col bg-card/30 ${a==null?"flex":"hidden md:flex"}`,children:[e.jsxs("div",{className:"p-3 border-b border-border space-y-2",children:[e.jsxs("h1",{className:"text-sm font-semibold text-foreground flex items-center gap-2",children:[e.jsx(ee,{className:"w-4 h-4 text-orange-400"}),"Studio-Posteingang"]}),e.jsxs("div",{className:"relative",children:[e.jsx(te,{className:"w-3.5 h-3.5 text-muted-foreground absolute left-2.5 top-1/2 -translate-y-1/2"}),e.jsx("input",{value:p,onChange:o=>A(o.target.value),placeholder:"Studio suchenâ¦",className:"w-full text-xs pl-8 pr-7 py-2 rounded-lg border border-border bg-background text-foreground outline-none focus:border-orange-400"}),p&&e.jsx("button",{onClick:()=>A(""),title:"Leeren",className:"absolute right-2 top-1/2 -translate-y-1/2 text-muted-foreground hover:text-foreground",children:e.jsx(F,{className:"w-3.5 h-3.5"})})]})]}),e.jsx("div",{className:"flex-1 overflow-y-auto",children:m.length===0?e.jsx("div",{className:"p-6 text-center text-xs text-muted-foreground",children:"Noch keine Studio-Kanäle verfügbar."}):h.length===0?e.jsxs("div",{className:"p-6 text-center text-xs text-muted-foreground",children:["Kein Studio zu â",p,'".']}):h.map(o=>e.jsx(de,{thread:o,active:a===o.brandId,onSelect:()=>d(o.brandId)},o.brandId))})]}),e.jsx("main",{className:`flex-1 flex-col bg-background min-w-0 ${a==null?"hidden md:flex":"flex"}`,children:a==null?e.jsx("div",{className:"flex-1 flex items-center justify-center text-muted-foreground",children:e.jsxs("div",{className:"text-center",children:[e.jsx(re,{className:"w-12 h-12 mx-auto mb-3 opacity-20"}),e.jsx("p",{className:"text-sm",children:"Wähle ein Studio aus dem Posteingang."})]})}):e.jsx(ue,{brandId:a,myId:s??0,brandName:f?.name??"Studio",logoUrl:f?.logoUrl??null,onBack:()=>d(null),onSent:()=>k()})})]})}function de({thread:r,active:s,onSelect:a}){const d=r.lastMessage?(r.lastMessage.fromMe?"Du: ":"")+r.lastMessage.content:"Noch keine Nachricht";return e.jsxs("button",{onClick:a,className:`w-full text-left px-3 py-2.5 border-b border-border/50 hover:bg-muted/30 transition-colors flex items-start gap-3 ${s?"bg-orange-500/10 border-l-2 border-l-orange-500":""}`,children:[r.logoUrl?e.jsx("img",{src:r.logoUrl,alt:"",className:"w-9 h-9 rounded-full object-cover bg-muted shrink-0"}):e.jsx("div",{className:"w-9 h-9 rounded-full bg-orange-500/20 text-orange-300 flex items-center justify-center text-xs font-semibold shrink-0",children:_(r.name)}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center justify-between gap-2",children:[e.jsx("p",{className:"text-sm font-medium text-foreground truncate",children:r.name}),r.lastMessage&&e.jsx("span",{className:"text-[10px] text-muted-foreground shrink-0",children:K(r.lastMessage.createdAt)})]}),e.jsxs("div",{className:"flex items-center justify-between gap-2 mt-0.5",children:[e.jsx("p",{className:"text-xs text-muted-foreground truncate",children:d}),r.unreadCount>0&&e.jsx("span",{className:"bg-orange-500 text-white text-[10px] font-bold rounded-full min-w-[18px] h-[18px] flex items-center justify-center px-1.5 shrink-0",children:r.unreadCount})]})]})]})}function ue({brandId:r,myId:s,brandName:a,logoUrl:d,onBack:p,onSent:A}){const m=w.useUtils(),[k,b]=c.useState(""),[x,j]=c.useState([]),[f,N]=c.useState(!1),[h,o]=c.useState(!1),C=c.useRef(null),I=c.useRef(null),T=c.useRef([]),D=c.useRef(null),U=w.tasks.uploadAttachment.useMutation(),{data:S=[],refetch:O}=w.brandChat.listMessages.useQuery({brandId:r,limit:200},{refetchInterval:5e3}),M=w.brandChat.sendMessage.useMutation({onSuccess:()=>
1{b(""),j([]),O(),A()},onError:t=>v.error(t?.message??"Senden fehlgeschlagen")}),q=w.brandChat.markRead.useMutation(),{data:B}=w.brandChat.studioReadAt.useQuery({brandId:r},{refetchInterval:1e4}),E=Number(B?.readAt??0),$=B?.byName??null,Q=(()=>{const t=S.filter(n=>n.fromUserId===s);return t.length?t[t.length-1].id:null})(),G=async t=>{if(!(!t||t.length===0)){N(!0);try{for(const n of Array.from(t)){if(n.size>50*1024*1024){v.error(`${n.name} ist gröÃer als 50 MB`);continue}const l=new Uint8Array(await n.arrayBuffer());let i="";for(let u=0;u<l.length;u++)i+=String.fromCharCode(l[u]);const g=await U.mutateAsync({fileBase64:btoa(i),fileName:n.name,mimeType:n.type||"application/octet-stream"});j(u=>[...u,g])}}catch(n){v.error(n?.message??"Upload fehlgeschlagen")}finally{N(!1),C.current&&(C.current.value="")}}},H=t=>j(n=>n.filter(l=>l.url!==t)),V=async()=>{try{const t=await navigator.mediaDevices.getUserMedia({audio:!0}),n=new MediaRecorder(t);T.current=[],n.ondataavailable=l=>{l.data.size&&T.current.push(l.data)},n.onstop=async()=>{t.getTracks().forEach(i=>i.stop());const l=new Blob(T.current,{type:n.mimeType||"audio/webm"});if(l.size<400){v.error("Aufnahme zu kurz");return}N(!0);try{const i=new Uint8Array(await l.arrayBuffer());let g="";for(let y=0;y<i.length;y++)g+=String.fromCharCode(i[y]);const u=(l.type.split("/")[1]||"webm").split(";")[0],R=await U.mutateAsync({fileBase64:btoa(g),fileName:`Sprachnachricht.${u}`,mimeType:l.type||"audio/webm"});j(y=>[...y,R])}catch(i){v.error(i?.message??"Upload fehlgeschlagen")}finally{N(!1)}},n.start(),I.current=n,o(!0)}catch{v.error("Mikrofon nicht verfügbar / nicht erlaubt")}},Z=()=>{try{I.current?.stop()}catch{}o(!1)};c.useEffect(()=>{q.mutate({brandId:r}),m.brandChat.listThreads.invalidate(),m.brandChat.unreadTotal.invalidate()},[r,S.length]),c.useEffect(()=>{D.current&&(D.current.scrollTop=D.current.scrollHeight)},[S.length]);const L=()=>{const t=k.trim();!t&&x.length===0||M.isPending||f||M.mutate({brandId:r,content:t,attachments:x.length?x:void 0})};return e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"px-4 py-3 border-b border-border flex items-center gap-3 bg-card/30",children:[e.jsx("button",{onClick:p,className:"md:hidden text-muted-foreground hover:text-foreground",children:e.jsx(se,{className:"w-4 h-4"})}),d?e.jsx("img",{src:d,alt:"",className:"w-9 h-9 rounded-full object-cover bg-muted"}):e.jsx("div",{className:"w-9 h-9 rounded-full bg-orange-500/20 text-orange-300 flex items-center justify-center text-xs font-semibold",children:_(a)}),e.jsxs("div",{className:"min-w-0",children:[e.jsx("p",{className:"text-sm font-semibold text-foreground truncate",children:a}),e.jsx("p",{className:"text-[11px] text-muted-foreground",children:"Marken-Kanal"})]})]}),e.jsx("div",{ref:D,className:"flex-1 overflow-y-auto p-4 space-y-2",children:S.length===0?e.jsx("div",{className:"text-center text-xs text-muted-foreground py-8",children:"Noch keine Nachrichten. Schreib die erste!"}):S.map((t,n,l)=>{const i=t.fromUserId===s,g=l[n-1],u=g&&g.fromUserId===t.fromUserId&&new Date(t.createdAt).getTime()-new Date(g.createdAt).getTime()<5*6e4,R=t.senderName||P[t.senderRole]||"Unbekannt";return e.jsx("div",{className:`flex ${i?"justify-end":"justify-start"}`,children:e.jsxs("div",{className:`max-w-[75%] ${u?"mt-0.5":"mt-2"}`,children:[!i&&!u&&e.jsxs("p",{className:"text-[10px] text-muted-foreground mb-0.5 px-1",children:[R,t.senderRole?` · ${P[t.senderRole]??t.senderRole}`:""]}),e.jsxs("div",{className:`rounded-2xl px-3 py-2 text-sm ${i?"bg-orange-500 text-white rounded-br-sm":"bg-muted text-foreground rounded-bl-sm"}`,children:[t.content?e.jsx("p",{className:"whitespace-pre-wrap break-words",children:t.content}):null,ie(t.attachments).map((y,J)=>e.jsx(ce,{att:y,dark:i},J))]}),!u&&e.jsx("p",{className:`text-[10px] text-muted-foreground mt-1 px-1 ${i?"text-right":""}`,children:K(new Date(t.createdAt).getTime())}),i&&t.id===Q&&e.jsx("p",{className:`text-[10px] px-1 text-right mt-0.5 ${new Date(t.createdAt).getTime()<=E?"text-emerald-500 font-semibold":"text-muted-foreground"}`,children:new Date(t.createdAt).getTime()<=E?`ââ Gelesen${$?` · ${$}`:""}`:"â Gesendet"})]})},t.id)})}),e.jsxs("div",{className:"border-t border-border p-3 bg-card/30",children:[x.length>0&&e.jsx("div",{className:"flex flex-wrap gap-2 mb-2",children:x.map(t=>{const n=t.mimeType?.startsWith("image/"),l=t.mimeType?.startsWith("audio/");return e.jsxs("div",{className:"flex items-center gap-1.5 rounded-lg border border-border bg-background px-2 py-1 text-xs text-foreground max-w-[220px]",children:[n?e.jsx("img",{src:t.url,alt:"",className:"w-6 h-6 rounded object-cover"}):e.jsx("span",{className:"text-muted-foreground",children:l?e.jsx(z,{className:"w-3.5 h-3.5"}):e.jsx(W,{className:"w-3.5 h-3.5"})}),e.jsx("span",{className:"truncate flex-1",children:t.name}),e.jsx("button",{onClick:()=>H(t.url),className:"text-muted-foreground hover:text-red-400 shrink-0",children:e.jsx(F,{className:"w-3.5 h-3.5"})})]},t.url)})}),e.jsxs("div",{className:"flex gap-2 items-end",children:[e.jsx("input",{ref:C,type:"file",multiple:!0,accept:"image/*,.pdf,.doc,.docx,.xls,.xlsx,.csv,.txt,audio/*,video/*",className:"hidden",onChange:t=>G(t.target.files)}),e.jsx("button",{type:"button",title:"Datei / Foto anhängen",onClick:()=>C.current?.click(),disabled:f,className:"shrink-0 h-10 w-10 flex items-center justify-center rounded-xl border border-border text-muted-foreground hover:text-foreground hover:bg-muted/40 disabled:opacity-50",children:e.jsx(ne,{className:"w-4 h-4"})}),e.jsx("button",{type:"button",title:h?"Aufnahme stoppen":"Sprachnachricht aufnehmen",onClick:h?Z:V,disabled:f&&!h,className:`shrink-0 h-10 w-10 flex items-center justify-center rounded-xl border disabled:opacity-50 ${h?"border-red-500 bg-red-500/10 text-red-400 animate-pulse":"border-border text-muted-foreground hover:text-foreground hover:bg-muted/40"}`,children:h?e.jsx(ae,{className:"w-4 h-4"}):e.jsx(z,{className:"w-4 h-4"})}),e.jsx("textarea",{value:k,onChange:t=>b(t.target.value),onKeyDown:t=>{t.key==="Enter"&&!t.shiftKey&&(t.preventDefault(),L())},placeholder:h?"Aufnahme läuftâ¦":"Nachricht schreiben⦠(Enter zum Senden, Shift+Enter neue Zeile)",rows:1,className:"flex-1 rounded-xl border border-border bg-background px-3 py-2 text-sm resize-none focus:outline-none focus:ring-2 focus:ring-orange-500/40 max-h-32 text-foreground",style:{minHeight:"40px"}}),e.jsx(Y,{onClick:L,disabled:!k.trim()&&x.length===0||M.isPending||f,className:"bg-orange-500 hover:bg-orange-600 text-white shrink-0 h-10",children:f?"â¦":e.jsx(oe,{className:"w-4 h-4"})})]})]})]})}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.