-
Notifications
You must be signed in to change notification settings - Fork 2k
MSD: Show version switch progress when changing WordPress version #109795
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
ec2c881
d6534b2
6e09644
cfe227c
4c5e440
e71adec
76236d9
9899f9a
6df43d5
72aa185
19cdec4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,86 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| queryClient, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| siteBackupsQuery, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| siteBySlugQuery, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sitePendingWordPressVersionQuery, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| siteWordPressVersionQuery, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| siteWordPressVersionMutation, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } from '@automattic/api-queries'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { useQuery, useMutation } from '@tanstack/react-query'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { usePrevious } from '@wordpress/compose'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { __ } from '@wordpress/i18n'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { useEffect, useState } from 'react'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { useBackupState } from '../backups/use-backup-state'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { BackupState } from '../backups/use-backup-state'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { Site } from '@automattic/api-core'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export interface VersionSwitchState { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| backupState: BackupState; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** The pending version tag while switching, or the last one after switch completes. */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| targetVersion: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isSwitching: boolean; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isSwitched: boolean; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mutation: ReturnType< typeof useMutation< void, Error, string > >; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export function useVersionSwitch( site: Site ): VersionSwitchState { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const backupState = useBackupState( site.ID ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Check if there's a pending version switch. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { data: pendingVersion } = useQuery( sitePendingWordPressVersionQuery( site.ID ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const isSwitching = !! pendingVersion; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const wasSwitching = usePrevious( isSwitching ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const [ isSwitched, setIsSwitched ] = useState( false ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const [ targetVersion, setTargetVersion ] = useState( '' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Remember the pending version so we can show it in the success notice. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| useEffect( () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( pendingVersion ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setTargetVersion( pendingVersion ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, [ pendingVersion ] ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Track the transition from switching to not switching. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| useEffect( () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( wasSwitching && ! isSwitching ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setIsSwitched( true ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| queryClient.invalidateQueries( siteWordPressVersionQuery( site.ID ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| queryClient.invalidateQueries( siteBySlugQuery( site.slug ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, [ wasSwitching, isSwitching, site.ID, site.slug ] ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Poll backups while a version switch is in progress. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| useQuery( { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ...siteBackupsQuery( site.ID ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| refetchInterval: isSwitching ? 3000 : false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| enabled: isSwitching, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // After backup completes, poll pending version until it clears. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| useQuery( { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ...sitePendingWordPressVersionQuery( site.ID ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| refetchInterval: isSwitching && backupState.hasRecentlyCompleted ? 5000 : false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const mutation = useMutation( { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ...siteWordPressVersionMutation( site.ID ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| onSuccess: () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| backupState.setEnqueued( true ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setIsSwitched( false ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| queryClient.invalidateQueries( sitePendingWordPressVersionQuery( site.ID ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| meta: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| snackbar: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| error: __( 'Failed to save WordPress version.' ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+106
to
+114
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spreading
The component-specific side-effects should be passed as a callback to
Suggested change
Then update |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| backupState, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| targetVersion, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isSwitching, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isSwitched, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mutation, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.