Chat interno 1-a-1 entre usuarios de la plataforma (addon skeen_chat + pagina Chat con badge de no leidos)
This commit is contained in:
27
frontend/src/lib/chat.ts
Normal file
27
frontend/src/lib/chat.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
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;
|
||||
};
|
||||
Reference in New Issue
Block a user