-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[nit][pre-req] Split EPM Home into components (#114431)
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
- Loading branch information
1 parent
7c087fe
commit ab5ecc4
Showing
6 changed files
with
408 additions
and
355 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
212 changes: 212 additions & 0 deletions
212
...s/fleet/public/applications/integrations/sections/epm/screens/home/available_packages.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React, { memo, useMemo } from 'react'; | ||
import { useLocation, useHistory, useParams } from 'react-router-dom'; | ||
import { i18n } from '@kbn/i18n'; | ||
|
||
import { pagePathGetters } from '../../../../constants'; | ||
import { | ||
useGetCategories, | ||
useGetPackages, | ||
useBreadcrumbs, | ||
useGetAppendCustomIntegrations, | ||
useGetReplacementCustomIntegrations, | ||
useLink, | ||
} from '../../../../hooks'; | ||
import { doesPackageHaveIntegrations } from '../../../../services'; | ||
import type { PackageList } from '../../../../types'; | ||
import { PackageListGrid } from '../../components/package_list_grid'; | ||
|
||
import type { CustomIntegration } from '../../../../../../../../../../src/plugins/custom_integrations/common'; | ||
|
||
import type { PackageListItem } from '../../../../types'; | ||
|
||
import type { IntegrationCategory } from '../../../../../../../../../../src/plugins/custom_integrations/common'; | ||
|
||
import { useMergeEprPackagesWithReplacements } from '../../../../../../hooks/use_merge_epr_with_replacements'; | ||
|
||
import { mergeAndReplaceCategoryCounts } from './util'; | ||
import { CategoryFacets } from './category_facets'; | ||
import type { CategoryFacet } from './category_facets'; | ||
|
||
import type { CategoryParams } from '.'; | ||
import { getParams, categoryExists, mapToCard } from '.'; | ||
|
||
// Packages can export multiple integrations, aka `policy_templates` | ||
// In the case where packages ship >1 `policy_templates`, we flatten out the | ||
// list of packages by bringing all integrations to top-level so that | ||
// each integration is displayed as its own tile | ||
const packageListToIntegrationsList = (packages: PackageList): PackageList => { | ||
return packages.reduce((acc: PackageList, pkg) => { | ||
const { policy_templates: policyTemplates = [], ...restOfPackage } = pkg; | ||
return [ | ||
...acc, | ||
restOfPackage, | ||
...(doesPackageHaveIntegrations(pkg) | ||
? policyTemplates.map((integration) => { | ||
const { name, title, description, icons } = integration; | ||
return { | ||
...restOfPackage, | ||
id: `${restOfPackage}-${name}`, | ||
integration: name, | ||
title, | ||
description, | ||
icons: icons || restOfPackage.icons, | ||
}; | ||
}) | ||
: []), | ||
]; | ||
}, []); | ||
}; | ||
|
||
const title = i18n.translate('xpack.fleet.epmList.allTitle', { | ||
defaultMessage: 'Browse by category', | ||
}); | ||
|
||
// TODO: clintandrewhall - this component is hard to test due to the hooks, particularly those that use `http` | ||
// or `location` to load data. Ideally, we'll split this into "connected" and "pure" components. | ||
export const AvailablePackages: React.FC = memo(() => { | ||
useBreadcrumbs('integrations_all'); | ||
|
||
const { selectedCategory, searchParam } = getParams( | ||
useParams<CategoryParams>(), | ||
useLocation().search | ||
); | ||
|
||
const history = useHistory(); | ||
|
||
const { getHref, getAbsolutePath } = useLink(); | ||
|
||
function setSelectedCategory(categoryId: string) { | ||
const url = pagePathGetters.integrations_all({ | ||
category: categoryId, | ||
searchTerm: searchParam, | ||
})[1]; | ||
history.push(url); | ||
} | ||
|
||
function setSearchTerm(search: string) { | ||
// Use .replace so the browser's back button is not tied to single keystroke | ||
history.replace( | ||
pagePathGetters.integrations_all({ category: selectedCategory, searchTerm: search })[1] | ||
); | ||
} | ||
|
||
const { data: allCategoryPackagesRes, isLoading: isLoadingAllPackages } = useGetPackages({ | ||
category: '', | ||
}); | ||
|
||
const { data: categoryPackagesRes, isLoading: isLoadingCategoryPackages } = useGetPackages({ | ||
category: selectedCategory, | ||
}); | ||
|
||
const { data: categoriesRes, isLoading: isLoadingCategories } = useGetCategories({ | ||
include_policy_templates: true, | ||
}); | ||
|
||
const eprPackages = useMemo( | ||
() => packageListToIntegrationsList(categoryPackagesRes?.response || []), | ||
[categoryPackagesRes] | ||
); | ||
|
||
const allEprPackages = useMemo( | ||
() => packageListToIntegrationsList(allCategoryPackagesRes?.response || []), | ||
[allCategoryPackagesRes] | ||
); | ||
|
||
const { value: replacementCustomIntegrations } = useGetReplacementCustomIntegrations(); | ||
|
||
const mergedEprPackages: Array<PackageListItem | CustomIntegration> = | ||
useMergeEprPackagesWithReplacements( | ||
eprPackages || [], | ||
replacementCustomIntegrations || [], | ||
selectedCategory as IntegrationCategory | ||
); | ||
|
||
const { loading: isLoadingAppendCustomIntegrations, value: appendCustomIntegrations } = | ||
useGetAppendCustomIntegrations(); | ||
|
||
const filteredAddableIntegrations = appendCustomIntegrations | ||
? appendCustomIntegrations.filter((integration: CustomIntegration) => { | ||
if (!selectedCategory) { | ||
return true; | ||
} | ||
return integration.categories.indexOf(selectedCategory as IntegrationCategory) >= 0; | ||
}) | ||
: []; | ||
|
||
const eprAndCustomPackages: Array<CustomIntegration | PackageListItem> = [ | ||
...mergedEprPackages, | ||
...filteredAddableIntegrations, | ||
]; | ||
|
||
eprAndCustomPackages.sort((a, b) => { | ||
return a.title.localeCompare(b.title); | ||
}); | ||
|
||
const categories = useMemo(() => { | ||
const eprAndCustomCategories: CategoryFacet[] = | ||
isLoadingCategories || | ||
isLoadingAppendCustomIntegrations || | ||
!appendCustomIntegrations || | ||
!categoriesRes | ||
? [] | ||
: mergeAndReplaceCategoryCounts( | ||
categoriesRes.response as CategoryFacet[], | ||
appendCustomIntegrations | ||
); | ||
|
||
return [ | ||
{ | ||
id: '', | ||
count: (allEprPackages?.length || 0) + (appendCustomIntegrations?.length || 0), | ||
}, | ||
...(eprAndCustomCategories ? eprAndCustomCategories : []), | ||
] as CategoryFacet[]; | ||
}, [ | ||
allEprPackages?.length, | ||
appendCustomIntegrations, | ||
categoriesRes, | ||
isLoadingAppendCustomIntegrations, | ||
isLoadingCategories, | ||
]); | ||
|
||
if (!isLoadingCategories && !categoryExists(selectedCategory, categories)) { | ||
history.replace(pagePathGetters.integrations_all({ category: '', searchTerm: searchParam })[1]); | ||
return null; | ||
} | ||
|
||
const controls = categories ? ( | ||
<CategoryFacets | ||
showCounts={false} | ||
isLoading={isLoadingCategories || isLoadingAllPackages || isLoadingAppendCustomIntegrations} | ||
categories={categories} | ||
selectedCategory={selectedCategory} | ||
onCategoryChange={({ id }: CategoryFacet) => { | ||
setSelectedCategory(id); | ||
}} | ||
/> | ||
) : null; | ||
|
||
const cards = eprAndCustomPackages.map((item) => { | ||
return mapToCard(getAbsolutePath, getHref, item); | ||
}); | ||
|
||
return ( | ||
<PackageListGrid | ||
isLoading={isLoadingCategoryPackages} | ||
title={title} | ||
controls={controls} | ||
initialSearch={searchParam} | ||
list={cards} | ||
setSelectedCategory={setSelectedCategory} | ||
onSearchChange={setSearchTerm} | ||
showMissingIntegrationMessage | ||
/> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.