Back

0% Statements 0/149
0% Branches 0/1
0% Functions 0/1
0% Lines 0/149

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                                                                                                                                                                           
import { __ } from '@wordpress/i18n';
import { useState } from 'react';
import { useSiteLaunchGatingVariant } from '../../../common/hooks';
import { getLaunchSiteUrl } from '../../../common/launch-site-url';
import PreLaunchSiteModal from '../../../common/pre-launch-site-modal';
import { shouldShowPreLaunchModal } from '../../../common/should-show-pre-launch-modal';
import { wpcomTrackEvent } from '../../../common/tracks';
import SitePreviewLink from '../site-preview-link';
import type { SitePreviewLinkObject } from '../site-preview-link';

interface Props {
	blogId: number;
	homeUrl: string;
	siteTitle: string;
	isUnlaunchedSite: boolean;
	hasSitePreviewLink: boolean;
	sitePreviewLink?: SitePreviewLinkObject;
	sitePreviewLinkNonce: string;
	blogPublic: number;
	wpcomComingSoon: number;
	wpcomPublicComingSoon: number;
	siteDomain: string;
	sitePlan?: { product_slug: string; product_name?: string };
	hasCustomDomain: boolean;
	usePublishCopy: boolean;
}

const LaunchSite = ( {
	homeUrl,
	siteTitle,
	isUnlaunchedSite,
	hasSitePreviewLink,
	sitePreviewLink,
	sitePreviewLinkNonce,
	blogPublic,
	wpcomComingSoon,
	wpcomPublicComingSoon,
	siteDomain,
	sitePlan,
	hasCustomDomain,
	usePublishCopy,
}: Props ) => {
	const [ , variant ] = useSiteLaunchGatingVariant();
	const [ showPreLaunchModal, setShowPreLaunchModal ] = useState( false );

	const qualifiesForPreLaunch = shouldShowPreLaunchModal( { sitePlan, hasCustomDomain } );

	// isPrivateAndUnlaunched means it is an unlaunched coming soon v1 site
	const isPrivateAndUnlaunched = -1 === blogPublic && isUnlaunchedSite;
	const isAnyComingSoonEnabled =
		( 0 === blogPublic && wpcomPublicComingSoon ) || isPrivateAndUnlaunched || wpcomComingSoon;

	const launchUrl = getLaunchSiteUrl( new URL( homeUrl ).host, window.location.href, {
		source: 'options-reading.php',
		new: siteTitle,
		search: 'yes',
		ref: 'wp-admin/options-reading.php',
	} );

	const showPreviewLink = isAnyComingSoonEnabled && hasSitePreviewLink;

	// Separate __() calls: terser folds `cond ? __( a ) : __( b )` into `__( cond ? a : b )`,
	// which i18n-check-webpack-plugin rejects.
	const launchDescriptions = {
		comingSoon: __(
			'Your site hasn\'t been launched yet. It is hidden from visitors behind a "Coming Soon" notice until it is launched.',
			'jetpack-mu-wpcom'
		),
		private: __(
			"Your site hasn't been launched yet. It's private; only you can see it until it is launched.",
			'jetpack-mu-wpcom'
		),
	};
	const publishDescriptions = {
		comingSoon: __(
			'Your site hasn\'t been published yet. It is hidden from visitors behind a "Coming Soon" notice until it is published.',
			'jetpack-mu-wpcom'
		),
		private: __(
			"Your site hasn't been published yet. It's private; only you can see it until it is published.",
			'jetpack-mu-wpcom'
		),
	};
	const descriptions = usePublishCopy ? publishDescriptions : launchDescriptions;
	const launchLabel = __( 'Launch site', 'jetpack-mu-wpcom' );
	const publishLabel = __( 'Publish site', 'jetpack-mu-wpcom' );

	const handleLaunchClick = () => {
		wpcomTrackEvent( 'wpcom_settings_reading_launch_site_button_click' );

		// Site launch gating: 'semi_gated_site_launch' is the shipped default. The other
		// branches are scaffolding for future experiments; see useSiteLaunchGatingVariant.
		switch ( variant ) {
			case 'semi_gated_site_launch':
			case null:
			default:
				// Qualifying sites confirm via the pre-launch modal; everyone else
				// goes straight to the launch flow, preserving today's behavior.
				if ( qualifiesForPreLaunch ) {
					wpcomTrackEvent( 'wpcom_launch_site_pre_launch_modal_shown' );
					setShowPreLaunchModal( true );
					return;
				}
				window.location.href = launchUrl;
		}
	};

	return (
		<>
			<p>{ isAnyComingSoonEnabled ? descriptions.comingSoon : descriptions.private }</p>
			<button
				className="button is-secondary"
				type="button"
				style={ { marginTop: '0.5em' } }
				onClick={ handleLaunchClick }
			>
				{ usePublishCopy ? publishLabel : launchLabel }
			</button>
			{ showPreviewLink && (
				<SitePreviewLink
					homeUrl={ homeUrl }
					sitePreviewLink={ sitePreviewLink }
					sitePreviewLinkNonce={ sitePreviewLinkNonce }
					description={
						<>
							{ __(
								'"Coming soon" sites are only visible to you and invited users.',
								'jetpack-mu-wpcom'
							) }
							&nbsp;
						</>
					}
				/>
			) }
			{ showPreLaunchModal && (
				<PreLaunchSiteModal
					siteName={ siteTitle }
					siteDomain={ siteDomain }
					homeUrl={ homeUrl }
					planName={ sitePlan?.product_name || __( 'Paid plan', 'jetpack-mu-wpcom' ) }
					launchUrl={ launchUrl }
					onClose={ () => setShowPreLaunchModal( false ) }
				/>
			) }
		</>
	);
};

export default LaunchSite;