laconic-console/packages/console-app/src/containers/VersionCheck.js

72 lines
1.9 KiB
JavaScript
Raw Normal View History

2020-05-26 02:16:25 +00:00
//
// Copyright 2020 DxOS.org
//
import compareVersions from 'compare-versions';
2020-06-13 04:30:50 +00:00
import get from 'lodash.get';
2020-05-26 02:16:25 +00:00
import React, { useEffect, useState } from 'react';
import { useQuery } from '@apollo/react-hooks';
import { makeStyles } from '@material-ui/core';
import SYSTEM_STATUS from '../gql/system_status.graphql';
import WNS_RECORDS from '../gql/wns_records.graphql';
2020-05-26 02:16:25 +00:00
import { useQueryStatusReducer } from '../hooks';
const CHECK_INTERVAL = 5 * 60 * 1000;
const useStyles = makeStyles(theme => ({
update: {
color: theme.palette.error.light
}
}));
/**
* Checks for a system upgrade.
*/
const VersionCheck = () => {
const classes = useStyles();
const [{ current, latest }, setUpgrade] = useState({});
2020-06-13 05:35:52 +00:00
const statusResponse = useQueryStatusReducer(useQuery(SYSTEM_STATUS));
2020-06-08 23:55:08 +00:00
const wnsResponse = useQueryStatusReducer(useQuery(WNS_RECORDS, {
2020-05-26 02:16:25 +00:00
pollInterval: CHECK_INTERVAL,
2020-06-09 03:50:45 +00:00
variables: { attributes: { type: 'wrn:resource' } }
2020-05-26 02:16:25 +00:00
}));
// Check version.
useEffect(() => {
2020-06-13 05:35:52 +00:00
if (statusResponse && wnsResponse) {
const statusData = JSON.parse(statusResponse.system_status.json);
2020-06-09 03:50:45 +00:00
const wnsData = JSON.parse(wnsResponse.wns_records.json);
2020-06-13 04:30:50 +00:00
const current = get(statusData, 'dxos.xbox.version', '0.0.0');
2020-06-09 03:50:45 +00:00
2020-05-26 02:16:25 +00:00
let latest = current;
2020-06-09 03:50:45 +00:00
wnsData.forEach(({ attributes: { name, version } }) => {
// TODO(burdon): Filter by type (WRN?)
2020-05-26 02:16:25 +00:00
if (name.startsWith('dxos/xbox:')) {
if (compareVersions(version, latest) > 0) {
latest = version;
}
}
});
2020-05-26 12:58:11 +00:00
setUpgrade({ current, latest: latest !== current ? latest : undefined });
2020-05-26 02:16:25 +00:00
}
2020-06-13 05:35:52 +00:00
}, [statusResponse, wnsResponse]);
2020-05-26 02:16:25 +00:00
// TODO(burdon): Link to Github page with upgrade info.
return (
<>
{current && (
<div>SYS: {current}</div>
)}
{latest && (
2020-06-13 05:35:52 +00:00
<div className={classes.update}>(LATEST: {latest})</div>
2020-05-26 02:16:25 +00:00
)}
</>
);
};
export default VersionCheck;