generated from openshift/console-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathuseArtifactHubTasksProvider.tsx
78 lines (71 loc) · 2.45 KB
/
useArtifactHubTasksProvider.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import * as React from 'react';
import { ARTIFACTHUB, useGetArtifactHubTasks } from '../apis/artifactHub';
import { TektonHubTask } from '../apis/tektonHub';
import {
CatalogItem,
ExtensionHook,
ResourceIcon,
useAccessReview,
} from '@openshift-console/dynamic-plugin-sdk';
import { TaskProviders } from '../../task-quicksearch/pipeline-quicksearch-utils';
import { TaskModel } from '../../../models';
import { getReferenceForModel } from '../../pipelines-overview/utils';
import { useTektonHubIntegration } from '../catalog-utils';
import { t } from '../../utils/common-utils';
import { ArtifactHubTask } from '../../../types';
const normalizeArtifactHubTasks = (
artifactHubTasks: ArtifactHubTask[],
): CatalogItem<any>[] => {
const normalizedArtifactHubTasks: CatalogItem<ArtifactHubTask>[] =
artifactHubTasks.reduce((acc, task) => {
// eslint-disable-next-line @typescript-eslint/naming-convention
const { package_id, name, description } = task;
const provider = TaskProviders.artifactHub;
const normalizedArtifactHubTask: CatalogItem<any> = {
uid: package_id.toString(),
type: TaskProviders.community,
name,
description,
provider,
icon: {
node: <ResourceIcon kind={getReferenceForModel(TaskModel)} />,
},
attributes: { installed: '' },
cta: {
label: t('Add'),
},
data: {
task,
source: ARTIFACTHUB,
},
};
acc.push(normalizedArtifactHubTask);
return acc;
}, []);
return normalizedArtifactHubTasks;
};
const useArtifactHubTasksProvider: ExtensionHook<CatalogItem[]> = ({
namespace,
}): [CatalogItem[], boolean, string] => {
const artifactHubIntegration = useTektonHubIntegration();
const canCreateTask = useAccessReview({
group: TaskModel.apiGroup,
resource: TaskModel.plural,
namespace,
verb: 'create',
});
const canUpdateTask = useAccessReview({
group: TaskModel.apiGroup,
resource: TaskModel.plural,
namespace,
verb: 'update',
});
const [artifactHubTasks, tasksLoaded, tasksError] = useGetArtifactHubTasks(
canCreateTask && canUpdateTask && artifactHubIntegration,
);
const normalizedArtifactHubTasks = React.useMemo<
CatalogItem<TektonHubTask>[]
>(() => normalizeArtifactHubTasks(artifactHubTasks), [artifactHubTasks]);
return [normalizedArtifactHubTasks, tasksLoaded, tasksError];
};
export default useArtifactHubTasksProvider;