Files
appco_dist/.output/public/_nuxt/DgD9KkjH.js
T
2025-08-24 13:00:31 +03:30

2 lines
1.4 KiB
JavaScript

import{b0 as w,K as b,H as p,r as n,u as S,bB as y,P as x,bx as g,h as I,aK as P,bC as T}from"./BYv1load.js";const k=w("chat",()=>{const t=b(),r=p(),a=n([]),u=n(!1),f=n(0),m=S(),c=n(10),o=n(!1),l=e=>{const s=a.value.findIndex(i=>i.chat_id===e.chat_id);s>=0?Object.assign(a.value[s],{...e}):a.value.push(e)},v=e=>{e.map(s=>l(s))},C=async()=>{try{if(!o.value){u.value=!0;const e=await y(t.user.id,++f.value,c.value);v(e),e?.length<c.value&&(o.value=!0)}}catch(e){console.log("e ===>",e)}finally{u.value=!1}},h=async e=>await T(e),_=async e=>{a.value.unshift(h(e))};return{chats:a,runChannel:()=>{x.channel("realtime:chats").on("postgres_changes",{event:"*",schema:"public",table:"chats"},async e=>{if(t.user?.id&&(e.new.user1_id==t.user.id||e.new.user2_id==t.user.id)){if(e.eventType==="INSERT")await _(e.new.chat_id);else if(e.eventType==="UPDATE"){const s=e.new;if(l(s),s?.last_message?.sender_id&&s?.last_message?.sender_id!==t.user?.id&&!s?.last_message?.is_read){const i=`open${Date.now()}`,d=await h(s.chat_id);g.open({message:r.useString("newMessage"),description:`${r.useString("YouHaveNewMessageFrom")}: ${t.user?.id===s.user1_id?d?.user2?.name:d?.user1?.name}`,btn:()=>I(P,{type:"primary",size:"small",onClick:async()=>{g.close(i),await m.push({name:"message-detail_message",query:{id:s.chat_id}})}},{default:()=>r.useString("show")}),key:i,onClose:close})}}}}).subscribe()},request:C}});export{k as u};