-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathwindow-node.tsx
More file actions
174 lines (147 loc) · 6.22 KB
/
window-node.tsx
File metadata and controls
174 lines (147 loc) · 6.22 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
'use client';
import { useGlobalContext } from '@/context/GlobalContext';
import { Handle, Position, useReactFlow } from '@xyflow/react';
import React from 'react';
import WindowComboBox, { type WindowOption } from './window-combo-box';
interface WindowNodeProps {
id?: string;
// data?: any;
}
export default function WindowNode({ id }: WindowNodeProps) {
const DEFAULT_WINDOW_SIZE = 64;
const DEFAULT_OVERLAP_SIZE = 0;
const [windowSize, setWindowSize] = React.useState<number>(DEFAULT_WINDOW_SIZE);
const [overlapSize, setOverlapSize] = React.useState<number>(DEFAULT_OVERLAP_SIZE);
const [selectedOption, setSelectedOption] = React.useState<WindowOption>('default');
const [isConnected, setIsConnected] = React.useState(false);
// Get React Flow instance
const reactFlowInstance = useReactFlow();
const { dataStreaming } = useGlobalContext();
const buildConfig = () => ({
chunk_size: windowSize,
overlap_size: overlapSize,
});
// Validate config values
const isValidConfig =
Number.isInteger(windowSize) &&
windowSize > 0 &&
Number.isInteger(overlapSize) &&
overlapSize >= 0 &&
overlapSize < windowSize;
// Push config to node data and dispatch processing config when relevant state changes
const pushConfigToNodeData = React.useCallback(() => {
if (!id) return;
if (!isValidConfig) return;
const config = buildConfig();
reactFlowInstance.setNodes((nds) =>
nds.map((n) =>
n.id === id ? { ...n, data: { ...n.data, config } } : n
)
);
}, [id, reactFlowInstance, windowSize, overlapSize, isValidConfig]);
// Check connection status and update state
const checkConnectionStatus = React.useCallback(() => {
try {
const edges = reactFlowInstance.getEdges();
const nodes = reactFlowInstance.getNodes();
// Check if this node is connected to source node or any activated node
const isConnectedToActivatedNode = (nodeId: string, visited: Set<string> = new Set()): boolean => {
if (visited.has(nodeId)) return false; // Prevent infinite loops
visited.add(nodeId);
// Find incoming edges to this node
const incomingEdges = edges.filter(edge => edge.target === nodeId);
for (const edge of incomingEdges) {
const sourceNode = nodes.find(n => n.id === edge.source);
if (!sourceNode) continue;
// If source is a source-node, we're activated
if (sourceNode.type === 'source-node') {
return true;
}
// If source is another node, check if it's activated
if (sourceNode.id && isConnectedToActivatedNode(sourceNode.id, visited)) {
return true;
}
}
return false;
};
const isActivated = id ? isConnectedToActivatedNode(id) : false;
setIsConnected(isActivated);
} catch (error) {
console.error('Error checking connection:', error);
setIsConnected(false);
}
}, [id, reactFlowInstance]);
// Check connection status on mount and when edges might change
React.useEffect(() => {
checkConnectionStatus();
// Listen for custom edge change events
const handleEdgeChange = () => {
checkConnectionStatus();
};
window.addEventListener('reactflow-edges-changed', handleEdgeChange);
// Also set up periodic check as backup
const interval = setInterval(checkConnectionStatus, 1000);
return () => {
window.removeEventListener('reactflow-edges-changed', handleEdgeChange);
clearInterval(interval);
};
}, [checkConnectionStatus]);
// Push config to node data and dispatch processing config when relevant state changes
React.useEffect(() => {
if(!isValidConfig) return;
pushConfigToNodeData();
}, [pushConfigToNodeData, isValidConfig]);
return (
<div className="relative">
{/* Input Handle - positioned to align with left circle */}
<Handle
type="target"
position={Position.Left}
id="window-input"
style={{
left: '24px',
top: '30px',
transform: 'translateY(-50%)',
width: '28px',
height: '28px',
backgroundColor: 'transparent',
border: '2px solid transparent',
borderRadius: '50%',
zIndex: 20,
cursor: 'crosshair',
pointerEvents: 'all'
}}
/>
{/* Output Handle - positioned to align with right circle */}
<Handle
type="source"
position={Position.Right}
id="window-output"
style={{
right: '24px',
top: '30px',
transform: 'translateY(-50%)',
width: '28px',
height: '28px',
backgroundColor: 'transparent',
border: '2px solid transparent',
borderRadius: '50%',
zIndex: 20,
cursor: 'crosshair',
pointerEvents: 'all'
}}
/>
{/* Just the ComboBox without Card wrapper */}
<WindowComboBox
windowSize={windowSize}
overlapSize={overlapSize}
selectedOption={selectedOption}
setWindowSize={setWindowSize}
setOverlapSize={setOverlapSize}
setSelectedOption={setSelectedOption}
isConnected={isConnected}
isDataStreamOn={dataStreaming}
/>
</div>
);
}