73 lines
2.0 KiB
TypeScript
73 lines
2.0 KiB
TypeScript
|
|
import { useCallback, useRef } from "react";
|
||
|
|
import { useShallow } from "zustand/react/shallow";
|
||
|
|
import { useAppState } from "../store/appState";
|
||
|
|
import * as commands from "../lib/tauri-commands";
|
||
|
|
|
||
|
|
const CHECK_INTERVAL_MS = 24 * 60 * 60 * 1000; // 24 hours
|
||
|
|
|
||
|
|
export function useUpdates() {
|
||
|
|
const { updateInfo, setUpdateInfo, appVersion, setAppVersion, appSettings } =
|
||
|
|
useAppState(
|
||
|
|
useShallow((s) => ({
|
||
|
|
updateInfo: s.updateInfo,
|
||
|
|
setUpdateInfo: s.setUpdateInfo,
|
||
|
|
appVersion: s.appVersion,
|
||
|
|
setAppVersion: s.setAppVersion,
|
||
|
|
appSettings: s.appSettings,
|
||
|
|
})),
|
||
|
|
);
|
||
|
|
|
||
|
|
const intervalRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
||
|
|
|
||
|
|
const loadVersion = useCallback(async () => {
|
||
|
|
try {
|
||
|
|
const version = await commands.getAppVersion();
|
||
|
|
setAppVersion(version);
|
||
|
|
} catch (e) {
|
||
|
|
console.error("Failed to load app version:", e);
|
||
|
|
}
|
||
|
|
}, [setAppVersion]);
|
||
|
|
|
||
|
|
const checkForUpdates = useCallback(async () => {
|
||
|
|
try {
|
||
|
|
const info = await commands.checkForUpdates();
|
||
|
|
if (info) {
|
||
|
|
// Respect dismissed version
|
||
|
|
const dismissed = appSettings?.dismissed_update_version;
|
||
|
|
if (dismissed && dismissed === info.version) {
|
||
|
|
setUpdateInfo(null);
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
setUpdateInfo(info);
|
||
|
|
return info;
|
||
|
|
} catch (e) {
|
||
|
|
console.error("Failed to check for updates:", e);
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
}, [setUpdateInfo, appSettings?.dismissed_update_version]);
|
||
|
|
|
||
|
|
const startPeriodicCheck = useCallback(() => {
|
||
|
|
if (intervalRef.current) return;
|
||
|
|
intervalRef.current = setInterval(() => {
|
||
|
|
if (appSettings?.auto_check_updates !== false) {
|
||
|
|
checkForUpdates();
|
||
|
|
}
|
||
|
|
}, CHECK_INTERVAL_MS);
|
||
|
|
return () => {
|
||
|
|
if (intervalRef.current) {
|
||
|
|
clearInterval(intervalRef.current);
|
||
|
|
intervalRef.current = null;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
}, [checkForUpdates, appSettings?.auto_check_updates]);
|
||
|
|
|
||
|
|
return {
|
||
|
|
updateInfo,
|
||
|
|
appVersion,
|
||
|
|
loadVersion,
|
||
|
|
checkForUpdates,
|
||
|
|
startPeriodicCheck,
|
||
|
|
};
|
||
|
|
}
|