-
-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathAutoUpdate.jsx
More file actions
95 lines (80 loc) · 2.35 KB
/
AutoUpdate.jsx
File metadata and controls
95 lines (80 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { Container, Row, Col, Alert } from 'reactstrap';
import { useState, useEffect } from 'react';
function AutoUpdate() {
const SECOND = 1000;
const MINUTE = 60 * SECOND;
const [updated, setUpdated] = useState(false);
const [hash, setHash] = useState(undefined);
const noCacheHeaders = new Headers();
noCacheHeaders.append('pragma', 'no-cache');
noCacheHeaders.append('cache-control', 'no-cache');
const requestParams = {
method: 'GET',
headers: noCacheHeaders,
};
const resolveHashUrl = () => {
const isTestEnv =
(typeof import.meta !== 'undefined' && import.meta.env?.MODE === 'test') ||
(typeof process !== 'undefined' && process.env?.NODE_ENV === 'test');
if (isTestEnv || typeof window === 'undefined' || !window.location) {
return null;
}
return new URL('/hash.txt', window.location.origin).toString();
};
useEffect(() => {
const hashUrl = resolveHashUrl();
if (!hashUrl || typeof fetch === 'undefined') {
return undefined;
}
let isMounted = true;
fetch(hashUrl, requestParams)
.then(response => response.text())
.then(text => {
if (isMounted) {
setHash(text);
}
})
.catch(err => {
console.error(err); // eslint-disable-line no-console
});
return () => {
isMounted = false;
};
}, []);
useEffect(() => {
if (hash === undefined) {
return undefined;
}
const hashUrl = resolveHashUrl();
if (!hashUrl || typeof fetch === 'undefined') {
return undefined;
}
const interval = setInterval(() => {
fetch(hashUrl, requestParams)
.then(response => response.text())
.then(text => {
if (text !== hash) {
setUpdated(true);
}
})
.catch(err => {
console.error(err); // eslint-disable-line no-console
});
}, 5 * MINUTE);
return () => clearInterval(interval);
}, [hash]);
if (!updated) return null;
return (
<Container fluid>
<Row>
<Col sm={{ size: 12 }}>
<Alert color="warning">
<b>Alert:</b> The Highest Good Network application has updated! Please refresh this page
after saving your work to apply the latest updates and bug fixes.
</Alert>
</Col>
</Row>
</Container>
);
}
export default AutoUpdate;