Skip to content

Commit

Permalink
remove most uses of tierPlansEnabled, add notes on what seems still f…
Browse files Browse the repository at this point in the history
…uzzy
  • Loading branch information
CGastrell committed Sep 9, 2024
1 parent 1339412 commit f0e83b5
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { useState, useEffect, useCallback, useRef } from 'react';
* Internal dependencies
*/
import {
DEFAULT_LOGO_COST,
EVENT_MODAL_OPEN,
EVENT_FEEDBACK,
EVENT_MODAL_CLOSE,
Expand Down Expand Up @@ -63,14 +62,8 @@ export const GeneratorModal: React.FC< GeneratorModalProps > = ( {
const requestedFeatureData = useRef< boolean >( false );
const [ needsFeature, setNeedsFeature ] = useState( false );
const [ needsMoreRequests, setNeedsMoreRequests ] = useState( false );
const {
selectedLogo,
getAiAssistantFeature,
generateFirstPrompt,
generateLogo,
setContext,
tierPlansEnabled,
} = useLogoGenerator();
const { selectedLogo, getAiAssistantFeature, generateFirstPrompt, generateLogo, setContext } =
useLogoGenerator();
const { featureFetchError, firstLogoPromptFetchError, clearErrors } = useRequestErrors();
const siteId = siteDetails?.ID;
const [ logoAccepted, setLogoAccepted ] = useState( false );
Expand Down Expand Up @@ -105,21 +98,15 @@ export const GeneratorModal: React.FC< GeneratorModalProps > = ( {
try {
const hasHistory = ! isLogoHistoryEmpty( String( siteId ) );

const logoCost = feature?.costs?.[ 'jetpack-ai-logo-generator' ]?.logo ?? DEFAULT_LOGO_COST;
const promptCreationCost = 1;
const currentLimit = feature?.currentTier?.value || 0;
const currentUsage = feature?.usagePeriod?.requestsCount || 0;
const isUnlimited = ! tierPlansEnabled ? currentLimit > 0 : currentLimit === 1;
const currentLimit = feature?.currentTier?.value || 0; // ? shouldn't we set this to free requests limit?
const currentUsage =
currentLimit === 0 ? feature?.requestsCount : feature?.usagePeriod?.requestsCount || 0;
const isUnlimited = currentLimit > 0;
const hasNoNextTier = ! feature?.nextTier; // If there is no next tier, the user cannot upgrade.

// The user needs an upgrade immediately if they have no logos and not enough requests remaining for one prompt and one logo generation.
const siteNeedsMoreRequests =
! isUnlimited &&
! hasNoNextTier &&
! hasHistory &&
( tierPlansEnabled
? currentLimit - currentUsage < logoCost + promptCreationCost
: currentLimit < currentUsage );
! isUnlimited && ! hasNoNextTier && ! hasHistory && currentLimit < currentUsage;

// If the site requires an upgrade, show the upgrade screen immediately.
setNeedsFeature( currentLimit === 0 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { useCheckout } from '../hooks/use-checkout.js';
import useLogoGenerator from '../hooks/use-logo-generator.js';
import useRequestErrors from '../hooks/use-request-errors.js';
import { FairUsageNotice } from './fair-usage-notice.js';
import { UpgradeNudge } from './upgrade-nudge.js';
import './prompt.scss';

const debug = debugFactory( 'jetpack-ai-calypso:prompt-box' );
Expand All @@ -45,7 +44,6 @@ export const Prompt: React.FC< { initialPrompt?: string } > = ( { initialPrompt
getAiAssistantFeature,
requireUpgrade,
context,
tierPlansEnabled,
} = useLogoGenerator();

const enhancingLabel = __( 'Enhancing…', 'jetpack-ai-client' );
Expand Down Expand Up @@ -197,8 +195,7 @@ export const Prompt: React.FC< { initialPrompt?: string } > = ( { initialPrompt
</Tooltip>
</div>
) }
{ requireUpgrade && tierPlansEnabled && <UpgradeNudge /> }
{ requireUpgrade && ! tierPlansEnabled && <FairUsageNotice /> }
{ requireUpgrade && <FairUsageNotice /> }
{ enhancePromptFetchError && (
<div className="jetpack-ai-logo-generator__prompt-error">
{ __( 'Error enhancing prompt. Please try again.', 'jetpack-ai-client' ) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ import type { Selectors } from '../store/types.js';
const debug = debugFactory( 'ai-client:logo-generator:use-checkout' );

export const useCheckout = () => {
const { nextTier, tierPlansEnabled } = useSelect( select => {
const { nextTier } = useSelect( select => {
const selectors: Selectors = select( STORE_NAME );
return {
nextTier: selectors.getAiAssistantFeature().nextTier,
tierPlansEnabled: selectors.getAiAssistantFeature().tierPlansEnabled,
};
}, [] );

Expand All @@ -35,10 +34,7 @@ export const useCheckout = () => {
wpcomCheckoutUrl.searchParams.set( 'source', 'jetpack-ai-yearly-tier-upgrade-nudge' );
wpcomCheckoutUrl.searchParams.set( 'site', getSiteFragment() as string );

wpcomCheckoutUrl.searchParams.set(
'path',
tierPlansEnabled ? `jetpack_ai_yearly:-q-${ nextTier?.limit }` : 'jetpack_ai_yearly'
);
wpcomCheckoutUrl.searchParams.set( 'path', 'jetpack_ai_yearly' );

/**
* Open the product interstitial page
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* Types
*/
import { DEFAULT_LOGO_COST } from '../constants.js';
import type { AiFeatureProps, LogoGeneratorStateProp, Logo, RequestError } from './types.js';
import type { SiteDetails } from '../types.js';

Expand Down Expand Up @@ -120,22 +119,7 @@ const selectors = {
* @return {boolean} The requireUpgrade flag.
*/
getRequireUpgrade( state: LogoGeneratorStateProp ): boolean {
const feature = state.features.aiAssistantFeature;

if ( ! feature?.tierPlansEnabled ) {
return feature?.requireUpgrade;
}
const logoCost = feature?.costs?.[ 'jetpack-ai-logo-generator' ]?.logo ?? DEFAULT_LOGO_COST;
const currentLimit = feature?.currentTier?.value || 0;
const currentUsage = feature?.usagePeriod?.requestsCount || 0;
const isUnlimited = currentLimit === 1;
const hasNoNextTier = ! feature?.nextTier; // If there is no next tier, the user cannot upgrade.

// Add a local check on top of the feature flag, based on the current usage and logo cost.
return (
state.features.aiAssistantFeature?.requireUpgrade ||
( ! isUnlimited && ! hasNoNextTier && currentLimit - currentUsage < logoCost )
);
return state.features?.aiAssistantFeature?.requireUpgrade || false;
},

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/*
* External dependencies
*/
import { getRedirectUrl } from '@automattic/jetpack-components';
import { useAnalytics } from '@automattic/jetpack-shared-extension-utils';
import { Notice } from '@wordpress/components';
import { createInterpolateElement, useCallback } from '@wordpress/element';
Expand Down Expand Up @@ -97,7 +96,7 @@ const DefaultUpgradePrompt = ( {

const { checkoutUrl } = useAICheckout();
const canUpgrade = canUserPurchasePlan();
const { nextTier, tierPlansEnabled, currentTier, requestsCount } = useAiFeature();
const { currentTier, requestsCount } = useAiFeature();

const { tracks } = useAnalytics();

Expand All @@ -110,13 +109,6 @@ const DefaultUpgradePrompt = ( {
} );
}, [ currentTier, requestsCount, tracks, placement ] );

const handleContactUsClick = useCallback( () => {
debug( 'contact us', placement );
tracks.recordEvent( 'jetpack_ai_upgrade_contact_us', {
placement: placement,
} );
}, [ tracks, placement ] );

if ( ! canUpgrade ) {
const cantUpgradeDescription = createInterpolateElement(
__(
Expand All @@ -141,64 +133,6 @@ const DefaultUpgradePrompt = ( {
);
}

if ( tierPlansEnabled ) {
if ( ! nextTier ) {
const contactHref = getRedirectUrl( 'jetpack-ai-tiers-more-requests-contact' );
const contactUsDescription = __(
'You have reached the request limit for your current plan.',
'jetpack'
);

return (
<Nudge
buttonText={ __( 'Contact Us', 'jetpack' ) }
description={ description || contactUsDescription }
className={ 'jetpack-ai-upgrade-banner' }
checkoutUrl={ contactHref }
visible={ true }
align={ null }
title={ null }
context={ null }
goToCheckoutPage={ handleContactUsClick }
target="_blank"
/>
);
}

const upgradeDescription = createInterpolateElement(
sprintf(
/* Translators: number of requests */
__(
'You have reached the requests limit for your current plan. <strong>Upgrade now to increase your requests limit to %d.</strong>',
'jetpack'
),
nextTier.limit
),
{
strong: <strong />,
}
);

return (
<Nudge
buttonText={ sprintf(
/* Translators: number of requests */
__( 'Upgrade to %d requests', 'jetpack' ),
nextTier.limit
) }
checkoutUrl={ checkoutUrl }
className={ 'jetpack-ai-upgrade-banner' }
description={ description || upgradeDescription }
goToCheckoutPage={ handleUpgradeClick }
visible={ true }
align={ 'center' }
title={ null }
context={ null }
target="_blank"
/>
);
}

return (
<Nudge
buttonText={ 'Upgrade' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export default function AIAssistantEdit( { attributes, setAttributes, clientId,
requestsLimit,
currentTier,
loading: loadingAiFeature,
tierPlansEnabled,
} = useAiFeature();
const requestsRemaining = Math.max( requestsLimit - requestsCount, 0 );

Expand Down Expand Up @@ -370,8 +369,7 @@ export default function AIAssistantEdit( { attributes, setAttributes, clientId,
{ __( 'Discover all features', 'jetpack' ) }
</ExternalLink>
</div>
{ ( planType === PLAN_TYPE_FREE ||
( tierPlansEnabled && planType !== PLAN_TYPE_UNLIMITED ) ) && (
{ planType === PLAN_TYPE_FREE && (
<PanelBody initialOpen={ true }>
<PanelRow>
<UsagePanel placement={ USAGE_PANEL_PLACEMENT_BLOCK_SETTINGS_SIDEBAR } />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ export default function AiAssistantPluginSidebar() {
tracks.recordEvent( 'jetpack_ai_panel_open', { placement } );
};

const showUsagePanel =
planType === PLAN_TYPE_FREE || ( tierPlansEnabled && planType !== PLAN_TYPE_UNLIMITED );
const showUsagePanel = planType === PLAN_TYPE_FREE;
const showFairUsageNotice = planType === PLAN_TYPE_UNLIMITED && isOverLimit;
const isBreveAvailable = getBreveAvailability();

Expand Down Expand Up @@ -237,6 +236,8 @@ export default function AiAssistantPluginSidebar() {
busy={ false }
disabled={ requireUpgrade }
/>

{ /* check this for removal, it will never be true */ }
{ requireUpgrade && tierPlansEnabled && (
<Upgrade
placement={ PLACEMENT_PRE_PUBLISH }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ export default function reducer( state = INITIAL_STATE, action ) {
let requestsLimit = state.features.aiAssistant.currentTier?.limit;

if ( isUnlimitedTierPlan ) {
// since the introduction of the unlimited plan limit, we could take it from there instead
// of a fixed constant
requestsLimit = UNLIMITED_PLAN_REQUESTS_LIMIT;
} else if ( isFreeTierPlan ) {
requestsLimit = state.features.aiAssistant.requestsLimit as TierLimitProp;
Expand All @@ -132,6 +134,7 @@ export default function reducer( state = INITIAL_STATE, action ) {
const isOverLimit = currentCount >= requestsLimit;

// highest tier holds a soft limit so requireUpgrade is false on that case (nextTier null means highest tier)
// we shouldn't need this anymore, use requireUpgrade as it comes from backend
const requireUpgrade = isOverLimit && state.features.aiAssistant.nextTier !== null;

return {
Expand Down

0 comments on commit f0e83b5

Please sign in to comment.