File size: 6,431 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 |
import {
FEATURE_UNLIMITED_STORAGE,
planHasFeature,
isBusinessPlan,
isEcommercePlan,
isWooExpressMediumPlan,
isProPlan,
} from '@automattic/calypso-products';
import { Tooltip } from '@automattic/components';
import { Site } from '@automattic/data-stores';
import clsx from 'clsx';
import { useTranslate } from 'i18n-calypso';
import { PropsWithChildren, useRef, useState } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { isPlansPageUntangled } from 'calypso/lib/plans/untangling-plans-experiment';
import { useStorageAddOnAvailable } from 'calypso/lib/plans/use-storage-add-on-available';
import { useStorageLimitOverride } from 'calypso/lib/plans/use-storage-limit-override';
import { recordTracksEvent } from 'calypso/state/analytics/actions';
import { canCurrentUser } from 'calypso/state/selectors/can-current-user';
import hasWpcomStagingSite from 'calypso/state/selectors/has-wpcom-staging-site';
import isAtomicSite from 'calypso/state/selectors/is-site-automated-transfer';
import isSiteWpcomStaging from 'calypso/state/selectors/is-site-wpcom-staging';
import { getSitePlanSlug, getSiteSlug, isJetpackSite } from 'calypso/state/sites/selectors';
import PlanStorageBar from './bar';
import './style.scss';
export function useDisplayUpgradeLink( siteId: number | null ) {
const isStagingSite = useSelector( ( state ) => isSiteWpcomStaging( state, siteId ) );
const sitePlanSlug = useSelector( ( state ) => getSitePlanSlug( state, siteId ) ) ?? '';
const canUserUpgrade = useSelector( ( state ) =>
canCurrentUser( state, siteId, 'manage_options' )
);
const planHasTopStorageSpace =
isBusinessPlan( sitePlanSlug ) ||
isEcommercePlan( sitePlanSlug ) ||
isProPlan( sitePlanSlug ) ||
isWooExpressMediumPlan( sitePlanSlug );
const isUntangled = useSelector( isPlansPageUntangled );
const isStorageAddOnAvailable = useStorageAddOnAvailable( siteId );
/**
* In the untangled plans experiment, users can purchase storage add-ons which is available for all plans from the modal.
* We don't want to show the upgrade link in this case.
*/
if ( isUntangled && isStorageAddOnAvailable ) {
return false;
}
return canUserUpgrade && ! planHasTopStorageSpace && ! isStagingSite;
}
type StorageBarProps = PropsWithChildren< any >;
type PlanStorageProps = PropsWithChildren< {
className?: string;
hideWhenNoStorage?: boolean;
siteId: number | null;
storageBarComponent?: React.ComponentType< StorageBarProps > | React.FC< StorageBarProps >;
} >;
export default function PlanStorage( {
children,
className,
hideWhenNoStorage = false,
siteId,
storageBarComponent: StorageBarComponent = PlanStorageBar,
}: PlanStorageProps ) {
const jetpackSite = useSelector( ( state ) => isJetpackSite( state, siteId ) );
const atomicSite = useSelector( ( state ) => isAtomicSite( state, siteId ) );
const isStagingSite = useSelector( ( state ) => isSiteWpcomStaging( state, siteId ) );
const hasStagingSite = useSelector( ( state ) => hasWpcomStagingSite( state, siteId ) );
const sitePlanSlug = useSelector( ( state ) => getSitePlanSlug( state, siteId ) );
const siteSlug = useSelector( ( state ) => getSiteSlug( state, siteId ) );
const canViewBar = useSelector( ( state ) => canCurrentUser( state, siteId, 'publish_posts' ) );
const translate = useTranslate();
const { data: mediaStorage } = Site.useSiteMediaStorage( { siteIdOrSlug: siteId } );
const [ isTooltipVisible, setTooltipVisible ] = useState( false );
const displayUpgradeLink = useDisplayUpgradeLink( siteId );
const tooltipAnchorRef = useRef( null );
const dispatch = useDispatch();
const maxStorageBytesOverride = useStorageLimitOverride( {
currentStorageBytes: mediaStorage?.maxStorageBytes,
siteId,
} );
if ( mediaStorage && maxStorageBytesOverride ) {
mediaStorage.maxStorageBytes = maxStorageBytesOverride;
}
if ( ( jetpackSite && ! atomicSite ) || ! canViewBar || ! sitePlanSlug ) {
return null;
}
if ( planHasFeature( sitePlanSlug, FEATURE_UNLIMITED_STORAGE ) ) {
return null;
}
const isSharedQuota = isStagingSite || hasStagingSite;
const hasMediaStorage = !! mediaStorage && mediaStorage.maxStorageBytes !== -1;
if ( hideWhenNoStorage && ! hasMediaStorage ) {
return null;
}
const planStorageComponents = (
<StorageBarComponent
sitePlanSlug={ sitePlanSlug }
mediaStorage={ mediaStorage }
displayUpgradeLink={ displayUpgradeLink }
>
{ children }
</StorageBarComponent>
);
const showTooltip = () => {
setTooltipVisible( true );
};
const hideTooltip = (
event:
| React.MouseEvent< HTMLDivElement | HTMLAnchorElement >
| React.FocusEvent< HTMLDivElement | HTMLAnchorElement >
) => {
const relatedTarget = event.relatedTarget;
// This checks if there is a blur event caused by the displaying of the tooltip.
// We don't want to move focus in this case, so return the focus to the target element.
if (
event.type === 'blur' &&
relatedTarget instanceof HTMLElement &&
relatedTarget.closest( '.popover.tooltip.is-top' )
) {
event.stopPropagation();
event.currentTarget.focus();
return;
}
setTooltipVisible( false );
};
if ( displayUpgradeLink ) {
return (
<>
<a
className={ clsx( className, 'plan-storage' ) }
href={ `/plans/${ siteSlug }` }
ref={ tooltipAnchorRef }
onMouseOver={ showTooltip }
onMouseLeave={ hideTooltip }
onFocus={ showTooltip }
onBlur={ hideTooltip }
onClick={ () => {
dispatch( recordTracksEvent( 'calypso_hosting_overview_need_more_storage_click' ) );
} }
>
{ planStorageComponents }
</a>
<Tooltip context={ tooltipAnchorRef.current } isVisible={ isTooltipVisible }>
{ translate( 'Upgrade your plan to increase your storage space.' ) }
</Tooltip>
</>
);
}
if ( isSharedQuota ) {
return (
<>
<div
className={ clsx( className, 'plan-storage plan-storage__shared_quota' ) }
ref={ tooltipAnchorRef }
onMouseOver={ showTooltip }
onMouseLeave={ hideTooltip }
onFocus={ showTooltip }
onBlur={ hideTooltip }
>
{ planStorageComponents }
</div>
<Tooltip context={ tooltipAnchorRef.current } isVisible={ isTooltipVisible }>
{ translate( 'Storage quota is shared between production and staging.' ) }
</Tooltip>
</>
);
}
return <div className={ clsx( className, 'plan-storage' ) }>{ planStorageComponents }</div>;
}
|