11import  {  __ ,  sprintf  }  from  '@wordpress/i18n' ; 
22import  React ,  {  createContext ,  useCallback ,  useContext ,  useState  }  from  'react' ; 
33import  {  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' ; 
84import  {  PullStates ,  UseSyncPull ,  useSyncPull  }  from  'src/hooks/sync-sites/use-sync-pull' ; 
95import  {  PushStates ,  UseSyncPush ,  useSyncPush  }  from  'src/hooks/sync-sites/use-sync-push' ; 
106import  {  useFormatLocalizedTimestamps  }  from  'src/hooks/use-format-localized-timestamps' ; 
117import  {  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
1411type  GetLastSyncTimeText  =  (  timestamp : string  |  null ,  type : 'pull'  |  'push'  )  =>  string ; 
1512type  UpdateSiteTimestamp  =  ( 
@@ -18,26 +15,17 @@ type UpdateSiteTimestamp = (
1815	type : 'pull'  |  'push' 
1916)  =>  Promise <  void   > ; 
2017
21- type  IsSyncSitesSelectorOpen  =  boolean  |  {  disconnectSiteId ?: number  } ; 
22- 
2318export  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
3224const  SyncSitesContext  =  createContext <  SyncSitesContextType  |  undefined  > (  undefined  ) ; 
3325
3426export  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  } 
0 commit comments