-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* EPM detail page now uses same icon as list page. * Moved hook from ./components to ./hooks * Add optional `tryApi` param to `<PackageIcon>` Trusts given values by default but can opt-in to side-effects like calling API. Prevents trying API when we know none are present (api explicitly returns none). This also fixes the issue in master where the API was called several times for each item in the datasources list.
- Loading branch information
John Schulz
authored
Apr 8, 2020
1 parent
0a7c421
commit 2ae38b7
Showing
7 changed files
with
94 additions
and
78 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
71 changes: 71 additions & 0 deletions
71
.../plugins/ingest_manager/public/applications/ingest_manager/hooks/use_package_icon_type.ts
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,71 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { useEffect, useState } from 'react'; | ||
import { ICON_TYPES } from '@elastic/eui'; | ||
import { PackageInfo, PackageListItem } from '../../../../common/types/models'; | ||
import { useLinks } from '../sections/epm/hooks'; | ||
import { sendGetPackageInfoByKey } from './index'; | ||
|
||
type Package = PackageInfo | PackageListItem; | ||
|
||
export interface UsePackageIconType { | ||
packageName: Package['name']; | ||
version: Package['version']; | ||
icons?: Package['icons']; | ||
tryApi?: boolean; // should it call API to try to find missing icons? | ||
} | ||
|
||
const CACHED_ICONS = new Map<string, string>(); | ||
|
||
export const usePackageIconType = ({ | ||
packageName, | ||
version, | ||
icons: paramIcons, | ||
tryApi = false, | ||
}: UsePackageIconType) => { | ||
const { toImage } = useLinks(); | ||
const [iconList, setIconList] = useState<UsePackageIconType['icons']>(); | ||
const [iconType, setIconType] = useState<string>(''); // FIXME: use `empty` icon during initialization - see: https://github.com/elastic/kibana/issues/60622 | ||
const pkgKey = `${packageName}-${version}`; | ||
|
||
// Generates an icon path or Eui Icon name based on an icon list from the package | ||
// or by using the package name against logo icons from Eui | ||
useEffect(() => { | ||
if (CACHED_ICONS.has(pkgKey)) { | ||
setIconType(CACHED_ICONS.get(pkgKey) || ''); | ||
return; | ||
} | ||
const svgIcons = (paramIcons || iconList)?.filter(iconDef => iconDef.type === 'image/svg+xml'); | ||
const localIconSrc = Array.isArray(svgIcons) && svgIcons[0]?.src; | ||
if (localIconSrc) { | ||
CACHED_ICONS.set(pkgKey, toImage(localIconSrc)); | ||
setIconType(CACHED_ICONS.get(pkgKey) || ''); | ||
return; | ||
} | ||
|
||
const euiLogoIcon = ICON_TYPES.find(key => key.toLowerCase() === `logo${packageName}`); | ||
if (euiLogoIcon) { | ||
CACHED_ICONS.set(pkgKey, euiLogoIcon); | ||
setIconType(euiLogoIcon); | ||
return; | ||
} | ||
|
||
if (tryApi && !paramIcons && !iconList) { | ||
sendGetPackageInfoByKey(pkgKey) | ||
.catch(error => undefined) // Ignore API errors | ||
.then(res => { | ||
CACHED_ICONS.delete(pkgKey); | ||
setIconList(res?.data?.response?.icons); | ||
}); | ||
} | ||
|
||
CACHED_ICONS.set(pkgKey, 'package'); | ||
setIconType('package'); | ||
}, [paramIcons, pkgKey, toImage, iconList, packageName, iconType, tryApi]); | ||
|
||
return iconType; | ||
}; |
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
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