11import { __experimentalGrid as Grid } from '@wordpress/components' ;
22import { useViewportMatch } from '@wordpress/compose' ;
3- import { useState , useEffect } from 'react' ;
3+ import { useMemo } from 'react' ;
44import { PerformanceTrackerStop } from '../../app/performance-tracking' ;
55import { Card , CardBody } from '../../components/card' ;
66import { BackupDetails } from './backup-details' ;
@@ -13,12 +13,54 @@ export function useIsBackupsSmallViewport() {
1313 return useViewportMatch ( 'xlarge' , '<' ) ;
1414}
1515
16- interface BackupsBrowserProps {
16+ // Single owner of the resolved selection so the page header and browser body can't disagree.
17+ export function useBackupsBrowserState ( {
18+ site,
19+ rewindId,
20+ dateRange,
21+ timezoneString,
22+ gmtOffset,
23+ enabled,
24+ } : {
1725 site : Site ;
1826 rewindId ?: string ;
1927 dateRange ?: { start : Date ; end : Date } ;
2028 timezoneString ?: string ;
2129 gmtOffset ?: number ;
30+ enabled ?: boolean ;
31+ } ) {
32+ const isSmallViewport = useIsBackupsSmallViewport ( ) ;
33+
34+ const { activityLog, isLoadingActivityLog } = useActivityLog ( {
35+ siteId : site . ID ,
36+ dateRange,
37+ gmtOffset,
38+ timezoneString,
39+ enabled,
40+ } ) ;
41+
42+ const selectedBackup = useMemo < ActivityLogEntry | null > ( ( ) => {
43+ if ( rewindId ) {
44+ return activityLog ?. find ( ( item ) => item . rewind_id === rewindId ) ?? null ;
45+ }
46+ if ( ! isSmallViewport ) {
47+ return activityLog ?. [ 0 ] ?? null ;
48+ }
49+ return null ;
50+ } , [ rewindId , activityLog , isSmallViewport ] ) ;
51+
52+ return { activityLog, isLoadingActivityLog, selectedBackup, isSmallViewport } ;
53+ }
54+
55+ interface BackupsBrowserProps {
56+ site : Site ;
57+ activityLog : ActivityLogEntry [ ] ;
58+ isLoadingActivityLog : boolean ;
59+ selectedBackup : ActivityLogEntry | null ;
60+ isSmallViewport : boolean ;
61+ dateRange ?: { start : Date ; end : Date } ;
62+ timezoneString ?: string ;
63+ gmtOffset ?: number ;
2264 searchParams ?: Record < string , unknown > ;
2365 onSelectBackup : ( backup : ActivityLogEntry | null ) => void ;
2466 onRequestRestore ?: ( backup : ActivityLogEntry ) => void ;
@@ -28,7 +70,10 @@ interface BackupsBrowserProps {
2870
2971export function BackupsBrowser ( {
3072 site,
31- rewindId,
73+ activityLog,
74+ isLoadingActivityLog,
75+ selectedBackup,
76+ isSmallViewport,
3277 dateRange,
3378 timezoneString,
3479 gmtOffset,
@@ -38,37 +83,6 @@ export function BackupsBrowser( {
3883 onRequestDownload,
3984 onGranularDownloadReady,
4085} : BackupsBrowserProps ) {
41- const isSmallViewport = useIsBackupsSmallViewport ( ) ;
42- const [ selectedBackup , setSelectedBackupInState ] = useState < ActivityLogEntry | null > ( null ) ;
43-
44- const { activityLog, isLoadingActivityLog } = useActivityLog ( {
45- siteId : site . ID ,
46- dateRange,
47- gmtOffset,
48- timezoneString,
49- } ) ;
50-
51- // Auto-select backup based on rewindId parameter.
52- useEffect ( ( ) => {
53- if ( rewindId && activityLog ) {
54- const targetBackup = activityLog . find ( ( item ) => item . rewind_id === rewindId ) ;
55- if ( targetBackup ) {
56- setSelectedBackupInState ( targetBackup ) ;
57- }
58- return ;
59- }
60-
61- // Select the first backup on the index route (desktop only) to keep the panel populated.
62- const backup = activityLog ?. [ 0 ] ;
63- if ( ! rewindId && backup && ! isSmallViewport ) {
64- setSelectedBackupInState ( backup ) ;
65- }
66-
67- if ( ! rewindId && ! backup ) {
68- setSelectedBackupInState ( null ) ;
69- }
70- } , [ rewindId , activityLog , isSmallViewport ] ) ;
71-
7286 const renderDetails = ( backup : ActivityLogEntry ) => (
7387 < BackupDetails
7488 backup = { backup }
0 commit comments