|
| 1 | +import { useEffect, useRef } from 'react'; |
| 2 | +import { socket } from '../services/socket'; |
| 3 | +import { useBrowserNotifications } from './useBrowserNotifications'; |
| 4 | +import { useNotificationSound } from './useNotificationSound'; |
| 5 | + |
| 6 | +interface UserRoom { |
| 7 | + _id: string; |
| 8 | + title: string; |
| 9 | + roomId: string; |
| 10 | +} |
| 11 | + |
| 12 | +export function useGlobalNotifications(userId: string | null) { |
| 13 | + const { showNotification, requestPermission } = useBrowserNotifications(); |
| 14 | + const { playSound } = useNotificationSound(); |
| 15 | + const userRoomsRef = useRef<UserRoom[]>([]); |
| 16 | + const currentRoomRef = useRef<string | null>(null); |
| 17 | + |
| 18 | + // Set current room (to avoid notifying when user is actively in that room) |
| 19 | + const setCurrentRoom = (roomId: string | null) => { |
| 20 | + currentRoomRef.current = roomId; |
| 21 | + }; |
| 22 | + |
| 23 | + useEffect(() => { |
| 24 | + if (!userId) return; |
| 25 | + |
| 26 | + // Request notification permission |
| 27 | + requestPermission(); |
| 28 | + |
| 29 | + // Fetch user's rooms and join them via socket |
| 30 | + const fetchAndJoinRooms = async () => { |
| 31 | + try { |
| 32 | + const response = await fetch('/api/room/joined', { |
| 33 | + credentials: 'include', |
| 34 | + }); |
| 35 | + |
| 36 | + if (response.ok) { |
| 37 | + const data = await response.json(); |
| 38 | + const rooms: UserRoom[] = data.rooms; |
| 39 | + userRoomsRef.current = rooms; |
| 40 | + |
| 41 | + // Connect socket if not already connected |
| 42 | + if (!socket.connected) { |
| 43 | + socket.connect(); |
| 44 | + } |
| 45 | + |
| 46 | + // Join all user's rooms |
| 47 | + rooms.forEach((room) => { |
| 48 | + socket.emit('join_room', { room: room._id, userId }); |
| 49 | + }); |
| 50 | + |
| 51 | + console.log(`[Global Notifications] Joined ${rooms.length} rooms`); |
| 52 | + } |
| 53 | + } catch (error) { |
| 54 | + console.error('[Global Notifications] Failed to fetch rooms:', error); |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + fetchAndJoinRooms(); |
| 59 | + |
| 60 | + // Listen for messages from ALL rooms |
| 61 | + const handleGlobalMessage = (message: any) => { |
| 62 | + const senderId = message.sender?._id || message.sender; |
| 63 | + const roomId = message.room; |
| 64 | + |
| 65 | + // Don't notify if: |
| 66 | + // 1. Message is from current user |
| 67 | + // 2. User is currently viewing this room |
| 68 | + if (senderId === userId || roomId === currentRoomRef.current) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + // Find room info |
| 73 | + const room = userRoomsRef.current.find((r) => r._id === roomId); |
| 74 | + const roomTitle = room?.title || 'Chat'; |
| 75 | + const senderName = message.sender?.name || 'Someone'; |
| 76 | + const messageText = message.text || 'Sent an image'; |
| 77 | + |
| 78 | + // Play sound |
| 79 | + playSound(); |
| 80 | + |
| 81 | + // Show browser notification |
| 82 | + showNotification(`${senderName} in #${roomTitle}`, { |
| 83 | + body: messageText, |
| 84 | + icon: '/tnc-logo.png', |
| 85 | + tag: roomId, |
| 86 | + data: { roomId, roomTitle }, |
| 87 | + }); |
| 88 | + }; |
| 89 | + |
| 90 | + socket.on('receive_message', handleGlobalMessage); |
| 91 | + socket.on('received_message', handleGlobalMessage); |
| 92 | + |
| 93 | + return () => { |
| 94 | + socket.off('receive_message', handleGlobalMessage); |
| 95 | + socket.off('received_message', handleGlobalMessage); |
| 96 | + }; |
| 97 | + }, [userId, showNotification, playSound, requestPermission]); |
| 98 | + |
| 99 | + return { setCurrentRoom }; |
| 100 | +} |
0 commit comments