Skip to content
Open
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
46 changes: 13 additions & 33 deletions apps/studio/src/hooks/sync-sites/use-listen-deep-link-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,30 @@ 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 { useAppDispatch } from 'src/stores';
import {
connectedSitesActions,
useConnectSiteMutation,
useGetConnectedSitesForLocalSiteQuery,
} from 'src/stores/sync/connected-sites';
import { useGetWpComSitesQuery } from 'src/stores/sync/wpcom-sites';
import { connectedSitesActions, useConnectSiteMutation } from 'src/stores/sync/connected-sites';

export function useListenDeepLinkConnection() {
const dispatch = useAppDispatch();
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, autoOpenPush } ) => {
// Fetch latest sites from network before checking
const result = await refetchWpComSites();
const latestSites = result.data ?? [];
const newConnectedSite = latestSites.find( ( site ) => site.id === remoteSiteId );
if ( newConnectedSite ) {
if ( selectedSite?.id && selectedSite.id !== studioSiteId ) {
// Select studio site that started the sync
setSelectedSiteId( studioSiteId );
}
await connectSite( { site: newConnectedSite, localSiteId: studioSiteId } );
if ( selectedTab !== 'sync' ) {
// Switch to sync tab
setSelectedTab( 'sync' );
}
// Only auto-open push dialog if explicitly requested (e.g., from "Publish site" button)
if ( autoOpenPush ) {
dispatch( connectedSitesActions.setSelectedRemoteSiteId( remoteSiteId ) );
}
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' );
}
// Only auto-open push dialog if explicitly requested (e.g., from "Publish site" button)
if ( autoOpenPush ) {
dispatch( connectedSitesActions.setSelectedRemoteSiteId( remoteSiteId ) );
}
}
);
Expand Down
24 changes: 18 additions & 6 deletions apps/studio/src/hooks/tests/use-add-site.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@ import { useContentTabs } from 'src/hooks/use-content-tabs';
import { useSiteDetails } from 'src/hooks/use-site-details';
import { store } from 'src/stores';
import { setProviderConstants } from 'src/stores/provider-constants-slice';
import { useConnectSiteMutation } from 'src/stores/sync/connected-sites';
import type { SyncSitesContextType } from 'src/hooks/sync-sites/sync-sites-context';
import type { SyncSite } from 'src/modules/sync/types';

vi.mock( 'src/hooks/use-site-details' );
vi.mock( 'src/hooks/use-feature-flags' );
vi.mock( 'src/hooks/sync-sites' );
vi.mock( 'src/hooks/use-content-tabs' );
vi.mock( 'src/stores/sync/connected-sites', async ( importOriginal ) => {
const original = await importOriginal< typeof import('src/stores/sync/connected-sites') >();
return {
...original,
useConnectSiteMutation: vi.fn(),
};
} );
vi.mock( 'src/hooks/use-import-export', () => ( {
useImportExport: () => ( {
importFile: vi.fn(),
Expand All @@ -24,6 +32,7 @@ vi.mock( 'src/hooks/use-import-export', () => ( {
} ),
} ) );

const mockConnectSite = vi.fn().mockReturnValue( { unwrap: () => Promise.resolve( [] ) } );
const mockConnectWpcomSites = vi.fn().mockResolvedValue( undefined );
const mockShowOpenFolderDialog = vi.fn();
const mockGenerateProposedSitePath = vi.fn().mockResolvedValue( {
Expand Down Expand Up @@ -62,6 +71,11 @@ describe( 'useAddSite', () => {
beforeEach( () => {
vi.clearAllMocks();

vi.mocked( useConnectSiteMutation ).mockReturnValue( [
mockConnectSite,
{ isLoading: false, reset: vi.fn() },
] as unknown as ReturnType< typeof useConnectSiteMutation > );

// Prepopulate store with provider constants
store.dispatch(
setProviderConstants( {
Expand Down Expand Up @@ -267,12 +281,10 @@ describe( 'useAddSite', () => {
await result.current.handleCreateSite( formValues );
} );

expect( mockConnectWpcomSites ).toHaveBeenCalledWith( [
{
sites: [ remoteSite ],
localSiteId: createdSite.id,
},
] );
expect( mockConnectSite ).toHaveBeenCalledWith( {
remoteSiteId: remoteSite.id,
localSiteId: createdSite.id,
} );
expect( mockPullSite ).toHaveBeenCalledWith( remoteSite, createdSite, {
optionsToSync: [ 'all' ],
} );
Expand Down
2 changes: 1 addition & 1 deletion apps/studio/src/hooks/use-add-site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ export function useAddSite() {
body: __( 'Your new site was imported' ),
} );
} else if ( selectedRemoteSite ) {
await connectSite( { site: selectedRemoteSite, localSiteId: newSite.id } );
await connectSite( { remoteSiteId: selectedRemoteSite.id, localSiteId: newSite.id } );
const pullOptions: SyncOption[] = [ 'all' ];
pullSite( selectedRemoteSite, newSite, {
optionsToSync: pullOptions,
Expand Down
Loading