2 lines
31 KiB
JavaScript
2 lines
31 KiB
JavaScript
const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=["assets/vendor-fFnFILYR.js","assets/vendor-react-YMhRUmcb.js","assets/vendor-BD_zwJK7.css"])))=>i.map(i=>d[i]);
|
|
import{a as n,j as e}from"./vendor-react-YMhRUmcb.js";import{g as Y,D as de,E as q,B as _,p as ee,a as M,d as b,u as A,l as O,L as te,_ as se,h as ue,i as K,C as W}from"./index-D2uzyr9g.js";import{u as re,a as ae,b as H}from"./vendor-tanstack-CoFNL2zy.js";import{L as V,I as ne}from"./input-DL1WY4HG.js";import{S as me}from"./Select-So_RP28r.js";import{u as T}from"./chatStore-VE5eaeTh.js";import{z as oe,g as he,U as xe,a8 as pe,a9 as fe,l as ge,T as ye,aa as be,ab as F,a0 as ve,j as je,S as ie,X as Q,ac as we,ad as B,ae as ke,af as Ne,ag as Ce,ah as Se,A as J}from"./vendor-icons-DINCNwpk.js";import{D as _e,a as Ee,b as Re,c as G,u as De}from"./useIsRateLimited-C43yhDlJ.js";import{C as Z}from"./confirmation-dialog-O1mUMh9e.js";import{D as ze,T as le,E as Te}from"./vendor-fFnFILYR.js";import"./vendor-router-DX94Iu-2.js";import"./vendor-utils-4BWoYre8.js";import"./dropdown-DLDDI8f2.js";function Le({open:t,onClose:l}){const[a,o]=n.useState(""),[m,c]=n.useState("public"),[d,s]=n.useState(!1),[i,u]=n.useState(null),[f,p]=n.useState(null),[r,v]=n.useState(0),g=n.useRef(null),w=Y(),{addConversation:E,setCurrentConversation:R}=T(),D=async()=>{if(u(null),p(null),!a.trim()){u("Room name is required");return}const k=a.trim(),x=m,j=async()=>{const S=await M.post("/conversations",{name:k,type:x}),N={id:S.data.id||S.data.conversation?.id,name:S.data.name||S.data.conversation?.name,type:S.data.type||S.data.conversation?.type||x,participants:S.data.participants||[],unread_count:0};E(N),R(N.id),w.success("Room created successfully"),o(""),c("public"),p(null),v(0),g.current=null,l()};g.current=j,s(!0);try{await j()}catch(S){const N=ee(S);p(new Error(N.message))}finally{s(!1)}},P=async()=>{if(!(!g.current||r>=3)){v(k=>k+1),s(!0);try{await g.current()}catch{}finally{s(!1)}}};return e.jsx(de,{open:t,onClose:l,title:"Create New Room",variant:"default",size:"md",children:e.jsxs("div",{className:"space-y-4",children:[f&&e.jsx(q,{error:f,variant:"banner",severity:"error",context:{action:"creating room",resource:"conversation"},onRetry:r<3?P:void 0,onDismiss:()=>{p(null),v(0),g.current=null}}),i&&e.jsx(q,{error:i,variant:"inline",severity:"error",size:"sm",dismissible:!1}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(V,{htmlFor:"room-name",children:"Room Name"}),e.jsx(ne,{id:"room-name",value:a,onChange:k=>{o(k.target.value),u(null)},placeholder:"Enter room name",maxLength:100})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(V,{htmlFor:"room-type",children:"Room Type"}),e.jsx(me,{options:[{value:"public",label:"Public"},{value:"private",label:"Private"}],value:m,onChange:k=>c(Array.isArray(k)?k[0]:k),name:"room-type"})]}),e.jsxs("div",{className:"flex justify-end gap-2 pt-4",children:[e.jsx(_,{variant:"outline",onClick:l,disabled:d,children:"Cancel"}),e.jsx(_,{onClick:D,disabled:d||!a.trim(),children:d?"Creating...":"Create Room"})]})]})})}function Me(t,l,a){const{data:o,isLoading:m,error:c}=re({queryKey:["chatConversations",t],queryFn:async()=>{if(!t)return[];const s=(await M.get("/conversations")).data,i=Array.isArray(s)?s:s?.conversations??[];return Array.isArray(i)?i:[]},enabled:!!t});return n.useEffect(()=>{o&&o.forEach(d=>{a.some(s=>s.id===d.id)||l({id:d.id,name:d.name,type:d.type??"direct",participants:Array.isArray(d.participants)?d.participants:[],unread_count:0})})},[o,a,l]),{data:o??[],isLoading:m,error:c}}function Pe({count:t,className:l}){return e.jsx("div",{className:b("p-4 border-b border-white/5 bg-white/2 backdrop-blur-sm",l),children:e.jsxs("div",{className:"flex items-center justify-between mb-1",children:[e.jsxs("h2",{className:"text-sm font-bold text-foreground tracking-wide uppercase flex items-center gap-2",children:[e.jsx(oe,{className:"w-4 h-4 text-muted-foreground"}),"Active Channels"]}),e.jsx("span",{className:"text-[10px] font-mono text-muted-foreground bg-muted px-1.5 py-0.5 rounded",children:t})]})})}function Ae({className:t}){return e.jsxs("div",{className:b("text-muted-foreground/70 text-sm p-4 text-center italic border border-dashed border-border rounded-xl m-2",t),children:["No active frequencies detected.",e.jsx("br",{}),"Initialize a new channel."]})}function Ie({className:t}){return e.jsx("div",{className:b("flex flex-1 items-center justify-center min-h-layout-story",t),"data-testid":"chat-sidebar-skeleton",children:e.jsx(he,{className:"animate-spin text-muted-foreground",size:24})})}function Oe(t,l){const a=ae(),o=Y(),m=T(s=>s.setCurrentConversation),c=H({mutationFn:async s=>{await M.delete(`/conversations/${s}/participants/${t}`)},onMutate:async s=>{await a.cancelQueries({queryKey:["chatConversations",t]});const i=a.getQueryData(["chatConversations",t]);return i&&Array.isArray(i)&&a.setQueryData(["chatConversations",t],i.filter(u=>u.id!==s)),{previous:i}},onError:(s,i,u)=>{u?.previous&&a.setQueryData(["chatConversations",t],u.previous);const f=s?.response?.data?.error??"Failed to leave room";l?.onLeaveError?.(new Error(f))},onSuccess:()=>{a.invalidateQueries({queryKey:["chatConversations",t]}),o.success("Left room successfully"),m(null),l?.onLeaveSuccess?.()}}),d=H({mutationFn:async s=>{await M.delete(`/conversations/${s}`)},onMutate:async s=>{await a.cancelQueries({queryKey:["chatConversations",t]});const i=a.getQueryData(["chatConversations",t]);return i&&Array.isArray(i)&&a.setQueryData(["chatConversations",t],i.filter(u=>u.id!==s)),{previous:i}},onError:(s,i,u)=>{u?.previous&&a.setQueryData(["chatConversations",t],u.previous);const f=s?.response?.data?.error??"Failed to delete room";l?.onDeleteError?.(new Error(f))},onSuccess:()=>{a.invalidateQueries({queryKey:["chatConversations",t]}),o.success("Room deleted successfully"),m(null),l?.onDeleteSuccess?.()}});return{leaveRoomMutation:c,deleteRoomMutation:d}}function X(t){if(t==null)return"";if(typeof t=="string")return t;try{return String(t)}catch{return"Invalid Value"}}function We({conversation:t,onSelect:l,isSelected:a}){const{data:o}=A(),[m,c]=n.useState(!1),[d,s]=n.useState(!1),[i,u]=n.useState(null),[f,p]=n.useState(0),[r,v]=n.useState(null),[g,w]=n.useState(null),{leaveRoomMutation:E,deleteRoomMutation:R}=Oe(o?.id,{onLeaveError:u,onDeleteError:u,onLeaveSuccess:()=>{c(!1),u(null)},onDeleteSuccess:()=>{s(!1),u(null)}}),D=()=>{v("leave"),w(t.id),E.mutate(t.id)},P=()=>{v("delete"),w(t.id),R.mutate(t.id)},k=()=>{!r||!g||f>=3||(p(x=>x+1),r==="leave"?E.mutate(g):R.mutate(g))};return e.jsxs(e.Fragment,{children:[i&&e.jsx(q,{error:i,variant:"banner",severity:"error",context:{action:"managing room",resource:"conversation",resourceId:t.id},onRetry:f<3?k:void 0,onDismiss:()=>{u(null),p(0),v(null),w(null)}}),e.jsxs("div",{role:"button",tabIndex:0,onClick:()=>l(t.id),onKeyDown:x=>{(x.key==="Enter"||x.key===" ")&&(x.preventDefault(),l(t.id))},className:b("group relative flex items-center justify-between p-4 rounded-xl cursor-pointer transition-all duration-300 border border-transparent",a?"bg-primary/10 border-primary/30 shadow-[0_0_15px_hsl(var(--primary)/0.1)]":"hover:bg-muted/50 hover:border-muted"),children:[e.jsxs("div",{className:"flex items-center gap-4 min-w-0",children:[e.jsx("div",{className:b("w-8 h-8 rounded-lg flex items-center justify-center transition-colors shrink-0",a?"bg-kodo-cyan text-kodo-void":"bg-white/5 text-kodo-secondary group-hover:text-white"),children:t.type==="direct"?e.jsx(xe,{size:14}):e.jsx(pe,{size:14})}),e.jsxs("div",{className:"flex flex-col min-w-0",children:[e.jsx("span",{className:b("text-sm font-medium truncate transition-colors",a?"text-foreground":"text-muted-foreground group-hover:text-foreground"),children:X(t.name||`Channel ${t.id.substring(0,4)}`)}),t.type!=="direct"&&e.jsx("span",{className:"text-[10px] text-kodo-secondary/50 uppercase tracking-wider",children:X(t.type)})]})]}),t.unread_count!=null&&Number(t.unread_count)>0?e.jsx("span",{className:"bg-primary text-primary-foreground text-[10px] px-1.5 py-0.5 rounded-full font-bold shadow-lg shrink-0",children:t.unread_count}):null,e.jsxs(_e,{children:[e.jsx(Ee,{asChild:!0,onClick:x=>x.stopPropagation(),children:e.jsx(_,{variant:"ghost",size:"sm",className:b("h-6 w-6 p-0 opacity-0 group-hover:opacity-100 transition-opacity shrink-0",a?"text-kodo-cyan hover:bg-kodo-cyan/20":"text-kodo-secondary hover:text-white"),children:e.jsx(fe,{className:"h-4 w-4"})})}),e.jsxs(Re,{align:"end",className:"bg-card border-border text-foreground",children:[e.jsxs(G,{onClick:x=>{x.stopPropagation(),c(!0)},className:"focus:bg-white/10 cursor-pointer",children:[e.jsx(ge,{className:"mr-2 h-4 w-4"}),"Leave Channel"]}),t.type!=="direct"&&e.jsxs(G,{onClick:x=>{x.stopPropagation(),s(!0)},className:"text-kodo-red focus:bg-kodo-red/10 cursor-pointer",children:[e.jsx(ye,{className:"mr-2 h-4 w-4"}),"Delete Channel"]})]})]}),a&&e.jsx("div",{className:"absolute left-0 top-3 bottom-3 w-0.5 bg-primary rounded-r-full shadow-[0_0_8px_hsl(var(--primary)/0.5)]"})]}),e.jsx(Z,{open:m,onClose:()=>c(!1),onConfirm:D,title:"Leave Channel",description:"Disconnect from this secure frequency? Incoming transmission will cease.",confirmLabel:"Disconnect",cancelLabel:"Cancel",variant:"default",isLoading:E.isPending}),e.jsx(Z,{open:d,onClose:()=>s(!1),onConfirm:P,title:"Delete Channel",description:"Permanently purge this channel from the network? This action is irreversible.",confirmLabel:"Purge",cancelLabel:"Cancel",variant:"destructive",isLoading:R.isPending})]})}const qe=()=>{const{data:t}=A(),l=t?.id,a=ae(),{conversations:o,currentConversationId:m,setCurrentConversation:c,addConversation:d}=T(),{isLoading:s,error:i}=Me(l,d,o),[u,f]=n.useState(!1);return s?e.jsx(Ie,{}):i?e.jsx("div",{className:"flex flex-1 items-center justify-center p-4 min-h-layout-page-sm",children:e.jsx(q,{error:i instanceof Error?i:new Error("Signal Lost"),variant:"card",severity:"error",context:{action:"fetching conversations",resource:"conversations"},onRetry:()=>a.invalidateQueries({queryKey:["chatConversations",l]})})}):e.jsxs("div",{className:b("flex flex-col h-full"),children:[e.jsx(Pe,{count:o.length}),e.jsx("div",{className:"flex-1 overflow-y-auto custom-scrollbar p-4 space-y-1",children:o.length===0?e.jsx(Ae,{}):o.map(p=>e.jsx(We,{conversation:{id:p.id,name:p.name,type:p.type,unread_count:p.unread_count},onSelect:r=>c(r),isSelected:p.id===m},p.id))}),e.jsx("div",{className:"p-4 border-t border-white/5 bg-white/2 backdrop-blur-sm",children:e.jsxs(_,{onClick:()=>f(!0),className:"w-full shadow-lg shadow-kodo-steel/10",variant:"default",children:[e.jsx(be,{className:"mr-2 h-4 w-4"}),"New Channel"]})}),e.jsx(Le,{open:u,onClose:()=>f(!1)})]})},$=()=>{const{data:t}=A(),l=t?.id,{wsToken:a,wsUrl:o,wsStatus:m,setWsStatus:c,addMessage:d,currentConversationId:s,loadMessages:i,addReaction:u,removeReaction:f,setUserTyping:p}=T(),r=n.useRef(null),v=n.useRef(0),[g,w]=n.useState([]),E=n.useCallback(()=>{if(!a||!o||r.current?.readyState===WebSocket.OPEN)return;r.current&&(r.current.onopen=null,r.current.onmessage=null,r.current.onclose=null,r.current.onerror=null,(r.current.readyState===WebSocket.OPEN||r.current.readyState===WebSocket.CONNECTING)&&r.current.close()),c("connecting");const y=`${o}?token=${a}`;r.current=new WebSocket(y);const C=()=>{c("connected"),v.current=0,w(L=>(L.forEach(z=>r.current?.send(JSON.stringify(z))),[]))},I=L=>{const z=JSON.parse(L.data);if(z.type==="NewMessage"){const h=z;h.conversation_id===s&&h.message_id&&h.sender_id&&h.content&&h.created_at&&d({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(z.type==="ReactionAdded"){const h=z;h.message_id&&h.user_id&&h.emoji&&u(h.conversation_id,h.message_id,h.user_id,h.emoji)}else if(z.type==="ReactionRemoved"){const h=z;h.message_id&&h.user_id&&f(h.conversation_id,h.message_id,h.user_id)}else if(z.type==="UserTyping"){const h=z;h.user_id&&p(h.conversation_id,h.user_id,h.is_typing??!1)}},U=()=>{c("disconnected")},ce=L=>{c("error"),v.current+=1,v.current<=3&&O.error("WebSocket error",{error:L instanceof Error?L.message:String(L),stack:L instanceof Error?L.stack:void 0})};r.current.onopen=C,r.current.onmessage=I,r.current.onclose=U,r.current.onerror=ce},[a,o,c,d,s,u,f,p]),R=n.useCallback(()=>{r.current&&(r.current.onopen=null,r.current.onmessage=null,r.current.onclose=null,r.current.onerror=null,(r.current.readyState===WebSocket.OPEN||r.current.readyState===WebSocket.CONNECTING)&&r.current.close(),r.current=null,c("disconnected"))},[c]),D=n.useRef(0),P=5;n.useEffect(()=>{let y;return a&&o&&m==="disconnected"&&D.current<P&&(y=setTimeout(()=>{D.current++,E()},1e3*Math.pow(2,D.current))),m==="connected"&&(D.current=0),()=>{y&&clearTimeout(y)}},[a,o,m,E]),n.useEffect(()=>()=>{R()},[R]);const k=n.useCallback((y,C)=>{if(!r.current||r.current.readyState!==WebSocket.OPEN||!s||!l){O.warn("WebSocket not open or missing conversation/user ID. Message queued.",{conversationId:s,userId:l}),w(U=>[...U,{type:"SendMessage",conversation_id:s||ze(),content:y,parent_message_id:null,attachments:C}]);return}const I={type:"SendMessage",conversation_id:s,content:y,parent_message_id:null,attachments:C};r.current.send(JSON.stringify(I))},[s,l]),x=n.useCallback(async y=>{try{const C=await M.get(`/conversations/${y}/history`);i(y,C.data.messages)}catch(C){O.error("Failed to fetch chat history",{error:C instanceof Error?C.message:String(C),stack:C instanceof Error?C.stack:void 0,conversationId:y})}},[i]),j=n.useCallback((y,C)=>{r.current?.readyState===WebSocket.OPEN&&s&&r.current.send(JSON.stringify({type:"AddReaction",conversation_id:s,message_id:y,emoji:C}))},[s]),S=n.useCallback(y=>{r.current?.readyState===WebSocket.OPEN&&s&&r.current.send(JSON.stringify({type:"RemoveReaction",conversation_id:s,message_id:y}))},[s]),N=n.useCallback(y=>{r.current?.readyState===WebSocket.OPEN&&s&&r.current.send(JSON.stringify({type:"Typing",conversation_id:s,is_typing:y}))},[s]);return{wsStatus:m,connect:E,disconnect:R,sendMessage:k,fetchHistory:x,addReaction:j,removeReaction:S,setTyping:N}},Ue=n.lazy(()=>se(()=>import("./vendor-fFnFILYR.js").then(t=>t.Z),__vite__mapDeps([0,1,2])).then(t=>({default:t.default}))),Fe=t=>{if(t==null)return"";if(typeof t=="string")return t;try{return String(t)}catch{return"Invalid Value"}},Qe=({message:t})=>{const{data:l}=A(),{addReaction:a}=$(),o=l?.id===t.sender_id,[m,c]=n.useState(!1),d=s=>{a(t.id,s.emoji),c(!1)};return e.jsxs("div",{className:b("group flex flex-col gap-1 max-w-[80%] mb-4 relative",o?"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.jsxs("span",{className:b("font-mono text-[10px] uppercase tracking-wider",o?"text-kodo-steel":"text-kodo-magenta"),children:["// ...",o?"You":Fe(t.sender_username||"Unknown_Signal")]}),e.jsx("span",{className:"text-[9px] text-kodo-secondary/60",children:new Date(t.created_at).toLocaleTimeString([],{hour:"2-digit",minute:"2-digit"})})]}),e.jsxs("div",{className:"relative flex items-end gap-2 group/bubble",children:[o&&e.jsx("button",{onClick:()=>c(!m),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(F,{size:14})}),e.jsxs("div",{className:b("px-4 py-2.5 rounded-2xl text-sm backdrop-blur-md shadow-lg transition-all",o?"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:[t.attachments&&t.attachments.length>0&&e.jsx("div",{className:"mb-2 flex flex-wrap gap-2",children:t.attachments.map((s,i)=>e.jsx("div",{className:"max-w-full overflow-hidden rounded-lg border border-white/10 bg-black/20",children:s.file_type.startsWith("image")?e.jsx("img",{src:s.file_url,alt:s.file_name,className:"max-h-60 object-contain cursor-pointer hover:opacity-90 transition-opacity",onClick:()=>window.open(s.file_url,"_blank")}):e.jsxs("a",{href:s.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(ve,{size:16,className:"text-kodo-steel"})}),e.jsx("span",{className:"truncate max-w-[150px] text-xs font-mono",children:s.file_name})]})},i))}),e.jsx("p",{className:"whitespace-pre-wrap break-words leading-relaxed",children:t.content})]}),!o&&e.jsx("button",{onClick:()=>c(!m),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(F,{size:14})}),m&&e.jsxs("div",{className:b("absolute z-50 bottom-full mb-2",o?"right-0":"left-0"),children:[e.jsx("div",{className:"fixed inset-0",onClick:()=>c(!1)}),e.jsx("div",{className:"relative shadow-2xl rounded-xl overflow-hidden border border-white/10 animate-scaleIn",children:e.jsx(n.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(Ue,{onEmojiClick:d,theme:le.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:t.reactions&&Object.entries(t.reactions).map(([s,i])=>e.jsxs("button",{onClick:()=>a(t.id,s),className:b("flex items-center gap-1 px-1.5 py-0.5 rounded-full text-[10px] border transition-all animate-scaleIn",i.includes(l?.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:s}),i.length>1&&e.jsx("span",{className:"font-bold",children:i.length})]},s))}),o&&e.jsx("div",{className:"text-kodo-secondary/40 ml-auto",children:e.jsx(je,{size:12})})]})]})};function $e({conversationId:t,onMessageSelect:l}){const[a,o]=n.useState(""),[m,c]=n.useState([]),[d,s]=n.useState(!1),[i,u]=n.useState(!1),f=async()=>{if(!(!a.trim()||!t))try{s(!0),u(!0);const r=await M.get(`/conversations/${t}/messages/search`,{params:{q:a,limit:20}});c(r.data.messages||[])}catch(r){const v=ee(r);O.warn("Search endpoint not available or failed",{error:v.message}),c([])}finally{s(!1)}},p=()=>{o(""),c([]),u(!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(ne,{type:"text",value:a,onChange:r=>o(r.target.value),onKeyDown:r=>{r.key==="Enter"&&(r.preventDefault(),f())},placeholder:"Search messages...",className:"pl-8 pr-8"}),a&&e.jsx(_,{variant:"ghost",size:"sm",className:"absolute right-1 top-1/2 transform -translate-y-1/2 h-6 w-6 p-0",onClick:p,children:e.jsx(Q,{className:"h-4 w-4"})})]}),e.jsx(_,{onClick:f,disabled:!a.trim()||d,size:"sm",children:d?"Searching...":"Search"})]}),i&&m.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:[m.length," result(s) found"]}),m.map(r=>e.jsxs("div",{className:"p-2 hover:bg-kodo-void rounded cursor-pointer",onClick:()=>{l?.(r.id),u(!1)},children:[e.jsx("div",{className:"text-sm font-medium",children:r.sender_username}),e.jsx("div",{className:"text-xs text-kodo-content-dim truncate",children:r.content}),e.jsx("div",{className:"text-xs text-kodo-content-dim",children:new Date(r.created_at).toLocaleString()})]},r.id))]})}),i&&m.length===0&&a&&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 Ke({conversationId:t}){const{typingUsers:l,userId:a}=T(),o=(l[t]||[]).filter(m=>m!==a);return o.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:o.length===1?"Quelqu'un écrit...":`${o.length} personnes écrivent...`})}const He=({conversationId:t})=>{const{messages:l,wsStatus:a}=T(),{fetchHistory:o}=$(),{data:m}=A(),c=m?.id,d=n.useRef(null),[s,i]=n.useState(!1),[u,f]=n.useState(null),p=l[t]||[],r=n.useRef({});n.useEffect(()=>{t&&!l[t]&&!r.current[t]&&(r.current[t]=!0,o(t).finally(()=>{}))},[t,l[t],o]),n.useEffect(()=>{d.current&&d.current.scrollIntoView({behavior:"smooth"})},[p.length,t]);const v=g=>{f(g);const w=document.getElementById(`message-${g}`);w&&(w.scrollIntoView({behavior:"smooth",block:"center"}),setTimeout(()=>f(null),3e3))};return t?e.jsxs("div",{className:"flex-1 flex flex-col h-full overflow-hidden",children:[e.jsx("div",{className:b("absolute top-0 left-0 right-0 z-20 px-4 py-2 transition-all duration-300",s?"bg-kodo-void/90 backdrop-blur-md border-b border-white/10":"bg-transparent pointer-events-none"),children:s?e.jsxs("div",{className:"flex items-center gap-2 max-w-2xl mx-auto",children:[e.jsx("div",{className:"flex-1",children:e.jsx($e,{conversationId:t,onMessageSelect:v})}),e.jsx(_,{variant:"ghost",size:"sm",onClick:()=>i(!1),className:"hover:bg-white/10",children:e.jsx(Q,{className:"h-4 w-4"})})]}):e.jsx("div",{className:"flex justify-end pointer-events-auto",children:e.jsxs(_,{variant:"ghost",size:"sm",onClick:()=>i(!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:[p.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(oe,{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."})]})]}),p.map((g,w)=>(c&&g.sender_id,w>0&&(p[w-1].sender_id,g.sender_id),e.jsx("div",{id:`message-${g.id}`,className:b("transition-all duration-500 animate-slideUp",u===g.id&&"bg-kodo-steel/10 rounded-xl -mx-4 px-4 py-2 ring-1 ring-kodo-steel/30"),children:e.jsx(Qe,{message:g})},g.id))),e.jsx(Ke,{conversationId:t}),e.jsx("div",{ref:d,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(we,{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"})]})},Ve=n.lazy(()=>se(()=>import("./vendor-fFnFILYR.js").then(t=>t.Z),__vite__mapDeps([0,1,2])).then(t=>({default:t.default}))),Be=()=>{const[t,l]=n.useState(""),[a,o]=n.useState([]),[m,c]=n.useState(!1),[d,s]=n.useState(!1),{sendMessage:i,setTyping:u}=$(),{currentConversationId:f}=T(),p=De(),r=n.useRef(null),v=x=>{x.preventDefault(),(t.trim()||a.length>0)&&f&&(i(t,a.length>0?a:void 0),l(""),o([]),r.current&&clearTimeout(r.current),u(!1))},g=n.useRef(null),w=n.useCallback(async x=>{c(!0);try{const j=x.map(async N=>{const y=new FormData;y.append("file",N);const I=(await M.post("/uploads",y,{headers:{"Content-Type":"multipart/form-data"}})).data;return{file_name:N.name,file_type:N.type,file_url:I.url,file_size:N.size}}),S=await Promise.all(j);o(N=>[...N,...S])}catch(j){O.error("Failed to upload files",{error:j instanceof Error?j.message:String(j)})}finally{c(!1)}},[]),{getRootProps:E,getInputProps:R,isDragActive:D}=Te({onDrop:w,noClick:!0}),P=x=>{l(j=>j+x.emoji),s(!1)},k=x=>{o(j=>j.filter((S,N)=>N!==x))};return n.useEffect(()=>{t.length>0?(u(!0),r.current&&clearTimeout(r.current),r.current=setTimeout(()=>{u(!1)},3e3)):u(!1)},[t,u]),e.jsxs("div",{...E(),className:"relative",children:[e.jsx("input",{...R(),ref:g,className:"hidden"}),D&&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(B,{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"})]})}),a.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:a.map((x,j)=>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:[x.file_type.startsWith("image")?e.jsx(ke,{size:14,className:"text-kodo-cyan"}):e.jsx(Ne,{size:14,className:"text-kodo-secondary"}),e.jsx("span",{className:"truncate flex-1",children:x.file_name}),e.jsx("button",{onClick:()=>k(j),className:"p-1 hover:bg-white/10 rounded-full text-kodo-red opacity-0 group-hover:opacity-100 transition-opacity",children:e.jsx(Q,{size:12})})]},j))}),e.jsxs("form",{onSubmit:v,className:"flex items-center gap-2",children:[e.jsxs("div",{className:"flex gap-1",children:[e.jsx(_,{type:"button",variant:"ghost",size:"icon",className:"text-kodo-secondary hover:text-white hover:bg-white/5",onClick:()=>g.current?.click(),children:e.jsx(B,{size:20})}),e.jsxs("div",{className:"relative",children:[e.jsx(_,{type:"button",variant:"ghost",size:"icon",className:b("text-kodo-secondary hover:text-white hover:bg-white/5",d&&"text-kodo-steel bg-white/5"),onClick:()=>s(!d),children:e.jsx(F,{size:20})}),d&&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:()=>s(!1)}),e.jsx("div",{className:"relative shadow-2xl rounded-xl overflow-hidden border border-white/10",children:e.jsx(n.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(Ve,{onEmojiClick:P,theme:le.DARK,lazyLoadEmojis:!0,width:350,height:450})})})]})]})]}),e.jsxs("div",{className:"flex-1 relative",children:[e.jsx("input",{type:"text",value:t,onChange:x=>l(x.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:!f||m}),t.length===0&&!m&&e.jsx(_,{type:"button",variant:"ghost",size:"icon",className:"absolute right-1 top-1/2 -translate-y-1/2 h-8 w-8 text-kodo-secondary/30 hover:text-white",children:e.jsx(Ce,{className:"w-4 h-4"})})]}),e.jsx(_,{type:"submit",variant:"primary",size:"icon",className:b("rounded-xl transition-all duration-300",t.trim()||a.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:!f||!t.trim()&&a.length===0||m||p,children:m?e.jsx("div",{className:"w-5 h-5 border-2 border-kodo-void/30 border-t-kodo-void rounded-full animate-spin"}):e.jsx(Se,{size:18,className:b(t.trim()?"translate-x-0.5":"")})})]})]})},lt=()=>{const{isAuthenticated:t}=ue(),{data:l}=A(),a=l?.id,{setWsToken:o,currentConversationId:m,wsStatus:c}=T(),{data:d,isLoading:s,error:i}=re({queryKey:["chatWsToken",a],queryFn:async()=>!t||!a?null:(await M.post("/chat/token",{})).data,enabled:t&&!!a&&c==="disconnected",refetchOnWindowFocus:!1,retry:!1,staleTime:300*1e3,gcTime:600*1e3});return n.useEffect(()=>{d?.token&&(d.token!==T.getState().wsToken||K.WS_URL!==T.getState().wsUrl)&&o(d.token,K.WS_URL)},[d,o]),t?s||c==="connecting"?e.jsxs("div",{className:"flex flex-col items-center justify-center h-[calc(100vh-6.25rem)]",children:[e.jsxs("div",{className:"relative mb-6",children:[e.jsx("div",{className:"w-16 h-16 border-2 border-primary/20 border-t-primary 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-primary rounded-full animate-pulse shadow-[0_0_10px_var(--color-primary)]"})})]}),e.jsx("p",{className:"font-mono text-sm text-primary animate-pulse tracking-widest",children:"ESTABLISHING UPLINK..."})]}):i?e.jsx("div",{className:"flex flex-col items-center justify-center h-[calc(100vh-6.25rem)]",children:e.jsxs(W,{variant:"glass",className:"p-8 text-center max-w-md border-red-500/30",children:[e.jsx(J,{className:"w-12 h-12 text-red-500 mb-4"}),e.jsx("h2",{className:"text-xl font-bold text-white mb-2",children:"Connection Terminated"}),e.jsx("p",{className:"text-red-400/80 mb-4",children:i.message||"Secure handshake failed."}),e.jsx(_,{variant:"outline",onClick:()=>window.location.reload(),children:"Retry Connection"})]})}):e.jsxs("div",{className:"h-[calc(100vh-6rem)] flex gap-6 overflow-hidden p-4 container mx-auto max-w-layout-content",children:[e.jsxs(W,{variant:"glass",className:"w-80 shrink-0 flex flex-col overflow-hidden p-0 border-white/5 bg-black/40 backdrop-blur-2xl",children:[e.jsxs("div",{className:"p-4 border-b border-white/5 flex items-center justify-between",children:[e.jsx("h3",{className:"font-bold text-sm tracking-widest text-muted-foreground uppercase",children:"Channels"}),e.jsx("div",{className:b("w-2 h-2 rounded-full shadow-[0_0_5px_currentColor]",c==="connected"?"bg-lime-500 text-lime-500":"bg-red-500 text-red-500")})]}),e.jsx(qe,{})]}),e.jsxs(W,{variant:"glass",className:"flex-1 flex flex-col overflow-hidden relative p-0 border-white/5 bg-black/40 backdrop-blur-2xl",children:[e.jsx("div",{className:"absolute inset-0 opacity-[0.03] pointer-events-none",style:{backgroundImage:"linear-gradient(var(--color-primary) 1px, transparent 1px), linear-gradient(90deg, var(--color-primary) 1px, transparent 1px)",backgroundSize:"40px 40px"}}),e.jsx("div",{className:"flex-1 overflow-hidden flex flex-col relative z-10",children:e.jsx(He,{conversationId:m||""})}),e.jsx("div",{className:"p-4 border-t border-white/5 bg-black/40 relative z-20 backdrop-blur-xl",children:e.jsx(Be,{})})]})]}):e.jsx("div",{className:"flex flex-col items-center justify-center h-[calc(100vh-6.25rem)]",children:e.jsxs(W,{variant:"glass",className:"p-8 text-center max-w-md border-primary/20",children:[e.jsx(J,{className:"w-12 h-12 text-primary 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 text-muted-foreground",children:"Encrypted channel access requires authorization."}),e.jsx(_,{onClick:()=>window.location.href="/login",children:"Initialize Handshake"})]})})};export{lt as ChatPage};
|