[8.11] [Fleet] Fix fetching package detail info after package policy creation (#169275) (#169448)

# Backport

This will backport the following commits from `main` to `8.11`:
- [[Fleet] Fix fetching package detail info after package policy
creation (#169275)](https://github.com/elastic/kibana/pull/169275)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Nicolas
Chaulet","email":"nicolas.chaulet@elastic.co"},"sourceCommit":{"committedDate":"2023-10-19T20:06:17Z","message":"[Fleet]
Fix fetching package detail info after package policy creation
(#169275)","sha":"36776af50f884d00168605bfae51877d7430b21e","branchLabelMapping":{"^v8.12.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:Fleet","ci:no-auto-commit","backport:prev-minor","v8.12.0"],"number":169275,"url":"https://github.com/elastic/kibana/pull/169275","mergeCommit":{"message":"[Fleet]
Fix fetching package detail info after package policy creation
(#169275)","sha":"36776af50f884d00168605bfae51877d7430b21e"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v8.12.0","labelRegex":"^v8.12.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/169275","number":169275,"mergeCommit":{"message":"[Fleet]
Fix fetching package detail info after package policy creation
(#169275)","sha":"36776af50f884d00168605bfae51877d7430b21e"}}]}]
BACKPORT-->

Co-authored-by: Nicolas Chaulet <nicolas.chaulet@elastic.co>
This commit is contained in:
Kibana Machine 2023-10-19 17:40:20 -04:00 committed by GitHub
parent 50cd9defab
commit 05769d55c7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 10 deletions

View file

@ -102,6 +102,9 @@ describe('when on integration detail', () => {
describe('and the package is not installed and prerelease enabled', () => {
beforeEach(async () => {
mockedApi.responseProvider.getSettings.mockReturnValue({
item: { prerelease_integrations_enabled: true, id: '', fleet_server_hosts: [] },
});
mockGAAndPrereleaseVersions('1.0.0-beta');
await render();
await act(() => mockedApi.waitForApi());
@ -165,6 +168,9 @@ describe('when on integration detail', () => {
describe('and a custom UI extension is NOT registered', () => {
beforeEach(async () => {
mockedApi.responseProvider.getSettings.mockReturnValue({
item: { prerelease_integrations_enabled: false, id: '', fleet_server_hosts: [] },
});
await render();
await act(() => mockedApi.waitForApi());
// All those waitForApi call are needed to avoid flakyness because details conditionnaly refetch multiple time
@ -201,6 +207,9 @@ describe('when on integration detail', () => {
beforeEach(async () => {
let setWasRendered: () => void;
mockedApi.responseProvider.getSettings.mockReturnValue({
item: { prerelease_integrations_enabled: false, id: '', fleet_server_hosts: [] },
});
lazyComponentWasRendered = new Promise((resolve) => {
setWasRendered = resolve;
});
@ -268,6 +277,12 @@ describe('when on integration detail', () => {
});
await render();
await act(() => mockedApi.waitForApi());
// All those waitForApi call are needed to avoid flakyness because details conditionnaly refetch multiple time
await act(() => mockedApi.waitForApi());
await act(() => mockedApi.waitForApi());
await act(() => mockedApi.waitForApi());
});
afterEach(() => {
@ -275,7 +290,7 @@ describe('when on integration detail', () => {
lazyComponentWasRendered = undefined;
});
it('should display "assets" tab in navigation', () => {
it('should display "assets" tab in navigation', async () => {
expect(renderResult.getByTestId('tab-assets'));
});
@ -293,6 +308,11 @@ describe('when on integration detail', () => {
describe('and the Add integration button is clicked', () => {
beforeEach(async () => {
await render();
await act(() => mockedApi.waitForApi());
// All those waitForApi call are needed to avoid flakyness because details conditionnaly refetch multiple time
await act(() => mockedApi.waitForApi());
await act(() => mockedApi.waitForApi());
await act(() => mockedApi.waitForApi());
});
it('should link to the create page', () => {

View file

@ -166,7 +166,6 @@ export function Detail() {
}
return getPackageInstallStatus(packageInfo?.name)?.status;
}, [packageInfo, getPackageInstallStatus]);
const isInstalled = useMemo(
() =>
packageInstallStatus === InstallStatus.installed ||
@ -184,7 +183,7 @@ export function Detail() {
boolean | undefined
>();
const { data: settings } = useGetSettingsQuery();
const { data: settings, isInitialLoading: isSettingsInitialLoading } = useGetSettingsQuery();
useEffect(() => {
const isEnabled = Boolean(settings?.item.prerelease_integrations_enabled) || prerelease;
@ -197,10 +196,19 @@ export function Detail() {
data: packageInfoData,
error: packageInfoError,
isLoading: packageInfoLoading,
isFetchedAfterMount: packageInfoIsFetchedAfterMount,
refetch: refetchPackageInfo,
} = useGetPackageInfoByKeyQuery(pkgName, pkgVersion, {
prerelease: prereleaseIntegrationsEnabled,
});
} = useGetPackageInfoByKeyQuery(
pkgName,
pkgVersion,
{
prerelease: prereleaseIntegrationsEnabled,
},
{
enabled: !isSettingsInitialLoading, // Load only after settings are loaded
refetchOnMount: 'always',
}
);
const [latestGAVersion, setLatestGAVersion] = useState<string | undefined>();
const [latestPrereleaseVersion, setLatestPrereleaseVersion] = useState<string | undefined>();
@ -244,14 +252,18 @@ export function Detail() {
}
}, [packageInstallStatus, oldPackageInstallStatus, refetchPackageInfo]);
const isLoading = packageInfoLoading || isPermissionCheckLoading || firstTimeUserLoading;
const isLoading =
packageInfoLoading ||
isPermissionCheckLoading ||
firstTimeUserLoading ||
!packageInfoIsFetchedAfterMount;
const showCustomTab =
useUIExtension(packageInfoData?.item?.name ?? '', 'package-detail-custom') !== undefined;
// Track install status state
useEffect(() => {
if (packageInfoData?.item) {
if (packageInfoIsFetchedAfterMount && packageInfoData?.item) {
const packageInfoResponse = packageInfoData.item;
setPackageInfo(packageInfoResponse);
@ -265,7 +277,7 @@ export function Detail() {
setPackageInstallStatus({ name, status, version: installedVersion || null });
}
}
}, [packageInfoData, setPackageInstallStatus, setPackageInfo]);
}, [packageInfoData, packageInfoIsFetchedAfterMount, setPackageInstallStatus, setPackageInfo]);
const integrationInfo = useMemo(
() =>

View file

@ -120,6 +120,7 @@ export const useGetPackageInfoByKeyQuery = (
queryOptions: {
// If enabled is false, the query will not be fetched
enabled?: boolean;
refetchOnMount?: boolean | 'always';
} = {
enabled: true,
}
@ -141,7 +142,7 @@ export const useGetPackageInfoByKeyQuery = (
...(ignoreUnverifiedQueryParam && { ignoreUnverified: ignoreUnverifiedQueryParam }),
},
}),
{ enabled: queryOptions.enabled }
{ enabled: queryOptions.enabled, refetchOnMount: queryOptions.refetchOnMount }
);
const confirm = async () => {