Files
appco_dist/.output/server/chunks/build/chatStore-CEJ4ipeC.mjs
T
2025-07-23 16:57:31 +03:30

101 lines
3.6 KiB
JavaScript

import { defineStore } from 'pinia';
import { ref, h } from 'vue';
import { b as useUserStore, a as useConfigStore, u as useRouter, aG as fetchUserChats, s as supabase, aF as api, a1 as Button, aH as fetchOneChat } from './server.mjs';
const useChatStore = defineStore("chat", () => {
const userStore = useUserStore();
const config = useConfigStore();
const chats = ref([]);
const loading = ref(false);
const currentPage = ref(0);
const router = useRouter();
const limit = ref(10);
const endPage = ref(false);
const setChat = (_chat) => {
const index = chats.value.findIndex((item) => item.chat_id === _chat.chat_id);
if (index >= 0) {
Object.assign(chats.value[index], {
..._chat
});
} else {
chats.value.push(_chat);
}
};
const setChats = (_chats) => {
_chats.map((item) => setChat(item));
};
const request = async () => {
try {
if (!endPage.value) {
loading.value = true;
const res = await fetchUserChats(userStore.user.id, ++currentPage.value, limit.value);
setChats(res);
if ((res == null ? void 0 : res.length) < limit.value) {
endPage.value = true;
}
}
} catch (e) {
console.log("e ===>", e);
} finally {
loading.value = false;
}
};
const getChatWithId = async (chat_id) => {
return await fetchOneChat(chat_id);
};
const addChatWithId = async (chat_id) => {
chats.value.unshift(getChatWithId(chat_id));
};
const runChannel = () => {
supabase.channel("realtime:chats").on(
"postgres_changes",
{
event: "*",
schema: "public",
table: "chats"
},
async (payload) => {
var _a, _b, _c, _d, _e, _f, _g, _h;
if (((_a = userStore.user) == null ? void 0 : _a.id) && (payload.new.user1_id == userStore.user.id || payload.new.user2_id == userStore.user.id)) {
if (payload.eventType === "INSERT") {
await addChatWithId(payload.new.chat_id);
} else if (payload.eventType === "UPDATE") {
const chat = payload.new;
setChat(chat);
if (((_b = chat == null ? void 0 : chat.last_message) == null ? void 0 : _b.sender_id) && ((_c = chat == null ? void 0 : chat.last_message) == null ? void 0 : _c.sender_id) !== ((_d = userStore.user) == null ? void 0 : _d.id) && !((_e = chat == null ? void 0 : chat.last_message) == null ? void 0 : _e.is_read)) {
const key = `open${Date.now()}`;
const getChat = await getChatWithId(chat.chat_id);
api.open({
message: config.useString("newMessage"),
description: `${config.useString("YouHaveNewMessageFrom")}: ${((_f = userStore.user) == null ? void 0 : _f.id) === chat.user1_id ? (_g = getChat == null ? void 0 : getChat.user2) == null ? void 0 : _g.name : (_h = getChat == null ? void 0 : getChat.user1) == null ? void 0 : _h.name}`,
btn: () => h(
Button,
{
type: "primary",
size: "small",
onClick: async () => {
api.close(key);
await router.push({ name: "message-detail_message", query: { id: chat.chat_id } });
}
},
{ default: () => config.useString("show") }
),
key,
onClose: close
});
}
}
}
}
).subscribe();
};
return {
chats,
runChannel,
request
};
});
export { useChatStore as u };
//# sourceMappingURL=chatStore-CEJ4ipeC.mjs.map