Skip to content

Commit d557dd3

Browse files
authored
Merge pull request #145 from oslabs-beta/dev
Dev
2 parents 009055d + 5ce58de commit d557dd3

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

client/src/App.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { SignedIn, SignedOut } from '@clerk/clerk-react';
1212
import { Routes, Route } from 'react-router-dom';
1313
import { ClusterProvider } from './contexts/ClusterContext';
1414
import { Toaster } from './components/ui/sonner';
15+
import ToastNotifications from './components/ToastNotifications';
1516

1617
const App = () => {
1718
const [isDark, setIsDark] = useState(() => {
@@ -74,6 +75,8 @@ const App = () => {
7475
duration={5000}
7576
closeButton={false}
7677
/>
78+
{/* SIMPLE WEBSOCKET TOAST NOTIFICATIONS */}
79+
<ToastNotifications />
7780
<Routes>
7881
<Route path="/" element={<Dashboard />} />
7982
<Route path="/historical" element={<HistoricalData />} />
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

Comments
 (0)