Files
SKEEN-Proyecto/frontend/src/lib/chat.ts

28 lines
788 B
TypeScript

import { useEffect, useState } from 'react';
import { odooApi } from '../services/odoo';
import { useAuth } from './auth';
/** Conteo global de mensajes de chat no leídos, con polling. */
export const useChatUnread = (intervalMs = 15000): number => {
const { user } = useAuth();
const [unread, setUnread] = useState(0);
useEffect(() => {
if (!user) {
setUnread(0);
return;
}
let alive = true;
const fetchCount = () => {
odooApi.getChatUnreadCount()
.then((res) => { if (alive && res.status === 'success') setUnread(res.unread); })
.catch(() => {});
};
fetchCount();
const id = setInterval(fetchCount, intervalMs);
return () => { alive = false; clearInterval(id); };
}, [user, intervalMs]);
return unread;
};