This repository was archived by the owner on Jun 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathDefinition.tsx
More file actions
84 lines (75 loc) · 2.41 KB
/
Definition.tsx
File metadata and controls
84 lines (75 loc) · 2.41 KB
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
79
80
81
82
83
84
import * as CircleCI from '@circleci/circleci-config-sdk';
import { Generable } from '@circleci/circleci-config-sdk/dist/src/lib/Components';
import { AnyParameterLiteral } from '@circleci/circleci-config-sdk/dist/src/lib/Components/Parameters/types/CustomParameterLiterals.types';
import {
OrbImport,
OrbRef,
} from '@circleci/circleci-config-sdk/dist/src/lib/Orb';
import InspectableMapping from '../../../mappings/InspectableMapping';
import { useStoreActions } from '../../state/Hooks';
import { InspectorDefinitionMenuNav } from '../menus/definitions/InspectorDefinitionMenu';
export const flattenGenerable = (data: Generable, nested?: boolean) => {
// this generated object should always have a single key
const generated = data.generate();
if (typeof generated === 'string') {
return { name: generated };
}
/**
* Flattens the keys of the input.
* Will nest under parameters if nested option is set
* For nested example, see WorkflowStage
*/
return Object.entries(generated as Record<string, any>).map(([key, value]) =>
nested
? {
name: key,
parameters: value,
}
: {
name: key,
...value,
},
)[0];
};
const Definition = ({ data, ...props}: {
data: Generable | OrbRef<AnyParameterLiteral>;
type: InspectableMapping;
index: number;
orb?: OrbImport;
}) => {
const Summary = props.type.components.summary;
const navigateTo = useStoreActions((actions) => actions.navigateTo);
const setDragging = useStoreActions((actions) => actions.setDragging);
return (
<button
className="w-full mb-2 p-4 py-2 cursor-pointer text-left text-circle-black
bg-white border border-circle-gray-300 rounded-md2 hover:border-gray-700 text-base"
draggable="true"
onDragStart={(e) => {
const dataMapping = props.type;
if (dataMapping?.dragTarget) {
setDragging({ dataMapping, data });
}
}}
onClick={(e) => {
if (data instanceof CircleCI.orb.OrbRef) {
return;
}
const flattened = flattenGenerable(data);
navigateTo({
component: InspectorDefinitionMenuNav,
props: {
data,
editing: true,
values: flattened,
dataType: props.type,
index: props.index,
},
});
}}
>
<Summary data={data} />
</button>
);
};
export default Definition;