-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathObjectOutputWidget.tsx
More file actions
186 lines (168 loc) · 5.59 KB
/
ObjectOutputWidget.tsx
File metadata and controls
186 lines (168 loc) · 5.59 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com) All Rights Reserved.
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
// tslint:disable: jsx-no-multiline-js
import React, { useState } from 'react';
import { DiagramEngine } from '@projectstorm/react-diagrams';
import { Button, Codicon, TruncatedLabel, TruncatedLabelGroup } from '@wso2/ui-toolkit';
import { IOType, Mapping, TypeKind } from '@wso2/ballerina-core';
import { IDataMapperContext } from "../../../../utils/DataMapperContext/DataMapperContext";
import { DataMapperPortWidget, PortState, InputOutputPortModel } from '../../Port';
import { TreeBody, TreeContainer, TreeHeader } from '../commons/Tree/Tree';
import { ObjectOutputFieldWidget } from "./ObjectOutputFieldWidget";
import { useIONodesStyles } from '../../../styles';
import { useDMCollapsedFieldsStore, useDMIOConfigPanelStore } from '../../../../store/store';
import { OutputSearchHighlight } from '../commons/Search';
import { useShallow } from 'zustand/react/shallow';
export interface ObjectOutputWidgetProps {
id: string; // this will be the root ID used to prepend for UUIDs of nested fields
outputType: IOType;
typeName: string;
value: any;
engine: DiagramEngine;
getPort: (portId: string) => InputOutputPortModel;
context: IDataMapperContext;
mappings?: Mapping[];
valueLabel?: string;
originalTypeName?: string;
}
export function ObjectOutputWidget(props: ObjectOutputWidgetProps) {
const {
id,
outputType,
typeName,
value,
engine,
getPort,
context,
valueLabel
} = props;
const classes = useIONodesStyles();
const [portState, setPortState] = useState<PortState>(PortState.Unselected);
const [isHovered, setIsHovered] = useState(false);
const collapsedFieldsStore = useDMCollapsedFieldsStore();
const { setIsIOConfigPanelOpen, setIOConfigPanelType, setIsSchemaOverridden } = useDMIOConfigPanelStore(
useShallow(state => ({
setIsIOConfigPanelOpen: state.setIsIOConfigPanelOpen,
setIOConfigPanelType: state.setIOConfigPanelType,
setIsSchemaOverridden: state.setIsSchemaOverridden
}))
);
// Handle both records (fields) and tuples (members)
const isTuple = outputType.kind === TypeKind.Tuple;
const fields = isTuple
? (outputType.members?.filter(t => t !== null) || [])
: (outputType.fields?.filter(t => t !== null) || []);
const hasFields = fields.length > 0;
const portIn = getPort(`${id}.IN`);
const isUnknownType = outputType.kind === TypeKind.Unknown;
let expanded = true;
if ((portIn && portIn.attributes.collapsed)) {
expanded = false;
}
const isDisabled = portIn?.attributes.descendantHasValue;
const indentation = (portIn && (!hasFields || !expanded)) ? 0 : 24;
const handleExpand = () => {
const collapsedFields = collapsedFieldsStore.fields;
if (!expanded) {
collapsedFieldsStore.setFields(collapsedFields.filter((element) => element !== id));
} else {
collapsedFieldsStore.setFields([...collapsedFields, id]);
}
};
const handlePortState = (state: PortState) => {
setPortState(state)
};
const onMouseEnter = () => {
setIsHovered(true);
};
const onMouseLeave = () => {
setIsHovered(false);
};
const label = (
<TruncatedLabelGroup style={{ marginRight: "auto", alignItems: "baseline" }}>
{valueLabel && (
<TruncatedLabel className={classes.valueLabelHeader}>
<OutputSearchHighlight>{valueLabel}</OutputSearchHighlight>
</TruncatedLabel>
)}
<TruncatedLabel className={isUnknownType ? classes.unknownTypeLabel : classes.typeLabel}>
{typeName || ''}
</TruncatedLabel>
</TruncatedLabelGroup>
);
const onRightClick = (event: React.MouseEvent) => {
event.preventDefault();
setIOConfigPanelType("Output");
setIsSchemaOverridden(true);
setIsIOConfigPanelOpen(true);
};
return (
<>
<TreeContainer data-testid={`${id}-node`} onContextMenu={onRightClick}>
<TreeHeader
isSelected={portState !== PortState.Unselected}
id={"recordfield-" + id}
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
>
<span className={classes.inPort}>
{portIn && (
<DataMapperPortWidget
engine={engine}
port={portIn}
handlePortState={handlePortState}
disable={isDisabled && !expanded}
/>)
}
</span>
<span className={classes.label}>
<Button
id={"expand-or-collapse-" + id}
appearance="icon"
tooltip="Expand/Collapse"
sx={{ marginLeft: indentation }}
onClick={handleExpand}
data-testid={`${id}-expand-icon-mapping-target-node`}
>
{expanded ? <Codicon name="chevron-down" /> : <Codicon name="chevron-right" />}
</Button>
{label}
</span>
</TreeHeader>
{(expanded && fields) && (
<TreeBody>
{fields?.map((item, index) => {
return (
<ObjectOutputFieldWidget
key={index}
engine={engine}
field={item}
getPort={getPort}
parentId={id}
context={context}
treeDepth={0}
hasHoveredParent={isHovered}
/>
);
})}
</TreeBody>
)}
</TreeContainer>
</>
);
}