Skip to content

Commit 3bc1aa9

Browse files
authored
Remove updateSingleConnectedWpcomSite IPC handler (#2823)
* Remove updateSingleConnectedWpcomSite IPC handler Replace all usages with the existing updateConnectedWpcomSites handler by wrapping the single site in an array. This eliminates a redundant IPC handler that duplicated logic already present in the bulk variant. * Restore timestamp updates on push/pull success The updateSiteTimestamp calls were dropped in #2037 when push/pull states moved from SyncSitesProvider to a Redux slice. Wire them back up by dispatching the RTK Query mutation from the thunks. * Remove updateSiteTimestamp RTK Query mutation, call IPC directly Replace the RTK Query mutation with a plain async function that calls getIpcApi().updateConnectedWpcomSites() directly from the thunks. Await the timestamp write before invalidating the cache to avoid a race where the re-fetch reads stale data.
1 parent 24b2a0a commit 3bc1aa9

7 files changed

Lines changed: 42 additions & 66 deletions

File tree

apps/studio/src/components/tests/site-management-actions.test.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import { store } from 'src/stores';
1111
import { connectedSitesApi } from 'src/stores/sync/connected-sites';
1212

1313
const mockGetConnectedWpcomSites = vi.fn();
14-
const mockUpdateSingleConnectedWpcomSite = vi.fn();
14+
const mockUpdateConnectedWpcomSites = vi.fn();
1515

1616
vi.mock( 'src/lib/get-ipc-api', () => ( {
1717
getIpcApi: vi.fn( () => ( {
1818
getConnectedWpcomSites: mockGetConnectedWpcomSites,
19-
updateSingleConnectedWpcomSite: mockUpdateSingleConnectedWpcomSite,
19+
updateConnectedWpcomSites: mockUpdateConnectedWpcomSites,
2020
} ) ),
2121
} ) );
2222

@@ -44,10 +44,10 @@ describe( 'SiteManagementActions', () => {
4444
beforeEach( () => {
4545
// Reset mock calls but preserve implementations
4646
mockGetConnectedWpcomSites.mockClear();
47-
mockUpdateSingleConnectedWpcomSite.mockClear();
47+
mockUpdateConnectedWpcomSites.mockClear();
4848
// Set default return values
4949
mockGetConnectedWpcomSites.mockResolvedValue( [] );
50-
mockUpdateSingleConnectedWpcomSite.mockResolvedValue( {} );
50+
mockUpdateConnectedWpcomSites.mockResolvedValue( {} );
5151
// Clear RTK Query cache between tests
5252
store.dispatch( connectedSitesApi.util.resetApiState() );
5353
} );

apps/studio/src/hooks/sync-sites/use-listen-deep-link-connection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export function useListenDeepLinkConnection() {
102102
localSiteId: studioSiteId,
103103
syncSupport: 'already-connected',
104104
};
105-
await getIpcApi().updateSingleConnectedWpcomSite( fullSiteData );
105+
await getIpcApi().updateConnectedWpcomSites( [ fullSiteData ] );
106106
dispatch( connectedSitesApi.util.invalidateTags( [ 'ConnectedSites' ] ) );
107107
}
108108
} catch ( error ) {

apps/studio/src/ipc-handlers.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ export {
123123
removeSyncBackup,
124124
resumeSyncUpload,
125125
updateConnectedWpcomSites,
126-
updateSingleConnectedWpcomSite,
127126
} from 'src/modules/sync/lib/ipc-handlers';
128127

129128
export {

apps/studio/src/modules/sync/lib/ipc-handlers.ts

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -529,34 +529,6 @@ export async function updateConnectedWpcomSites(
529529
}
530530
}
531531

532-
export async function updateSingleConnectedWpcomSite(
533-
event: IpcMainInvokeEvent,
534-
updatedSite: SyncSite
535-
) {
536-
try {
537-
await lockAppdata();
538-
const userData = await loadUserData();
539-
const currentUserId = userData.authToken?.id;
540-
541-
if ( ! currentUserId ) {
542-
throw new Error( 'User not authenticated' );
543-
}
544-
545-
const connections = userData.connectedWpcomSites?.[ currentUserId ] || [];
546-
const index = connections.findIndex(
547-
( conn ) => conn.id === updatedSite.id && conn.localSiteId === updatedSite.localSiteId
548-
);
549-
550-
if ( index !== -1 ) {
551-
connections[ index ] = updatedSite;
552-
}
553-
554-
await saveUserData( userData );
555-
} finally {
556-
await unlockAppdata();
557-
}
558-
}
559-
560532
export async function getConnectedWpcomSites(
561533
event: IpcMainInvokeEvent,
562534
localSiteId?: string

apps/studio/src/preload.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ const api: IpcApi = {
4848
disconnectWpcomSites: ( ...args ) => ipcRendererInvoke( 'disconnectWpcomSites', ...args ),
4949
updateConnectedWpcomSites: ( ...args ) =>
5050
ipcRendererInvoke( 'updateConnectedWpcomSites', ...args ),
51-
updateSingleConnectedWpcomSite: ( updatedSite ) =>
52-
ipcRendererInvoke( 'updateSingleConnectedWpcomSite', updatedSite ),
5351
authenticate: ( isSignup ) => ipcRendererSend( 'authenticate', isSignup ),
5452
exportSite: ( options ) => ipcRendererInvoke( 'exportSite', options ),
5553
isAuthenticated: () => ipcRendererInvoke( 'isAuthenticated' ),

apps/studio/src/stores/sync/connected-sites.ts

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -130,41 +130,11 @@ export const connectedSitesApi = createApi( {
130130
{ type: 'ConnectedSites', localSiteId },
131131
],
132132
} ),
133-
134-
updateSiteTimestamp: builder.mutation<
135-
void,
136-
{ siteId: number; localSiteId: string; type: 'pull' | 'push' }
137-
>( {
138-
queryFn: async ( { siteId, localSiteId, type } ) => {
139-
const connectedSites = await getIpcApi().getConnectedWpcomSites( localSiteId );
140-
const connectedSite = connectedSites.find(
141-
( { id, localSiteId: siteLocalId } ) => siteId === id && localSiteId === siteLocalId
142-
);
143-
144-
if ( ! connectedSite ) {
145-
return { error: { status: 'CUSTOM_ERROR', error: 'Site not found' } };
146-
}
147-
148-
const timestampKey = type === 'pull' ? 'lastPullTimestamp' : 'lastPushTimestamp';
149-
const updatedConnectedSite = {
150-
...connectedSite,
151-
[ timestampKey ]: new Date().toISOString(),
152-
};
153-
154-
await getIpcApi().updateSingleConnectedWpcomSite( updatedConnectedSite );
155-
156-
return { data: undefined };
157-
},
158-
invalidatesTags: ( result, error, { localSiteId } ) => [
159-
{ type: 'ConnectedSites', localSiteId },
160-
],
161-
} ),
162133
} ),
163134
} );
164135

165136
export const {
166137
useGetConnectedSitesForLocalSiteQuery,
167138
useConnectSiteMutation,
168139
useDisconnectSiteMutation,
169-
useUpdateSiteTimestampMutation,
170140
} = connectedSitesApi;

apps/studio/src/stores/sync/sync-operations-slice.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,33 @@ import type { AppDispatch, RootState } from 'src/stores';
1717
import type { SyncOption } from 'src/types';
1818
import type { WPCOM } from 'wpcom/types';
1919

20+
async function updateSiteTimestamp( {
21+
siteId,
22+
localSiteId,
23+
type,
24+
}: {
25+
siteId: number;
26+
localSiteId: string;
27+
type: 'pull' | 'push';
28+
} ) {
29+
const connectedSites = await getIpcApi().getConnectedWpcomSites( localSiteId );
30+
const connectedSite = connectedSites.find(
31+
( { id, localSiteId: siteLocalId } ) => siteId === id && localSiteId === siteLocalId
32+
);
33+
34+
if ( ! connectedSite ) {
35+
return;
36+
}
37+
38+
const timestampKey = type === 'pull' ? 'lastPullTimestamp' : 'lastPushTimestamp';
39+
await getIpcApi().updateConnectedWpcomSites( [
40+
{
41+
...connectedSite,
42+
[ timestampKey ]: new Date().toISOString(),
43+
},
44+
] );
45+
}
46+
2047
export type SyncBackupState = {
2148
remoteSiteId: number;
2249
backupId: number | null;
@@ -611,6 +638,11 @@ const pollPushProgressThunk = createTypedAsyncThunk(
611638
switch ( response.status ) {
612639
case 'finished':
613640
status = pushStatesProgressInfo.finished;
641+
await updateSiteTimestamp( {
642+
siteId: remoteSiteId,
643+
localSiteId: selectedSiteId,
644+
type: 'push',
645+
} );
614646
void dispatch( connectedSitesApi.util.invalidateTags( [ 'ConnectedSites' ] ) );
615647
getIpcApi().showNotification( {
616648
title: currentPushState.selectedSite.name,
@@ -823,6 +855,11 @@ const pollPullBackupThunk = createTypedAsyncThunk(
823855

824856
await getIpcApi().removeSyncBackup( remoteSiteId );
825857

858+
await updateSiteTimestamp( {
859+
siteId: remoteSiteId,
860+
localSiteId: selectedSiteId,
861+
type: 'pull',
862+
} );
826863
void dispatch( connectedSitesApi.util.invalidateTags( [ 'ConnectedSites' ] ) );
827864

828865
dispatch(

0 commit comments

Comments
 (0)