Sidebar: Chat als Unterpunkt von Kunden mit Zahl der ungelesenen Nachrichten
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
717a3323fa
commit
005875d970
7 files changed
+187
-17
No files matched your search
@@ -0,0 +1,31 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import { getChatUnreadCount } from '../lib/teamApi'
|
||||
|
||||
// Zahl ungelesener Kundennachrichten fuer die Sidebar: jede Minute bei sichtbarem Tab und sofort beim
|
||||
// Zurueckkehren; Fehler behalten den letzten Wert.
|
||||
const POLL_MS = 60 * 1000
|
||||
|
||||
export function useChatUnread() {
|
||||
const [unread, setUnread] = useState(0)
|
||||
|
||||
useEffect(() => {
|
||||
let dead = false
|
||||
const load = () => {
|
||||
if (document.visibilityState === 'hidden') return
|
||||
getChatUnreadCount()
|
||||
.then((r) => { if (!dead) setUnread(Number(r?.unread) || 0) })
|
||||
.catch(() => {})
|
||||
}
|
||||
load()
|
||||
const iv = setInterval(load, POLL_MS)
|
||||
const onVisible = () => { if (document.visibilityState === 'visible') load() }
|
||||
document.addEventListener('visibilitychange', onVisible)
|
||||
return () => {
|
||||
dead = true
|
||||
clearInterval(iv)
|
||||
document.removeEventListener('visibilitychange', onVisible)
|
||||
}
|
||||
}, [])
|
||||
|
||||
return unread
|
||||
}
|
||||
Reference in new issue
Block a user