1- import React , { useState , useRef , useImperativeHandle , useEffect } from "react" ;
1+ import React , {
2+ useState ,
3+ useRef ,
4+ useImperativeHandle ,
5+ useEffect ,
6+ useCallback ,
7+ } from "react" ;
28
39import { Line } from "react-chartjs-2" ;
410
@@ -11,10 +17,11 @@ import ChartStreaming from "chartjs-plugin-streaming";
1117import { ChartJSOrUndefined } from "react-chartjs-2/dist/types" ;
1218import { MessageToBoard } from "./MessageToBoard" ;
1319
14- Chart . register ( ChartStreaming ) ;
15-
1620// eslint-disable-next-line
1721import Worker from "worker-loader!./msgAggregatorWorker" ;
22+ import { Snackbar } from "@arduino/arc" ;
23+
24+ Chart . register ( ChartStreaming ) ;
1825const worker = new Worker ( ) ;
1926
2027function _Chart (
@@ -124,9 +131,21 @@ function _Chart(
124131 } ,
125132 } ) ;
126133
134+ const enableTooltips = useCallback (
135+ ( newState : boolean ) => {
136+ ( opts . plugins as any ) . tooltip . enabled = newState ;
137+ opts . datasets ! . line ! . pointHoverRadius = newState ? 3 : 0 ;
138+ setOpts ( opts ) ;
139+ chartRef . current ?. update ( ) ;
140+ } ,
141+ [ opts ]
142+ ) ;
143+
127144 useEffect ( ( ) => {
128145 if ( ! config . connected ) {
129146 setConnected ( false ) ;
147+ // when disconnected, force tooltips to be enabled
148+ enableTooltips ( true ) ;
130149 return ;
131150 }
132151
@@ -135,8 +154,11 @@ function _Chart(
135154 // cleanup buffer state
136155 worker . postMessage ( { command : "cleanup" } ) ;
137156 setConnected ( true ) ;
157+
158+ // restore the tooltips state (which match the pause state when connected)
159+ enableTooltips ( pause ) ;
138160 }
139- } , [ config . connected , connected ] ) ;
161+ } , [ config . connected , connected , pause , enableTooltips ] ) ;
140162
141163 const togglePause = ( newState : boolean ) => {
142164 if ( newState === pause ) {
@@ -146,9 +168,8 @@ function _Chart(
146168 ( chartRef . current as any ) . options . scales . x . realtime . pause = pause ;
147169 }
148170 setPause ( newState ) ;
149- ( opts . plugins as any ) . tooltip . enabled = newState ;
150- opts . datasets ! . line ! . pointHoverRadius = newState ? 3 : 0 ;
151- setOpts ( opts ) ;
171+ worker . postMessage ( { command : "cleanup" } ) ;
172+ enableTooltips ( newState ) ;
152173 } ;
153174
154175 const setInterpolate = ( interpolate : boolean ) => {
@@ -221,6 +242,22 @@ function _Chart(
221242 < Line data = { initialData } ref = { chartRef as any } options = { opts } />
222243 </ div >
223244 < MessageToBoard config = { config } wsSend = { wsSend } />
245+
246+ { ! connected && (
247+ < Snackbar
248+ anchorOrigin = { {
249+ horizontal : "center" ,
250+ vertical : "bottom" ,
251+ } }
252+ autoHideDuration = { 7000 }
253+ className = "snackbar"
254+ closeable
255+ isOpen
256+ message = "Board disconnected"
257+ theme = { config . darkTheme ? "dark" : "light" }
258+ turnOffAutoHide
259+ />
260+ ) }
224261 </ div >
225262 </ >
226263 ) ;
0 commit comments