Fix Neo4j node and edge edit problem
This commit is contained in:
@@ -17,30 +17,32 @@ interface EdgeToUpdate {
|
||||
* @param propertyName - Name of the property being updated
|
||||
* @param newValue - New value for the property
|
||||
*/
|
||||
export const updateGraphNode = async (nodeId: string, propertyName: string, newValue: string) => {
|
||||
export const updateGraphNode = async (nodeId: string, entityId:string, propertyName: string, newValue: string) => {
|
||||
// Get graph state from store
|
||||
const sigmaGraph = useGraphStore.getState().sigmaGraph
|
||||
const rawGraph = useGraphStore.getState().rawGraph
|
||||
|
||||
// Validate graph state
|
||||
if (!sigmaGraph || !rawGraph || !sigmaGraph.hasNode(String(nodeId))) {
|
||||
if (!sigmaGraph || !rawGraph || !sigmaGraph.hasNode(nodeId)) {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const nodeAttributes = sigmaGraph.getNodeAttributes(String(nodeId))
|
||||
const nodeAttributes = sigmaGraph.getNodeAttributes(nodeId)
|
||||
|
||||
// Special handling for entity_id changes (node renaming)
|
||||
if (propertyName === 'entity_id') {
|
||||
console.log('updateGraphNode', nodeId, entityId, propertyName, newValue)
|
||||
|
||||
// For entity_id changes (node renaming) with NetworkX graph storage
|
||||
if ((nodeId === entityId) && (propertyName === 'entity_id')) {
|
||||
// Create new node with updated ID but same attributes
|
||||
sigmaGraph.addNode(newValue, { ...nodeAttributes, label: newValue })
|
||||
|
||||
const edgesToUpdate: EdgeToUpdate[] = []
|
||||
|
||||
// Process all edges connected to this node
|
||||
sigmaGraph.forEachEdge(String(nodeId), (edge, attributes, source, target) => {
|
||||
const otherNode = source === String(nodeId) ? target : source
|
||||
const isOutgoing = source === String(nodeId)
|
||||
sigmaGraph.forEachEdge(nodeId, (edge, attributes, source, target) => {
|
||||
const otherNode = source === nodeId ? target : source
|
||||
const isOutgoing = source === nodeId
|
||||
|
||||
// Get original edge dynamic ID for later reference
|
||||
const originalEdgeDynamicId = edge
|
||||
@@ -67,14 +69,15 @@ export const updateGraphNode = async (nodeId: string, propertyName: string, newV
|
||||
})
|
||||
|
||||
// Remove the old node after all edges are processed
|
||||
sigmaGraph.dropNode(String(nodeId))
|
||||
sigmaGraph.dropNode(nodeId)
|
||||
|
||||
// Update node reference in raw graph data
|
||||
const nodeIndex = rawGraph.nodeIdMap[String(nodeId)]
|
||||
const nodeIndex = rawGraph.nodeIdMap[nodeId]
|
||||
if (nodeIndex !== undefined) {
|
||||
rawGraph.nodes[nodeIndex].id = newValue
|
||||
rawGraph.nodes[nodeIndex].labels = [newValue]
|
||||
rawGraph.nodes[nodeIndex].properties.entity_id = newValue
|
||||
delete rawGraph.nodeIdMap[String(nodeId)]
|
||||
delete rawGraph.nodeIdMap[nodeId]
|
||||
rawGraph.nodeIdMap[newValue] = nodeIndex
|
||||
}
|
||||
|
||||
@@ -82,10 +85,10 @@ export const updateGraphNode = async (nodeId: string, propertyName: string, newV
|
||||
edgesToUpdate.forEach(({ originalDynamicId, newEdgeId, edgeIndex }) => {
|
||||
if (rawGraph.edges[edgeIndex]) {
|
||||
// Update source/target references
|
||||
if (rawGraph.edges[edgeIndex].source === String(nodeId)) {
|
||||
if (rawGraph.edges[edgeIndex].source === nodeId) {
|
||||
rawGraph.edges[edgeIndex].source = newValue
|
||||
}
|
||||
if (rawGraph.edges[edgeIndex].target === String(nodeId)) {
|
||||
if (rawGraph.edges[edgeIndex].target === nodeId) {
|
||||
rawGraph.edges[edgeIndex].target = newValue
|
||||
}
|
||||
|
||||
@@ -99,10 +102,14 @@ export const updateGraphNode = async (nodeId: string, propertyName: string, newV
|
||||
// Update selected node in store
|
||||
useGraphStore.getState().setSelectedNode(newValue)
|
||||
} else {
|
||||
// For other properties, just update the property in raw graph
|
||||
// for none NetworkX nodes or none entity_id changes
|
||||
const nodeIndex = rawGraph.nodeIdMap[String(nodeId)]
|
||||
if (nodeIndex !== undefined) {
|
||||
rawGraph.nodes[nodeIndex].properties[propertyName] = newValue
|
||||
if (propertyName === 'entity_id') {
|
||||
rawGraph.nodes[nodeIndex].labels = [newValue]
|
||||
sigmaGraph.setNodeAttribute(String(nodeId), 'label', newValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -119,7 +126,7 @@ export const updateGraphNode = async (nodeId: string, propertyName: string, newV
|
||||
* @param propertyName - Name of the property being updated
|
||||
* @param newValue - New value for the property
|
||||
*/
|
||||
export const updateGraphEdge = async (sourceId: string, targetId: string, propertyName: string, newValue: string) => {
|
||||
export const updateGraphEdge = async (edgeId: string, dynamicId: string, sourceId: string, targetId: string, propertyName: string, newValue: string) => {
|
||||
// Get graph state from store
|
||||
const sigmaGraph = useGraphStore.getState().sigmaGraph
|
||||
const rawGraph = useGraphStore.getState().rawGraph
|
||||
@@ -130,44 +137,15 @@ export const updateGraphEdge = async (sourceId: string, targetId: string, proper
|
||||
}
|
||||
|
||||
try {
|
||||
// Find the edge between source and target nodes
|
||||
const allEdges = sigmaGraph.edges()
|
||||
let keyToUse = null
|
||||
|
||||
for (const edge of allEdges) {
|
||||
const edgeSource = sigmaGraph.source(edge)
|
||||
const edgeTarget = sigmaGraph.target(edge)
|
||||
|
||||
// Match edge in either direction (undirected graph support)
|
||||
if ((edgeSource === sourceId && edgeTarget === targetId) ||
|
||||
(edgeSource === targetId && edgeTarget === sourceId)) {
|
||||
keyToUse = edge
|
||||
break
|
||||
const edgeIndex = rawGraph.edgeIdMap[String(edgeId)]
|
||||
if (edgeIndex !== undefined && rawGraph.edges[edgeIndex]) {
|
||||
rawGraph.edges[edgeIndex].properties[propertyName] = newValue
|
||||
if(dynamicId !== undefined && propertyName === 'keywords') {
|
||||
sigmaGraph.setEdgeAttribute(dynamicId, 'label', newValue)
|
||||
}
|
||||
}
|
||||
|
||||
if (keyToUse !== null) {
|
||||
// Special handling for keywords property (updates edge label)
|
||||
if(propertyName === 'keywords') {
|
||||
sigmaGraph.setEdgeAttribute(keyToUse, 'label', newValue)
|
||||
} else {
|
||||
sigmaGraph.setEdgeAttribute(keyToUse, propertyName, newValue)
|
||||
}
|
||||
|
||||
// Update edge in raw graph data using dynamic ID mapping
|
||||
if (keyToUse && rawGraph.edgeDynamicIdMap[keyToUse] !== undefined) {
|
||||
const edgeIndex = rawGraph.edgeDynamicIdMap[keyToUse]
|
||||
if (rawGraph.edges[edgeIndex]) {
|
||||
rawGraph.edges[edgeIndex].properties[propertyName] = newValue
|
||||
}
|
||||
} else if (keyToUse !== null) {
|
||||
// Fallback: try to find edge by key in edge ID map
|
||||
const edgeIndexByKey = rawGraph.edgeIdMap[keyToUse]
|
||||
if (edgeIndexByKey !== undefined && rawGraph.edges[edgeIndexByKey]) {
|
||||
rawGraph.edges[edgeIndexByKey].properties[propertyName] = newValue
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error updating edge ${sourceId}->${targetId} in graph:`, error)
|
||||
throw new Error('Failed to update edge in graph')
|
||||
|
Reference in New Issue
Block a user