Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
247 changes: 150 additions & 97 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
}, []);

const onDragStart = (event, nodeType) => {
setType(nodeType);

Check failure on line 62 in src/App.jsx

View workflow job for this annotation

GitHub Actions / test (20.x, 3.10)

'setType' is not defined

Check failure on line 62 in src/App.jsx

View workflow job for this annotation

GitHub Actions / test (20.x, 3.11)

'setType' is not defined
event.dataTransfer.setData('text/plain', nodeType);
event.dataTransfer.effectAllowed = 'move';
};
Expand All @@ -81,10 +81,10 @@
// Global variables state
const [globalVariables, setGlobalVariables] = useState([]);
const [events, setEvents] = useState([]);

// Python code editor state
const [pythonCode, setPythonCode] = useState("# Define your Python variables and functions here\n# Example:\n# my_variable = 42\n# def my_function(x):\n# return x * 2\n");

const [defaultValues, setDefaultValues] = useState({});
const [isEditingLabel, setIsEditingLabel] = useState(false);
const [tempLabel, setTempLabel] = useState('');
Expand Down Expand Up @@ -144,34 +144,77 @@
}
};

// Function to preload all documentation at startup
const preloadAllDocumentation = async () => {
const availableTypes = Object.keys(nodeTypes);

try {
// Convert types array to a string (or could be sent as JSON array)
const response = await fetch(getApiEndpoint(`/get-all-docs`));

if (response.ok) {
const allDocs = await response.json();
setNodeDocumentation(allDocs);
} else {
console.error('Failed to preload documentation');
// Fallback: initialize empty documentation for all types
const documentationCache = {};
availableTypes.forEach(nodeType => {
documentationCache[nodeType] = {
html: '<p>No documentation available for this node type.</p>',
text: 'No documentation available for this node type.'
};
});
setNodeDocumentation(documentationCache);
}
} catch (error) {
console.error('Error preloading documentation:', error);
// Fallback: initialize empty documentation for all types
const documentationCache = {};
availableTypes.forEach(nodeType => {
documentationCache[nodeType] = {
html: '<p>Error loading documentation.</p>',
text: 'Error loading documentation.'
};
});
setNodeDocumentation(documentationCache);
}
};

// Function to preload all default values at startup
const preloadDefaultValues = async () => {
const availableTypes = Object.keys(nodeTypes);
const promises = availableTypes.map(async (nodeType) => {
try {
const response = await fetch(getApiEndpoint(`/default-values/${nodeType}`));
if (response.ok) {
const defaults = await response.json();
return { nodeType, defaults };
}
} catch (error) {
console.warn(`Failed to preload defaults for ${nodeType}:`, error);
}
return { nodeType, defaults: {} };
});

const results = await Promise.all(promises);
const defaultValuesCache = {};
results.forEach(({ nodeType, defaults }) => {
defaultValuesCache[nodeType] = defaults;
});
try {
const response = await fetch(getApiEndpoint(`/default-values-all`));

setDefaultValues(defaultValuesCache);
if (response.ok) {
const allDefaults = await response.json();
setDefaultValues(allDefaults);
} else {
console.error('Failed to preload default values');
// Fallback: initialize empty defaults for all types
const defaultValuesCache = {};
availableTypes.forEach(nodeType => {
defaultValuesCache[nodeType] = {};
});
setDefaultValues(defaultValuesCache);
}
} catch (error) {
console.error('Error preloading default values:', error);
// Fallback: initialize empty defaults for all types
const defaultValuesCache = {};
availableTypes.forEach(nodeType => {
defaultValuesCache[nodeType] = {};
});
setDefaultValues(defaultValuesCache);
}
};

// Preload all default values when component mounts
// Preload all default values and documentation when component mounts
useEffect(() => {
preloadDefaultValues();
preloadAllDocumentation();
}, []);

const onDrop = useCallback(
Expand Down Expand Up @@ -216,7 +259,7 @@
setNodes((nds) => [...nds, newNode]);
setNodeCounter((count) => count + 1);
},
[screenToFlowPosition, type, nodeCounter, fetchDefaultValues, setDefaultValues, setNodes, setNodeCounter],

Check warning on line 262 in src/App.jsx

View workflow job for this annotation

GitHub Actions / test (20.x, 3.10)

React Hook useCallback has an unnecessary dependency: 'setDefaultValues'. Either exclude it or remove the dependency array

Check warning on line 262 in src/App.jsx

View workflow job for this annotation

GitHub Actions / test (20.x, 3.11)

React Hook useCallback has an unnecessary dependency: 'setDefaultValues'. Either exclude it or remove the dependency array
);

// Function to save a graph to computer with "Save As" dialog
Expand Down Expand Up @@ -308,12 +351,12 @@
}

// Load the graph data
const {
nodes: loadedNodes,
edges: loadedEdges,
nodeCounter: loadedNodeCounter,
solverParams: loadedSolverParams,
globalVariables: loadedGlobalVariables,
const {
nodes: loadedNodes,
edges: loadedEdges,
nodeCounter: loadedNodeCounter,
solverParams: loadedSolverParams,
globalVariables: loadedGlobalVariables,
events: loadedEvents,
pythonCode: loadedPythonCode
} = graphData;
Expand Down Expand Up @@ -369,12 +412,12 @@
return;
}

const {
nodes: loadedNodes,
edges: loadedEdges,
nodeCounter: loadedNodeCounter,
solverParams: loadedSolverParams,
globalVariables: loadedGlobalVariables,
const {
nodes: loadedNodes,
edges: loadedEdges,
nodeCounter: loadedNodeCounter,
solverParams: loadedSolverParams,
globalVariables: loadedGlobalVariables,
events: loadedEvents,
pythonCode: loadedPythonCode
} = graphData;
Expand Down Expand Up @@ -752,22 +795,31 @@

const selectedType = availableTypes[choiceIndex];
const newNodeId = nodeCounter.toString();

// Fetch default values and documentation for this node type
const defaults = await fetchDefaultValues(selectedType);
const docs = await fetchNodeDocumentation(selectedType);

// Store default values and documentation for this node type
setDefaultValues(prev => ({
...prev,
[selectedType]: defaults
}));

setNodeDocumentation(prev => ({
...prev,
[selectedType]: docs
}));


// Get default values and documentation for this node type (should be cached from preload)
let defaults = defaultValues[selectedType] || {};
let docs = nodeDocumentation[selectedType] || {
html: '<p>No documentation available for this node type.</p>',
text: 'No documentation available for this node type.'
};

// Fallback: fetch if not cached (shouldn't happen normally)
if (!defaultValues[selectedType]) {
defaults = await fetchDefaultValues(selectedType);
setDefaultValues(prev => ({
...prev,
[selectedType]: defaults
}));
}

if (!nodeDocumentation[selectedType]) {
docs = await fetchNodeDocumentation(selectedType);
setNodeDocumentation(prev => ({
...prev,
[selectedType]: docs
}));
}

// Create node data with label and initialize all expected fields as empty strings
let nodeData = { label: `${selectedType} ${newNodeId}` };

Expand Down Expand Up @@ -932,8 +984,8 @@
return () => {
document.removeEventListener('keydown', handleKeyDown);
};
}, [selectedEdge, selectedNode, copiedNode, duplicateNode, setCopyFeedback]);

Check warning on line 987 in src/App.jsx

View workflow job for this annotation

GitHub Actions / test (20.x, 3.10)

React Hook useEffect has missing dependencies: 'deleteSelectedEdge' and 'deleteSelectedNode'. Either include them or remove the dependency array

Check warning on line 987 in src/App.jsx

View workflow job for this annotation

GitHub Actions / test (20.x, 3.11)

React Hook useEffect has missing dependencies: 'deleteSelectedEdge' and 'deleteSelectedNode'. Either include them or remove the dependency array

return (
<div style={{ width: '100vw', height: '100vh', display: 'flex', flexDirection: 'column' }}>
{/* Tab Navigation */}
Expand Down Expand Up @@ -1022,7 +1074,8 @@
<div style={{
display: 'flex', flex: 1,
height: 'calc(100vh - 50px)', // Subtract the tab bar height
overflow: 'hidden'}}>
overflow: 'hidden'
}}>
{/* Sidebar */}
<div style={{
width: '250px',
Expand All @@ -1031,7 +1084,7 @@
}}>
<Sidebar />
</div>

{/* Main Graph Area */}
<div className="dndflow" style={{ flex: 1, position: 'relative' }}>
<div className="reactflow-wrapper" ref={reactFlowWrapper} style={{ width: '100%', height: '100%' }}>
Expand Down Expand Up @@ -1272,50 +1325,50 @@
}}
>
<div style={{ padding: '20px' }}>
<h3>Selected Edge</h3>
<div style={{ marginBottom: '10px' }}>
<strong>ID:</strong> {selectedEdge.id}
</div>
<div style={{ marginBottom: '10px' }}>
<strong>Source:</strong> {selectedEdge.source}
</div>
<div style={{ marginBottom: '10px' }}>
<strong>Target:</strong> {selectedEdge.target}
</div>
<div style={{ marginBottom: '10px' }}>
<strong>Type:</strong> {selectedEdge.type}
</div>
<h3>Selected Edge</h3>
<div style={{ marginBottom: '10px' }}>
<strong>ID:</strong> {selectedEdge.id}
</div>
<div style={{ marginBottom: '10px' }}>
<strong>Source:</strong> {selectedEdge.source}
</div>
<div style={{ marginBottom: '10px' }}>
<strong>Target:</strong> {selectedEdge.target}
</div>
<div style={{ marginBottom: '10px' }}>
<strong>Type:</strong> {selectedEdge.type}
</div>

<br />
<button
style={{
marginTop: 10,
marginRight: 10,
padding: '8px 12px',
backgroundColor: '#78A083',
color: 'white',
border: 'none',
borderRadius: 5,
cursor: 'pointer',
}}
onClick={() => setSelectedEdge(null)}
>
Close
</button>
<button
style={{
marginTop: 10,
padding: '8px 12px',
backgroundColor: '#e74c3c',
color: 'white',
border: 'none',
borderRadius: 5,
cursor: 'pointer',
}}
onClick={deleteSelectedEdge}
>
Delete Edge
</button>
<br />
<button
style={{
marginTop: 10,
marginRight: 10,
padding: '8px 12px',
backgroundColor: '#78A083',
color: 'white',
border: 'none',
borderRadius: 5,
cursor: 'pointer',
}}
onClick={() => setSelectedEdge(null)}
>
Close
</button>
<button
style={{
marginTop: 10,
padding: '8px 12px',
backgroundColor: '#e74c3c',
color: 'white',
border: 'none',
borderRadius: 5,
cursor: 'pointer',
}}
onClick={deleteSelectedEdge}
>
Delete Edge
</button>
</div>
</div>
)}
Expand Down Expand Up @@ -1674,7 +1727,7 @@

{/* Global Variables Tab */}
{activeTab === 'globals' && (
<GlobalVariablesTab
<GlobalVariablesTab
globalVariables={globalVariables}
setGlobalVariables={setGlobalVariables}
setActiveTab={setActiveTab}
Expand Down Expand Up @@ -1752,7 +1805,7 @@
);
}

export function App () {
export function App() {
return (
<ReactFlowProvider>
<DnDProvider>
Expand Down
Loading
Loading