-
Notifications
You must be signed in to change notification settings - Fork 592
/
hooks.ts
45 lines (36 loc) · 1.56 KB
/
hooks.ts
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
import { merge } from 'lodash';
// FIXME react-redux types are 6.x while react-redux is 7.x
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
import { useSelector } from 'react-redux';
import { KebabAction, Kebab } from '@console/internal/components/utils';
import { K8sResourceCommon } from '@console/internal/module/k8s';
import { PipelineRunKind } from '../../../types';
import { StartedByAnnotation } from '../../pipelines/const';
type AnnotationMap = { [annotationKey: string]: string };
const mergeAnnotationsWithResource = (annotations: AnnotationMap, resource: K8sResourceCommon) => {
return merge({}, resource, { metadata: { annotations }, spec: {} });
};
export const useUserAnnotationForManualStart = (): AnnotationMap => {
const user = useSelector((state) => state.core.user);
if (!user?.metadata?.name) {
return {};
}
return {
[StartedByAnnotation.user]: user.metadata.name,
};
};
export const usePipelineRunWithUserAnnotation = (plr: PipelineRunKind): PipelineRunKind => {
const annotations = useUserAnnotationForManualStart();
return plr && mergeAnnotationsWithResource(annotations, plr);
};
export const useMenuActionsWithUserAnnotation = (menuActions: KebabAction[]): KebabAction[] => {
const annotations = useUserAnnotationForManualStart();
return menuActions.map((kebabAction) => {
if (Object.values(Kebab.factory).includes(kebabAction)) {
return kebabAction;
}
return (kind, resource, ...rest) =>
kebabAction(kind, mergeAnnotationsWithResource(annotations, resource), ...rest);
});
};