Files
PlugSnatcher/src/hooks/useAppUpdates.ts
T

59 lines
1.9 KiB
TypeScript

import { useState, useCallback, useEffect } from 'react';
import { useUIContext } from '../context/UIContext/useUIContext';
import { checkForAppUpdate as checkForAppUpdateAPI, UpdateInfo } from '../utils/appUpdates';
/**
* Custom hook for managing application updates
*/
export const useAppUpdates = () => {
const [isCheckingAppUpdate, setIsCheckingAppUpdate] = useState<boolean>(false);
const [appUpdateAvailable, setAppUpdateAvailable] = useState<boolean>(false);
const [appUpdateVersion, setAppUpdateVersion] = useState<string | null>(null);
const { showUpdateAvailableMessage } = useUIContext();
// Check for application updates
const checkForAppUpdate = useCallback(async () => {
if (isCheckingAppUpdate) return;
setIsCheckingAppUpdate(true);
try {
console.log('Checking for application updates...');
const updateInfo: UpdateInfo = await checkForAppUpdateAPI();
setAppUpdateAvailable(updateInfo.available);
setAppUpdateVersion(updateInfo.version || null);
if (updateInfo.available && updateInfo.version) {
console.log(`Application update available: ${updateInfo.version}`);
showUpdateAvailableMessage(updateInfo.version);
} else {
console.log('No application updates available');
}
} catch (error) {
console.error('Error checking for app updates:', error);
} finally {
setIsCheckingAppUpdate(false);
}
}, [isCheckingAppUpdate, showUpdateAvailableMessage]);
// Auto-check for updates on initial load
useEffect(() => {
// Wait a moment after initial load before checking
const timer = setTimeout(() => {
checkForAppUpdate();
}, 2000);
return () => clearTimeout(timer);
}, [checkForAppUpdate]);
return {
isCheckingAppUpdate,
appUpdateAvailable,
appUpdateVersion,
checkForAppUpdate
};
};
export default useAppUpdates;