-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[nit][pre-req] Split EPM Home into components #114431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
clintandrewhall
merged 5 commits into
elastic:master
from
clintandrewhall:fleet/pre-req-home-split
Oct 12, 2021
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e2d3671
[nit] Strongly-type Shipper and Category
clintandrewhall aa559c9
[pre-req] Split EPM Home into components
clintandrewhall 2e3fd02
Merge branch 'master' into fleet/pre-req-home-split
kibanamachine 9f605cd
Merge branch 'master' into fleet/pre-req-home-split
clintandrewhall 12e6e6f
Fixing bad merge
clintandrewhall File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.