useAppUpdates.ts
1 import { useState, useCallback } from 'react'; 2 import { 3 UpdateStatus, 4 UpdateResult, 5 performFullUpdateFlow, 6 applyUpdate, 7 UpdateOptions, 8 checkForUpdate as checkForUpdateService, 9 } from '@/services/updateService'; 10 11 export function useAppUpdates() { 12 const [updateStatus, setUpdateStatus] = useState<UpdateStatus>({ 13 isChecking: false, 14 isUpdateAvailable: false, 15 isDownloading: false, 16 isReady: false, 17 error: null, 18 }); 19 20 const [lastResult, setLastResult] = useState<UpdateResult | null>(null); 21 22 // Check if an update is available without downloading 23 const checkForUpdate = useCallback(async (_options: UpdateOptions = {}) => { 24 setUpdateStatus((prev) => ({ ...prev, isChecking: true })); 25 try { 26 console.log('Checking for available updates...'); 27 const result = await checkForUpdateService(); 28 29 setUpdateStatus((prev) => ({ 30 ...prev, 31 isChecking: false, 32 isUpdateAvailable: result.success, 33 })); 34 35 setLastResult(result); 36 return result; 37 } catch (error) { 38 console.error('Error checking for update:', error); 39 setUpdateStatus((prev) => ({ 40 ...prev, 41 isChecking: false, 42 error: error instanceof Error ? error.message : 'Unknown error', 43 })); 44 45 const errorResult = { 46 success: false, 47 message: `Error checking for updates: ${error instanceof Error ? error.message : 'Unknown error'}`, 48 }; 49 50 setLastResult(errorResult); 51 return errorResult; 52 } 53 }, []); 54 55 // Check, download, and potentially apply an update 56 const checkAndDownload = useCallback(async (options: UpdateOptions = {}) => { 57 try { 58 console.log('Starting update flow with options:', options); 59 const result = await performFullUpdateFlow(options, setUpdateStatus); 60 setLastResult(result); 61 return result; 62 } catch (error) { 63 console.error('Error in update flow:', error); 64 const errorResult = { 65 success: false, 66 message: `Update process failed: ${error instanceof Error ? error.message : 'Unknown error'}`, 67 }; 68 setLastResult(errorResult); 69 return errorResult; 70 } 71 }, []); 72 73 // Apply an already downloaded update 74 const applyReadyUpdate = useCallback(async () => { 75 if (updateStatus.isReady) { 76 try { 77 console.log('Applying ready update...'); 78 const result = await applyUpdate(); 79 setLastResult(result); 80 return result; 81 } catch (error) { 82 console.error('Error applying update:', error); 83 const errorResult = { 84 success: false, 85 message: `Failed to apply update: ${error instanceof Error ? error.message : 'Unknown error'}`, 86 }; 87 setLastResult(errorResult); 88 return errorResult; 89 } 90 } 91 92 console.log('No update is ready to apply'); 93 const noUpdateResult = { 94 success: false, 95 message: 'No update is ready to apply', 96 }; 97 98 setLastResult(noUpdateResult); 99 return noUpdateResult; 100 }, [updateStatus.isReady]); 101 102 // Check for update, download, and apply immediately 103 const updateAndReload = useCallback(async () => { 104 try { 105 console.log('Starting update and reload process...'); 106 const result = await performFullUpdateFlow( 107 { 108 silent: true, 109 forceReload: true, 110 }, 111 setUpdateStatus 112 ); 113 114 setLastResult(result); 115 return result; 116 } catch (error) { 117 console.error('Error during update and reload:', error); 118 const errorResult = { 119 success: false, 120 message: `Update and reload failed: ${error instanceof Error ? error.message : 'Unknown error'}`, 121 }; 122 setLastResult(errorResult); 123 return errorResult; 124 } 125 }, []); 126 127 const isUpdateInProgress = 128 updateStatus.isChecking || updateStatus.isDownloading; 129 130 return { 131 updateStatus, 132 lastResult, 133 checkForUpdate, 134 checkAndDownload, 135 applyReadyUpdate, 136 updateAndReload, 137 isUpdateInProgress, 138 }; 139 }