Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import { store } from 'src/stores';
import { connectedSitesApi } from 'src/stores/sync/connected-sites';

const mockGetConnectedWpcomSites = vi.fn();
const mockUpdateSingleConnectedWpcomSite = vi.fn();
const mockUpdateConnectedWpcomSites = vi.fn();

vi.mock( 'src/lib/get-ipc-api', () => ( {
getIpcApi: vi.fn( () => ( {
getConnectedWpcomSites: mockGetConnectedWpcomSites,
updateSingleConnectedWpcomSite: mockUpdateSingleConnectedWpcomSite,
updateConnectedWpcomSites: mockUpdateConnectedWpcomSites,
} ) ),
} ) );

Expand Down Expand Up @@ -44,10 +44,10 @@ describe( 'SiteManagementActions', () => {
beforeEach( () => {
// Reset mock calls but preserve implementations
mockGetConnectedWpcomSites.mockClear();
mockUpdateSingleConnectedWpcomSite.mockClear();
mockUpdateConnectedWpcomSites.mockClear();
// Set default return values
mockGetConnectedWpcomSites.mockResolvedValue( [] );
mockUpdateSingleConnectedWpcomSite.mockResolvedValue( {} );
mockUpdateConnectedWpcomSites.mockResolvedValue( {} );
// Clear RTK Query cache between tests
store.dispatch( connectedSitesApi.util.resetApiState() );
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export function useListenDeepLinkConnection() {
localSiteId: studioSiteId,
syncSupport: 'already-connected',
};
await getIpcApi().updateSingleConnectedWpcomSite( fullSiteData );
await getIpcApi().updateConnectedWpcomSites( [ fullSiteData ] );
dispatch( connectedSitesApi.util.invalidateTags( [ 'ConnectedSites' ] ) );
}
} catch ( error ) {
Expand Down
1 change: 0 additions & 1 deletion apps/studio/src/ipc-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ export {
removeSyncBackup,
resumeSyncUpload,
updateConnectedWpcomSites,
updateSingleConnectedWpcomSite,
} from 'src/modules/sync/lib/ipc-handlers';

export {
Expand Down
28 changes: 0 additions & 28 deletions apps/studio/src/modules/sync/lib/ipc-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,34 +529,6 @@ export async function updateConnectedWpcomSites(
}
}

export async function updateSingleConnectedWpcomSite(
event: IpcMainInvokeEvent,
updatedSite: SyncSite
) {
try {
await lockAppdata();
const userData = await loadUserData();
const currentUserId = userData.authToken?.id;

if ( ! currentUserId ) {
throw new Error( 'User not authenticated' );
}

const connections = userData.connectedWpcomSites?.[ currentUserId ] || [];
const index = connections.findIndex(
( conn ) => conn.id === updatedSite.id && conn.localSiteId === updatedSite.localSiteId
);

if ( index !== -1 ) {
connections[ index ] = updatedSite;
}

await saveUserData( userData );
} finally {
await unlockAppdata();
}
}

export async function getConnectedWpcomSites(
event: IpcMainInvokeEvent,
localSiteId?: string
Expand Down
2 changes: 0 additions & 2 deletions apps/studio/src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ const api: IpcApi = {
disconnectWpcomSites: ( ...args ) => ipcRendererInvoke( 'disconnectWpcomSites', ...args ),
updateConnectedWpcomSites: ( ...args ) =>
ipcRendererInvoke( 'updateConnectedWpcomSites', ...args ),
updateSingleConnectedWpcomSite: ( updatedSite ) =>
ipcRendererInvoke( 'updateSingleConnectedWpcomSite', updatedSite ),
authenticate: ( isSignup ) => ipcRendererSend( 'authenticate', isSignup ),
exportSite: ( options ) => ipcRendererInvoke( 'exportSite', options ),
isAuthenticated: () => ipcRendererInvoke( 'isAuthenticated' ),
Expand Down
30 changes: 0 additions & 30 deletions apps/studio/src/stores/sync/connected-sites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,41 +130,11 @@ export const connectedSitesApi = createApi( {
{ type: 'ConnectedSites', localSiteId },
],
} ),

updateSiteTimestamp: builder.mutation<
void,
{ siteId: number; localSiteId: string; type: 'pull' | 'push' }
>( {
queryFn: async ( { siteId, localSiteId, type } ) => {
const connectedSites = await getIpcApi().getConnectedWpcomSites( localSiteId );
const connectedSite = connectedSites.find(
( { id, localSiteId: siteLocalId } ) => siteId === id && localSiteId === siteLocalId
);

if ( ! connectedSite ) {
return { error: { status: 'CUSTOM_ERROR', error: 'Site not found' } };
}

const timestampKey = type === 'pull' ? 'lastPullTimestamp' : 'lastPushTimestamp';
const updatedConnectedSite = {
...connectedSite,
[ timestampKey ]: new Date().toISOString(),
};

await getIpcApi().updateSingleConnectedWpcomSite( updatedConnectedSite );

return { data: undefined };
},
invalidatesTags: ( result, error, { localSiteId } ) => [
{ type: 'ConnectedSites', localSiteId },
],
} ),
} ),
} );

export const {
useGetConnectedSitesForLocalSiteQuery,
useConnectSiteMutation,
useDisconnectSiteMutation,
useUpdateSiteTimestampMutation,
} = connectedSitesApi;
37 changes: 37 additions & 0 deletions apps/studio/src/stores/sync/sync-operations-slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,33 @@ import type { AppDispatch, RootState } from 'src/stores';
import type { SyncOption } from 'src/types';
import type { WPCOM } from 'wpcom/types';

async function updateSiteTimestamp( {
siteId,
localSiteId,
type,
}: {
siteId: number;
localSiteId: string;
type: 'pull' | 'push';
} ) {
const connectedSites = await getIpcApi().getConnectedWpcomSites( localSiteId );
const connectedSite = connectedSites.find(
( { id, localSiteId: siteLocalId } ) => siteId === id && localSiteId === siteLocalId
);

if ( ! connectedSite ) {
return;
}

const timestampKey = type === 'pull' ? 'lastPullTimestamp' : 'lastPushTimestamp';
await getIpcApi().updateConnectedWpcomSites( [
{
...connectedSite,
[ timestampKey ]: new Date().toISOString(),
},
] );
}

export type SyncBackupState = {
remoteSiteId: number;
backupId: number | null;
Expand Down Expand Up @@ -611,6 +638,11 @@ const pollPushProgressThunk = createTypedAsyncThunk(
switch ( response.status ) {
case 'finished':
status = pushStatesProgressInfo.finished;
await updateSiteTimestamp( {
siteId: remoteSiteId,
localSiteId: selectedSiteId,
type: 'push',
} );
void dispatch( connectedSitesApi.util.invalidateTags( [ 'ConnectedSites' ] ) );
getIpcApi().showNotification( {
title: currentPushState.selectedSite.name,
Expand Down Expand Up @@ -823,6 +855,11 @@ const pollPullBackupThunk = createTypedAsyncThunk(

await getIpcApi().removeSyncBackup( remoteSiteId );

await updateSiteTimestamp( {
siteId: remoteSiteId,
localSiteId: selectedSiteId,
type: 'pull',
} );
void dispatch( connectedSitesApi.util.invalidateTags( [ 'ConnectedSites' ] ) );

dispatch(
Expand Down
Loading