Spaces:
Running
Running
Download Frontend/src/hooks/useRealtimeNotifications.js from ritesh19180/ai-helpdesk-api: direct link, hf CLI and curl.
- Browser
- Download file 1.66 kB
-
https://huggingface.co/spaces/ritesh19180/ai-helpdesk-api/resolve/main/Frontend/src/hooks/useRealtimeNotifications.js
- Command line
-
hf download hf://spaces/ritesh19180/ai-helpdesk-api/Frontend/src/hooks/useRealtimeNotifications.js
-
curl -L -o useRealtimeNotifications.js https://huggingface.co/spaces/ritesh19180/ai-helpdesk-api/resolve/main/Frontend/src/hooks/useRealtimeNotifications.js
1.66 kB
| import { useEffect } from 'react'; | |
| import { supabase } from '../lib/supabaseClient'; | |
| import useAuthStore from '../store/authStore'; | |
| import useTicketStore from '../store/ticketStore'; | |
| const useTicketsRealtime = () => { | |
| const { user, profile } = useAuthStore(); | |
| const { addTicket, updateTicket, removeTicket } = useTicketStore(); | |
| useEffect(() => { | |
| if (!user || !profile) return; | |
| // Only admins see the live ticket queue | |
| const isAdmin = profile.role === 'admin' || profile.role === 'master_admin'; | |
| if (!isAdmin) return; | |
| const channel = supabase | |
| .channel('tickets-realtime-dashboard') | |
| .on( | |
| 'postgres_changes', | |
| { | |
| event: '*', | |
| schema: 'public', | |
| table: 'tickets', | |
| filter: `company_id=eq.${profile.company_id}`, | |
| }, | |
| (payload) => { | |
| const { eventType, new: newRecord, old: oldRecord } = payload; | |
| if (eventType === 'INSERT') { | |
| addTicket(newRecord); | |
| } | |
| if (eventType === 'UPDATE') { | |
| updateTicket(newRecord.ticket_id, newRecord); | |
| } | |
| if (eventType === 'DELETE') { | |
| removeTicket(oldRecord.ticket_id); | |
| } | |
| } | |
| ) | |
| .subscribe(); | |
| return () => { | |
| supabase.removeChannel(channel); | |
| }; | |
| }, [user, profile, addTicket, updateTicket, removeTicket]); | |
| }; | |
| export default useTicketsRealtime; |