Back

100% Statements 20/20
72.34% Branches 34/47
100% Functions 3/3
100% Lines 19/19

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 151 152 153 154 155 156                                                          2x 4x 4x 4x 4x     4x 4x   4x   4x 36x 4x   4x 24x 4x                       4x                                                                                     4x   4x 1x     3x                                                                                              
import { __ } from '@wordpress/i18n';
import { dashboard, shield, trendingUp } from '@wordpress/icons';
import clsx from 'clsx';
import { PRODUCT_STATUSES } from '../../../constants';
import { PRODUCT_SLUGS } from '../../../data/constants';
import { useAllProducts } from '../../../data/products/use-all-products';
import useProduct from '../../../data/products/use-product';
import { getMyJetpackWindowInitialState } from '../../../data/utils/get-my-jetpack-window-state';
import useFilteredProducts from '../../../hooks/use-filtered-products';
import LoadingBlock from '../../loading-block';
import BackupOverviewItem from '../backup-card/overview';
import BoostOverviewItem from '../boost-card/overview';
import ProductOverviewItem from '../overview-item/product-item';
import OverviewSection from '../overview-section';
import ProtectOverviewItem from '../protect-card/overview';
import StatsOverviewItem from '../stats-card/overview';
import StatsPeriodControl from '../stats-card/period-control';
import { useStatsPeriod } from '../stats-card/use-stats-period';
import VideoPressOverviewItem from '../videopress-card/overview';
import styles from './style.module.scss';
import type { FC } from 'react';
 
/**
 * The Overview sections.
 *
 * @param {object} props           - Component props.
 * @param {string} props.className - Added once the sections have loaded.
 * @return {import('react').JSX.Element} The sections.
 */
const OverviewSections: FC< { className?: string } > = ( { className = '' } ) => {
	const { filteredOwnedProducts: slugs, isLoading } = useFilteredProducts();
	const { showFullJetpackStatsCard = false } = getMyJetpackWindowInitialState( 'myJetpackFlags' );
	const { userIsAdmin = false, canUserViewStats = false } = getMyJetpackWindowInitialState();
	const admin = userIsAdmin === '1';
	// showFullJetpackStatsCard means Jetpack or the Stats plugin is active.
	const showStats =
		canUserViewStats && ( showFullJetpackStatsCard || slugs.includes( PRODUCT_SLUGS.STATS ) );
	const { detail: statsDetail } = useProduct( PRODUCT_SLUGS.STATS );
	const isStatsActive =
		statsDetail?.status === PRODUCT_STATUSES.ACTIVE ||
		statsDetail?.status === PRODUCT_STATUSES.CAN_UPGRADE;
	const statsPeriod = useStatsPeriod();
	const owns = ( slug: JetpackModule ) => slugs.includes( slug );
	const { data: products } = useAllProducts();
	// A row that is only the account Connect action is left out: the header's connection popover offers it.
	const showsStatus = ( slug: JetpackModule ) =>
		owns( slug ) && products?.[ slug ]?.status !== PRODUCT_STATUSES.USER_CONNECTION_ERROR;
	const protectedItems = [
		owns( PRODUCT_SLUGS.ANTI_SPAM ) && (
			<ProductOverviewItem
				key="anti-spam"
				slug={ PRODUCT_SLUGS.ANTI_SPAM }
				admin={ admin }
				actionLabel={ __( 'More insights', 'jetpack-my-jetpack' ) }
			/>
		),
		owns( PRODUCT_SLUGS.BACKUP ) && <BackupOverviewItem key="backup" admin={ admin } />,
		owns( PRODUCT_SLUGS.PROTECT ) && <ProtectOverviewItem key="protect" admin={ admin } />,
	].filter( Boolean );
	const statusItems = [
		showsStatus( PRODUCT_SLUGS.BOOST ) && <BoostOverviewItem key="boost" admin={ admin } />,
		showsStatus( PRODUCT_SLUGS.JETPACK_AI ) && (
			<ProductOverviewItem
				key="jetpack-ai"
				slug={ PRODUCT_SLUGS.JETPACK_AI }
				admin={ admin }
				// The product name, 'AI', also heads its card; Features and the menu say Jetpack AI.
				name={ __( 'Jetpack AI', 'jetpack-my-jetpack' ) }
				actionLabel={ __( 'More about AI', 'jetpack-my-jetpack' ) }
				upgradeLabel={ __( 'Increase usage', 'jetpack-my-jetpack' ) }
			/>
		),
		showsStatus( PRODUCT_SLUGS.SEARCH ) && (
			<ProductOverviewItem
				key="search"
				slug={ PRODUCT_SLUGS.SEARCH }
				admin={ admin }
				actionLabel={ __( 'More about Search', 'jetpack-my-jetpack' ) }
			/>
		),
		showsStatus( PRODUCT_SLUGS.VIDEOPRESS ) && (
			<VideoPressOverviewItem key="videopress" admin={ admin } />
		),
		showsStatus( PRODUCT_SLUGS.CRM ) && (
			<ProductOverviewItem
				key="crm"
				slug={ PRODUCT_SLUGS.CRM }
				admin={ admin }
				actionLabel={ __( 'Manage CRM', 'jetpack-my-jetpack' ) }
			/>
		),
		showsStatus( PRODUCT_SLUGS.SOCIAL ) && (
			<ProductOverviewItem
				key="social"
				slug={ PRODUCT_SLUGS.SOCIAL }
				admin={ admin }
				actionLabel={ __( 'Manage Social', 'jetpack-my-jetpack' ) }
			/>
		),
	].filter( Boolean );
 
	// A section whose partner is hidden takes the whole row.
	const paired = protectedItems.length > 0 && statusItems.length > 0;
 
	if ( ! isLoading && ! showStats && ! protectedItems.length && ! statusItems.length ) {
		return null;
	}
 
	return (
		<div className={ clsx( styles.sections, { [ className ]: ! isLoading } ) }>
			{ isLoading ? (
				<>
					<div className={ styles.wide }>
						<LoadingBlock width="100%" height="350px" />
					</div>
					<LoadingBlock width="100%" height="150px" />
					<LoadingBlock width="100%" height="150px" />
				</>
			) : (
				<>
					{ showStats && (
						<OverviewSection
							icon={ trendingUp }
							title={ __( 'How is your site growing?', 'jetpack-my-jetpack' ) }
							headerEnd={ isStatsActive && <StatsPeriodControl period={ statsPeriod } /> }
							className={ styles.wide }
						>
							<StatsOverviewItem admin={ admin } period={ statsPeriod } />
						</OverviewSection>
					) }
					{ protectedItems.length > 0 && (
						<OverviewSection
							icon={ shield }
							title={ __( 'How well is my site protected?', 'jetpack-my-jetpack' ) }
							className={ clsx( { [ styles.wide ]: ! paired } ) }
						>
							{ protectedItems }
						</OverviewSection>
					) }
					{ statusItems.length > 0 && (
						<OverviewSection
							icon={ dashboard }
							title={ __( 'What is my site status?', 'jetpack-my-jetpack' ) }
							className={ clsx( { [ styles.wide ]: ! paired } ) }
						>
							{ statusItems }
						</OverviewSection>
					) }
				</>
			) }
		</div>
	);
};
 
export default OverviewSections;