added trigger for chat sms

This commit is contained in:
2026-08-04 18:59:52 +03:30
parent 45647e7266
commit e8d5dc9f66
3 changed files with 105 additions and 2 deletions
+83 -1
View File
@@ -2,6 +2,7 @@ import { computed, ref, watch } from 'vue'
import { useWebSocket } from '@vueuse/core'
import { useUserStore } from '@/store/userStore'
import { createUuid } from '@/utils/index'
import { postAdminChatFocus, postAdminChatUnfocus, postAdminChatEnsureWebhook } from '@/api/chatFocus'
type RawChatRoom = Record<string, any>
type RawChatMessage = Record<string, any>
@@ -29,6 +30,7 @@ const MAX_RESEND_ATTEMPTS = 1
const CONNECTING_TIMEOUT_MS = 12000
const RECONNECT_WATCH_INTERVAL_MS = 5000
const READ_SIGNAL_DEBOUNCE_MS = 1200
const FOCUS_HEARTBEAT_MS = 20000
type AdminOutgoingMessageType = 'text' | 'image' | 'voice'
@@ -70,6 +72,8 @@ const lastReadSignalAt = ref(0)
const lastReadSignalRoomId = ref('')
let readSignalTimer: number | null = null
let pendingAdminReadSignalRoomId = ''
let focusHeartbeatTimer: number | null = null
let lastReportedFocusRoomId = ''
let bootstrapPromise: Promise<void> | null = null
@@ -132,7 +136,12 @@ export const setAdminSupportPageOpen = (value: boolean) => {
isAdminActiveRoomAtBottom.value = false
clearReadSignalTimer()
pendingAdminReadSignalRoomId = ''
const previousRoom = activeRoomId.value || lastReportedFocusRoomId
activeRoomId.value = ''
stopFocusHeartbeat()
if (previousRoom) {
void syncAdminChatFocus(null, previousRoom)
}
return
}
@@ -146,6 +155,8 @@ export const setAdminSupportPageOpen = (value: boolean) => {
scheduleAdminReadSignal(80)
}
refreshFocusHeartbeat()
if (value && !manuallyClosed && status.value !== 'OPEN') {
safeReconnect('admin support page opened').catch((error) => {
console.error('admin reconnect on page open failed:', error)
@@ -164,6 +175,63 @@ const isViewingRoomNow = (roomId: string) => {
)
}
const syncAdminChatFocus = async (roomId: string | null, previousRoomId?: string) => {
try {
if (!roomId) {
if (previousRoomId || lastReportedFocusRoomId) {
await postAdminChatUnfocus(previousRoomId || lastReportedFocusRoomId)
}
lastReportedFocusRoomId = ''
return
}
await postAdminChatFocus({
room_id: roomId,
previous_room_id: previousRoomId && previousRoomId !== roomId ? previousRoomId : undefined
})
lastReportedFocusRoomId = roomId
} catch (error) {
console.error('admin chat focus sync failed:', error)
}
}
const stopFocusHeartbeat = () => {
if (focusHeartbeatTimer !== null) {
window.clearInterval(focusHeartbeatTimer)
focusHeartbeatTimer = null
}
}
const refreshFocusHeartbeat = () => {
stopFocusHeartbeat()
const roomId = activeRoomId.value
if (!isViewingRoomNow(roomId)) {
if (lastReportedFocusRoomId) {
void syncAdminChatFocus(null, lastReportedFocusRoomId)
}
return
}
void syncAdminChatFocus(
roomId,
lastReportedFocusRoomId && lastReportedFocusRoomId !== roomId
? lastReportedFocusRoomId
: undefined
)
focusHeartbeatTimer = window.setInterval(() => {
if (!isViewingRoomNow(activeRoomId.value)) {
stopFocusHeartbeat()
if (lastReportedFocusRoomId) {
void syncAdminChatFocus(null, lastReportedFocusRoomId)
}
return
}
void syncAdminChatFocus(activeRoomId.value)
}, FOCUS_HEARTBEAT_MS)
}
const canTreatAdminRoomAsRead = (roomId = activeRoomId.value) => {
return Boolean(
roomId &&
@@ -1032,7 +1100,13 @@ const bindNetworkListeners = () => {
})
document.addEventListener('visibilitychange', () => {
if (document.visibilityState !== 'visible') return
if (document.visibilityState !== 'visible') {
stopFocusHeartbeat()
if (lastReportedFocusRoomId) {
void syncAdminChatFocus(null, lastReportedFocusRoomId)
}
return
}
if (isAdminSupportPageOpen.value && activeRoomId.value) {
if (canTreatAdminRoomAsRead(activeRoomId.value)) {
@@ -1041,6 +1115,8 @@ const bindNetworkListeners = () => {
}
}
refreshFocusHeartbeat()
if (!shouldKeepAdminSocketAlive()) return
if (status.value !== 'OPEN') {
@@ -1435,6 +1511,8 @@ export const setAdminActiveRoom = async (
pendingAdminReadSignalRoomId = ''
}
refreshFocusHeartbeat()
if (isViewingRoomNow(roomId)) {
clearRoomUnread(roomId)
}
@@ -1609,6 +1687,10 @@ export const bootstrapAdminChat = async (
bootstrapPromise = (async () => {
try {
void postAdminChatEnsureWebhook().catch((error) => {
console.error('admin chat ensure webhook failed:', error)
})
await fetchAdminRooms()
const shouldConnect = connectEvenIfNoRooms || rooms.value.length > 0