diff --git a/apps/content-insights/src/hooks/useInstallationParameters.ts b/apps/content-insights/src/hooks/useInstallationParameters.ts index 07ec5a913d..11e92a1622 100644 --- a/apps/content-insights/src/hooks/useInstallationParameters.ts +++ b/apps/content-insights/src/hooks/useInstallationParameters.ts @@ -1,53 +1,25 @@ -import { useEffect, useState } from 'react'; import { BaseAppSDK } from '@contentful/app-sdk'; -import { AppInstallationProps, KeyValueMap } from 'contentful-management'; import { AppInstallationParameters } from '../locations/ConfigScreen'; +/** + * Returns installation parameters directly from the SDK. + * + * Previously this hook made an expensive org-scoped CMA call + * (`appInstallation.getForOrganization`) on every mount to re-fetch params. + * That is unnecessary because `sdk.parameters.installation` already contains + * the current installation parameters, and the extra call was contributing to + * CMA rate-limit pressure (see INC-1276). + * + * The return shape `{ installation, refetchInstallationParameters }` is + * preserved for backward compatibility; `refetchInstallationParameters` is now + * a no-op. + */ export const useInstallationParameters = (sdk: BaseAppSDK) => { - const [parameters, setParameters] = useState(sdk.parameters.installation); - - useEffect(() => { - const fetch = async () => { - const newParameters = await fetchParameters(sdk); - setParameters(newParameters); - }; - fetch(); - }, [sdk]); + const installation = (sdk.parameters.installation ?? {}) as AppInstallationParameters; const refetchInstallationParameters = async () => { - const newParameters = await fetchParameters(sdk); - setParameters(newParameters); + // No-op: parameters are read directly from the SDK at render time. }; - const installation = (parameters ?? {}) as AppInstallationParameters; - return { installation, refetchInstallationParameters }; }; - -const fetchParameters = async (sdk: BaseAppSDK): Promise => { - try { - if (!sdk.ids.organization || !sdk.ids.app || !sdk.ids.space || !sdk.ids.environment) { - throw new Error('Required SDK IDs not available'); - } - - const appInstallation = await sdk.cma.appInstallation.getForOrganization({ - appDefinitionId: sdk.ids.app, - organizationId: sdk.ids.organization, - }); - - const currentInstallation = appInstallation.items.find( - (installation: AppInstallationProps) => - installation.sys.space.sys.id === sdk.ids.space && - installation.sys.environment.sys.id === sdk.ids.environment - ); - - if (currentInstallation?.parameters) { - return currentInstallation.parameters as KeyValueMap; - } - return sdk.parameters.installation; - } catch (error) { - console.warn('Failed to fetch fresh parameters from CMA:', error); - } - - return sdk.parameters.installation; -}; diff --git a/apps/content-insights/test/mocks/mockCma.ts b/apps/content-insights/test/mocks/mockCma.ts index 4741d9d7b3..6173be4123 100644 --- a/apps/content-insights/test/mocks/mockCma.ts +++ b/apps/content-insights/test/mocks/mockCma.ts @@ -15,22 +15,7 @@ const mockCma: any = { entry: { getMany: vi.fn(), }, - appInstallation: { - getForOrganization: vi.fn().mockResolvedValue({ - items: [ - { - sys: { - space: { - sys: { - id: 'test-space', - }, - }, - }, - parameters: {}, - }, - ], - }), - }, + appInstallation: {}, user: { getForSpace: vi.fn().mockResolvedValue({ sys: {