Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c1e85f
commit 304c8ab
Showing
6 changed files
with
126 additions
and
7 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
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
88 changes: 88 additions & 0 deletions
88
frontend/packages/dev-console/src/components/catalog/providers/useDevfile.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,88 @@ | ||
import * as React from 'react'; | ||
import { TFunction } from 'i18next'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { APIError } from '@console/shared'; | ||
import { ExtensionHook, CatalogItem } from '@console/dynamic-plugin-sdk'; | ||
import { coFetchJSON } from '@console/internal/co-fetch'; | ||
import { ExternalLink } from '@console/internal/components/utils'; | ||
import { DevfileSample } from '../../import/devfile/devfile-types'; | ||
|
||
const normalizeDevfile = (devfileSamples: DevfileSample[], t: TFunction): CatalogItem[] => { | ||
const normalizedDevfileSamples = devfileSamples?.map((sample) => { | ||
const { name: uid, displayName, description, tags, git, icon } = sample; | ||
const gitRepoUrl = Object.values(git.remotes)[0]; | ||
const iconUrl = icon ? `data:image/png;base64,${icon}` : ''; | ||
const href = `/import?importType=devfile&devfileName=${uid}&gitRepo=${gitRepoUrl}`; | ||
const createLabel = t('devconsole~Create Application'); | ||
const type = 'Devfile'; | ||
|
||
const detailsProperties = [ | ||
{ | ||
label: t('devconsole~Sample repository'), | ||
value: ( | ||
<ExternalLink href={gitRepoUrl} additionalClassName="co-break-all" text={gitRepoUrl} /> | ||
), | ||
}, | ||
]; | ||
|
||
const detailsDescriptions = [ | ||
{ | ||
value: <p>{description}</p>, | ||
}, | ||
]; | ||
|
||
const item: CatalogItem = { | ||
uid: `${type}-${uid}`, | ||
type, | ||
name: displayName, | ||
description, | ||
tags, | ||
cta: { | ||
label: createLabel, | ||
href, | ||
}, | ||
icon: { url: iconUrl }, | ||
details: { | ||
properties: detailsProperties, | ||
descriptions: detailsDescriptions, | ||
}, | ||
}; | ||
|
||
return item; | ||
}); | ||
|
||
return normalizedDevfileSamples; | ||
}; | ||
|
||
const useDevfile: ExtensionHook<CatalogItem[]> = (): [CatalogItem[], boolean, any] => { | ||
const [devfileSamples, setDevfileSamples] = React.useState<DevfileSample[]>([]); | ||
const [loadedError, setLoadedError] = React.useState<APIError>(); | ||
const { t } = useTranslation(); | ||
|
||
React.useEffect(() => { | ||
let mounted = true; | ||
const payload = { | ||
registry: 'pkg/devfile/sample-placeholder.json', | ||
}; | ||
coFetchJSON | ||
.put('/api/devfile/samples', payload) | ||
.then((resp) => { | ||
if (mounted) setDevfileSamples(resp); | ||
}) | ||
.catch((err) => { | ||
if (mounted) setLoadedError(err); | ||
}); | ||
return () => (mounted = false); | ||
}, []); | ||
|
||
const normalizedDevfileSamples = React.useMemo(() => normalizeDevfile(devfileSamples, t), [ | ||
devfileSamples, | ||
t, | ||
]); | ||
|
||
const loaded = !!devfileSamples; | ||
|
||
return [normalizedDevfileSamples, loaded, loadedError]; | ||
}; | ||
|
||
export default useDevfile; |
3 changes: 0 additions & 3 deletions
3
frontend/packages/dev-console/src/components/catalog/providers/useDevfileSamples.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
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