Skip to content

Commit f6a3fdb

Browse files
committed
Add basic caching and minor updates to UI
1 parent b3ed248 commit f6a3fdb

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

dash/dash-renderer/src/components/error/menu/DebugMenu.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
bottom: 100%;
2525
right: 0;
2626
gap: 8px;
27+
max-height: calc(100vh - 75px);
28+
justify-content: flex-end;
2729
}
2830

2931
.dash-debug-menu__version-opt-out {
@@ -66,6 +68,8 @@
6668
align-items: center;
6769
border-radius: 4px;
6870
cursor: pointer;
71+
line-height: 18px;
72+
padding: 16px 12px;
6973
}
7074

7175
.dash-debug-menu__version-opt-out__buttons-container {

dash/dash-renderer/src/components/error/menu/VersionInfo.react.js

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, {useEffect, useState} from 'react';
22

33
import './VersionInfo.css';
44

5+
const HOUR_IN_MS = 3600000;
56
const DAY_IN_MS = 86400000;
67

78
function compareVersions(v1, v2) {
@@ -20,15 +21,34 @@ function compareVersions(v1, v2) {
2021
}
2122

2223
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+
}
3252
}
3353

3454
function shouldShowUpgradeNotification(
@@ -68,11 +88,9 @@ export const VersionInfo = ({
6888
showNotifications,
6989
setShowNotifications
7090
}) => {
71-
const [upgradeInfo, setUpgradeInfo] = useState([]);
91+
const [newDashVersion, setNewDashVersion] = useState(undefined);
7292
const [upgradeTooltipOpened, setUpgradeTooltipOpened] = useState(false);
7393

74-
const newDashVersion = upgradeInfo[0] ? upgradeInfo[0].version : undefined;
75-
7694
const setDontShowAgain = () => {
7795
// Set local storage to record the last dismissed notification
7896
setUpgradeTooltipOpened(false);
@@ -96,8 +114,8 @@ export const VersionInfo = ({
96114
requestDashVersionInfo(
97115
config.dash_version,
98116
config.dash_version_url
99-
).then(body => {
100-
setUpgradeInfo(body);
117+
).then(version => {
118+
setNewDashVersion(version);
101119
});
102120
}
103121
}, [showNotifications]);

0 commit comments

Comments
 (0)