File size: 5,995 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
import { LoadingPlaceholder, Badge } from '@automattic/components';
import { useTranslate } from 'i18n-calypso';
import { useState } from 'react';
import { useSelector } from 'react-redux';
import QuerySitePlans from 'calypso/components/data/query-site-plans';
import { HostingCard, HostingCardLinkButton } from 'calypso/components/hosting-card';
import { isPlansPageUntangled } from 'calypso/lib/plans/untangling-plans-experiment';
import { isPartnerPurchase, purchaseType } from 'calypso/lib/purchases';
import { getManagePurchaseUrlFor } from 'calypso/my-sites/purchases/paths';
import SitePreviewModal from 'calypso/sites-dashboard/components/site-preview-modal';
import { isStagingSite } from 'calypso/sites-dashboard/utils';
import { recordTracksEvent } from 'calypso/state/analytics/actions';
import { isA4AUser } from 'calypso/state/partner-portal/partner/selectors';
import getCurrentPlanPurchaseId from 'calypso/state/selectors/get-current-plan-purchase-id';
import { isJetpackSite } from 'calypso/state/sites/selectors';
import { getSelectedPurchase, getSelectedSite } from 'calypso/state/ui/selectors';
import { AppState } from 'calypso/types';
import ManageAddOnsButton from '../../components/add-ons/manage-add-ons-button';
import PlanPricing from '../../components/plan-pricing';
import PlanStats from '../../components/plan-stats';
import { LaunchIcon, ShareLinkIcon } from './icons';
import { Action } from './quick-actions-card';

import './style.scss';

const DevelopmentSiteActions = () => {
	const translate = useTranslate();
	const site = useSelector( getSelectedSite );

	const [ isSitePreviewModalVisible, setSitePreviewModalVisible ] = useState( false );
	const openSitePreviewModal = () => setSitePreviewModalVisible( true );
	const closeSitePreviewModal = () => setSitePreviewModalVisible( false );

	return (
		<>
			<div className="hosting-overview__development-site-ctas">
				<ul className="hosting-overview__actions-list">
					<Action
						icon={ <ShareLinkIcon /> }
						onClick={ openSitePreviewModal }
						text={ translate( 'Share a preview link with clients' ) }
					/>
					<Action
						icon={ <LaunchIcon /> }
						href={ `/sites/settings/site/${ site?.slug }` }
						text={ translate( 'Prepare for launch' ) }
					/>
				</ul>
			</div>

			<SitePreviewModal
				siteUrl={ site?.URL ?? '' }
				siteId={ site?.ID ?? 0 }
				isVisible={ isSitePreviewModalVisible }
				closeModal={ closeSitePreviewModal }
			/>
		</>
	);
};

const PlanCard = () => {
	const translate = useTranslate();
	const site = useSelector( getSelectedSite );
	const planDetails = site?.plan;
	const isFreePlan = planDetails?.is_free;
	const isJetpack = useSelector( ( state ) =>
		isJetpackSite( state, site?.ID, { treatAtomicAsJetpackSite: false } )
	);
	const isStaging = isStagingSite( site ?? undefined );
	const isOwner = planDetails?.user_is_owner;
	const planPurchaseId = useSelector( ( state: AppState ) =>
		getCurrentPlanPurchaseId( state, site?.ID ?? 0 )
	);
	const planPurchase = useSelector( getSelectedPurchase );
	const isAgencyPurchase = planPurchase && isPartnerPurchase( planPurchase );
	const isDevelopmentSite = Boolean( site?.is_a4a_dev_site );
	const isA4A = useSelector( isA4AUser );

	// Show that this is an Agency Managed plan for agency purchases.
	const planName = isAgencyPurchase
		? purchaseType( planPurchase )
		: planDetails?.product_name_short ?? '';
	const planPurchaseLoading = ! isFreePlan && planPurchase === null;
	const isLoading = ! planDetails || planPurchaseLoading;

	const plansPageIsUntangled = useSelector( isPlansPageUntangled );

	const renderManageButton = () => {
		if ( isJetpack || ! site || isStaging || isAgencyPurchase || isDevelopmentSite ) {
			return false;
		}
		if ( isFreePlan ) {
			if ( plansPageIsUntangled ) {
				return (
					<ManageAddOnsButton tracksEventName="calypso_hosting_overview_manage_add_ons_button_click" />
				);
			}
			return (
				<HostingCardLinkButton to={ `/add-ons/${ site?.slug }` } hideOnMobile>
					{ translate( 'Manage add-ons' ) }
				</HostingCardLinkButton>
			);
		}
		if ( isOwner ) {
			return (
				<HostingCardLinkButton
					to={ getManagePurchaseUrlFor( site?.slug, planPurchaseId ?? 0 ) }
					hideOnMobile
				>
					{ translate( 'Manage plan' ) }
				</HostingCardLinkButton>
			);
		}
	};

	return (
		<>
			<QuerySitePlans siteId={ site?.ID } />
			<HostingCard className="hosting-overview__plan">
				<div className="hosting-overview__plan-card-header">
					{ isLoading ? (
						<LoadingPlaceholder width="100px" height="16px" />
					) : (
						<>
							<h3 className="hosting-overview__plan-card-title">
								{ isStaging ? translate( 'Staging site' ) : planName }
							</h3>
							{ isDevelopmentSite && (
								<Badge className="hosting-overview__development-site-badge" type="info-purple">
									{ translate( 'Development' ) }
								</Badge>
							) }
							{ renderManageButton() }
						</>
					) }
				</div>

				{ isAgencyPurchase && (
					<>
						<div className="hosting-overview__plan-agency-purchase">
							<p>
								{ translate( 'This site is managed through {{a}}Automattic for Agencies{{/a}}.', {
									components: {
										a: isA4A ? (
											<a
												href={ `https://agencies.automattic.com/sites/overview/${ site?.slug }` }
												onClick={ () => {
													recordTracksEvent( 'calypso_overview_agency_managed_site_click' );
												} }
											></a>
										) : (
											<strong></strong>
										),
									},
								} ) }
							</p>
						</div>

						{
							/* Enclosing the following check within isAgencyPurchase helps eliminate some layout shifts during load time. */
							isDevelopmentSite && <DevelopmentSiteActions />
						}
					</>
				) }
				{ ! isAgencyPurchase && ! isStaging && <PlanPricing /> }
				<PlanStats needMoreStorageTracksEventName="calypso_hosting_overview_need_more_storage_click" />
			</HostingCard>
		</>
	);
};

export default PlanCard;