Skip to content

Commit

Permalink
Change isOverLimit and requireUpgrade evaluation for upgrade prompts (#…
Browse files Browse the repository at this point in the history
…34287)

* depends on diff

* fix a couple of default values

* change requireUpgrade reducer, suspend action while we work on it

* use new isOverLimit - requireUpgrade definitions

* do not call setAiAssistantFeature, this should be derived on the reducer from isOverLimit and tier situation

* changelog

* Handle the require upgrade action since it's used in at least one place

* Fix tests

---------

Co-authored-by: Luiz Kowalski <lhkowalski@gmail.com>
  • Loading branch information
CGastrell and lhkowalski authored Nov 27, 2023
1 parent 81af7db commit 8b7e069
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 46 deletions.
24 changes: 2 additions & 22 deletions projects/plugins/jetpack/_inc/lib/class-jetpack-ai-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,36 +348,16 @@ public static function get_ai_assistance_feature() {
}
}

$is_over_limit = WPCOM\Jetpack_AI\Usage\Helper::is_over_limit( $blog_id );

/**
* Check if the site requires an upgrade.
*
* The site will require an upgrade when it's
* over the limit of requests, be it the free
* allowance of the current tier allowance.
*
* Previously, we were checking if the site
* does not have the AI Assistant feature,
* meaning we only checked the free limit.
*
* With tiered plans, we need to check if the
* site is over the limit even when it has the
* feature, and this is now handled by Helper::is_over_limit.
* site-require-upgrade/$require_upgrade remains for backward compatibility.
*/
$require_upgrade = $is_over_limit;

// Determine the upgrade type
$upgrade_type = wpcom_is_vip( $blog_id ) ? 'vip' : 'default';

return array(
'has-feature' => $has_ai_assistant_feature,
'is-over-limit' => $is_over_limit,
'is-over-limit' => WPCOM\Jetpack_AI\Usage\Helper::is_over_limit( $blog_id ),
'requests-count' => WPCOM\Jetpack_AI\Usage\Helper::get_all_time_requests_count( $blog_id ),
'requests-limit' => WPCOM\Jetpack_AI\Usage\Helper::get_free_requests_limit( $blog_id ),
'usage-period' => WPCOM\Jetpack_AI\Usage\Helper::get_period_data( $blog_id ),
'site-require-upgrade' => $require_upgrade,
'site-require-upgrade' => WPCOM\Jetpack_AI\Usage\Helper::site_requires_upgrade( $blog_id ),
'upgrade-type' => $upgrade_type,
'current-tier' => WPCOM\Jetpack_AI\Usage\Helper::get_current_tier( $blog_id ),
'next-tier' => WPCOM\Jetpack_AI\Usage\Helper::get_next_tier( $blog_id ),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: other

Refactor isOverLimit and how to reduce requireUpgrade
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export default function AIAssistantEdit( { attributes, setAttributes, clientId,
};
}, [] );

const { requireUpgrade, increaseRequestsCount } = useAiFeature();
const { isOverLimit, requireUpgrade, increaseRequestsCount } = useAiFeature();

const focusOnPrompt = () => {
/*
Expand Down Expand Up @@ -505,7 +505,7 @@ export default function AIAssistantEdit( { attributes, setAttributes, clientId,
</InspectorControls>
) }

{ requireUpgrade && isSelected && <UpgradePrompt /> }
{ isOverLimit && isSelected && <UpgradePrompt /> }
{ ! connected && <ConnectPrompt /> }
{ ! isWaitingState && connected && ! requireUpgrade && (
<ToolbarControls
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,6 @@ const useSuggestionsFromOpenAI = ( {
status: 'info',
} );

// Dispatch the action to set the feature as requiring an upgrade.
setAiAssistantFeatureRequireUpgrade( true );

return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ ${ postContent }
reset();
}

const { requireUpgrade: isQuotaExceeded } = useAiFeature();
const { requireUpgrade, isOverLimit } = useAiFeature();

// Set the docs link depending on the site type
const docsLink =
Expand Down Expand Up @@ -219,7 +219,7 @@ ${ postContent }
</Notice>
) }

{ isQuotaExceeded && <UpgradePrompt /> }
{ isOverLimit && <UpgradePrompt /> }

<AiExcerptControl
words={ excerptWordsNumber }
Expand All @@ -242,7 +242,7 @@ ${ postContent }
setModel( newModel );
setReenable( true );
} }
disabled={ isBusy || isQuotaExceeded }
disabled={ isBusy || requireUpgrade }
/>

<BaseControl
Expand All @@ -255,22 +255,22 @@ ${ postContent }
onClick={ discardExcerpt }
variant="secondary"
isDestructive
disabled={ requestingState !== 'done' || isQuotaExceeded }
disabled={ requestingState !== 'done' || requireUpgrade }
>
{ __( 'Discard', 'jetpack' ) }
</Button>
<Button
onClick={ setExcerpt }
variant="secondary"
disabled={ requestingState !== 'done' || isQuotaExceeded }
disabled={ requestingState !== 'done' || requireUpgrade }
>
{ __( 'Accept', 'jetpack' ) }
</Button>
<Button
onClick={ requestExcerpt }
variant="secondary"
isBusy={ isBusy }
disabled={ isGenerateButtonDisabled || isQuotaExceeded || ! postContent }
disabled={ isGenerateButtonDisabled || requireUpgrade || ! postContent }
>
{ __( 'Generate', 'jetpack' ) }
</Button>
Expand Down
23 changes: 12 additions & 11 deletions projects/plugins/jetpack/extensions/store/wordpress-com/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* Types & Constants
*/
import { __ } from '@wordpress/i18n';
import {
ACTION_DECREASE_NEW_ASYNC_REQUEST_COUNTDOWN,
ACTION_ENQUEUE_ASYNC_REQUEST,
Expand Down Expand Up @@ -35,16 +34,11 @@ const INITIAL_STATE: PlanStateProps = {
limit: 20,
},
usagePeriod: {
currentStart: 'ai-assistant-tier-free',
currentStart: '',
nextStart: '',
requestsCount: 0,
},
nextTier: {
slug: 'ai-assistant-tier-unlimited',
value: 1,
limit: UNLIMITED_PLAN_REQUESTS_LIMIT,
readableLimit: __( 'Unlimited', 'jetpack' ),
},
nextTier: null,
tierPlansEnabled: false,
_meta: {
isRequesting: false,
Expand Down Expand Up @@ -136,7 +130,9 @@ export default function reducer( state = INITIAL_STATE, action ) {
* @see _inc/lib/class-jetpack-ai-helper.php
*/
const isOverLimit = currentCount >= requestsLimit;
const requireUpgrade = isOverLimit;

// highest tier holds a soft limit so requireUpgrade is false on that case (nextTier null means highest tier)
const requireUpgrade = isOverLimit && state.features.aiAssistant.nextTier !== null;

return {
...state,
Expand Down Expand Up @@ -186,15 +182,20 @@ export default function reducer( state = INITIAL_STATE, action ) {
}

case ACTION_SET_AI_ASSISTANT_FEATURE_REQUIRE_UPGRADE: {
/*
* If we require an upgrade, we are also over the limit;
* The opposite is not true, we can be over the limit without
* requiring an upgrade, for example when we are on the highest tier.
* In this case, we don't want to set isOverLimit to false.
*/
return {
...state,
features: {
...state.features,
aiAssistant: {
...state.features.aiAssistant,
requireUpgrade: action.requireUpgrade,
hasFeature: ! action.requireUpgrade, // If we require an upgrade, we don't have the feature.
isOverLimit: true, // If we require an upgrade, we are over the limit.
...( action.requireUpgrade ? { isOverLimit: true } : {} ),
},
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,17 @@ describe( 'reducer', () => {
requestsLimit: FREE_PLAN_REQUESTS_LIMIT,
requireUpgrade: false,
upgradeType: 'default',
currentTier: null,
nextTier: null,
currentTier: {
slug: 'ai-assistant-tier-free',
value: 0,
limit: 20,
},
nextTier: {
// the next tier now is important, so we need to set some on the testing data
slug: 'ai-assistant-tier-100',
value: 100,
limit: 100,
},
usagePeriod: {
currentStart: 'ai-assistant-tier-free',
nextStart: '',
Expand Down

0 comments on commit 8b7e069

Please sign in to comment.