Skip to content

Commit a6a96b5

Browse files
committed
color code buffer handles; join node no longer allows data edges
Signed-off-by: Teo Koon Peng <[email protected]>
1 parent 2ef857e commit a6a96b5

File tree

6 files changed

+57
-9
lines changed

6 files changed

+57
-9
lines changed

diagram-editor/frontend/app.css

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,15 @@
2323
top: -100%;
2424
}
2525
.react-flow__handle.connectingto {
26-
background: var(--mui-palette-error-main);
26+
background-color: var(--mui-palette-error-main);
2727
}
2828
.react-flow__handle.valid {
29-
background: var(--mui-palette-success-main);
29+
background-color: var(--mui-palette-success-main);
3030
}
31+
.react-flow__handle.handle-buffer {
32+
background-color: var(--mui-palette-secondary-main);
33+
}
34+
/* Some operations can output either Data or Stream so we cannot color code Stream handles. */
35+
/* .react-flow__handle.handle-stream {
36+
background-color: var(--mui-palette-warning-main);
37+
} */

diagram-editor/frontend/nodes/base-node.tsx

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
import { Box, Button, type ButtonProps, Paper } from '@mui/material';
22
import { Handle, type NodeProps, Position } from '@xyflow/react';
3-
import React from 'react';
3+
import { memo, useCallback } from 'react';
4+
import { EdgeCategory } from '../edges';
5+
import { exhaustiveCheck } from '../utils/exhaustive-check';
46
import { LAYOUT_OPTIONS } from '../utils/layout';
57

68
export interface BaseNodeProps extends NodeProps {
79
color?: ButtonProps['color'];
810
icon?: React.JSX.Element | string;
911
label: string;
1012
variant: 'input' | 'output' | 'inputOutput';
13+
/**
14+
* defaults to `EdgeCateogry.Data`.
15+
*/
16+
inputHandleType?: EdgeCategory;
17+
/**
18+
* defaults to `EdgeCateogry.Data`.
19+
*/
20+
outputHandleType?: EdgeCategory;
1121
}
1222

1323
function BaseNode({
1424
color,
1525
icon: materialIconOrSymbol,
1626
label,
1727
variant,
28+
inputHandleType = EdgeCategory.Data,
29+
outputHandleType = EdgeCategory.Data,
1830
isConnectable,
1931
selected,
2032
sourcePosition = Position.Bottom,
@@ -26,13 +38,38 @@ function BaseNode({
2638
) : (
2739
materialIconOrSymbol
2840
);
41+
42+
const handleClassName = useCallback((handleType?: EdgeCategory) => {
43+
if (handleType === undefined) {
44+
return undefined;
45+
}
46+
47+
switch (handleType) {
48+
case EdgeCategory.Data: {
49+
// use the default style
50+
return undefined;
51+
}
52+
case EdgeCategory.Buffer: {
53+
return 'handle-buffer';
54+
}
55+
case EdgeCategory.Stream: {
56+
return undefined;
57+
}
58+
default: {
59+
exhaustiveCheck(handleType);
60+
throw new Error('unknown edge category');
61+
}
62+
}
63+
}, []);
64+
2965
return (
3066
<Paper>
3167
{(variant === 'input' || variant === 'inputOutput') && (
3268
<Handle
3369
type="target"
3470
position={targetPosition}
3571
isConnectable={isConnectable}
72+
className={handleClassName(inputHandleType)}
3673
/>
3774
)}
3875
<Button
@@ -64,10 +101,11 @@ function BaseNode({
64101
type="source"
65102
position={sourcePosition}
66103
isConnectable={isConnectable}
104+
className={handleClassName(outputHandleType)}
67105
/>
68106
)}
69107
</Paper>
70108
);
71109
}
72110

73-
export default React.memo(BaseNode);
111+
export default memo(BaseNode);

diagram-editor/frontend/nodes/buffer-node.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { NodeProps } from '@xyflow/react';
2+
import { EdgeCategory } from '../edges';
23
import type { OperationNode } from '.';
34
import BaseNode from './base-node';
45
import { BufferIcon } from './icons';
@@ -10,6 +11,7 @@ function BufferNodeComp(props: NodeProps<OperationNode<'buffer'>>) {
1011
icon={<BufferIcon />}
1112
label="Buffer"
1213
variant="inputOutput"
14+
outputHandleType={EdgeCategory.Buffer}
1315
/>
1416
);
1517
}

diagram-editor/frontend/nodes/join-node.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { NodeProps } from '@xyflow/react';
2+
import { EdgeCategory } from '../edges';
23
import type { OperationNode } from '.';
34
import BaseNode from './base-node';
45
import { JoinIcon } from './icons';
@@ -10,6 +11,7 @@ function JoinNodeComp(props: NodeProps<OperationNode<'join'>>) {
1011
icon={<JoinIcon />}
1112
label="Join"
1213
variant="inputOutput"
14+
inputHandleType={EdgeCategory.Buffer}
1315
/>
1416
);
1517
}

diagram-editor/frontend/utils/connection.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ describe('validate edges', () => {
157157
}
158158
});
159159

160-
test('"join" node accepts both data and buffer edges', () => {
160+
test('"join" node only accepts buffer edges', () => {
161161
const nodeNode = createOperationNode(
162162
ROOT_NAMESPACE,
163163
undefined,
@@ -190,8 +190,7 @@ describe('validate edges', () => {
190190
for (const targetNode of [joinNode, serializedJoinNode]) {
191191
{
192192
const validEdges = getValidEdgeTypes(nodeNode, targetNode);
193-
expect(validEdges.length).toBe(1);
194-
expect(validEdges).toContain('default');
193+
expect(validEdges.length).toBe(0);
195194
}
196195
{
197196
const validEdges = getValidEdgeTypes(bufferNode, targetNode);

diagram-editor/frontend/utils/connection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ const ALLOWED_INPUT_EDGE_CATEGORIES: Record<NodeTypes, EdgeCategory[]> = {
3535
buffer_access: [EdgeCategory.Data, EdgeCategory.Buffer],
3636
fork_clone: [EdgeCategory.Data],
3737
fork_result: [EdgeCategory.Data],
38-
join: [EdgeCategory.Buffer, EdgeCategory.Data],
38+
join: [EdgeCategory.Buffer],
3939
listen: [EdgeCategory.Buffer],
4040
node: [EdgeCategory.Data],
4141
scope: [EdgeCategory.Data],
4242
section: [EdgeCategory.Data, EdgeCategory.Buffer],
4343
sectionInput: [],
4444
sectionOutput: [EdgeCategory.Data],
4545
sectionBuffer: [],
46-
serialized_join: [EdgeCategory.Data, EdgeCategory.Buffer],
46+
serialized_join: [EdgeCategory.Buffer],
4747
split: [EdgeCategory.Data],
4848
start: [],
4949
stream_out: [EdgeCategory.Stream],

0 commit comments

Comments
 (0)