Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 15 additions & 43 deletions apps/content-insights/src/hooks/useInstallationParameters.ts
Original file line number Diff line number Diff line change
@@ -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<KeyValueMap>(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<KeyValueMap> => {
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;
};
17 changes: 1 addition & 16 deletions apps/content-insights/test/mocks/mockCma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
Loading