-
Notifications
You must be signed in to change notification settings - Fork 476
/
UpdateChecker.tsx
153 lines (136 loc) · 4.17 KB
/
UpdateChecker.tsx
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { useState, useEffect } from "react";
import createPersistedState from "use-persisted-state";
import { differenceInDays } from "date-fns";
import {
makeStyles,
createStyles,
MenuItem,
ListItemText,
ListItemSecondaryAction,
Link,
} from "@material-ui/core";
import OpenInNewIcon from "@material-ui/icons/OpenInNew";
import meta from "../../../package.json";
import WIKI_LINKS from "constants/wikiLinks";
const useLastCheckedUpdateState = createPersistedState(
"_FT_LAST_CHECKED_UPDATE"
);
export const useLatestUpdateState = createPersistedState("_FT_LATEST_UPDATE");
const useStyles = makeStyles((theme) =>
createStyles({
secondaryAction: { pointerEvents: "none" },
secondaryIcon: {
display: "block",
color: theme.palette.action.active,
},
version: {
display: "block",
padding: theme.spacing(1, 2),
userSelect: "none",
color: theme.palette.text.disabled,
},
})
);
export default function UpdateChecker() {
const classes = useStyles();
const [
lastCheckedUpdate,
setLastCheckedUpdate,
] = useLastCheckedUpdateState<string>();
const [latestUpdate, setLatestUpdate] = useLatestUpdateState<null | Record<
string,
any
>>(null);
const [checkState, setCheckState] = useState<null | "LOADING" | "NO_UPDATE">(
null
);
const checkForUpdate = async () => {
setCheckState("LOADING");
// https://docs.github.com/en/rest/reference/repos#get-the-latest-release
const endpoint = meta.repository.url
.replace("github.com", "api.github.com/repos")
.replace(/.git$/, "/releases/latest");
try {
const res = await fetch(endpoint, {
headers: {
Accept: "application/vnd.github.v3+json",
},
});
const json = await res.json();
if (json.tag_name > "v" + meta.version) {
setLatestUpdate(json);
setCheckState(null);
} else {
setCheckState("NO_UPDATE");
}
setLastCheckedUpdate(new Date().toISOString());
} catch (e) {
console.error(e);
setLatestUpdate(null);
setCheckState("NO_UPDATE");
}
};
// Check for new updates on page load, if last check was more than 7 days ago
useEffect(() => {
if (!lastCheckedUpdate) checkForUpdate();
else if (differenceInDays(new Date(), new Date(lastCheckedUpdate)) > 7)
checkForUpdate();
}, [lastCheckedUpdate]);
// Verify latest update is not installed yet
useEffect(() => {
if (latestUpdate?.tag_name <= "v" + meta.version) setLatestUpdate(null);
}, [latestUpdate, setLatestUpdate]);
return (
<>
{checkState === "LOADING" ? (
<MenuItem disabled>Checking for updates…</MenuItem>
) : checkState === "NO_UPDATE" ? (
<MenuItem disabled>No updates available</MenuItem>
) : latestUpdate === null ? (
<MenuItem onClick={checkForUpdate}>Check for updates</MenuItem>
) : (
<>
<MenuItem
component="a"
href={latestUpdate?.html_url}
target="_blank"
rel="noopener"
>
<ListItemText
primary="Update available"
secondary={latestUpdate?.tag_name}
/>
<ListItemSecondaryAction className={classes.secondaryAction}>
<OpenInNewIcon className={classes.secondaryIcon} />
</ListItemSecondaryAction>
</MenuItem>
<MenuItem
component="a"
href={WIKI_LINKS.updatingFiretable}
target="_blank"
rel="noopener"
>
<ListItemText secondary="How to update Firetable" />
<ListItemSecondaryAction className={classes.secondaryAction}>
<OpenInNewIcon
color="secondary"
fontSize="small"
className={classes.secondaryIcon}
/>
</ListItemSecondaryAction>
</MenuItem>
</>
)}
<Link
variant="caption"
component="a"
href={meta.repository.url.replace(".git", "") + "/releases"}
target="_blank"
rel="noopener"
className={classes.version}
>
{meta.name} v{meta.version}
</Link>
</>
);
}