forked from Cloud-Pipelines/pipeline-editor
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathIONode.tsx
More file actions
199 lines (169 loc) · 6 KB
/
IONode.tsx
File metadata and controls
199 lines (169 loc) · 6 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
187
188
189
190
191
192
193
194
195
196
197
198
199
import { Handle, Position } from "@xyflow/react";
import { memo, useEffect, useMemo } from "react";
import { InputValueEditor } from "@/components/Editor/IOEditor/InputValueEditor";
import { OutputNameEditor } from "@/components/Editor/IOEditor/OutputNameEditor";
import { getOutputConnectedDetails } from "@/components/Editor/utils/getOutputConnectedDetails";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { BlockStack, InlineStack } from "@/components/ui/layout";
import { Paragraph } from "@/components/ui/typography";
import { cn } from "@/lib/utils";
import { useComponentSpec } from "@/providers/ComponentSpecProvider";
import { useContextPanel } from "@/providers/ContextPanelProvider";
import { useExecutionDataOptional } from "@/providers/ExecutionDataProvider";
import { getArgumentValue } from "@/utils/nodes/taskArguments";
import { isViewingSubgraph } from "@/utils/subgraphUtils";
import { getGhostHandleId, GHOST_NODE_ID } from "../GhostNode/utils";
export const DEFAULT_IO_NODE_WIDTH = 300;
interface IONodeProps {
id: string;
type: "input" | "output";
data: {
label: string;
value?: string;
default?: string;
type?: string;
readOnly?: boolean;
};
selected: boolean;
deletable: boolean;
}
const IONode = ({ id, type, data, selected = false }: IONodeProps) => {
const { currentGraphSpec, currentSubgraphSpec, currentSubgraphPath } =
useComponentSpec();
const executionData = useExecutionDataOptional();
const taskArguments = executionData?.rootDetails?.task_spec.arguments;
const {
setContent,
clearContent,
setOpen: setContextPanelOpen,
} = useContextPanel();
const isInput = type === "input";
const isOutput = type === "output";
const readOnly = !!data.readOnly;
const isInSubgraph = isViewingSubgraph(currentSubgraphPath);
const handleType = isInput ? "source" : "target";
const handlePosition = isInput ? Position.Right : Position.Left;
const selectedColor = isInput
? "border-blue-500 bg-blue-100"
: "border-violet-500 bg-violet-100";
const defaultColor = isInput
? "border-blue-300 bg-blue-100"
: "border-violet-300 bg-violet-100";
const borderColor = selected ? selectedColor : defaultColor;
const input = useMemo(
() =>
currentSubgraphSpec.inputs?.find((input) => input.name === data.label),
[currentSubgraphSpec.inputs, data.label],
);
const inputTaskArgument = getArgumentValue(taskArguments, input?.name);
const output = useMemo(
() =>
currentSubgraphSpec.outputs?.find((output) => output.name === data.label),
[currentSubgraphSpec.outputs, data.label],
);
useEffect(() => {
if (selected) {
if (input && isInput) {
setContent(
<InputValueEditor
input={input}
key={input.name}
disabled={readOnly}
argumentValue={inputTaskArgument}
/>,
);
}
if (output && isOutput) {
const outputConnectedDetails = getOutputConnectedDetails(
currentGraphSpec,
output.name,
);
setContent(
<OutputNameEditor
output={output}
connectedDetails={outputConnectedDetails}
key={output.name}
disabled={readOnly}
/>,
);
}
setContextPanelOpen(true);
}
return () => {
if (selected) {
clearContent();
}
};
}, [input, output, selected, readOnly]);
const connectedOutput = getOutputConnectedDetails(
currentGraphSpec,
data.label,
);
const outputConnectedValue = connectedOutput.outputName;
const outputConnectedType = connectedOutput.outputType;
const outputConnectedTaskId = connectedOutput.taskId;
const handleDefaultClassName =
"w-3! h-3! border-0! transform-none! bg-gray-500!";
const handleClassName = isInput ? "translate-x-1.5" : "-translate-x-1.5";
const hasDataValue = !!data.value || !!inputTaskArgument;
const hasDataDefault = !!data.default;
const inputValue = hasDataValue
? (inputTaskArgument ?? data.value)
: hasDataDefault
? data.default
: isInSubgraph
? "→subgraph arguments"
: null;
const outputValue = outputConnectedValue ?? data.value ?? null;
const value = isInput ? inputValue : outputValue;
// The rest of our code doesn't support IO Handles having ids (yet).
// For now, only assign an id to the Handle if this is a ghost node.
const handleId = id === GHOST_NODE_ID ? getGhostHandleId() : undefined;
return (
<Card className={cn("border-2 max-w-[300px] p-0", borderColor)}>
<CardHeader className="px-2 py-2.5">
<CardTitle className="wrap-break-word text-sm">{data.label}</CardTitle>
</CardHeader>
<CardContent className="p-2 max-w-[250px]">
<BlockStack gap="2">
{/* type */}
<Paragraph size="xs" font="mono" className="truncate text-slate-700">
<span className="font-bold">Type:</span>{" "}
{outputConnectedType ?? data.type ?? "Any"}
</Paragraph>
{!!outputConnectedTaskId && (
<Paragraph size="xs" weight="bold" className="text-slate-700">
{outputConnectedTaskId}
</Paragraph>
)}
{/* value */}
<InlineStack gap="1" className="p-2 bg-white rounded-lg w-full">
<Paragraph
size="xs"
font="mono"
weight="bold"
className="text-slate-700"
>
Value:
</Paragraph>
<Paragraph
size="xs"
font="mono"
tone={!value ? "critical" : "subdued"}
className="truncate"
>
{value ?? "No value"}
</Paragraph>
</InlineStack>
</BlockStack>
<Handle
id={handleId}
type={handleType}
position={handlePosition}
className={cn(handleDefaultClassName, handleClassName)}
/>
</CardContent>
</Card>
);
};
export default memo(IONode);