@@ -2,6 +2,7 @@ import React, {useEffect, useState} from 'react';
2
2
3
3
import './VersionInfo.css' ;
4
4
5
+ const HOUR_IN_MS = 3600000 ;
5
6
const DAY_IN_MS = 86400000 ;
6
7
7
8
function compareVersions ( v1 , v2 ) {
@@ -20,15 +21,34 @@ function compareVersions(v1, v2) {
20
21
}
21
22
22
23
async function requestDashVersionInfo ( currentDashVersion , dashVersionUrl ) {
23
- return fetch ( dashVersionUrl , {
24
- method : 'POST' ,
25
- body : JSON . stringify ( {
26
- dash_version : currentDashVersion
27
- } ) ,
28
- headers : {
29
- 'Content-Type' : 'application/json'
30
- }
31
- } ) . then ( response => response . json ( ) ) ;
24
+ const cachedVersionInfo = localStorage . getItem ( 'cachedNewDashVersion' ) ;
25
+ const lastFetched = localStorage . getItem ( 'lastFetched' ) ;
26
+ if (
27
+ lastFetched &&
28
+ Date . now ( ) - Number ( lastFetched ) < HOUR_IN_MS &&
29
+ cachedVersionInfo
30
+ ) {
31
+ return JSON . parse ( cachedVersionInfo ) ;
32
+ } else {
33
+ return fetch ( dashVersionUrl , {
34
+ method : 'POST' ,
35
+ body : JSON . stringify ( {
36
+ dash_version : currentDashVersion
37
+ } ) ,
38
+ headers : {
39
+ 'Content-Type' : 'application/json'
40
+ }
41
+ } )
42
+ . then ( response => response . json ( ) )
43
+ . then ( body => {
44
+ localStorage . setItem (
45
+ 'cachedNewDashVersion' ,
46
+ JSON . stringify ( body . version )
47
+ ) ;
48
+ localStorage . setItem ( 'lastFetched' , Date . now ( ) ) ;
49
+ return body . version ;
50
+ } ) ;
51
+ }
32
52
}
33
53
34
54
function shouldShowUpgradeNotification (
@@ -68,11 +88,9 @@ export const VersionInfo = ({
68
88
showNotifications,
69
89
setShowNotifications
70
90
} ) => {
71
- const [ upgradeInfo , setUpgradeInfo ] = useState ( [ ] ) ;
91
+ const [ newDashVersion , setNewDashVersion ] = useState ( undefined ) ;
72
92
const [ upgradeTooltipOpened , setUpgradeTooltipOpened ] = useState ( false ) ;
73
93
74
- const newDashVersion = upgradeInfo [ 0 ] ? upgradeInfo [ 0 ] . version : undefined ;
75
-
76
94
const setDontShowAgain = ( ) => {
77
95
// Set local storage to record the last dismissed notification
78
96
setUpgradeTooltipOpened ( false ) ;
@@ -96,8 +114,8 @@ export const VersionInfo = ({
96
114
requestDashVersionInfo (
97
115
config . dash_version ,
98
116
config . dash_version_url
99
- ) . then ( body => {
100
- setUpgradeInfo ( body ) ;
117
+ ) . then ( version => {
118
+ setNewDashVersion ( version ) ;
101
119
} ) ;
102
120
}
103
121
} , [ showNotifications ] ) ;
0 commit comments