Skip to content

Commit 4a55281

Browse files
authored
Merge cdf054b into 3c29a1f
2 parents 3c29a1f + cdf054b commit 4a55281

File tree

7 files changed

+83
-29
lines changed

7 files changed

+83
-29
lines changed

src/hooks/sync-sites/use-sync-push.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ export function useSyncPush( {
9494
if ( isKeyFailed( statusKey ) || isKeyFinished( statusKey ) || isKeyCancelled( statusKey ) ) {
9595
getIpcApi().clearSyncOperation( generateStateId( selectedSiteId, remoteSiteId ) );
9696
} else {
97-
getIpcApi().addSyncOperation( generateStateId( selectedSiteId, remoteSiteId ) );
97+
getIpcApi().addSyncOperation(
98+
generateStateId( selectedSiteId, remoteSiteId ),
99+
state.status
100+
);
98101
}
99102
},
100103
[ isKeyFailed, isKeyFinished, isKeyCancelled, updateState ]

src/hooks/use-sync-states-progress-info.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -277,22 +277,6 @@ export function useSyncStatesProgressInfo() {
277277
]
278278
);
279279

280-
const canCancelPull = useCallback( ( key: PullStateProgressInfo[ 'key' ] | undefined ) => {
281-
const cancellableStateKeys: PullStateProgressInfo[ 'key' ][] = [ 'in-progress', 'downloading' ];
282-
if ( ! key ) {
283-
return false;
284-
}
285-
return cancellableStateKeys.includes( key );
286-
}, [] );
287-
288-
const canCancelPush = useCallback( ( key: PushStateProgressInfo[ 'key' ] | undefined ) => {
289-
const cancellableStateKeys: PushStateProgressInfo[ 'key' ][] = [ 'creatingBackup' ];
290-
if ( ! key ) {
291-
return false;
292-
}
293-
return cancellableStateKeys.includes( key );
294-
}, [] );
295-
296280
return {
297281
pullStatesProgressInfo,
298282
pushStatesProgressInfo,
@@ -305,7 +289,5 @@ export function useSyncStatesProgressInfo() {
305289
getBackupStatusWithProgress,
306290
getPullStatusWithProgress,
307291
getPushStatusWithProgress,
308-
canCancelPull,
309-
canCancelPush,
310292
};
311293
}

src/index.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ import { suppressPunycodeWarning } from 'common/lib/suppress-punycode-warning';
2222
import { StatsGroup } from 'common/types/stats';
2323
import { IPC_VOID_HANDLERS } from 'src/constants';
2424
import * as ipcHandlers from 'src/ipc-handlers';
25-
import { hasActiveSyncOperations } from 'src/lib/active-sync-operations';
25+
import {
26+
hasActiveSyncOperations,
27+
hasCancelableSyncOperations,
28+
} from 'src/lib/active-sync-operations';
2629
import { bumpAggregatedUniqueStat, bumpStat } from 'src/lib/bump-stats';
2730
import { getPlatformMetric } from 'src/lib/bump-stats/lib';
2831
import { getUserLocaleWithFallback } from 'src/lib/locale-node';
@@ -351,6 +354,19 @@ async function appBoot() {
351354
globalShortcut.unregisterAll();
352355
} );
353356

357+
function getQuitConfirmationMessage(): string {
358+
if ( ! hasCancelableSyncOperations() ) {
359+
return __(
360+
"There's a sync operation in progress. The process will continue on WordPress.com servers even after quitting Studio. We will send you an email when it completes. Are you sure you want to quit?"
361+
);
362+
}
363+
364+
// Default message for creatingBackup, uploading, or pull operations
365+
return __(
366+
"There's a sync operation in progress. Quitting the app will abort that operation. Are you sure you want to quit?"
367+
);
368+
}
369+
354370
app.on( 'before-quit', ( event ) => {
355371
if ( ! hasActiveSyncOperations() ) {
356372
return;
@@ -359,11 +375,11 @@ async function appBoot() {
359375
const QUIT_APP_BUTTON_INDEX = 0;
360376
const CANCEL_BUTTON_INDEX = 1;
361377

378+
const detailMessage = getQuitConfirmationMessage();
379+
362380
const clickedButtonIndex = dialog.showMessageBoxSync( {
363381
message: __( 'Sync in progress' ),
364-
detail: __(
365-
'There’s a sync operation in progress. Quitting the app will abort that operation. Are you sure you want to quit?'
366-
),
382+
detail: detailMessage,
367383
buttons: [ __( 'Yes, quit the app' ), __( 'No, take me back' ) ],
368384
cancelId: CANCEL_BUTTON_INDEX,
369385
defaultId: QUIT_APP_BUTTON_INDEX,

src/ipc-handlers.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ import {
8989
unlockAppdata,
9090
updateAppdata,
9191
} from 'src/storage/user-data';
92+
import {
93+
PullStateProgressInfo,
94+
PushStateProgressInfo,
95+
} from './hooks/use-sync-states-progress-info';
9296
import { Blueprint } from './stores/wpcom-api';
9397
import type { SyncSite } from 'src/hooks/use-fetch-wpcom-sites/types';
9498
import type { WpCliResult } from 'src/lib/wp-cli-process';
@@ -1330,8 +1334,12 @@ export async function isImportExportSupported( _event: IpcMainInvokeEvent, siteI
13301334
/**
13311335
* Store the ID of a push/pull operation in a deduped set.
13321336
*/
1333-
export function addSyncOperation( event: IpcMainInvokeEvent, id: string ) {
1334-
ACTIVE_SYNC_OPERATIONS.add( id );
1337+
export function addSyncOperation(
1338+
event: IpcMainInvokeEvent,
1339+
id: string,
1340+
state?: PullStateProgressInfo | PushStateProgressInfo
1341+
) {
1342+
ACTIVE_SYNC_OPERATIONS.set( id, state );
13351343
}
13361344

13371345
/**

src/lib/active-sync-operations.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,58 @@
1+
import type {
2+
PullStateProgressInfo,
3+
PushStateProgressInfo,
4+
} from 'src/hooks/use-sync-states-progress-info';
15
/**
26
* This set is used to store the IDs of active sync operations. It's used to determine if we should
37
* display a confirmation modal before quitting the app.
48
*/
5-
export const ACTIVE_SYNC_OPERATIONS = new Set();
9+
export const ACTIVE_SYNC_OPERATIONS = new Map<
10+
string,
11+
PullStateProgressInfo | PushStateProgressInfo | undefined
12+
>();
613

714
/**
815
* Determine if the set of active push/pull operations has any members.
916
*/
1017
export function hasActiveSyncOperations(): boolean {
1118
return ACTIVE_SYNC_OPERATIONS.size > 0;
1219
}
20+
21+
export function getSyncOperations(): Map<
22+
string,
23+
PullStateProgressInfo | PushStateProgressInfo | undefined
24+
> {
25+
return ACTIVE_SYNC_OPERATIONS;
26+
}
27+
28+
/**
29+
* Check if a pull operation can be cancelled based on its current state.
30+
*/
31+
export function canCancelPull( key: PullStateProgressInfo[ 'key' ] | undefined ): boolean {
32+
const cancellableStateKeys: PullStateProgressInfo[ 'key' ][] = [ 'in-progress', 'downloading' ];
33+
if ( ! key ) {
34+
return false;
35+
}
36+
return cancellableStateKeys.includes( key );
37+
}
38+
39+
/**
40+
* Check if a push operation can be cancelled based on its current state.
41+
*/
42+
export function canCancelPush( key: PushStateProgressInfo[ 'key' ] | undefined ): boolean {
43+
const cancellableStateKeys: PushStateProgressInfo[ 'key' ][] = [ 'creatingBackup' ];
44+
if ( ! key ) {
45+
return false;
46+
}
47+
return cancellableStateKeys.includes( key );
48+
}
49+
50+
export function hasCancelableSyncOperations(): boolean {
51+
// Iterate over all the sites and check if any operation is cancelable
52+
for ( const [ , state ] of ACTIVE_SYNC_OPERATIONS ) {
53+
if ( state && 'key' in state && canCancelPush( state.key as PushStateProgressInfo[ 'key' ] ) ) {
54+
return true;
55+
}
56+
}
57+
return false;
58+
}

src/modules/sync/components/sync-connected-sites.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { useSyncSites } from 'src/hooks/sync-sites';
1717
import { useImportExport } from 'src/hooks/use-import-export';
1818
import { useOffline } from 'src/hooks/use-offline';
1919
import { useSyncStatesProgressInfo } from 'src/hooks/use-sync-states-progress-info';
20+
import { canCancelPull, canCancelPush } from 'src/lib/active-sync-operations';
2021
import { cx } from 'src/lib/cx';
2122
import { getIpcApi } from 'src/lib/get-ipc-api';
2223
import { getLocalizedLink } from 'src/lib/get-localized-link';
@@ -193,8 +194,6 @@ const SyncConnectedSitesList = ( {
193194
isKeyFailed,
194195
isKeyCancelled,
195196
getPullStatusWithProgress,
196-
canCancelPull,
197-
canCancelPush,
198197
} = useSyncStatesProgressInfo();
199198

200199
return (

src/preload.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ const api: IpcApi = {
104104
removeSyncBackup: ( remoteSiteId ) => ipcRendererInvoke( 'removeSyncBackup', remoteSiteId ),
105105
getConnectedWpcomSites: ( localSiteId ) =>
106106
ipcRendererInvoke( 'getConnectedWpcomSites', localSiteId ),
107-
addSyncOperation: ( id ) => ipcRendererSend( 'addSyncOperation', id ),
107+
addSyncOperation: ( id, status ) => ipcRendererSend( 'addSyncOperation', id, status ),
108108
clearSyncOperation: ( id ) => ipcRendererSend( 'clearSyncOperation', id ),
109109
cancelSyncOperation: ( id ) => ipcRendererSend( 'cancelSyncOperation', id ),
110110
getDirectorySize: ( id, subdir ) => ipcRendererInvoke( 'getDirectorySize', id, subdir ),

0 commit comments

Comments
 (0)