feat(projects): support scheduled detection and update system. close #657

This commit is contained in:
Azir-11 2024-11-15 22:29:16 +08:00
parent 91de4a8ef7
commit 571f3bc953

View File

@ -10,26 +10,33 @@ export function setupAppErrorHandle(app: App) {
};
}
// Update check interval in milliseconds
const UPDATE_CHECK_INTERVAL = 3 * 60 * 1000;
export function setupAppVersionNotification() {
const canAutoUpdateApp = import.meta.env.VITE_AUTOMATICALLY_DETECT_UPDATE === 'Y';
if (!canAutoUpdateApp) return;
let isShow = false;
let updateInterval: ReturnType<typeof setInterval> | undefined;
document.addEventListener('visibilitychange', async () => {
const preConditions = [!isShow, document.visibilityState === 'visible', !import.meta.env.DEV];
// Check if updates should be checked
const shouldCheckForUpdates = [!isShow, document.visibilityState === 'visible', !import.meta.env.DEV].every(Boolean);
if (!preConditions.every(Boolean)) return;
const checkForUpdates = async () => {
if (!shouldCheckForUpdates) return;
const buildTime = await getHtmlBuildTime();
// If build time hasn't changed, no update is needed
if (buildTime === BUILD_TIME) {
return;
}
isShow = true;
// Show update notification
const n = window.$notification?.create({
title: $t('system.updateTitle'),
content: $t('system.updateContent'),
@ -60,7 +67,28 @@ export function setupAppVersionNotification() {
isShow = false;
}
});
};
const startUpdateInterval = () => {
if (updateInterval) {
clearInterval(updateInterval);
}
updateInterval = setInterval(checkForUpdates, UPDATE_CHECK_INTERVAL);
};
// If updates should be checked, set up the visibility change listener and start the update interval
if (shouldCheckForUpdates) {
// Check for updates when the document is visible
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'visible') {
checkForUpdates();
startUpdateInterval();
}
});
// Start the update interval
startUpdateInterval();
}
}
async function getHtmlBuildTime() {