File size: 4,608 Bytes
1e92f2d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | import { Card, Spinner } from '@automattic/components';
import { useQuery } from '@tanstack/react-query';
import { useTranslate } from 'i18n-calypso';
import DocumentHead from 'calypso/components/data/document-head';
import { useLocalizedMoment } from 'calypso/components/localized-moment';
import Main from 'calypso/components/main';
import SidebarNavigation from 'calypso/components/sidebar-navigation';
import isJetpackCloud from 'calypso/lib/jetpack/is-jetpack-cloud';
import { applySiteOffset } from 'calypso/lib/site/timezone';
import wpcom from 'calypso/lib/wp';
import { useSelector } from 'calypso/state';
import { fromActivityApi } from 'calypso/state/data-layer/wpcom/sites/activity/from-api';
import getSiteGmtOffset from 'calypso/state/selectors/get-site-gmt-offset';
import getSiteTimezoneValue from 'calypso/state/selectors/get-site-timezone-value';
import getSiteUrl from 'calypso/state/sites/selectors/get-site-url';
import { getSelectedSiteId } from 'calypso/state/ui/selectors';
import BackupDownloadFlow from './download';
import Error from './error';
import BackupGranularRestoreFlow from './granular-restore';
import Loading from './loading';
import BackupRestoreFlow from './restore';
import { RewindFlowPurpose } from './types';
import type { FunctionComponent, ReactNode } from 'react';
interface Activity {
activityIsRewindable: boolean;
}
interface ActivityError {
code?: string;
}
import './style.scss';
interface Props {
rewindId: string;
purpose: RewindFlowPurpose;
}
const BackupRewindFlow: FunctionComponent< Props > = ( { rewindId, purpose } ) => {
const moment = useLocalizedMoment();
const translate = useTranslate();
const siteId = useSelector( getSelectedSiteId );
const siteUrl = useSelector( ( state ) => ( siteId && getSiteUrl( state, siteId ) ) || '' );
const activityQuery = useQuery< Activity, ActivityError >( {
queryKey: [ 'activity', siteId, rewindId ],
queryFn: () =>
wpcom.req
.get( {
apiNamespace: 'wpcom/v2',
path: `/sites/${ siteId }/activity/${ rewindId }`,
} )
.then( fromActivityApi ),
retry: false,
} );
const gmtOffset = useSelector( ( state ) => getSiteGmtOffset( state, siteId ?? 0 ) );
const timezone = useSelector( ( state ) => getSiteTimezoneValue( state, siteId ?? 0 ) );
const wrapWithCard = ( content: ReactNode ) => <Card>{ content }</Card>;
const render = () => {
if ( null === applySiteOffset || ! activityQuery.isFetched ) {
return wrapWithCard( <Loading /> );
} else if ( activityQuery.error?.code === 'no_activity_for_site_and_rewind_id' ) {
return wrapWithCard(
<Error
siteUrl={ siteUrl }
errorText={ translate( 'The activity referenced by %(rewindId)s does not exist.', {
args: { rewindId },
} ) }
/>
);
} else if ( activityQuery.data?.activityIsRewindable === false ) {
return wrapWithCard(
<Error
siteUrl={ siteUrl }
errorText={ translate( 'The activity referenced by %(rewindId)s is not rewindable.', {
args: { rewindId },
} ) }
/>
);
} else if ( ! activityQuery.isSuccess ) {
return wrapWithCard(
<Error
siteUrl={ siteUrl }
errorText={ translate( 'An error occurred while trying to retrieve the activity' ) }
/>
);
}
const backupDisplayDate = applySiteOffset( moment( parseFloat( rewindId ) * 1000 ), {
gmtOffset,
timezone,
} ).format( 'LLL' );
if ( siteId && rewindId && backupDisplayDate ) {
if ( purpose === RewindFlowPurpose.RESTORE ) {
return (
<BackupRestoreFlow
backup={ activityQuery.data }
backupDisplayDate={ backupDisplayDate }
rewindId={ rewindId }
siteId={ siteId }
siteUrl={ siteUrl }
/>
);
} else if ( purpose === RewindFlowPurpose.GRANULAR_RESTORE ) {
return (
<BackupGranularRestoreFlow
backupDisplayDate={ backupDisplayDate }
rewindId={ rewindId }
siteId={ siteId }
siteUrl={ siteUrl }
/>
);
}
// Default to RewindFlowPurpose.DOWNLOAD
return (
<BackupDownloadFlow
backupDisplayDate={ backupDisplayDate }
rewindId={ rewindId }
siteId={ siteId }
siteUrl={ siteUrl }
/>
);
}
// TODO: improve this placeholder
return <Spinner />;
};
return (
<Main className="rewind-flow">
<DocumentHead
title={
purpose === RewindFlowPurpose.RESTORE ? translate( 'Restore' ) : translate( 'Download' )
}
/>
{ isJetpackCloud() && <SidebarNavigation /> }
<div className="rewind-flow__content">{ render() }</div>
</Main>
);
};
export { RewindFlowPurpose, BackupRewindFlow as default };
|