1import{r as s}from"./vendor-react-ZTecBUnV.js";import{aE as E,s as c,ag as u}from"./index-KGKWUjdc.js";const C=f=>{const{user:o}=E(),[g,m]=s.useState([]),[w,_]=s.useState(!0),d=s.useRef(null),r=f?`challenge-live-chat-day-${f}`:void 0,h=s.useCallback(async()=>{try{_(!0);let e=c.from("warriors_way_comments").select("id, user_id, module_id, content, created_at, author_name, parent_id").order("created_at",{ascending:!0});r?e=e.eq("module_id",r):e=e.like("module_id","challenge-live-chat-%");const{data:t,error:a}=await e.limit(200);if(a)throw a;m(t||[])}catch(e){console.error("Error fetching live chat messages:",e)}finally{_(!1)}},[r]);s.useEffect(()=>{h();const e=r?`module_id=eq.${r}`:"module_id=like.challenge-live-chat-%",t=c.channel(`live-chat-${r||"all"}`).on("postgres_changes",{event:"INSERT",schema:"public",table:"warriors_way_comments",filter:e},a=>{const n=a.new;m(i=>[...i,n])}).on("postgres_changes",{event:"DELETE",schema:"public",table:"warriors_way_comments",filter:e},a=>{const n=a.old.id;m(i=>i.filter(l=>l.id!==n))}).subscribe();return d.current=t,()=>{d.current&&c.removeChannel(d.current)}},[h,r]);const y=s.useCallback(async e=>{var a,n,i;if(!o||!r)return u.error("Trebuie sÄ fii autentificat"),!1;if(!e.trim())return!1;const t=((a=o.user_metadata)==null?void 0:a.full_name)||((n=o.user_metadata)==null?void 0:n.name)||((i=o.email)==null?void 0:i.split("@")[0])||"Utilizator";try{const{error:l}=await c.from("warriors_way_comments").insert({user_id:o.id,module_id:r,content:e.trim(),author_name:t,parent_id:null});if(l)throw l;return!0}catch(l){return console.error("Error sending message:",l),u.error("Eroare la trimiterea mesajului"),!1}},[o,r]),p=s.useCallback(async e=>{try{const{error:t}=await c.from("warriors_way_comments").delete().eq("id",e);if(t)throw t;return u.success("Mesaj Èters"),!0}catch(t){return console.error("Error deleting message:",t),u.error("Eroare la Ètergerea mesajului"),!1}},[]);return{messages:g,isLoading:w,sendMessage:y,deleteMessage:p,refetch:h}};export{C as u};
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.