2020-09-30 06:22:08 +02:00
|
|
|
import { useEffect, useCallback } from 'react';
|
2022-02-23 07:47:59 +01:00
|
|
|
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';
|
2020-09-26 13:01:42 +02:00
|
|
|
|
2020-10-02 19:25:31 +02:00
|
|
|
export default function useVersion(check) {
|
2022-02-23 07:47:59 +01:00
|
|
|
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(() => {
|
2020-10-02 19:25:31 +02:00
|
|
|
setItem(VERSION_CHECK, { version: versions.latest, time: Date.now() });
|
2020-09-30 06:22:08 +02:00
|
|
|
}, [versions]);
|
2020-09-26 13:01:42 +02:00
|
|
|
|
2020-09-30 01:25:44 +02:00
|
|
|
useEffect(() => {
|
2020-10-02 19:25:31 +02:00
|
|
|
if (check && !versions.latest) {
|
2022-02-23 07:47:59 +01:00
|
|
|
checkVersion();
|
2020-09-30 01:25:44 +02:00
|
|
|
}
|
2020-10-02 19:25:31 +02:00
|
|
|
}, [versions, check]);
|
2020-09-26 13:01:42 +02:00
|
|
|
|
2020-10-02 19:34:22 +02:00
|
|
|
return { ...versions, checked, updateCheck };
|
2020-09-26 13:01:42 +02:00
|
|
|
}
|