Skip to content

Commit fd65621

Browse files
authored
Move Sync connected sites to redux slice (#1680)
* Refactor connected sites to use redux slice. * Remove useSiteSyncManagement * Update tests to use the Redux store * No user-facing changes
1 parent 60c5609 commit fd65621

15 files changed

+607
-387
lines changed

src/components/content-tab-import-export.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { cx } from 'src/lib/cx';
1919
import { getIpcApi } from 'src/lib/get-ipc-api';
2020
import { getLocalizedLink } from 'src/lib/get-localized-link';
2121
import { useI18nLocale } from 'src/stores';
22+
import { useConnectedSitesData } from 'src/stores/sync';
2223

2324
interface ContentTabImportExportProps {
2425
selectedSite: SiteDetails;
@@ -318,7 +319,8 @@ const ImportSite = ( {
318319
export function ContentTabImportExport( { selectedSite }: ContentTabImportExportProps ) {
319320
const { __ } = useI18n();
320321
const [ isSupported, setIsSupported ] = useState< boolean | null >( null );
321-
const { isSiteIdPulling, isSiteIdPushing, connectedSites } = useSyncSites();
322+
const { isSiteIdPulling, isSiteIdPushing } = useSyncSites();
323+
const { connectedSites } = useConnectedSitesData();
322324
const isPulling = connectedSites.some( ( site ) => isSiteIdPulling( selectedSite.id, site.id ) );
323325
const isPushing = connectedSites.some( ( site ) => isSiteIdPushing( selectedSite.id, site.id ) );
324326
const isThisSiteSyncing = isPulling || isPushing;

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
import { render, screen } from '@testing-library/react';
22
import { userEvent } from '@testing-library/user-event';
3+
import { Provider } from 'react-redux';
34
import {
45
SiteManagementActionProps,
56
SiteManagementActions,
67
} from 'src/components/site-management-actions';
78
import { SyncSitesProvider } from 'src/hooks/sync-sites';
89
import { ContentTabsProvider } from 'src/hooks/use-content-tabs';
10+
import { store } from 'src/stores';
11+
12+
jest.mock( 'src/lib/get-ipc-api', () => ( {
13+
getIpcApi: () => ( {
14+
getConnectedWpcomSites: jest.fn().mockResolvedValue( [] ),
15+
updateSingleConnectedWpcomSite: jest.fn().mockResolvedValue( {} ),
16+
} ),
17+
} ) );
918

1019
const defaultProps = {
1120
onStart: jest.fn(),
@@ -18,9 +27,11 @@ describe( 'SiteManagementActions', () => {
1827
} );
1928
const renderWithProvider = ( children: React.ReactElement ) => {
2029
return render(
21-
<ContentTabsProvider>
22-
<SyncSitesProvider>{ children }</SyncSitesProvider>
23-
</ContentTabsProvider>
30+
<Provider store={ store }>
31+
<ContentTabsProvider>
32+
<SyncSitesProvider>{ children }</SyncSitesProvider>
33+
</ContentTabsProvider>
34+
</Provider>
2435
);
2536
};
2637
it( 'should not render when selectedSite is undefined', () => {

src/hooks/sync-sites/sync-sites-context.tsx

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import { __, sprintf } from '@wordpress/i18n';
22
import React, { createContext, useCallback, useContext, useState } from 'react';
33
import { useListenDeepLinkConnection } from 'src/hooks/sync-sites/use-listen-deep-link-connection';
4-
import {
5-
UseSiteSyncManagement,
6-
useSiteSyncManagement,
7-
} from 'src/hooks/sync-sites/use-site-sync-management';
84
import { PullStates, UseSyncPull, useSyncPull } from 'src/hooks/sync-sites/use-sync-pull';
95
import { PushStates, UseSyncPush, useSyncPush } from 'src/hooks/sync-sites/use-sync-push';
106
import { useFormatLocalizedTimestamps } from 'src/hooks/use-format-localized-timestamps';
117
import { getIpcApi } from 'src/lib/get-ipc-api';
12-
import type { SyncSite } from 'src/hooks/use-fetch-wpcom-sites/types';
8+
import { useAppDispatch } from 'src/stores';
9+
import { useConnectedSitesData, useSyncSitesData, connectedSitesActions } from 'src/stores/sync';
1310

1411
type GetLastSyncTimeText = ( timestamp: string | null, type: 'pull' | 'push' ) => string;
1512
type UpdateSiteTimestamp = (
@@ -18,26 +15,17 @@ type UpdateSiteTimestamp = (
1815
type: 'pull' | 'push'
1916
) => Promise< void >;
2017

21-
type IsSyncSitesSelectorOpen = boolean | { disconnectSiteId?: number };
22-
2318
export type SyncSitesContextType = Omit< UseSyncPull, 'pullStates' > &
2419
Omit< UseSyncPush, 'pushStates' > &
25-
Omit< UseSiteSyncManagement, 'loadConnectedSites' > & {
20+
ReturnType< typeof useSyncSitesData > & {
2621
getLastSyncTimeText: GetLastSyncTimeText;
27-
isSyncSitesSelectorOpen: IsSyncSitesSelectorOpen;
28-
setIsSyncSitesSelectorOpen: ( open: IsSyncSitesSelectorOpen ) => void;
29-
closeSyncSitesSelector: () => void;
3022
};
3123

3224
const SyncSitesContext = createContext< SyncSitesContextType | undefined >( undefined );
3325

3426
export function SyncSitesProvider( { children }: { children: React.ReactNode } ) {
3527
const { formatRelativeTime } = useFormatLocalizedTimestamps();
3628
const [ pullStates, setPullStates ] = useState< PullStates >( {} );
37-
const [ connectedSites, setConnectedSites ] = useState< SyncSite[] >( [] );
38-
const [ isSyncSitesSelectorOpen, setIsSyncSitesSelectorOpen ] =
39-
useState< IsSyncSitesSelectorOpen >( false );
40-
const closeSyncSitesSelector = useCallback( () => setIsSyncSitesSelectorOpen( false ), [] );
4129

4230
const getLastSyncTimeText = useCallback< GetLastSyncTimeText >(
4331
( timestamp, type ) => {
@@ -57,10 +45,13 @@ export function SyncSitesProvider( { children }: { children: React.ReactNode } )
5745
[ formatRelativeTime ]
5846
);
5947

48+
const { connectedSites } = useConnectedSitesData();
49+
const dispatch = useAppDispatch();
50+
6051
const updateSiteTimestamp = useCallback< UpdateSiteTimestamp >(
61-
async ( siteId, localSiteId, type ) => {
52+
async ( siteId, localSiteIdParam, type ) => {
6253
const site = connectedSites.find(
63-
( { id, localSiteId: siteLocalId } ) => siteId === id && localSiteId === siteLocalId
54+
( { id, localSiteId: siteLocalId } ) => siteId === id && localSiteIdParam === siteLocalId
6455
);
6556

6657
if ( ! site ) {
@@ -74,14 +65,18 @@ export function SyncSitesProvider( { children }: { children: React.ReactNode } )
7465
};
7566

7667
await getIpcApi().updateSingleConnectedWpcomSite( updatedSite );
77-
setConnectedSites( ( sites ) =>
78-
sites.map( ( s ) => ( s.id === site.id ? updatedSite : s ) )
68+
69+
dispatch(
70+
connectedSitesActions.updateSite( {
71+
localSiteId: localSiteIdParam,
72+
site: updatedSite,
73+
} )
7974
);
8075
} catch ( error ) {
8176
console.error( 'Failed to update timestamp:', error );
8277
}
8378
},
84-
[ connectedSites ]
79+
[ connectedSites, dispatch ]
8580
);
8681

8782
const { pullSite, isAnySitePulling, isSiteIdPulling, clearPullState, getPullState } = useSyncPull(
@@ -103,14 +98,8 @@ export function SyncSitesProvider( { children }: { children: React.ReactNode } )
10398
}
10499
);
105100

106-
const { connectSite, disconnectSite, syncSites, isFetching, refetchSites } =
107-
useSiteSyncManagement( {
108-
connectedSites,
109-
setConnectedSites,
110-
closeSyncSitesSelector,
111-
} );
112-
113-
useListenDeepLinkConnection( { connectSite, refetchSites } );
101+
const { syncSites, isFetching, refetchSites } = useSyncSitesData();
102+
useListenDeepLinkConnection( { refetchSites } );
114103

115104
return (
116105
<SyncSitesContext.Provider
@@ -119,9 +108,6 @@ export function SyncSitesProvider( { children }: { children: React.ReactNode } )
119108
isAnySitePulling,
120109
isSiteIdPulling,
121110
clearPullState,
122-
connectedSites,
123-
connectSite,
124-
disconnectSite,
125111
syncSites,
126112
refetchSites,
127113
isFetching,
@@ -132,9 +118,6 @@ export function SyncSitesProvider( { children }: { children: React.ReactNode } )
132118
isSiteIdPushing,
133119
clearPushState,
134120
getLastSyncTimeText,
135-
isSyncSitesSelectorOpen,
136-
setIsSyncSitesSelectorOpen,
137-
closeSyncSitesSelector,
138121
} }
139122
>
140123
{ children }

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import { SyncSitesContextType } from 'src/hooks/sync-sites/sync-sites-context';
22
import { useContentTabs } from 'src/hooks/use-content-tabs';
33
import { useIpcListener } from 'src/hooks/use-ipc-listener';
44
import { useSiteDetails } from 'src/hooks/use-site-details';
5+
import { useConnectedSitesOperations } from 'src/stores/sync';
56

67
export function useListenDeepLinkConnection( {
7-
connectSite,
88
refetchSites,
99
}: {
10-
connectSite: SyncSitesContextType[ 'connectSite' ];
1110
refetchSites: SyncSitesContextType[ 'refetchSites' ];
1211
} ) {
12+
const { connectSite } = useConnectedSitesOperations();
1313
const { selectedSite, setSelectedSiteId } = useSiteDetails();
1414
const { setSelectedTab, selectedTab } = useContentTabs();
1515

src/hooks/sync-sites/use-site-sync-management.ts

Lines changed: 0 additions & 131 deletions
This file was deleted.

0 commit comments

Comments
 (0)