File size: 5,861 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { Card } from '@automattic/components';
import { localize } from 'i18n-calypso';
import PropTypes from 'prop-types';
import { Component, Fragment } from 'react';
import { connect } from 'react-redux';
import QuerySiteSettings from 'calypso/components/data/query-site-settings';
import QuerySites from 'calypso/components/data/query-sites';
import { withLocalizedMoment } from 'calypso/components/localized-moment';
import Pagination from 'calypso/components/pagination';
import TileGrid from 'calypso/components/tile-grid';
import Tile from 'calypso/components/tile-grid/tile';
import useRewindableActivityLogQuery from 'calypso/data/activity-log/use-rewindable-activity-log-query';
import { applySiteOffset } from 'calypso/lib/site/timezone';
import ActivityLogItem from 'calypso/my-sites/activity/activity-log-item';
import StepWrapper from 'calypso/signup/step-wrapper';
import { submitSignupStep } from 'calypso/state/signup/progress/actions';
import { getSiteOption } from 'calypso/state/sites/selectors';
import './style.scss';

const PAGE_SIZE = 20;

class ClonePointStep extends Component {
	static propTypes = {
		flowName: PropTypes.string,
		goToNextStep: PropTypes.func.isRequired,
		positionInFlow: PropTypes.number,
		stepName: PropTypes.string,
		signupDependencies: PropTypes.object,
	};

	state = {
		showLog: false,
		currentPage: 1,
	};

	selectCurrent = () => {
		this.props.submitSignupStep( { stepName: this.props.stepName }, { clonePoint: 0 } );
		this.props.goToNextStep();
	};

	selectedPoint = ( activityTs ) => {
		this.props.submitSignupStep( { stepName: this.props.stepName }, { clonePoint: activityTs } );
		this.props.goToNextStep();
	};

	selectPrevious = () => {
		this.setState( { showLog: true } );
	};

	applySiteOffset( date ) {
		const { timezone, gmtOffset } = this.props;
		return applySiteOffset( date, { timezone, gmtOffset } );
	}

	changePage = ( pageNumber ) => {
		this.setState( { currentPage: pageNumber } );
		window.scrollTo( 0, 0 );
	};

	renderActivityLog() {
		const { siteId, logs, moment, translate } = this.props;

		const actualPage = Math.max(
			1,
			Math.min( this.state.currentPage, Math.ceil( logs.length / PAGE_SIZE ) )
		);

		const theseLogs = logs.slice( ( actualPage - 1 ) * PAGE_SIZE, actualPage * PAGE_SIZE );

		const timePeriod = ( () => {
			const today = this.applySiteOffset( moment() );
			let last = null;

			return ( { rewindId } ) => {
				const ts = this.applySiteOffset( moment( rewindId * 1000 ) );

				if ( null === last || ! ts.isSame( last, 'day' ) ) {
					last = ts;
					return (
						<h2 className="clone-point__time-period" key={ `time-period-${ ts }` }>
							{ ts.isSame( today, 'day' )
								? ts.format( translate( 'LL[ — Today]', { context: 'moment format string' } ) )
								: ts.format( 'LL' ) }
						</h2>
					);
				}

				return null;
			};
		} )();

		return (
			<div>
				<Card className="clone-point__card">
					<QuerySites siteId={ siteId } />
					<QuerySiteSettings siteId={ siteId } />
					<section className="clone-point__wrapper">
						{ theseLogs.map( ( log ) => (
							<Fragment key={ log.activityId }>
								{ timePeriod( log ) }
								<ActivityLogItem
									key={ log.activityId }
									siteId={ siteId }
									activity={ log }
									cloneOnClick={ this.selectedPoint }
									disableRestore
									disableBackup
									enableClone
								/>
							</Fragment>
						) ) }
					</section>
					<Pagination
						className="clone-point__pagination"
						key="clone-point-pagination"
						nextLabel={ translate( 'Older' ) }
						page={ this.state.currentPage }
						pageClick={ this.changePage }
						perPage={ PAGE_SIZE }
						prevLabel={ translate( 'Newer' ) }
						total={ logs.length }
					/>
				</Card>
			</div>
		);
	}

	renderSelector() {
		const { translate } = this.props;

		return (
			<TileGrid>
				<Tile
					className="clone-point__current"
					buttonLabel={ translate( 'Clone current state' ) }
					description={ translate( 'Create a clone of your site as it is right now.' ) }
					image="/calypso/images/illustrations/clone-site-origin.svg"
					onClick={ this.selectCurrent }
				/>
				<Tile
					className="clone-point__previous"
					buttonLabel={ translate( 'Clone previous state' ) }
					description={ translate(
						'Browse your event history and choose an earlier state to clone from.'
					) }
					image="/calypso/images/illustrations/backup.svg"
					onClick={ this.selectPrevious }
				/>
			</TileGrid>
		);
	}

	renderStepContent() {
		return (
			<div className="clone-point__wrap">
				{ this.state.showLog ? this.renderActivityLog() : this.renderSelector() }
			</div>
		);
	}

	render() {
		const { flowName, stepName, positionInFlow, translate } = this.props;

		const headerText = translate( 'Clone point' );
		const subHeaderText = translate(
			"Which point in your site's history would you like to clone from?"
		);

		return (
			<StepWrapper
				flowName={ flowName }
				stepName={ stepName }
				headerText={ headerText }
				fallbackHeaderText={ headerText }
				subHeaderText={ subHeaderText }
				fallbackSubHeaderText={ subHeaderText }
				positionInFlow={ positionInFlow }
				stepContent={ this.renderStepContent() }
			/>
		);
	}
}

function withActivityLog( Inner ) {
	return ( props ) => {
		const { siteId } = props;
		const { data: logs } = useRewindableActivityLogQuery( siteId, {}, { enabled: !! siteId } );
		return <Inner { ...props } logs={ logs ?? [] } />;
	};
}

export default connect(
	( state, { signupDependencies } ) => {
		const siteId = signupDependencies?.originBlogId;

		return {
			siteId,
			timezone: getSiteOption( state, siteId, 'timezone' ),
			gmtOffset: getSiteOption( state, siteId, 'gmt_offset' ),
		};
	},
	{ submitSignupStep }
)( withActivityLog( localize( withLocalizedMoment( ClonePointStep ) ) ) );