11import { useEffect } from 'react'
22import { useLocation } from 'react-router-dom'
33import dayjs from 'dayjs'
4- import useSWR from 'swr'
54
65import { useAppDispatch , useProfile } from '@/store'
76import { trafficActions } from '@/store/slices/traffic'
@@ -17,67 +16,64 @@ const useTrafficUpdater = () => {
1716
1817 const isInForeground =
1918 location . pathname === '/traffic' || location . pathname === '/home'
20- const { data : traffic } = useSWR (
21- profile !== undefined ? '/traffic' : null ,
22- ( url ) =>
23- fetcher < Traffic & { nowTime : number } > ( { url } ) . then ( ( res ) => {
24- res . nowTime = Date . now ( )
25- return res
26- } ) ,
27- {
28- revalidateOnFocus : false ,
29- revalidateOnReconnect : false ,
30- refreshWhenHidden : true ,
31- refreshInterval : isInForeground ? REFRESH_RATE : 0 ,
32- dedupingInterval : isInForeground ? REFRESH_RATE : 0 ,
33- } ,
34- )
3519
3620 useEffect ( ( ) => {
37- if ( ! traffic ) return undefined
21+ if ( profile === undefined ) return
3822
39- const now = Date . now ( )
23+ const fetchTraffic = ( ) => {
24+ fetcher < Traffic & { nowTime : number } > ( { url : '/traffic' } ) . then (
25+ ( res ) => {
26+ res . nowTime = Date . now ( )
27+ dispatch ( trafficActions . updateConnector ( res . connector ) )
28+ dispatch ( trafficActions . updateInterface ( res . interface ) )
29+ dispatch (
30+ trafficActions . updateStartTime (
31+ dayjs . unix ( res . startTime ) . toDate ( ) . getTime ( ) ,
32+ ) ,
33+ )
4034
41- dispatch ( trafficActions . updateConnector ( traffic . connector ) )
35+ const aggregation : ConnectorTraffic = {
36+ outCurrentSpeed : 0 ,
37+ in : 0 ,
38+ inCurrentSpeed : 0 ,
39+ outMaxSpeed : 0 ,
40+ out : 0 ,
41+ inMaxSpeed : 0 ,
42+ }
4243
43- dispatch ( trafficActions . updateInterface ( traffic . interface ) )
44+ for ( const name in res . interface ) {
45+ const conn = res . interface [ name ]
46+ aggregation . in += conn . in
47+ aggregation . out += conn . out
48+ aggregation . outCurrentSpeed += conn . outCurrentSpeed
49+ aggregation . inCurrentSpeed += conn . inCurrentSpeed
50+ }
4451
45- dispatch (
46- trafficActions . updateStartTime (
47- dayjs . unix ( traffic . startTime ) . toDate ( ) . getTime ( ) ,
48- ) ,
49- )
50-
51- const aggregation : ConnectorTraffic = {
52- outCurrentSpeed : 0 ,
53- in : 0 ,
54- inCurrentSpeed : 0 ,
55- outMaxSpeed : 0 ,
56- out : 0 ,
57- inMaxSpeed : 0 ,
52+ const now = Date . now ( )
53+ dispatch (
54+ trafficActions . updateHistory ( {
55+ up : {
56+ x : now ,
57+ y : aggregation . outCurrentSpeed ,
58+ } ,
59+ down : {
60+ x : now ,
61+ y : aggregation . inCurrentSpeed ,
62+ } ,
63+ } ) ,
64+ )
65+ } ,
66+ )
5867 }
5968
60- for ( const name in traffic . interface ) {
61- const conn = traffic . interface [ name ]
62- aggregation . in += conn . in
63- aggregation . out += conn . out
64- aggregation . outCurrentSpeed += conn . outCurrentSpeed
65- aggregation . inCurrentSpeed += conn . inCurrentSpeed
66- }
69+ const intervalId = setInterval ( ( ) => {
70+ if ( isInForeground ) {
71+ fetchTraffic ( )
72+ }
73+ } , REFRESH_RATE )
6774
68- dispatch (
69- trafficActions . updateHistory ( {
70- up : {
71- x : now ,
72- y : aggregation . outCurrentSpeed ,
73- } ,
74- down : {
75- x : now ,
76- y : aggregation . inCurrentSpeed ,
77- } ,
78- } ) ,
79- )
80- } , [ dispatch , traffic ] )
75+ return ( ) => clearInterval ( intervalId )
76+ } , [ dispatch , profile , isInForeground ] )
8177
8278 return undefined
8379}
0 commit comments