@@ -31,7 +31,7 @@ import { StoredData } from '@/lib/types/storedData';
3131import { Tab } from '@/lib/types/tab' ;
3232import { View } from '@/lib/types/view' ;
3333import { ThemeName , getTheme } from '@/theme' ;
34- import { writeStoredData , getStoredData , defaultStoredData } from '@/lib/localStorage' ;
34+ import { writeStoredData , getStoredData , defaultStoredData , flushStoredData } from '@/lib/localStorage' ;
3535import RequestDetailsWrapper from './components/requestDetailsWrapper' ;
3636import RequestsTableWrapper from './components/requestsTableWrapper' ;
3737import './styles.scss' ;
@@ -304,6 +304,40 @@ const HomePage = () => {
304304 } , [ isClient ] ) ;
305305
306306 const pollingIntervalRef = useRef < number | undefined > ( undefined ) ;
307+ const isPageVisibleRef = useRef ( true ) ;
308+
309+ useEffect ( ( ) => {
310+ const handleVisibilityChange = ( ) => {
311+ isPageVisibleRef . current = ! document . hidden ;
312+
313+ if ( document . hidden ) {
314+ if ( pollingIntervalRef . current ) {
315+ window . clearInterval ( pollingIntervalRef . current ) ;
316+ pollingIntervalRef . current = undefined ;
317+ }
318+ flushStoredData ( ) ;
319+ } else if ( isClient && storedData . tabs . length > 0 ) {
320+ if ( ! pollingIntervalRef . current ) {
321+ processPolledData ( ) ; // Immediate poll on return
322+ pollingIntervalRef . current = window . setInterval ( ( ) => {
323+ processPolledData ( ) ;
324+ } , 4000 ) ;
325+ }
326+ }
327+ } ;
328+
329+ document . addEventListener ( 'visibilitychange' , handleVisibilityChange ) ;
330+
331+ const handleBeforeUnload = ( ) => {
332+ flushStoredData ( ) ;
333+ } ;
334+ window . addEventListener ( 'beforeunload' , handleBeforeUnload ) ;
335+
336+ return ( ) => {
337+ document . removeEventListener ( 'visibilitychange' , handleVisibilityChange ) ;
338+ window . removeEventListener ( 'beforeunload' , handleBeforeUnload ) ;
339+ } ;
340+ } , [ isClient , storedData . tabs . length , processPolledData ] ) ;
307341
308342 useEffect ( ( ) => {
309343 if ( ! isClient || storedData . tabs . length === 0 ) return ;
@@ -312,9 +346,11 @@ const HomePage = () => {
312346 window . clearInterval ( pollingIntervalRef . current ) ;
313347 }
314348
315- pollingIntervalRef . current = window . setInterval ( ( ) => {
316- processPolledData ( ) ;
317- } , 4000 ) ;
349+ if ( isPageVisibleRef . current ) {
350+ pollingIntervalRef . current = window . setInterval ( ( ) => {
351+ processPolledData ( ) ;
352+ } , 4000 ) ;
353+ }
318354
319355 const tempFilteredData = storedData . data
320356 . filter ( ( item ) => item [ 'unique-id' ] === storedData . selectedTab [ 'unique-id' ] ) ;
@@ -333,13 +369,15 @@ const HomePage = () => {
333369 item === storedData . selectedTab
334370 ) ;
335371
372+ const theme = useMemo ( ( ) => getTheme ( storedData . theme ) , [ storedData . theme ] ) ;
373+
336374 if ( ! isClient ) {
337375 return null ;
338376 }
339377
340378 return (
341- < ThemeProvider theme = { getTheme ( storedData . theme ) } >
342- < GlobalStyles theme = { getTheme ( storedData . theme ) } />
379+ < ThemeProvider theme = { theme } >
380+ < GlobalStyles theme = { theme } />
343381 < div className = "main" >
344382 < AppLoader isRegistered = { isRegistered } mode = { loaderAnimationMode } />
345383 { aboutPopupVisibility && (
0 commit comments