2 lines
31 KiB
JavaScript
2 lines
31 KiB
JavaScript
const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=["assets/vendor-IYr-MHu4.js","assets/vendor-react-BHG7lGYR.js","assets/vendor-BD_zwJK7.css"])))=>i.map(i=>d[i]);
|
|
import{a as l,j as e}from"./vendor-react-BHG7lGYR.js";import{l as le,t as ce,D as de,E as ue,T as Z,F as me}from"./vendor-IYr-MHu4.js";import{g as ee,D as he,E as A,B as U,p as se,a as I,u as O,d as _,l as q,L as te,_ as ae,f as F,h as xe,i as J}from"./index-BWcMVITa.js";import{a as re,u as ne,b as B}from"./vendor-tanstack-BzWBL1hV.js";import{I as oe}from"./input-BGBI2Wze.js";import{L as H}from"./label-N1Bsuay_.js";import{S as fe}from"./select-laDQECdR.js";import{D as pe,a as ge,b as ve,c as V,u as be}from"./useIsRateLimited-HpmSVPdB.js";import{C as X}from"./confirmation-dialog-D8kbmfRa.js";import{i as ye,J as we,ah as je,U as ke,ai as Ne,aj as Ce,p as Se,T as _e,ak as Q,al as Ee,k as Re,n as ie,X as $,am as De,an as G,ao as Te,ap as Me,aq as ze,ar as Le,A as Y}from"./vendor-icons-DaGlTw4_.js";import"./vendor-router-D-s5vIeO.js";import"./vendor-utils-CgOSfOkx.js";import"./dropdown-BYn_8IId.js";const z=le()(ce(de(s=>({userId:null,username:null,currentConversationId:null,conversations:[],messages:{},typingUsers:{},wsToken:null,wsUrl:null,wsStatus:"disconnected",setUserId:(o,n)=>s(r=>{r.userId=o,r.username=n}),setWsToken:(o,n)=>s(r=>{r.wsToken=o,r.wsUrl=n}),setWsStatus:o=>s(n=>{n.wsStatus=o}),addConversation:o=>s(n=>{n.conversations.some(r=>r.id===o.id)||n.conversations.push(o)}),setCurrentConversation:o=>s(n=>{n.currentConversationId=o}),addMessage:o=>s(n=>{n.messages[o.conversation_id]||(n.messages[o.conversation_id]=[]),n.messages[o.conversation_id].push(o)}),loadMessages:(o,n)=>s(r=>{const i=r.messages[o]||[],d=new Set(n.map(c=>c.id)),a=[...i.filter(c=>!d.has(c.id)),...n];a.sort((c,x)=>new Date(c.created_at).getTime()-new Date(x.created_at).getTime()),r.messages[o]=a}),addReaction:(o,n,r,i)=>s(d=>{const u=d.messages[o];if(u){const a=u.find(c=>c.id===n);a&&(a.reactions||(a.reactions={}),Object.keys(a.reactions).forEach(c=>{a.reactions[c]=a.reactions[c].filter(x=>x!==r),a.reactions[c].length===0&&delete a.reactions[c]}),a.reactions[i]||(a.reactions[i]=[]),a.reactions[i].includes(r)||a.reactions[i].push(r))}}),removeReaction:(o,n,r)=>s(i=>{const d=i.messages[o];if(d){const u=d.find(a=>a.id===n);u&&u.reactions&&Object.keys(u.reactions).forEach(a=>{u.reactions[a]=u.reactions[a].filter(c=>c!==r),u.reactions[a].length===0&&delete u.reactions[a]})}}),setUserTyping:(o,n,r)=>s(i=>{i.typingUsers[o]||(i.typingUsers[o]=[]),r?i.typingUsers[o].includes(n)||i.typingUsers[o].push(n):i.typingUsers[o]=i.typingUsers[o].filter(d=>d!==n)})})),{name:"ChatStore",enabled:!1}));function Pe({open:s,onClose:o}){const[n,r]=l.useState(""),[i,d]=l.useState("public"),[u,a]=l.useState(!1),[c,x]=l.useState(null),[v,f]=l.useState(null),[t,w]=l.useState(0),p=l.useRef(null),C=ee(),{addConversation:D,setCurrentConversation:T}=z(),E=async()=>{if(x(null),f(null),!n.trim()){x("Room name is required");return}const j=n.trim(),b=i,y=async()=>{const N=await I.post("/conversations",{name:j,type:b}),k={id:N.data.id||N.data.conversation?.id,name:N.data.name||N.data.conversation?.name,type:N.data.type||N.data.conversation?.type||b,participants:N.data.participants||[],unread_count:0};D(k),T(k.id),C.success("Room created successfully"),r(""),d("public"),f(null),w(0),p.current=null,o()};p.current=y,a(!0);try{await y()}catch(N){const k=se(N);f(new Error(k.message))}finally{a(!1)}},L=async()=>{if(!(!p.current||t>=3)){w(j=>j+1),a(!0);try{await p.current()}catch{}finally{a(!1)}}};return e.jsx(he,{open:s,onClose:o,title:"Create New Room",variant:"default",size:"md",children:e.jsxs("div",{className:"space-y-4",children:[v&&e.jsx(A,{error:v,variant:"banner",severity:"error",context:{action:"creating room",resource:"conversation"},onRetry:t<3?L:void 0,onDismiss:()=>{f(null),w(0),p.current=null}}),c&&e.jsx(A,{error:c,variant:"inline",severity:"error",size:"sm",dismissible:!1}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(H,{htmlFor:"room-name",children:"Room Name"}),e.jsx(oe,{id:"room-name",value:n,onChange:j=>{r(j.target.value),x(null)},placeholder:"Enter room name",maxLength:100})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(H,{htmlFor:"room-type",children:"Room Type"}),e.jsx(fe,{options:[{value:"public",label:"Public"},{value:"private",label:"Private"}],value:i,onChange:j=>d(Array.isArray(j)?j[0]:j),name:"room-type"})]}),e.jsxs("div",{className:"flex justify-end gap-2 pt-4",children:[e.jsx(U,{variant:"outline",onClick:o,disabled:u,children:"Cancel"}),e.jsx(U,{onClick:E,disabled:u||!n.trim(),children:u?"Creating...":"Create Room"})]})]})})}const Ue=({conversation:s,onSelect:o,isSelected:n})=>{const{data:r}=O(),i=re(),d=ee(),{setCurrentConversation:u}=z(),[a,c]=l.useState(!1),[x,v]=l.useState(!1),[f,t]=l.useState(null),[w,p]=l.useState(0),[C,D]=l.useState(null),[T,E]=l.useState(null),L=B({mutationFn:async m=>{await I.delete(`/conversations/${m}/participants/${r?.id}`)},onMutate:async m=>{await i.cancelQueries({queryKey:["chatConversations",r?.id]});const S=i.getQueryData(["chatConversations",r?.id]);return S&&i.setQueryData(["chatConversations",r?.id],S.filter(R=>R.id!==m)),{previousConversations:S}},onError:(m,S,R)=>{R?.previousConversations&&i.setQueryData(["chatConversations",r?.id],R.previousConversations);const W=m.response?.data?.error||"Failed to leave room";t(new Error(W))},onSuccess:()=>{i.invalidateQueries({queryKey:["chatConversations",r?.id]}),d.success("Left room successfully"),u(null),c(!1),t(null)}}),j=B({mutationFn:async m=>{await I.delete(`/conversations/${m}`)},onMutate:async m=>{await i.cancelQueries({queryKey:["chatConversations",r?.id]});const S=i.getQueryData(["chatConversations",r?.id]);return S&&i.setQueryData(["chatConversations",r?.id],S.filter(R=>R.id!==m)),{previousConversations:S}},onError:(m,S,R)=>{R?.previousConversations&&i.setQueryData(["chatConversations",r?.id],R.previousConversations);const W=m.response?.data?.error||"Failed to delete room";t(new Error(W))},onSuccess:()=>{i.invalidateQueries({queryKey:["chatConversations",r?.id]}),d.success("Room deleted successfully"),u(null),v(!1),t(null),p(0),D(null),E(null)}}),b=m=>{m.stopPropagation(),c(!0)},y=m=>{m.stopPropagation(),v(!0)},N=()=>{D("leave"),E(s.id),L.mutate(s.id)},k=()=>{D("delete"),E(s.id),j.mutate(s.id)},g=async()=>{if(!(!C||!T||w>=3)){p(m=>m+1);try{C==="leave"?await L.mutateAsync(T):await j.mutateAsync(T)}catch{}}};return e.jsxs(e.Fragment,{children:[f&&e.jsx(A,{error:f,variant:"banner",severity:"error",context:{action:"managing room",resource:"conversation",resourceId:s.id},onRetry:w<3?g:void 0,onDismiss:()=>{t(null),p(0),D(null),E(null)}}),e.jsxs("div",{onClick:()=>o(s.id),className:_("group relative flex items-center justify-between p-4 rounded-xl cursor-pointer transition-all duration-300 border border-transparent",n?"bg-kodo-cyan/10 border-kodo-cyan/30 shadow-[0_0_15px_rgba(102,252,241,0.1)]":"hover:bg-white/5 hover:border-white/5"),children:[e.jsxs("div",{className:"flex items-center gap-4 min-w-0",children:[e.jsx("div",{className:_("w-8 h-8 rounded-lg flex items-center justify-center transition-colors",n?"bg-kodo-cyan text-kodo-void":"bg-white/5 text-kodo-secondary group-hover:text-white"),children:s.type==="direct"?e.jsx(ke,{size:14}):e.jsx(Ne,{size:14})}),e.jsxs("div",{className:"flex flex-col min-w-0",children:[e.jsx("span",{className:_("text-sm font-medium truncate transition-colors",n?"text-white":"text-kodo-secondary group-hover:text-white"),children:s.name||`Channel ${s.id.substring(0,4)}`}),s.type!=="direct"&&e.jsx("span",{className:"text-[10px] text-kodo-secondary/50 uppercase tracking-wider",children:s.type})]})]}),s.unread_count&&s.unread_count>0?e.jsx("span",{className:"bg-kodo-magenta text-white text-[10px] px-1.5 py-0.5 rounded-full font-bold shadow-lg shadow-kodo-magenta/20",children:s.unread_count}):null,e.jsxs(pe,{children:[e.jsx(ge,{asChild:!0,onClick:m=>m.stopPropagation(),children:e.jsx(U,{variant:"ghost",size:"sm",className:_("h-6 w-6 p-0 opacity-0 group-hover:opacity-100 transition-opacity",n?"text-kodo-cyan hover:bg-kodo-cyan/20":"text-kodo-secondary hover:text-white"),children:e.jsx(Ce,{className:"h-4 w-4"})})}),e.jsxs(ve,{align:"end",className:"bg-kodo-void border-white/10 text-white",children:[e.jsxs(V,{onClick:b,className:"focus:bg-white/10 cursor-pointer",children:[e.jsx(Se,{className:"mr-2 h-4 w-4"}),"Leave Channel"]}),s.type!=="direct"&&e.jsxs(V,{onClick:y,className:"text-kodo-red focus:bg-kodo-red/10 cursor-pointer",children:[e.jsx(_e,{className:"mr-2 h-4 w-4"}),"Delete Channel"]})]})]}),n&&e.jsx("div",{className:"absolute left-0 top-3 bottom-3 w-0.5 bg-kodo-cyan rounded-r-full shadow-[0_0_8px_rgba(102,252,241,0.8)]"})]}),e.jsx(X,{open:a,onClose:()=>c(!1),onConfirm:N,title:"Leave Channel",description:"Disconnect from this secure frequency? Incoming transmission will cease.",confirmLabel:"Disconnect",cancelLabel:"Cancel",variant:"default",isLoading:L.isPending}),e.jsx(X,{open:x,onClose:()=>v(!1),onConfirm:k,title:"Delete Channel",description:"Permanently purge this channel from the network? This action is irreversible.",confirmLabel:"Purge",cancelLabel:"Cancel",variant:"destructive",isLoading:j.isPending})]})},Ie=()=>{const{data:s}=O(),o=s?.id,n=re(),{conversations:r,currentConversationId:i,setCurrentConversation:d,addConversation:u}=z(),[a,c]=l.useState(!1),{data:x,isLoading:v,error:f}=ne({queryKey:["chatConversations",o],queryFn:async()=>o?(await I.get("/conversations")).data.conversations:[],enabled:!!o});return l.useEffect(()=>{x&&x.forEach(t=>{r.some(w=>w.id===t.id)||u({id:t.id,name:t.name,type:t.type,participants:t.participants,unread_count:0})})},[x,r,u]),v?e.jsx("div",{className:"flex-1 flex items-center justify-center",children:e.jsx(ye,{className:"animate-spin text-kodo-steel",size:24})}):f?e.jsx("div",{className:"flex-1 flex items-center justify-center p-4",children:e.jsx(A,{error:f instanceof Error?f:new Error("Signal Lost"),variant:"card",severity:"error",context:{action:"fetching conversations",resource:"conversations"},onRetry:()=>n.invalidateQueries({queryKey:["chatConversations",o]})})}):e.jsxs("div",{className:"flex flex-col h-full",children:[e.jsx("div",{className:"p-4 border-b border-white/5 bg-white/2 backdrop-blur-sm",children:e.jsxs("div",{className:"flex items-center justify-between mb-1",children:[e.jsxs("h2",{className:"text-sm font-bold text-white tracking-wide uppercase flex items-center gap-2",children:[e.jsx(we,{className:"w-4 h-4 text-kodo-steel"}),"Active Channels"]}),e.jsx("span",{className:"text-[10px] font-mono text-kodo-secondary bg-white/5 px-1.5 py-0.5 rounded",children:r.length})]})}),e.jsx("div",{className:"flex-1 overflow-y-auto custom-scrollbar p-4 space-y-1",children:r.length===0?e.jsxs("div",{className:"text-kodo-secondary/50 text-sm p-4 text-center italic border border-dashed border-white/5 rounded-xl m-2",children:["No active frequencies detected.",e.jsx("br",{}),"Initialize a new channel."]}):r.map(t=>e.jsx(Ue,{conversation:t,onSelect:d,isSelected:t.id===i},t.id))}),e.jsx("div",{className:"p-4 border-t border-white/5 bg-white/2 backdrop-blur-sm",children:e.jsxs(U,{onClick:()=>c(!0),className:"w-full shadow-lg shadow-kodo-steel/10",variant:"default",children:[e.jsx(je,{className:"mr-2 h-4 w-4"}),"New Channel"]})}),e.jsx(Pe,{open:a,onClose:()=>c(!1)})]})},K=()=>{const{data:s}=O(),o=s?.id,{wsToken:n,wsUrl:r,wsStatus:i,setWsStatus:d,addMessage:u,currentConversationId:a,loadMessages:c,addReaction:x,removeReaction:v,setUserTyping:f}=z(),t=l.useRef(null),w=l.useRef(0),[p,C]=l.useState([]),D=l.useCallback(()=>{if(!n||!r||t.current?.readyState===WebSocket.OPEN)return;t.current&&(t.current.onopen=null,t.current.onmessage=null,t.current.onclose=null,t.current.onerror=null,(t.current.readyState===WebSocket.OPEN||t.current.readyState===WebSocket.CONNECTING)&&t.current.close()),d("connecting");const g=`${r}?token=${n}`;t.current=new WebSocket(g);const m=()=>{d("connected"),w.current=0,C(P=>(P.forEach(M=>t.current?.send(JSON.stringify(M))),[]))},S=P=>{const M=JSON.parse(P.data);if(M.type==="NewMessage"){const h=M;h.conversation_id===a&&h.message_id&&h.sender_id&&h.content&&h.created_at&&u({id:h.message_id,conversation_id:h.conversation_id,sender_id:h.sender_id,sender_username:h.sender_username||"Unknown",content:h.content,created_at:h.created_at,attachments:h.attachments})}else if(M.type==="ReactionAdded"){const h=M;h.message_id&&h.user_id&&h.emoji&&x(h.conversation_id,h.message_id,h.user_id,h.emoji)}else if(M.type==="ReactionRemoved"){const h=M;h.message_id&&h.user_id&&v(h.conversation_id,h.message_id,h.user_id)}else if(M.type==="UserTyping"){const h=M;h.user_id&&f(h.conversation_id,h.user_id,h.is_typing??!1)}},R=()=>{d("disconnected")},W=P=>{d("error"),w.current+=1,w.current<=3&&q.error("WebSocket error",{error:P instanceof Error?P.message:String(P),stack:P instanceof Error?P.stack:void 0})};t.current.onopen=m,t.current.onmessage=S,t.current.onclose=R,t.current.onerror=W},[n,r,d,u,a,x,v,f]),T=l.useCallback(()=>{t.current&&(t.current.onopen=null,t.current.onmessage=null,t.current.onclose=null,t.current.onerror=null,(t.current.readyState===WebSocket.OPEN||t.current.readyState===WebSocket.CONNECTING)&&t.current.close(),t.current=null,d("disconnected"))},[d]),E=l.useRef(0),L=5;l.useEffect(()=>{let g;return n&&r&&i==="disconnected"&&E.current<L&&(g=setTimeout(()=>{E.current++,D()},1e3*Math.pow(2,E.current))),i==="connected"&&(E.current=0),()=>{g&&clearTimeout(g)}},[n,r,i,D]),l.useEffect(()=>()=>{T()},[T]);const j=l.useCallback((g,m)=>{if(!t.current||t.current.readyState!==WebSocket.OPEN||!a||!o){q.warn("WebSocket not open or missing conversation/user ID. Message queued.",{conversationId:a,userId:o}),C(R=>[...R,{type:"SendMessage",conversation_id:a||ue(),content:g,parent_message_id:null,attachments:m}]);return}const S={type:"SendMessage",conversation_id:a,content:g,parent_message_id:null,attachments:m};t.current.send(JSON.stringify(S))},[a,o]),b=l.useCallback(async g=>{try{const m=await I.get(`/conversations/${g}/history`);c(g,m.data.messages)}catch(m){q.error("Failed to fetch chat history",{error:m instanceof Error?m.message:String(m),stack:m instanceof Error?m.stack:void 0,conversationId:g})}},[c]),y=l.useCallback((g,m)=>{t.current?.readyState===WebSocket.OPEN&&a&&t.current.send(JSON.stringify({type:"AddReaction",conversation_id:a,message_id:g,emoji:m}))},[a]),N=l.useCallback(g=>{t.current?.readyState===WebSocket.OPEN&&a&&t.current.send(JSON.stringify({type:"RemoveReaction",conversation_id:a,message_id:g}))},[a]),k=l.useCallback(g=>{t.current?.readyState===WebSocket.OPEN&&a&&t.current.send(JSON.stringify({type:"Typing",conversation_id:a,is_typing:g}))},[a]);return{wsStatus:i,connect:D,disconnect:T,sendMessage:j,fetchHistory:b,addReaction:y,removeReaction:N,setTyping:k}},Oe=l.lazy(()=>ae(()=>import("./vendor-IYr-MHu4.js").then(s=>s.X),__vite__mapDeps([0,1,2])).then(s=>({default:s.default}))),We=({message:s})=>{const{data:o}=O(),{addReaction:n}=K(),r=o?.id===s.sender_id,[i,d]=l.useState(!1),u=a=>{n(s.id,a.emoji),d(!1)};return e.jsxs("div",{className:_("group flex flex-col gap-1 max-w-[80%] mb-4 relative",r?"ml-auto items-end":"mr-auto items-start"),children:[e.jsxs("div",{className:"flex items-center gap-2 px-1 mb-0.5",children:[e.jsx("span",{className:_("font-mono text-[10px] uppercase tracking-wider",r?"text-kodo-steel":"text-kodo-magenta"),children:r?"You":s.sender_username||"Unknown_Signal"}),e.jsx("span",{className:"text-[9px] text-kodo-secondary/60",children:new Date(s.created_at).toLocaleTimeString([],{hour:"2-digit",minute:"2-digit"})})]}),e.jsxs("div",{className:"relative flex items-end gap-2 group/bubble",children:[r&&e.jsx("button",{onClick:()=>d(!i),className:"opacity-0 group-hover/bubble:opacity-100 p-1.5 hover:bg-white/10 rounded-full transition-all text-kodo-secondary hover:text-white",children:e.jsx(Q,{size:14})}),e.jsxs("div",{className:_("px-4 py-2.5 rounded-2xl text-sm backdrop-blur-md shadow-lg transition-all",r?"bg-kodo-cyan/10 border border-kodo-cyan/20 text-white rounded-tr-sm shadow-[0_0_15px_rgba(102,252,241,0.05)]":"bg-white/5 border border-white/10 text-kodo-text-main rounded-tl-sm hover:bg-white/10"),children:[s.attachments&&s.attachments.length>0&&e.jsx("div",{className:"mb-2 flex flex-wrap gap-2",children:s.attachments.map((a,c)=>e.jsx("div",{className:"max-w-full overflow-hidden rounded-lg border border-white/10 bg-black/20",children:a.file_type.startsWith("image")?e.jsx("img",{src:a.file_url,alt:a.file_name,className:"max-h-60 object-contain cursor-pointer hover:opacity-90 transition-opacity",onClick:()=>window.open(a.file_url,"_blank")}):e.jsxs("a",{href:a.file_url,target:"_blank",rel:"noopener noreferrer",className:"flex items-center gap-4 p-4 hover:bg-white/5 transition-colors",children:[e.jsx("div",{className:"w-8 h-8 rounded bg-white/10 flex items-center justify-center",children:e.jsx(Ee,{size:16,className:"text-kodo-steel"})}),e.jsx("span",{className:"truncate max-w-[150px] text-xs font-mono",children:a.file_name})]})},c))}),e.jsx("p",{className:"whitespace-pre-wrap break-words leading-relaxed",children:s.content})]}),!r&&e.jsx("button",{onClick:()=>d(!i),className:"opacity-0 group-hover/bubble:opacity-100 p-1.5 hover:bg-white/10 rounded-full transition-all text-kodo-secondary hover:text-white",children:e.jsx(Q,{size:14})}),i&&e.jsxs("div",{className:_("absolute z-50 bottom-full mb-2",r?"right-0":"left-0"),children:[e.jsx("div",{className:"fixed inset-0",onClick:()=>d(!1)}),e.jsx("div",{className:"relative shadow-2xl rounded-xl overflow-hidden border border-white/10 animate-scaleIn",children:e.jsx(l.Suspense,{fallback:e.jsx("div",{className:"w-[300px] h-[400px] bg-kodo-ink flex items-center justify-center",children:e.jsx(te,{size:"sm"})}),children:e.jsx(Oe,{onEmojiClick:u,theme:Z.DARK,lazyLoadEmojis:!0,width:300,height:400})})})]})]}),e.jsxs("div",{className:"flex items-center justify-between w-full px-1 mt-1",children:[e.jsx("div",{className:"flex flex-wrap gap-1",children:s.reactions&&Object.entries(s.reactions).map(([a,c])=>e.jsxs("button",{onClick:()=>n(s.id,a),className:_("flex items-center gap-1 px-1.5 py-0.5 rounded-full text-[10px] border transition-all animate-scaleIn",c.includes(o?.id||"")?"bg-kodo-cyan/20 border-kodo-cyan/40 text-kodo-cyan shadow-[0_0_10px_rgba(102,252,241,0.2)]":"bg-white/5 border-white/10 text-kodo-secondary hover:bg-white/10 hover:border-white/20"),children:[e.jsx("span",{children:a}),c.length>1&&e.jsx("span",{className:"font-bold",children:c.length})]},a))}),r&&e.jsx("div",{className:"text-kodo-secondary/40 ml-auto",children:e.jsx(Re,{size:12})})]})]})};function qe({conversationId:s,onMessageSelect:o}){const[n,r]=l.useState(""),[i,d]=l.useState([]),[u,a]=l.useState(!1),[c,x]=l.useState(!1),v=async()=>{if(!(!n.trim()||!s))try{a(!0),x(!0);const t=await I.get(`/conversations/${s}/messages/search`,{params:{q:n,limit:20}});d(t.data.messages||[])}catch(t){const w=se(t);q.warn("Search endpoint not available or failed",{error:w.message}),d([])}finally{a(!1)}},f=()=>{r(""),d([]),x(!1)};return e.jsxs("div",{className:"relative",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs("div",{className:"relative flex-1",children:[e.jsx(ie,{className:"absolute left-2 top-1/2 transform -translate-y-1/2 h-4 w-4 text-kodo-content-dim"}),e.jsx(oe,{type:"text",value:n,onChange:t=>r(t.target.value),onKeyDown:t=>{t.key==="Enter"&&(t.preventDefault(),v())},placeholder:"Search messages...",className:"pl-8 pr-8"}),n&&e.jsx(U,{variant:"ghost",size:"sm",className:"absolute right-1 top-1/2 transform -translate-y-1/2 h-6 w-6 p-0",onClick:f,children:e.jsx($,{className:"h-4 w-4"})})]}),e.jsx(U,{onClick:v,disabled:!n.trim()||u,size:"sm",children:u?"Searching...":"Search"})]}),c&&i.length>0&&e.jsx("div",{className:"absolute z-10 w-full mt-2 bg-white border rounded-lg shadow-lg max-h-64 overflow-y-auto",children:e.jsxs("div",{className:"p-2",children:[e.jsxs("div",{className:"text-xs text-kodo-content-dim mb-2",children:[i.length," result(s) found"]}),i.map(t=>e.jsxs("div",{className:"p-2 hover:bg-kodo-void rounded cursor-pointer",onClick:()=>{o?.(t.id),x(!1)},children:[e.jsx("div",{className:"text-sm font-medium",children:t.sender_username}),e.jsx("div",{className:"text-xs text-kodo-content-dim truncate",children:t.content}),e.jsx("div",{className:"text-xs text-kodo-content-dim",children:new Date(t.created_at).toLocaleString()})]},t.id))]})}),c&&i.length===0&&n&&e.jsx("div",{className:"absolute z-10 w-full mt-2 bg-white border rounded-lg shadow-lg p-4 text-sm text-kodo-content-dim",children:"No messages found"})]})}function Ae({conversationId:s}){const{typingUsers:o,userId:n}=z(),r=(o[s]||[]).filter(i=>i!==n);return r.length===0?e.jsx("div",{className:"h-6"}):e.jsx("div",{className:"px-4 py-1 text-xs text-kodo-content-dim italic animate-pulse",children:r.length===1?"Quelqu'un écrit...":`${r.length} personnes écrivent...`})}const Fe=({conversationId:s})=>{const{messages:o,wsStatus:n}=z(),{fetchHistory:r}=K(),{data:i}=O(),d=i?.id,u=l.useRef(null),[a,c]=l.useState(!1),[x,v]=l.useState(null),f=o[s]||[],t=l.useRef({});l.useEffect(()=>{s&&!o[s]&&!t.current[s]&&(t.current[s]=!0,r(s).finally(()=>{}))},[s,o[s],r]),l.useEffect(()=>{u.current&&u.current.scrollIntoView({behavior:"smooth"})},[f.length,s]);const w=p=>{v(p);const C=document.getElementById(`message-${p}`);C&&(C.scrollIntoView({behavior:"smooth",block:"center"}),setTimeout(()=>v(null),3e3))};return s?e.jsxs("div",{className:"flex-1 flex flex-col h-full overflow-hidden",children:[e.jsx("div",{className:_("absolute top-0 left-0 right-0 z-20 px-4 py-2 transition-all duration-300",a?"bg-kodo-void/90 backdrop-blur-md border-b border-white/10":"bg-transparent pointer-events-none"),children:a?e.jsxs("div",{className:"flex items-center gap-2 max-w-2xl mx-auto",children:[e.jsx("div",{className:"flex-1",children:e.jsx(qe,{conversationId:s,onMessageSelect:w})}),e.jsx(U,{variant:"ghost",size:"sm",onClick:()=>c(!1),className:"hover:bg-white/10",children:e.jsx($,{className:"h-4 w-4"})})]}):e.jsx("div",{className:"flex justify-end pointer-events-auto",children:e.jsxs(U,{variant:"ghost",size:"sm",onClick:()=>c(!0),className:"text-kodo-secondary/50 hover:text-white hover:bg-white/5 bg-black/20 backdrop-blur-sm rounded-full h-8 px-4 border border-white/5",children:[e.jsx(ie,{className:"h-3 w-3 mr-2"}),e.jsx("span",{className:"text-xs font-mono uppercase",children:"Search Log"})]})})}),e.jsxs("div",{className:"flex-1 overflow-y-auto custom-scrollbar p-6 space-y-4 scroll-smooth",children:[f.length===0&&e.jsxs("div",{className:"flex flex-col items-center justify-center h-[50vh] text-center space-y-4 opacity-60",children:[e.jsx("div",{className:"w-12 h-12 rounded-xl bg-kodo-steel/10 flex items-center justify-center border border-kodo-steel/20",children:e.jsx(MessageSquare,{className:"w-6 h-6 text-kodo-steel"})}),e.jsxs("div",{children:[e.jsx("p",{className:"text-white font-medium",children:"Channel Established"}),e.jsx("p",{className:"text-sm text-kodo-secondary mt-1",children:"Begin transmission on this frequency."})]})]}),f.map((p,C)=>(d&&p.sender_id,C>0&&(f[C-1].sender_id,p.sender_id),e.jsx("div",{id:`message-${p.id}`,className:_("transition-all duration-500 animate-slideUp",x===p.id&&"bg-kodo-steel/10 rounded-xl -mx-4 px-4 py-2 ring-1 ring-kodo-steel/30"),children:e.jsx(We,{message:p})},p.id))),e.jsx(Ae,{conversationId:s}),e.jsx("div",{ref:u,className:"h-4"})]})]}):e.jsxs("div",{className:"flex-1 flex flex-col items-center justify-center text-kodo-secondary opacity-50 space-y-4",children:[e.jsx("div",{className:"w-24 h-24 rounded-full bg-white/5 flex items-center justify-center animate-pulse",children:e.jsx(De,{className:"w-10 h-10 text-kodo-steel opacity-50"})}),e.jsx("p",{className:"text-sm font-mono uppercase tracking-widest",children:"Awaiting Frequency Selection"})]})},Qe=l.lazy(()=>ae(()=>import("./vendor-IYr-MHu4.js").then(s=>s.X),__vite__mapDeps([0,1,2])).then(s=>({default:s.default}))),$e=()=>{const[s,o]=l.useState(""),[n,r]=l.useState([]),[i,d]=l.useState(!1),[u,a]=l.useState(!1),{sendMessage:c,setTyping:x}=K(),{currentConversationId:v}=z(),f=be(),t=l.useRef(null),w=b=>{b.preventDefault(),(s.trim()||n.length>0)&&v&&(c(s,n.length>0?n:void 0),o(""),r([]),t.current&&clearTimeout(t.current),x(!1))},p=l.useRef(null),C=l.useCallback(async b=>{d(!0);try{const y=b.map(async k=>{const g=new FormData;g.append("file",k);const S=(await I.post("/uploads",g,{headers:{"Content-Type":"multipart/form-data"}})).data;return{file_name:k.name,file_type:k.type,file_url:S.url,file_size:k.size}}),N=await Promise.all(y);r(k=>[...k,...N])}catch(y){q.error("Failed to upload files",{error:y instanceof Error?y.message:String(y)})}finally{d(!1)}},[]),{getRootProps:D,getInputProps:T,isDragActive:E}=me({onDrop:C,noClick:!0}),L=b=>{o(y=>y+b.emoji),a(!1)},j=b=>{r(y=>y.filter((N,k)=>k!==b))};return l.useEffect(()=>{s.length>0?(x(!0),t.current&&clearTimeout(t.current),t.current=setTimeout(()=>{x(!1)},3e3)):x(!1)},[s,x]),e.jsxs("div",{...D(),className:"relative",children:[e.jsx("input",{...T(),ref:p,className:"hidden"}),E&&e.jsx("div",{className:"absolute bottom-full left-0 right-0 h-48 z-50 bg-kodo-steel/10 backdrop-blur-md flex items-center justify-center border-t-2 border-kodo-steel border-dashed rounded-t-2xl animate-fadeIn",children:e.jsxs("div",{className:"text-center",children:[e.jsx("div",{className:"w-12 h-12 rounded-full bg-kodo-steel/20 flex items-center justify-center mx-auto mb-2 animate-bounce",children:e.jsx(G,{className:"w-6 h-6 text-kodo-steel"})}),e.jsx("p",{className:"text-kodo-steel font-mono uppercase tracking-widest text-sm",children:"Initiate Data Transfer"})]})}),n.length>0&&e.jsx("div",{className:"absolute bottom-full left-0 right-0 p-4 bg-kodo-void/90 backdrop-blur-xl border-t border-white/10 flex gap-2 overflow-x-auto",children:n.map((b,y)=>e.jsxs("div",{className:"relative group flex items-center gap-2 p-2 bg-white/5 rounded-lg border border-white/10 text-xs text-white min-w-[150px]",children:[b.file_type.startsWith("image")?e.jsx(Te,{size:14,className:"text-kodo-cyan"}):e.jsx(Me,{size:14,className:"text-kodo-secondary"}),e.jsx("span",{className:"truncate flex-1",children:b.file_name}),e.jsx("button",{onClick:()=>j(y),className:"p-1 hover:bg-white/10 rounded-full text-kodo-red opacity-0 group-hover:opacity-100 transition-opacity",children:e.jsx($,{size:12})})]},y))}),e.jsxs("form",{onSubmit:w,className:"flex items-center gap-2",children:[e.jsxs("div",{className:"flex gap-1",children:[e.jsx(F,{type:"button",variant:"ghost",size:"icon",className:"text-kodo-secondary hover:text-white hover:bg-white/5",onClick:()=>p.current?.click(),children:e.jsx(G,{size:20})}),e.jsxs("div",{className:"relative",children:[e.jsx(F,{type:"button",variant:"ghost",size:"icon",className:_("text-kodo-secondary hover:text-white hover:bg-white/5",u&&"text-kodo-steel bg-white/5"),onClick:()=>a(!u),children:e.jsx(Q,{size:20})}),u&&e.jsxs("div",{className:"absolute bottom-full left-0 mb-4 z-50 animate-scaleIn origin-bottom-left",children:[e.jsx("div",{className:"fixed inset-0",onClick:()=>a(!1)}),e.jsx("div",{className:"relative shadow-2xl rounded-xl overflow-hidden border border-white/10",children:e.jsx(l.Suspense,{fallback:e.jsx("div",{className:"w-[350px] h-[450px] bg-kodo-ink flex items-center justify-center",children:e.jsx(te,{})}),children:e.jsx(Qe,{onEmojiClick:L,theme:Z.DARK,lazyLoadEmojis:!0,width:350,height:450})})})]})]})]}),e.jsxs("div",{className:"flex-1 relative",children:[e.jsx("input",{type:"text",value:s,onChange:b=>o(b.target.value),placeholder:"Broadcast message...",className:"w-full bg-white/5 border border-white/10 rounded-xl px-4 py-2.5 text-white placeholder:text-kodo-secondary/50 focus:outline-none focus:border-kodo-steel/50 focus:ring-1 focus:ring-kodo-steel/50 transition-all font-mono text-sm",disabled:!v||i}),s.length===0&&!i&&e.jsx("div",{className:"absolute right-2 top-1/2 -translate-y-1/2",children:e.jsx(ze,{className:"w-4 h-4 text-kodo-secondary/30 hover:text-white cursor-pointer transition-colors"})})]}),e.jsx(F,{type:"submit",variant:"primary",size:"icon",className:_("rounded-xl transition-all duration-300",s.trim()||n.length>0?"bg-kodo-cyan text-kodo-void hover:bg-kodo-cyan-dim shadow-neon-cyan":"bg-white/5 text-kodo-secondary hover:bg-white/10"),disabled:!v||!s.trim()&&n.length===0||i||f,children:i?e.jsx("div",{className:"w-5 h-5 border-2 border-kodo-void/30 border-t-kodo-void rounded-full animate-spin"}):e.jsx(Le,{size:18,className:_(s.trim()?"translate-x-0.5":"")})})]})]})},rs=()=>{const{isAuthenticated:s}=xe(),{data:o}=O(),n=o?.id,{setWsToken:r,currentConversationId:i,wsStatus:d}=z(),{data:u,isLoading:a,error:c}=ne({queryKey:["chatWsToken",n],queryFn:async()=>!s||!n?null:(await I.post("/chat/token",{})).data,enabled:s&&!!n&&d==="disconnected",refetchOnWindowFocus:!1,retry:!1,staleTime:300*1e3,gcTime:600*1e3});return l.useEffect(()=>{u?.token&&(u.token!==z.getState().wsToken||J.WS_URL!==z.getState().wsUrl)&&r(u.token,J.WS_URL)},[u,r]),s?a||d==="connecting"?e.jsxs("div",{className:"flex flex-col items-center justify-center h-[calc(100vh-100px)] glass-hud rounded-2xl",children:[e.jsxs("div",{className:"relative",children:[e.jsx("div",{className:"w-16 h-16 border-4 border-kodo-steel/20 border-t-kodo-steel rounded-full animate-spin"}),e.jsx("div",{className:"absolute inset-0 flex items-center justify-center",children:e.jsx("div",{className:"w-2 h-2 bg-kodo-steel rounded-full animate-pulse"})})]}),e.jsx("p",{className:"mt-6 text-sm font-mono text-kodo-steel animate-pulse uppercase tracking-widest",children:"Establishing Uplink..."})]}):c?e.jsxs("div",{className:"flex flex-col items-center justify-center h-[calc(100vh-100px)] glass-hud rounded-2xl border-kodo-red/20",children:[e.jsx(Y,{className:"w-12 h-12 text-kodo-red mb-4"}),e.jsx("h2",{className:"text-xl font-bold text-white mb-2",children:"Connection Failure"}),e.jsx("p",{className:"text-kodo-red/80 max-w-md text-center",children:c.message||"Unable to retrieve secure token via Handshake Protocol."})]}):e.jsxs("div",{className:"h-[calc(100vh-theme(spacing.24))] flex gap-6 overflow-hidden animate-fadeIn",children:[e.jsx("div",{className:"w-80 shrink-0 flex flex-col glass-hud rounded-2xl border-white/5 overflow-hidden",children:e.jsx(Ie,{})}),e.jsxs("div",{className:"flex-1 flex flex-col glass-hud rounded-2xl border-white/5 overflow-hidden relative",children:[e.jsx("div",{className:"absolute inset-0 opacity-[0.02] pointer-events-none",style:{backgroundImage:"linear-gradient(rgba(102, 252, 241, 0.5) 1px, transparent 1px)",backgroundSize:"20px 20px"}}),e.jsx("div",{className:"flex-1 overflow-hidden flex flex-col relative z-10",children:e.jsx(Fe,{conversationId:i||""})}),e.jsx("div",{className:"p-4 border-t border-white/5 bg-black/20 backdrop-blur-xl relative z-20",children:e.jsx($e,{})})]})]}):e.jsx("div",{className:"flex flex-col items-center justify-center h-[calc(100vh-100px)] text-kodo-secondary glass-hud rounded-2xl",children:e.jsxs("div",{className:"p-8 text-center max-w-md",children:[e.jsx(Y,{className:"w-12 h-12 text-kodo-steel mx-auto mb-4 opacity-50"}),e.jsx("h2",{className:"text-xl font-bold text-white mb-2",children:"Access Restricted"}),e.jsx("p",{className:"mb-6",children:"Authorization required to access secure communication channels."}),e.jsx(U,{variant:"default",onClick:()=>window.location.href="/login",children:"Initialize Login"})]})})};export{rs as ChatPage};
|