Files
appco_dist/.output/public/_nuxt/CjA4i-tX.js
T
2026-02-01 21:53:26 +03:30

2 lines
1.4 KiB
JavaScript

import{cB as C,H as p,G as S,r as n,u as y,df as b,M as I,dg as g,h as T,ao as x,dh as M}from"./CqMGdh3i.js";const $=C("chat",()=>{const t=p(),r=S(),a=n([]),u=n(!1),f=n(0),m=y(),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))},_=async()=>{try{if(!o.value){u.value=!0;const e=await b(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 M(e),w=async e=>{a.value.unshift(h(e))};return{chats:a,runChannel:()=>{I.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 w(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:()=>T(x,{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:_}});export{$ as u};