11'use client' ;
22import { useWebSocket } from '@/hooks/socket-provider' ;
33import { ContainerData , SystemStatsType } from '@/redux/types/monitor' ;
4- import { useEffect , useState , useRef } from 'react' ;
4+ import { useEffect , useState , useRef , useCallback } from 'react' ;
55
66function use_monitor ( ) {
77 const { sendJsonMessage, message, isReady } = useWebSocket ( ) ;
@@ -12,6 +12,33 @@ function use_monitor() {
1212 const reconnectTimeoutRef = useRef < NodeJS . Timeout | undefined > ( undefined ) ;
1313 const isInitializedRef = useRef ( false ) ;
1414
15+ const startMonitoring = useCallback ( ( ) => {
16+ if ( ! isReady ) {
17+ console . log ( 'WebSocket not ready, skipping monitoring start' ) ;
18+ return ;
19+ }
20+
21+ console . log ( 'Starting dashboard monitoring' ) ;
22+ sendJsonMessage ( {
23+ action : 'dashboard_monitor' ,
24+ data : {
25+ interval : 10 ,
26+ operations : [ 'get_containers' , 'get_system_stats' ]
27+ }
28+ } ) ;
29+ setIsMonitoring ( true ) ;
30+ setLastError ( null ) ;
31+ } , [ isReady , sendJsonMessage ] ) ;
32+
33+ const stopMonitoring = useCallback ( ( ) => {
34+ console . log ( 'Stopping dashboard monitoring' ) ;
35+ sendJsonMessage ( {
36+ action : 'stop_dashboard_monitor'
37+ } ) ;
38+ setIsMonitoring ( false ) ;
39+ } , [ sendJsonMessage ] ) ;
40+
41+ // Handle incoming WebSocket messages
1542 useEffect ( ( ) => {
1643 if ( message ) {
1744 try {
@@ -30,65 +57,50 @@ function use_monitor() {
3057 setLastError ( null ) ;
3158 } else if ( parsedMessage . action === 'error' ) {
3259 setLastError ( parsedMessage . error || 'Unknown error occurred' ) ;
33- if ( isMonitoring ) {
34- stopMonitoring ( ) ;
35- setTimeout ( startMonitoring , 5000 ) ;
36- }
60+ // Retry after error
61+ setTimeout ( ( ) => {
62+ if ( isReady ) {
63+ startMonitoring ( ) ;
64+ }
65+ } , 5000 ) ;
3766 }
3867 } catch ( error ) {
3968 console . error ( 'Error parsing WebSocket message:' , error ) ;
4069 setLastError ( 'Failed to parse message' ) ;
4170 }
4271 }
43- } , [ message ] ) ;
44-
45- const startMonitoring = ( ) => {
46- if ( ! isMonitoring ) {
47- sendJsonMessage ( {
48- action : 'dashboard_monitor' ,
49- data : {
50- interval : 10 ,
51- operations : [ 'get_containers' , 'get_system_stats' ]
52- }
53- } ) ;
54- setIsMonitoring ( true ) ;
55- setLastError ( null ) ;
56- }
57- } ;
58-
59- const stopMonitoring = ( ) => {
60- if ( isMonitoring ) {
61- sendJsonMessage ( {
62- action : 'stop_dashboard_monitor'
63- } ) ;
64- setIsMonitoring ( false ) ;
65- }
66- } ;
72+ } , [ message , isReady , startMonitoring ] ) ;
6773
74+ // Initialize monitoring when WebSocket is ready
6875 useEffect ( ( ) => {
6976 if ( isReady && ! isInitializedRef . current ) {
77+ console . log ( 'WebSocket ready, initializing monitoring' ) ;
7078 startMonitoring ( ) ;
7179 isInitializedRef . current = true ;
7280 }
81+
7382 return ( ) => {
7483 if ( reconnectTimeoutRef . current ) {
7584 clearTimeout ( reconnectTimeoutRef . current ) ;
7685 }
7786 } ;
78- } , [ isReady ] ) ;
87+ } , [ isReady , startMonitoring ] ) ;
7988
89+ // Retry monitoring on error
8090 useEffect ( ( ) => {
8191 if ( isReady && ! isMonitoring && lastError ) {
92+ console . log ( 'Retrying monitoring after error' ) ;
8293 reconnectTimeoutRef . current = setTimeout ( ( ) => {
8394 startMonitoring ( ) ;
8495 } , 5000 ) ;
8596 }
97+
8698 return ( ) => {
8799 if ( reconnectTimeoutRef . current ) {
88100 clearTimeout ( reconnectTimeoutRef . current ) ;
89101 }
90102 } ;
91- } , [ isReady , isMonitoring , lastError ] ) ;
103+ } , [ isReady , isMonitoring , lastError , startMonitoring ] ) ;
92104
93105 return {
94106 containersData,
0 commit comments