|
| 1 | +// ---------------------------------------------------------- |
| 2 | +// >> SONNER TOAST NOTIFICATIONS << |
| 3 | +// ---------------------------------------------------------- |
| 4 | +// * This component sends real-time notifications to the dashboard * |
| 5 | +// https://ui.shadcn.com/docs/components/sonner |
| 6 | + |
| 7 | +import { useEffect } from 'react'; |
| 8 | +import { io } from 'socket.io-client'; |
| 9 | +import { toast } from 'sonner'; |
| 10 | + |
| 11 | +interface K8sNotification { |
| 12 | + id: string; |
| 13 | + type: 'error' | 'warning' | 'info'; |
| 14 | + title: string; |
| 15 | + message: string; |
| 16 | + metadata: { |
| 17 | + namespace: string; |
| 18 | + pod: string; |
| 19 | + reason: string; |
| 20 | + severity: 'low' | 'medium' | 'high' | 'critical'; |
| 21 | + }; |
| 22 | +} |
| 23 | + |
| 24 | +const ToastNotifications = () => { |
| 25 | + useEffect(() => { |
| 26 | + // Connect to WebSocket server |
| 27 | + const socket = io(import.meta.env.VITE_API_URL || 'http://localhost:3000'); |
| 28 | + |
| 29 | + // Listen for K8s notifications |
| 30 | + socket.on('k8s-notification', (notification: K8sNotification) => { |
| 31 | + console.log('[Toast] Received K8s notification:', notification); |
| 32 | + |
| 33 | + // Create toast message |
| 34 | + const message = `${notification.title}\n${notification.metadata.namespace}/${notification.metadata.pod}\n${notification.message}`; |
| 35 | + |
| 36 | + // Show toast based on type and severity |
| 37 | + switch (notification.type) { |
| 38 | + case 'error': |
| 39 | + if (notification.metadata.severity === 'critical') { |
| 40 | + toast.error(message, { |
| 41 | + duration: 15000, |
| 42 | + action: { |
| 43 | + label: 'View Details', |
| 44 | + onClick: () => |
| 45 | + console.log('View details for:', notification.id), |
| 46 | + }, |
| 47 | + }); |
| 48 | + } else { |
| 49 | + toast.error(message, { duration: 10000 }); |
| 50 | + } |
| 51 | + break; |
| 52 | + case 'warning': |
| 53 | + toast.warning(message, { duration: 7000 }); |
| 54 | + break; |
| 55 | + case 'info': |
| 56 | + toast.info(message, { duration: 5000 }); |
| 57 | + break; |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + socket.on('connect', () => { |
| 62 | + console.log('[Toast] Connected to WebSocket server'); |
| 63 | + toast.success('Connected to monitoring server'); |
| 64 | + }); |
| 65 | + |
| 66 | + socket.on('disconnect', () => { |
| 67 | + console.log('[Toast] Disconnected from WebSocket server'); |
| 68 | + toast.warning('Disconnected from monitoring server'); |
| 69 | + }); |
| 70 | + |
| 71 | + // Cleanup on unmount |
| 72 | + return () => { |
| 73 | + socket.disconnect(); |
| 74 | + }; |
| 75 | + }, []); |
| 76 | + |
| 77 | + // This component doesn't render anything visible |
| 78 | + return null; |
| 79 | +}; |
| 80 | + |
| 81 | +export default ToastNotifications; |
0 commit comments