umami/hooks/useVersion.js

24 lines
765 B
JavaScript
Raw Normal View History

2020-09-30 06:22:08 +02:00
import { useEffect, useCallback } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { checkVersion } from 'redux/actions/app';
2020-09-30 06:22:08 +02:00
import { VERSION_CHECK } from 'lib/constants';
2020-10-02 19:34:22 +02:00
import { getItem, setItem } from 'lib/web';
export default function useVersion(check) {
const dispatch = useDispatch();
const versions = useSelector(state => state.app.versions);
2020-10-02 19:34:22 +02:00
const checked = versions.latest === getItem(VERSION_CHECK)?.version;
2020-09-30 06:22:08 +02:00
const updateCheck = useCallback(() => {
setItem(VERSION_CHECK, { version: versions.latest, time: Date.now() });
2020-09-30 06:22:08 +02:00
}, [versions]);
useEffect(() => {
if (check && !versions.latest) {
dispatch(checkVersion());
}
}, [versions, check]);
2020-10-02 19:34:22 +02:00
return { ...versions, checked, updateCheck };
}