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

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions src/modules/add-site/components/pull-remote-site.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ import offlineIcon from 'src/components/offline-icon';
import { Tooltip } from 'src/components/tooltip';
import { useAuth } from 'src/hooks/use-auth';
import { useOffline } from 'src/hooks/use-offline';
import { useSiteDetails } from 'src/hooks/use-site-details';
import { getIpcApi } from 'src/lib/get-ipc-api';
import { NoWpcomSitesContent } from 'src/modules/sync/components/no-wpcom-sites-content';
import { SitesListContent } from 'src/modules/sync/components/sync-sites-modal-selector';
import { SyncTabImage } from 'src/modules/sync/components/sync-tab-image';
import { useGetConnectedSitesForLocalSiteQuery } from 'src/stores/sync/connected-sites';
import { useGetWpComSitesQuery } from 'src/stores/sync/wpcom-sites';
import type { SyncSite } from 'src/modules/sync/types';

Expand Down Expand Up @@ -138,19 +136,12 @@ export function PullRemoteSite( {
const { __ } = useI18n();
const { isAuthenticated, user } = useAuth();

const { selectedSite } = useSiteDetails();
const { data: connectedSites = [] } = useGetConnectedSitesForLocalSiteQuery( {
localSiteId: selectedSite?.id,
userId: user?.id,
} );
const connectedSiteIds = connectedSites.map( ( { id } ) => id );
const {
data: syncSites = [],
isLoading,
isSuccess,
} = useGetWpComSitesQuery(
{
connectedSiteIds,
userId: user?.id,
},
{ refetchOnMountOrArgChange: true }
Expand Down
20 changes: 13 additions & 7 deletions src/stores/sync/wpcom-sites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ function transformSingleSiteResponse(
* Transforms the WordPress.com sites API response into SyncSite objects.
*
* @param sites - Raw site data from the WordPress.com API
* @param connectedSiteIds - IDs of sites already connected to the current local site.
* Used to: 1) keep deleted sites in the list if they're connected, and
* 2) determine sync support status (already-connected vs syncable)
* @param connectedSiteIds - Optional IDs of sites already connected to the current local site.
* When provided, used to: 1) keep deleted sites in the list if they're connected, and
* 2) determine sync support status (already-connected vs syncable).
* When not provided, no filtering based on connected sites is applied.
*/
function transformSitesResponse( sites: unknown[], connectedSiteIds: number[] ): SyncSite[] {
function transformSitesResponse( sites: unknown[], connectedSiteIds?: number[] ): SyncSite[] {
const validatedSites = sites.reduce< SitesEndpointSite[] >( ( acc, rawSite ) => {
try {
const site = sitesEndpointSiteSchema.parse( rawSite );
Expand All @@ -100,12 +101,17 @@ function transformSitesResponse( sites: unknown[], connectedSiteIds: number[] ):

return validatedSites
.filter( ( site ) => ! site.is_a8c )
.filter( ( site ) => ! site.is_deleted || connectedSiteIds.some( ( id ) => id === site.ID ) )
.filter(
// Filter out deleted sites, except if they're in the connectedSiteIds list
( site ) =>
! site.is_deleted ||
( connectedSiteIds && connectedSiteIds.some( ( id ) => id === site.ID ) )
)
.map( ( site ) => {
// The API returns the wrong value for the `is_wpcom_staging_site` prop while staging sites
// are being created. Hence the check in other sites' `wpcom_staging_blog_ids` arrays.
const isStaging = allStagingSiteIds.includes( site.ID );
const syncSupport = getSyncSupport( site, connectedSiteIds );
const syncSupport = getSyncSupport( site, connectedSiteIds ?? [] );

return transformSingleSiteResponse( site, syncSupport, isStaging );
} );
Expand All @@ -116,7 +122,7 @@ export const wpcomSitesApi = createApi( {
baseQuery: fetchBaseQuery(),
tagTypes: [ 'WpComSites' ],
endpoints: ( builder ) => ( {
getWpComSites: builder.query< SyncSite[], { connectedSiteIds: number[]; userId?: number } >( {
getWpComSites: builder.query< SyncSite[], { connectedSiteIds?: number[]; userId?: number } >( {
queryFn: async ( { connectedSiteIds } ) => {
const wpcomClient = getWpcomClient();
if ( ! wpcomClient ) {
Expand Down