-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathone-edge-node.tsx
More file actions
116 lines (103 loc) · 3.57 KB
/
Copy pathone-edge-node.tsx
File metadata and controls
116 lines (103 loc) · 3.57 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
import { Handle, Position, type Node, useNodeConnections } from '@xyflow/react'
import clsx from 'clsx'
import type { CustomNodeData } from '~/types/datamapper_types/react-node-types'
import EditButton from '../basic-components/edit-button'
import DeleteButton from '../basic-components/delete-button'
import HighlightButton from '../basic-components/highlight-button'
import VariableTypeIcon from '../basic-components/variable-type-icon'
import HoverInfo from '../basic-components/hover-info'
export interface OneEdgeNodeProperties {
id: string
data: CustomNodeData
variant?: 'source' | 'target'
onEdit?: (data: CustomNodeData) => void
onDelete?: (id: string) => void
onHighlight?: (id: string) => void
}
function OneEdgeNode({ id, data, variant = 'source', onEdit, onDelete, onHighlight }: OneEdgeNodeProperties) {
const checked = data?.checked
const connections = useNodeConnections({
id,
})
const isConnectable = connections.length === 0 || variant === 'source'
const updateChecked = () => {
const newChecked = !checked
data.setNodes?.((nodes: Node[]) =>
nodes.map((node) => (node.id === id ? { ...node, data: { ...node.data, checked: newChecked } } : node)),
)
if (variant != 'source') {
data.setNodes?.((nodes: Node[]) =>
nodes.map((node) =>
node.id !== id && node.parentId?.includes('target')
? { ...node, data: { ...node.data, checked: false } }
: node,
),
)
}
}
return (
<div
onClick={updateChecked}
className={clsx(
'border-border group mr-2 flex cursor-pointer items-center gap-2 rounded-md border-2 p-2',
checked ? 'bg-foreground-active text-neutral' : 'bg-backdrop',
data.isHidden && 'opacity-20',
)}
style={{ width: `${data.width}px` }}
>
{/* Left side */}
<span
className="flex shrink-0"
style={{
//Style needed here because conditional statement won't work with tailwind
marginLeft: isConnectable && variant === 'target' ? '10px' : '',
}}
>
{data.isAttribute && 'attribute: '}
<VariableTypeIcon variableType={data.variableType} variableTypeBasic={data.variableTypeBasic ?? ''} />
</span>
<div className="group/hoverInfoGroup min-w-0 flex-1 truncate text-left">
{data.label}
<HoverInfo info={data.label} />
</div>
{/* Right side */}
<div className="ml-auto hidden items-center gap-1 group-hover:flex">
<HighlightButton
onClick={() => {
onHighlight?.(id)
}}
/>
<EditButton
onClick={() => {
onEdit?.(data)
}}
/>
<DeleteButton
onClick={() => {
onDelete?.(id)
}}
/>
</div>
{/* Active handle */}
<Handle
key={variant}
type={variant}
position={variant === 'source' ? Position.Right : Position.Left}
isConnectable={isConnectable}
style={{
width: 10,
height: 10,
}} //Can't set this with tailwind for some reason
className={`${variant == 'target' ? 'translate-x-1.25' : '-translate-x-2.5'} ${isConnectable ? 'opacity-100' : 'opacity-0'} `}
/>
{/* Hidden opposite handle */}
<Handle
key={variant === 'source' ? 'target' : 'source'}
type={variant === 'source' ? 'target' : 'source'}
position={variant === 'source' ? Position.Left : Position.Right}
className="pointer-events-none h-0 w-0 opacity-0"
/>
</div>
)
}
export default OneEdgeNode