-
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.
Moved hook from ./components to ./hooks
- Loading branch information
John Schulz
committed
Mar 31, 2020
1 parent
7eeacc9
commit 0a0a319
Showing
4 changed files
with
73 additions
and
62 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
68 changes: 68 additions & 0 deletions
68
.../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,68 @@ | ||
/* | ||
* 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, useMemo, useState } from 'react'; | ||
import { ICON_TYPES } from '@elastic/eui'; | ||
import { PackageInfo, PackageListItem } from '../../../../common/types/models'; | ||
import { useLinks } from '../sections/epm/hooks'; | ||
|
||
type Package = PackageInfo | PackageListItem; | ||
|
||
export interface UsePackageIconType { | ||
packageName: Package['name']; | ||
version?: Package['version']; | ||
icons?: Package['icons']; | ||
} | ||
|
||
const CACHED_ICONS = new Map<string, string>(); | ||
|
||
export const usePackageIconType = ({ | ||
packageName = '', | ||
version = '', | ||
icons = [], | ||
}: UsePackageIconType) => { | ||
const { toImage } = useLinks(); | ||
const [iconType, setIconType] = useState<string>(''); // FIXME: use `empty` icon during initialization - see: https://github.com/elastic/kibana/issues/60622 | ||
const pkgKey = `${packageName}-${version}`; | ||
const defaultType = 'package'; | ||
|
||
// 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 | ||
const fromInput = useMemo(() => { | ||
return (iconList?: Package['icons']) => { | ||
const svgIcons = 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) as string); | ||
return; | ||
} | ||
|
||
const euiLogoIcon = ICON_TYPES.find(key => key.toLowerCase() === `logo${packageName}`); | ||
if (euiLogoIcon) { | ||
CACHED_ICONS.set(pkgKey, euiLogoIcon); | ||
setIconType(euiLogoIcon); | ||
return; | ||
} | ||
|
||
CACHED_ICONS.set(pkgKey, defaultType); | ||
setIconType(defaultType); | ||
}; | ||
}, [packageName, pkgKey, toImage]); | ||
|
||
useEffect(() => { | ||
if (CACHED_ICONS.has(pkgKey)) { | ||
setIconType(CACHED_ICONS.get(pkgKey) || ''); | ||
return; | ||
} | ||
|
||
if (icons) { | ||
fromInput(icons); | ||
} | ||
}, [icons, toImage, packageName, version, fromInput, pkgKey, iconType]); | ||
|
||
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