Skip to content

Commit 189a099

Browse files
committed
fix: Render issue on traffic update
1 parent 53574e0 commit 189a099

File tree

5 files changed

+62
-66
lines changed

5 files changed

+62
-66
lines changed

src/hooks/useTrafficUpdater/useTrafficUpdater.ts

Lines changed: 49 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { useEffect } from 'react'
22
import { useLocation } from 'react-router-dom'
33
import dayjs from 'dayjs'
4-
import useSWR from 'swr'
54

65
import { useAppDispatch, useProfile } from '@/store'
76
import { 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
}

src/pages/Devices/components/DeviceSettingsModal.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,16 +191,16 @@ const DeviceSettingsModal = ({
191191
/>
192192

193193
<DialogFooter>
194-
<DialogClose asChild>
195-
<Button variant="outline">{t('common.cancel')}</Button>
196-
</DialogClose>
197194
<Button
198195
isLoading={isLoading}
199196
type="submit"
200197
loadingLabel={t('common.is_loading')}
201198
>
202199
{t('common.save')}
203200
</Button>
201+
<DialogClose asChild className="md:hidden">
202+
<Button variant="outline">{t('common.cancel')}</Button>
203+
</DialogClose>
204204
</DialogFooter>
205205
</form>
206206
</Form>

src/pages/Home/components/SetHostModal.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,14 @@ const SetHostModal: React.FC = () => {
101101
</div>
102102

103103
<DialogFooter>
104+
<Button onClick={() => onAddNewProfile()}>
105+
{t('landing.add_new_host')}
106+
</Button>
104107
<DialogClose asChild className="md:hidden">
105108
<Button autoFocus variant="outline">
106109
{t('common.close')}
107110
</Button>
108111
</DialogClose>
109-
<Button onClick={() => onAddNewProfile()}>
110-
{t('landing.add_new_host')}
111-
</Button>
112112
</DialogFooter>
113113
</DialogContent>
114114
</Dialog>

src/router/router.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77

88
import App from '@/App'
99
import AppContainer from '@/AppContainer'
10-
import ErrorBoundary from '@/components/ErrorBoundary'
10+
import { ErrorBoundary } from '@/components/ErrorBoundary'
1111

1212
import { RouterContext } from './context'
1313

yarn.lock

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3590,12 +3590,12 @@
35903590
dependencies:
35913591
undici-types "~5.26.4"
35923592

3593-
"@types/node@^22":
3594-
version "22.9.4"
3595-
resolved "https://registry.yarnpkg.com/@types/node/-/node-22.9.4.tgz#31eefcdbe163a51f53cbfbb3e121b8ae9b16fdb2"
3596-
integrity sha512-d9RWfoR7JC/87vj7n+PVTzGg9hDyuFjir3RxUHbjFSKNd9mpxbxwMEyaCim/ddCmy4IuW7HjTzF3g9p3EtWEOg==
3593+
"@types/node@^20":
3594+
version "20.17.9"
3595+
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.17.9.tgz#5f141d4b7ee125cdee5faefe28de095398865bab"
3596+
integrity sha512-0JOXkRyLanfGPE2QRCwgxhzlBAvaRdCNMcvbd7jFfpmD4eEXll7LRwy5ymJmyeZqk7Nh7eD2LeUyQ68BbndmXw==
35973597
dependencies:
3598-
undici-types "~6.19.8"
3598+
undici-types "~6.19.2"
35993599

36003600
"@types/normalize-package-data@^2.4.0":
36013601
version "2.4.4"
@@ -13398,7 +13398,7 @@ undici-types@~5.26.4:
1339813398
resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-5.26.5.tgz#bcd539893d00b56e964fd2657a4866b221a65617"
1339913399
integrity sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==
1340013400

13401-
undici-types@~6.19.8:
13401+
undici-types@~6.19.2, undici-types@~6.19.8:
1340213402
version "6.19.8"
1340313403
resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-6.19.8.tgz#35111c9d1437ab83a7cdc0abae2f26d88eda0a02"
1340413404
integrity sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==

0 commit comments

Comments
 (0)