umami/hooks/useVersion.js

22 lines
641 B
JavaScript
Raw Normal View History

2020-09-30 06:22:08 +02:00
import { useEffect, useCallback } from 'react';
import useStore, { checkVersion } from 'store/version';
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 versions = useStore();
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) {
checkVersion();
}
}, [versions, check]);
2020-10-02 19:34:22 +02:00
return { ...versions, checked, updateCheck };
}