-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathindex.tsx
More file actions
206 lines (185 loc) · 6.78 KB
/
Copy pathindex.tsx
File metadata and controls
206 lines (185 loc) · 6.78 KB
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
205
206
import { recordTracksEvent } from '@automattic/calypso-analytics';
import page from '@automattic/calypso-router';
import clsx from 'clsx';
import { useTranslate } from 'i18n-calypso';
import { useCallback, useState } from 'react';
import { LayoutWithGuidedTour as Layout } from 'calypso/a8c-for-agencies/components/layout/layout-with-guided-tour';
import LayoutTop from 'calypso/a8c-for-agencies/components/layout/layout-with-payment-notification';
import MobileSidebarNavigation from 'calypso/a8c-for-agencies/components/sidebar/mobile-sidebar-navigation';
import { A4A_SITES_LINK } from 'calypso/a8c-for-agencies/components/sidebar-menu/lib/constants';
import SiteConfigurationsModal from 'calypso/a8c-for-agencies/components/site-configurations-modal';
import { useRandomSiteName } from 'calypso/a8c-for-agencies/components/site-configurations-modal/use-random-site-name';
import useCreateWPCOMSiteMutation from 'calypso/a8c-for-agencies/data/sites/use-create-wpcom-site';
import useFetchPendingSites from 'calypso/a8c-for-agencies/data/sites/use-fetch-pending-sites';
import useSiteCreatedCallback from 'calypso/a8c-for-agencies/hooks/use-site-created-callback';
import useTrackProvisioningSites from 'calypso/a8c-for-agencies/hooks/use-track-provisioning-sites';
import useClearCartOnCheckoutSuccess from 'calypso/a8c-for-agencies/sections/marketplace/hooks/use-clear-cart-on-checkout-success';
import LayoutColumn from 'calypso/layout/hosting-dashboard/column';
import LayoutHeader, {
LayoutHeaderTitle as Title,
LayoutHeaderActions as Actions,
} from 'calypso/layout/hosting-dashboard/header';
import SitesHeaderActions from '../sites-header-actions';
import ClientSite from './client-site';
import { AvailablePlans } from './plan-field';
import PurchaseConfirmationMessage from './purchase-confirmation-message';
import NeedSetupTable from './table';
import type { ReferralAPIResponse } from '../../referrals/types';
type Props = {
licenseKey?: string;
};
type NeedsSetupSite = {
features: {
wpcom_atomic: {
license_key: string;
state: string;
referral: ReferralAPIResponse;
};
};
id: number;
};
export default function NeedSetup( { licenseKey }: Props ) {
const { randomSiteName, isRandomSiteNameLoading, refetchRandomSiteName } = useRandomSiteName();
const translate = useTranslate();
const [ currentSiteConfigurationId, setCurrentSiteConfigurationId ] = useState< number | null >(
null
);
useClearCartOnCheckoutSuccess();
const closeModal = () => setCurrentSiteConfigurationId( null );
const title = translate( 'Sites' );
const { data: pendingSites, isFetching, refetch: refetchPendingSites } = useFetchPendingSites();
const { mutate: createWPCOMSite, isPending: isCreatingSite } = useCreateWPCOMSiteMutation();
const { trackSiteId } = useTrackProvisioningSites();
const allAvailableSites =
pendingSites?.filter(
( { features }: NeedsSetupSite ) =>
features.wpcom_atomic.state === 'pending' && !! features.wpcom_atomic.license_key
) ?? [];
// Filter out sites that have a referral
const availableSites =
allAvailableSites.filter(
( { features }: NeedsSetupSite ) => ! features.wpcom_atomic.referral
) ?? [];
// Find the site license by license key
const foundSiteLicenseByLicenseKey = pendingSites?.find(
( { features }: NeedsSetupSite ) => features.wpcom_atomic.license_key === licenseKey
);
const hasReferral = !! foundSiteLicenseByLicenseKey?.features.wpcom_atomic.referral;
// Filter out the site license we found by license key
const otherReferralSites = allAvailableSites.filter(
( { features }: NeedsSetupSite ) =>
!! features.wpcom_atomic.referral &&
( hasReferral ? features.wpcom_atomic.license_key !== licenseKey : true )
);
const availablePlans: AvailablePlans[] = [
// If the site license we found by license key has a referral, we should show it first
...( hasReferral
? [
{
name: translate( 'WordPress.com' ),
available: 1,
subTitle: (
<ClientSite
referral={ foundSiteLicenseByLicenseKey.features.wpcom_atomic.referral }
/>
),
ids: [ foundSiteLicenseByLicenseKey.id ],
},
]
: [] ),
// If there are other referral sites, we should show them next
...( otherReferralSites.length
? otherReferralSites.map( ( { features, id }: NeedsSetupSite ) => ( {
name: translate( 'WordPress.com' ),
available: 1,
subTitle: <ClientSite referral={ features.wpcom_atomic.referral } />,
ids: [ id ],
} ) )
: [] ),
...( availableSites.length
? [
{
name: translate( 'WordPress.com' ),
subTitle: translate( '%(count)d site available', '%(count)d sites available', {
args: {
count: availableSites.length,
},
count: availableSites.length,
comment: 'The `count` is the number of available sites.',
} ),
ids: availableSites.map( ( { id }: { id: number } ) => id ),
},
]
: [] ),
];
const isProvisioning =
isCreatingSite ||
pendingSites?.find(
( { features }: { features: { wpcom_atomic: { state: string; license_key: string } } } ) =>
features.wpcom_atomic.state === 'provisioning' && !! features.wpcom_atomic.license_key
);
const onCreateSiteSuccess = useSiteCreatedCallback( refetchRandomSiteName );
const onCreateSiteWithConfig = useCallback(
( id: number ) => {
recordTracksEvent( 'calypso_a4a_create_site_config' );
setCurrentSiteConfigurationId( id );
},
[ setCurrentSiteConfigurationId ]
);
const onMigrateSite = useCallback(
( id: number ) => {
createWPCOMSite(
{ id },
{
onSuccess: () => {
refetchPendingSites();
trackSiteId( id, { migration: true } );
page( A4A_SITES_LINK );
},
}
);
},
[ createWPCOMSite, refetchPendingSites, trackSiteId ]
);
return (
<Layout
className={ clsx(
'sites-dashboard sites-dashboard__layout is-without-filters preview-hidden',
{
'has-product-referral': !! otherReferralSites.length || hasReferral,
}
) }
wide
title={ title }
>
<LayoutColumn className="sites-overview" wide>
<LayoutTop isFullWidth>
<PurchaseConfirmationMessage />
<LayoutHeader>
<Title>{ translate( 'Sites' ) }</Title>
<Actions>
<MobileSidebarNavigation />
<SitesHeaderActions />
</Actions>
</LayoutHeader>
</LayoutTop>
{ currentSiteConfigurationId && (
<SiteConfigurationsModal
closeModal={ closeModal }
randomSiteName={ randomSiteName }
isRandomSiteNameLoading={ isRandomSiteNameLoading }
siteId={ currentSiteConfigurationId }
onCreateSiteSuccess={ onCreateSiteSuccess }
/>
) }
<NeedSetupTable
availablePlans={ availablePlans }
isLoading={ isFetching }
provisioning={ isProvisioning }
onCreateSite={ onCreateSiteWithConfig }
onMigrateSite={ onMigrateSite }
/>
</LayoutColumn>
</Layout>
);
}