2020-09-30 06:22:08 +02:00
|
|
|
import { useEffect, useCallback } from 'react';
|
2020-09-30 01:25:44 +02:00
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
2020-09-30 06:22:08 +02:00
|
|
|
import semver from 'semver';
|
|
|
|
import { getItem, setItem } from 'lib/web';
|
2020-09-30 01:25:44 +02:00
|
|
|
import { checkVersion } from 'redux/actions/app';
|
2020-09-30 06:22:08 +02:00
|
|
|
import { VERSION_CHECK } from 'lib/constants';
|
2020-09-26 13:01:42 +02:00
|
|
|
|
2020-09-30 01:25:44 +02:00
|
|
|
export default function useVersion() {
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
const versions = useSelector(state => state.app.versions);
|
2020-09-30 06:22:08 +02:00
|
|
|
const lastCheck = getItem(VERSION_CHECK);
|
|
|
|
|
|
|
|
const { current, latest } = versions;
|
|
|
|
const hasUpdate = latest && semver.gt(latest, current) && lastCheck?.version !== latest;
|
|
|
|
|
|
|
|
const updateCheck = useCallback(() => {
|
|
|
|
setItem(VERSION_CHECK, { version: latest, time: Date.now() });
|
|
|
|
}, [versions]);
|
2020-09-26 13:01:42 +02:00
|
|
|
|
2020-09-30 01:25:44 +02:00
|
|
|
useEffect(() => {
|
2020-09-30 06:22:08 +02:00
|
|
|
if (!versions.latest) {
|
2020-09-30 01:25:44 +02:00
|
|
|
dispatch(checkVersion());
|
|
|
|
}
|
2020-09-30 06:22:08 +02:00
|
|
|
}, [versions]);
|
2020-09-26 13:01:42 +02:00
|
|
|
|
2020-09-30 06:22:08 +02:00
|
|
|
return { ...versions, hasUpdate, updateCheck };
|
2020-09-26 13:01:42 +02:00
|
|
|
}
|