| 1 |
import { WaitingOverlay } from '@auto-launch/components/Waiting'; |
| 2 |
import { forceReinstallPlugin, runUpdates } from '@auto-launch/functions/setup'; |
| 3 |
import { launchStrings } from '@auto-launch/strings'; |
| 4 |
import { digest } from '@shared/api/digest'; |
| 5 |
import { useCallback, useEffect } from '@wordpress/element'; |
| 6 |
|
| 7 |
// The reload is also the retry; the server bounds how many, so it can't loop. |
| 8 |
export const LaunchUpdate = ({ |
| 9 |
pluginUpdateNeeded, |
| 10 |
themeUpdateNeeded, |
| 11 |
pluginUpdateViaRest, |
| 12 |
pluginSlug, |
| 13 |
attempt, |
| 14 |
}) => { |
| 15 |
const applyUpdates = useCallback(async () => { |
| 16 |
// Run serially, not concurrently: two WP upgraders fight over the shared |
| 17 |
// maintenance flag and filesystem. |
| 18 |
const phpUpdateNeeded = |
| 19 |
themeUpdateNeeded || (pluginUpdateNeeded && !pluginUpdateViaRest); |
| 20 |
try { |
| 21 |
if (phpUpdateNeeded) { |
| 22 |
const result = await runUpdates(); |
| 23 |
if (result?.errors?.length) { |
| 24 |
throw new Error(result.errors.join('; ')); |
| 25 |
} |
| 26 |
} |
| 27 |
if (pluginUpdateNeeded && pluginUpdateViaRest) { |
| 28 |
await forceReinstallPlugin(pluginSlug); |
| 29 |
} |
| 30 |
} catch (error) { |
| 31 |
digest({ |
| 32 |
error, |
| 33 |
details: { source: 'auto-launch', caller: 'launch-update', attempt }, |
| 34 |
}); |
| 35 |
} finally { |
| 36 |
window.location.reload(); |
| 37 |
} |
| 38 |
}, [ |
| 39 |
pluginUpdateNeeded, |
| 40 |
themeUpdateNeeded, |
| 41 |
pluginUpdateViaRest, |
| 42 |
pluginSlug, |
| 43 |
attempt, |
| 44 |
]); |
| 45 |
|
| 46 |
useEffect(() => { |
| 47 |
applyUpdates(); |
| 48 |
}, [applyUpdates]); |
| 49 |
|
| 50 |
const strings = launchStrings(); |
| 51 |
|
| 52 |
return ( |
| 53 |
<WaitingOverlay |
| 54 |
show |
| 55 |
message={strings.updating} |
| 56 |
note={strings.updatingNote} |
| 57 |
/> |
| 58 |
); |
| 59 |
}; |
| 60 |
|