Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
c4efc6c
Implement RTK Query approach for fetching wpcom sites
epeicher Nov 25, 2025
7d8fd66
Use the new hook and update loading conditions
epeicher Nov 26, 2025
ba89f0d
Rename function name for clarity
epeicher Nov 26, 2025
ee7ea22
Remove use-fetch-wpcom-sites and update imports
epeicher Nov 26, 2025
afd3fbf
Fix tests
epeicher Nov 26, 2025
d7113b1
Refetch sites when opening the sites modal instead of using an effect
epeicher Nov 27, 2025
9a6f74f
Do not fetch sites on mount
epeicher Nov 27, 2025
7d0b2a5
Merge branch 'trunk' of github.com:Automattic/studio into stu-1035-ct…
epeicher Nov 27, 2025
b09206b
Update files after merge
epeicher Nov 27, 2025
427d3ab
Infer the SitesEndpointSite type from the schema
epeicher Nov 28, 2025
63cc6b8
Remove unnecessary effect on sites modal
epeicher Nov 28, 2025
48ffd67
Merge branch 'trunk' of github.com:Automattic/studio into stu-1035-ct…
epeicher Nov 28, 2025
7e1bbdb
Unify Publish site button in one component with a redirect prop
epeicher Nov 28, 2025
c86048c
Fix comment wording
epeicher Dec 1, 2025
b134968
Remove isBusy from the publish site button
epeicher Dec 1, 2025
5610e0e
Merge branch 'trunk' of github.com:Automattic/studio into stu-1035-ct…
epeicher Dec 2, 2025
5079edf
Move fetching sites logic to sites modal selector
epeicher Dec 2, 2025
99a6b55
Add and remove comments
epeicher Dec 2, 2025
5cfc0f2
Refactor connected-sites mutation to accept an id and fetch the sites
epeicher Dec 3, 2025
8fa10dd
Revert "Refactor connected-sites mutation to accept an id and fetch t…
epeicher Dec 3, 2025
61e8a4b
Use the more declarative refetchOnMount instead of an effect on pull-…
epeicher Dec 3, 2025
a2fd40a
Merge branch 'trunk' of github.com:Automattic/studio into stu-1035-ct…
epeicher Dec 3, 2025
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
25 changes: 3 additions & 22 deletions src/hooks/sync-sites/sync-sites-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,16 @@ import {
mapImportResponseToPushState,
} from 'src/hooks/sync-sites/use-sync-push';
import { useAuth } from 'src/hooks/use-auth';
import { useFetchWpComSites } from 'src/hooks/use-fetch-wpcom-sites';
import { useFormatLocalizedTimestamps } from 'src/hooks/use-format-localized-timestamps';
import { useSiteDetails } from 'src/hooks/use-site-details';
import { useSyncStatesProgressInfo } from 'src/hooks/use-sync-states-progress-info';
import { getIpcApi } from 'src/lib/get-ipc-api';
import {
useGetConnectedSitesForLocalSiteQuery,
useUpdateSiteTimestampMutation,
} from 'src/stores/sync/connected-sites';
import { useUpdateSiteTimestampMutation } from 'src/stores/sync/connected-sites';
import type { ImportResponse } from 'src/hooks/use-sync-states-progress-info';

type GetLastSyncTimeText = ( timestamp: string | null, type: 'pull' | 'push' ) => string;

export type SyncSitesContextType = Omit< UseSyncPull, 'pullStates' > &
Omit< UseSyncPush, 'pushStates' > &
ReturnType< typeof useFetchWpComSites > & {
Omit< UseSyncPush, 'pushStates' > & {
getLastSyncTimeText: GetLastSyncTimeText;
};

Expand Down Expand Up @@ -53,13 +47,6 @@ export function SyncSitesProvider( { children }: { children: React.ReactNode } )
[ formatRelativeTime ]
);

const { selectedSite } = useSiteDetails();
const { user } = useAuth();
const { data: connectedSites = [] } = useGetConnectedSitesForLocalSiteQuery( {
localSiteId: selectedSite?.id,
userId: user?.id,
} );

const [ updateSiteTimestamp ] = useUpdateSiteTimestampMutation();

const { pullSite, isAnySitePulling, isSiteIdPulling, clearPullState, getPullState, cancelPull } =
Expand All @@ -79,10 +66,7 @@ export function SyncSitesProvider( { children }: { children: React.ReactNode } )
updateSiteTimestamp( { siteId: remoteSiteId, localSiteId, type: 'push' } ),
} );

const { syncSites, isFetching, refetchSites } = useFetchWpComSites(
connectedSites.map( ( { id } ) => id )
);
useListenDeepLinkConnection( { refetchSites } );
useListenDeepLinkConnection();

const { client } = useAuth();
const { pushStatesProgressInfo } = useSyncStatesProgressInfo();
Expand Down Expand Up @@ -154,9 +138,6 @@ export function SyncSitesProvider( { children }: { children: React.ReactNode } )
isSiteIdPulling,
clearPullState,
cancelPull,
syncSites,
refetchSites,
isFetching,
getPullState,
getPushState,
pushSite,
Expand Down
27 changes: 19 additions & 8 deletions src/hooks/sync-sites/use-listen-deep-link-connection.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest moving more of this logic to src/stores/sync/connected-sites.ts by updating the connectSite mutation like so:

connectSite: builder.mutation<
	SyncSite[],
	{ remoteSiteId: number; localSiteId: string; userId?: number }
>( {
	queryFn: async ( { remoteSiteId, localSiteId, userId }, api ) => {
		const connectedSites = await getIpcApi().getConnectedWpcomSites( localSiteId );
		const { data: remoteSites = [] } = await api.dispatch(
			wpcomSitesApi.endpoints.getWpComSites.initiate( {
				connectedSiteIds: connectedSites.map( ( site ) => site.id ),
				userId,
			} )
		);
		const siteToConnect = remoteSites.find( ( site ) => site.id === remoteSiteId );

		if ( ! siteToConnect ) {
			return {
				error: { status: 'CUSTOM_ERROR', error: 'Site not found in WordPress.com sites' },
			};
		}

		await getIpcApi().connectWpcomSites( [
			{
				sites: [ siteToConnect ],
				localSiteId,
			},
		] );
	
		const actualConnectedSites = await getIpcApi().getConnectedWpcomSites( localSiteId );
	
		return { data: actualConnectedSites };
	},

This would allow us to shorten this hook significantly:

export function useListenDeepLinkConnection() {
	const [ connectSite ] = useConnectSiteMutation();
	const { selectedSite, setSelectedSiteId } = useSiteDetails();
	const { setSelectedTab, selectedTab } = useContentTabs();
	const { user } = useAuth();

	useIpcListener( 'sync-connect-site', async ( _event, { remoteSiteId, studioSiteId } ) => {
		if ( selectedSite?.id && selectedSite.id !== studioSiteId ) {
			// Select studio site that started the sync
			setSelectedSiteId( studioSiteId );
		}
		await connectSite( { remoteSiteId, localSiteId: studioSiteId, userId: user?.id } );
		if ( selectedTab !== 'sync' ) {
			// Switch to sync tab
			setSelectedTab( 'sync' );
		}
	} );
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion, I will work on that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have been working on this, but I have found that this will trigger an additional call to the /me/sites endpoint when the connectSite mutation is used from the add-site and from the ContentTabSync handleConnect, where they already have the site, so I am not sure we are improving here.

What do you think about tackling this specific refactor as a follow-up and progressing with the current changes?

Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
import { SyncSitesContextType } from 'src/hooks/sync-sites/sync-sites-context';
import { useAuth } from 'src/hooks/use-auth';
import { useContentTabs } from 'src/hooks/use-content-tabs';
import { useIpcListener } from 'src/hooks/use-ipc-listener';
import { useSiteDetails } from 'src/hooks/use-site-details';
import { useConnectSiteMutation } from 'src/stores/sync/connected-sites';
import {
useConnectSiteMutation,
useGetConnectedSitesForLocalSiteQuery,
} from 'src/stores/sync/connected-sites';
import { useGetWpComSitesQuery } from 'src/stores/sync/wpcom-sites';

export function useListenDeepLinkConnection( {
refetchSites,
}: {
refetchSites: SyncSitesContextType[ 'refetchSites' ];
} ) {
export function useListenDeepLinkConnection() {
const [ connectSite ] = useConnectSiteMutation();
const { selectedSite, setSelectedSiteId } = useSiteDetails();
const { setSelectedTab, selectedTab } = useContentTabs();
const { user } = useAuth();
const { data: connectedSites = [] } = useGetConnectedSitesForLocalSiteQuery( {
localSiteId: selectedSite?.id,
userId: user?.id,
} );
const connectedSiteIds = connectedSites.map( ( { id } ) => id );
const { refetch: refetchWpComSites } = useGetWpComSitesQuery( {
connectedSiteIds,
userId: user?.id,
} );

useIpcListener( 'sync-connect-site', async ( _event, { remoteSiteId, studioSiteId } ) => {
// Fetch latest sites from network before checking
const latestSites = await refetchSites();
const result = await refetchWpComSites();
const latestSites = result.data ?? [];
const newConnectedSite = latestSites.find( ( site ) => site.id === remoteSiteId );
if ( newConnectedSite ) {
if ( selectedSite?.id && selectedSite.id !== studioSiteId ) {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/sync-sites/use-sync-pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
} from 'src/hooks/use-sync-states-progress-info';
import { getIpcApi } from 'src/lib/get-ipc-api';
import { getHostnameFromUrl } from 'src/lib/url-utils';
import type { SyncSite } from 'src/hooks/use-fetch-wpcom-sites/types';
import type { SyncSite } from 'src/modules/sync/types';
import type { SyncOption } from 'src/types';

export type SyncBackupState = {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/sync-sites/use-sync-push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import {
} from 'src/hooks/use-sync-states-progress-info';
import { getIpcApi } from 'src/lib/get-ipc-api';
import { getHostnameFromUrl } from 'src/lib/url-utils';
import type { SyncSite } from 'src/hooks/use-fetch-wpcom-sites/types';
import type { ImportResponse } from 'src/hooks/use-sync-states-progress-info';
import type { SyncSite } from 'src/modules/sync/types';
import type { SyncOption } from 'src/types';

export type SyncPushState = {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/tests/get-sync-support.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getSyncSupport } from 'src/hooks/use-fetch-wpcom-sites';
import { getSyncSupport } from 'src/modules/sync/lib/sync-support';

// Mocks for site shapes
const baseSite = {
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/tests/reconcile-connected-sites.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { reconcileConnectedSites } from 'src/hooks/use-fetch-wpcom-sites/reconcile-connected-sites';
import type { SyncSite } from 'src/hooks/use-fetch-wpcom-sites/types';
import { reconcileConnectedSites } from 'src/modules/sync/lib/reconcile-connected-sites';
import type { SyncSite } from 'src/modules/sync/types';

describe( 'reconcileConnectedSites', () => {
test( 'should update relevant properties', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/tests/use-add-site.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useSiteDetails } from 'src/hooks/use-site-details';
import { getWordPressProvider } from 'src/lib/wordpress-provider';
import { store } from 'src/stores';
import { setProviderConstants } from 'src/stores/provider-constants-slice';
import type { SyncSite } from 'src/hooks/use-fetch-wpcom-sites/types';
import type { SyncSite } from 'src/modules/sync/types';

jest.mock( 'src/hooks/use-site-details' );
jest.mock( 'src/hooks/use-feature-flags' );
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/use-add-site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
selectDefaultWordPressVersion,
} from 'src/stores/provider-constants-slice';
import { useConnectSiteMutation } from 'src/stores/sync/connected-sites';
import type { SyncSite } from 'src/hooks/use-fetch-wpcom-sites/types';
import type { SyncSite } from 'src/modules/sync/types';
import type { Blueprint } from 'src/stores/wpcom-api';
import type { SyncOption } from 'src/types';

Expand Down
241 changes: 0 additions & 241 deletions src/hooks/use-fetch-wpcom-sites/index.tsx

This file was deleted.

Loading