Merge branch 'main' into example

This commit is contained in:
Soybean
2024-11-17 22:34:18 +08:00
20 changed files with 863 additions and 796 deletions

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,11 +67,34 @@ 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() {
const res = await fetch(`/index.html?time=${Date.now()}`);
const baseUrl = import.meta.env.VITE_BASE_URL || '/';
const res = await fetch(`${baseUrl}index.html?time=${Date.now()}`);
const html = await res.text();