From de675f8043c2662dcc180c6ff745335e5b47a2a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Su=C3=A1rez?= Date: Tue, 7 Nov 2023 18:42:52 +0000 Subject: [PATCH] AI Assistant: map the AI Assistant feature data (#33993) * map response data * introduce actions file * rename AIFeatureProps with AiFeatureProps * changelog --- ...update-plan-map-ai-assistant-feature-props | 4 ++ .../hooks/use-ai-feature/index.ts | 6 +- .../components/usage-bar/types.ts | 4 +- .../extensions/store/wordpress-com/actions.ts | 30 ++++++++++ .../extensions/store/wordpress-com/index.ts | 60 +++++++++++-------- .../extensions/store/wordpress-com/types.ts | 4 +- 6 files changed, 77 insertions(+), 31 deletions(-) create mode 100644 projects/plugins/jetpack/changelog/update-plan-map-ai-assistant-feature-props create mode 100644 projects/plugins/jetpack/extensions/store/wordpress-com/actions.ts diff --git a/projects/plugins/jetpack/changelog/update-plan-map-ai-assistant-feature-props b/projects/plugins/jetpack/changelog/update-plan-map-ai-assistant-feature-props new file mode 100644 index 0000000000000..866a83d8c8102 --- /dev/null +++ b/projects/plugins/jetpack/changelog/update-plan-map-ai-assistant-feature-props @@ -0,0 +1,4 @@ +Significance: patch +Type: enhancement + +AI Assistant: map the AI Assistant feature data diff --git a/projects/plugins/jetpack/extensions/blocks/ai-assistant/hooks/use-ai-feature/index.ts b/projects/plugins/jetpack/extensions/blocks/ai-assistant/hooks/use-ai-feature/index.ts index d46c15922df44..0ffc176487fcd 100644 --- a/projects/plugins/jetpack/extensions/blocks/ai-assistant/hooks/use-ai-feature/index.ts +++ b/projects/plugins/jetpack/extensions/blocks/ai-assistant/hooks/use-ai-feature/index.ts @@ -6,7 +6,7 @@ import { useEffect, useState } from '@wordpress/element'; /** * Types & constants */ -import { AIFeatureProps } from '../../../../store/wordpress-com/types'; +import { AiFeatureProps } from '../../../../store/wordpress-com/types'; import type { SiteAIAssistantFeatureEndpointResponseProps } from '../../../../types'; const NUM_FREE_REQUESTS_LIMIT = 20; @@ -32,7 +32,7 @@ export const AI_Assistant_Initial_State = { }, }; -export async function getAIFeatures(): Promise< AIFeatureProps > { +export async function getAIFeatures(): Promise< AiFeatureProps > { const response: SiteAIAssistantFeatureEndpointResponseProps = await apiFetch( { path: '/wpcom/v2/jetpack-ai/ai-assistant-feature', } ); @@ -58,7 +58,7 @@ export async function getAIFeatures(): Promise< AIFeatureProps > { } export default function useAIFeature() { - const [ data, setData ] = useState< AIFeatureProps >( AI_Assistant_Initial_State ); + const [ data, setData ] = useState< AiFeatureProps >( AI_Assistant_Initial_State ); const [ loading, setLoading ] = useState< boolean >( false ); const [ error, setError ] = useState< Error >( null ); diff --git a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/usage-bar/types.ts b/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/usage-bar/types.ts index 6121bdb419cbe..8d4b335eba320 100644 --- a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/usage-bar/types.ts +++ b/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/usage-bar/types.ts @@ -1,7 +1,7 @@ /** * Types */ -import { AIFeatureProps } from '../../../../blocks/ai-assistant/hooks/use-ai-feature'; +import { AiFeatureProps } from '../../../../blocks/ai-assistant/hooks/use-ai-feature'; export type UsageBarProps = { /** @@ -15,6 +15,6 @@ export type UsageBarProps = { limitReached: boolean; }; export type UsageControlProps = Pick< - AIFeatureProps, + AiFeatureProps, 'isOverLimit' | 'hasFeature' | 'requestsCount' | 'requestsLimit' >; diff --git a/projects/plugins/jetpack/extensions/store/wordpress-com/actions.ts b/projects/plugins/jetpack/extensions/store/wordpress-com/actions.ts new file mode 100644 index 0000000000000..dc1519a162e28 --- /dev/null +++ b/projects/plugins/jetpack/extensions/store/wordpress-com/actions.ts @@ -0,0 +1,30 @@ +/** + * Types + */ +import type { Plan } from './types'; +import type { AiFeatureProps } from './types'; + +const actions = { + setPlans( plans: Array< Plan > ) { + return { + type: 'SET_PLANS', + plans, + }; + }, + + fetchFromAPI( url: string ) { + return { + type: 'FETCH_FROM_API', + url, + }; + }, + + storeAiAssistantFeature( feature: AiFeatureProps ) { + return { + type: 'STORE_AI_ASSISTANT_FEATURE', + feature, + }; + }, +}; + +export default actions; diff --git a/projects/plugins/jetpack/extensions/store/wordpress-com/index.ts b/projects/plugins/jetpack/extensions/store/wordpress-com/index.ts index 9a3c74de29b2d..96cdd3d321b54 100644 --- a/projects/plugins/jetpack/extensions/store/wordpress-com/index.ts +++ b/projects/plugins/jetpack/extensions/store/wordpress-com/index.ts @@ -6,7 +6,11 @@ import { createReduxStore, register } from '@wordpress/data'; /** * Internal dependencies */ -import type { AIFeatureProps, Plan, PlanStateProps } from '../../store/wordpress-com/types'; +import actions from './actions'; +/** + * Types + */ +import type { AiFeatureProps, PlanStateProps } from './types'; import type { SiteAIAssistantFeatureEndpointResponseProps } from '../../types'; const store = 'wordpress-com/plans'; @@ -16,28 +20,34 @@ const INITIAL_STATE: PlanStateProps = { features: {}, }; -const actions = { - setPlans( plans: Array< Plan > ) { - return { - type: 'SET_PLANS', - plans, - }; - }, - - fetchFromAPI( url: string ) { - return { - type: 'FETCH_FROM_API', - url, - }; - }, - - storeAiAssistantFeature( feature: AIFeatureProps ) { - return { - type: 'STORE_AI_ASSISTANT_FEATURE', - feature, - }; - }, -}; +/** + * Map the response from the `sites/$site/ai-assistant-feature` + * endpoint to the AI Assistant feature props. + * @param { SiteAIAssistantFeatureEndpointResponseProps } response - The response from the endpoint. + * @returns { AiFeatureProps } The AI Assistant feature props. + */ +function mapAIFeatureResponseToAiFeatureProps( + response: SiteAIAssistantFeatureEndpointResponseProps +): AiFeatureProps { + return { + hasFeature: !! response[ 'has-feature' ], + isOverLimit: !! response[ 'is-over-limit' ], + requestsCount: response[ 'requests-count' ], + requestsLimit: response[ 'requests-limit' ], + requireUpgrade: !! response[ 'site-require-upgrade' ], + errorMessage: response[ 'error-message' ], + errorCode: response[ 'error-code' ], + upgradeType: response[ 'upgrade-type' ], + usagePeriod: { + currentStart: response[ 'usage-period' ]?.[ 'current-start' ], + nextStart: response[ 'usage-period' ]?.[ 'next-start' ], + requestsCount: response[ 'usage-period' ]?.[ 'requests-count' ] || 0, + }, + currentTier: { + value: response[ 'current-tier' ]?.value || 1, + }, + }; +} const wordpressPlansStore = createReduxStore( store, { __experimentalUseThunks: true, @@ -115,7 +125,9 @@ const wordpressPlansStore = createReduxStore( store, { } ); // Store the feature in the store. - dispatch( actions.storeAiAssistantFeature( response ) ); + dispatch( + actions.storeAiAssistantFeature( mapAIFeatureResponseToAiFeatureProps( response ) ) + ); }, }, } ); diff --git a/projects/plugins/jetpack/extensions/store/wordpress-com/types.ts b/projects/plugins/jetpack/extensions/store/wordpress-com/types.ts index 1ffb4c62c695b..37d871d912fac 100644 --- a/projects/plugins/jetpack/extensions/store/wordpress-com/types.ts +++ b/projects/plugins/jetpack/extensions/store/wordpress-com/types.ts @@ -7,14 +7,14 @@ export type Plan = { export type PlanStateProps = { plans: Array< Plan >; features: { - aiAssistant?: AIFeatureProps; + aiAssistant?: AiFeatureProps; }; }; // AI Assistant feature props export type UpgradeTypeProp = 'vip' | 'default'; export type TierValueProp = 1 | 20 | 100 | 200 | 500; -export type AIFeatureProps = { +export type AiFeatureProps = { hasFeature: boolean; isOverLimit: boolean; requestsCount: number;