fix(graph): Fixed the issue of incorrect handling of edges and nodes during node ID updates

This commit is contained in:
choizhang
2025-04-12 10:36:05 +08:00
parent 7e3e685763
commit ea43f3537e
6 changed files with 164 additions and 43 deletions

View File

@@ -119,8 +119,34 @@ const EditablePropertyRow = ({
if (sigmaInstance && sigmaGraph && rawGraph) { if (sigmaInstance && sigmaGraph && rawGraph) {
// Update the node in sigma graph // Update the node in sigma graph
if (sigmaGraph.hasNode(String(value))) { if (sigmaGraph.hasNode(String(value))) {
// Update the node label in the sigma graph try {
sigmaGraph.setNodeAttribute(String(value), 'label', editValue) // Create a new node with the updated ID
const oldNodeAttributes = sigmaGraph.getNodeAttributes(String(value))
// Add a new node with the new ID but keep all other attributes
sigmaGraph.addNode(editValue, {
...oldNodeAttributes,
label: editValue
})
// Copy all edges from the old node to the new node
sigmaGraph.forEachEdge(String(value), (edge, attributes, source, target) => {
const otherNode = source === String(value) ? target : source
const isOutgoing = source === String(value)
// Create a new edge with the same attributes but connected to the new node ID
if (isOutgoing) {
sigmaGraph.addEdge(editValue, otherNode, attributes)
} else {
sigmaGraph.addEdge(otherNode, editValue, attributes)
}
// Remove the old edge
sigmaGraph.dropEdge(edge)
})
// Remove the old node after all edges have been transferred
sigmaGraph.dropNode(String(value))
// Also update the node in the raw graph // Also update the node in the raw graph
const nodeIndex = rawGraph.nodeIdMap[String(value)] const nodeIndex = rawGraph.nodeIdMap[String(value)]
@@ -139,20 +165,16 @@ const EditablePropertyRow = ({
if (selectedNode === String(value)) { if (selectedNode === String(value)) {
useGraphStore.getState().setSelectedNode(editValue) useGraphStore.getState().setSelectedNode(editValue)
} }
}
} else {
// Fallback to full graph reload if direct update is not possible
useGraphStore.getState().setGraphDataFetchAttempted(false)
useGraphStore.getState().setLabelsFetchAttempted(false)
// Get current label to trigger reload // Update focused node ID if it was the edited node
const currentLabel = useSettingsStore.getState().queryLabel const focusedNode = useGraphStore.getState().focusedNode
if (currentLabel) { if (focusedNode === String(value)) {
// Trigger data reload by temporarily clearing and resetting the label useGraphStore.getState().setFocusedNode(editValue)
useSettingsStore.getState().setQueryLabel('') }
setTimeout(() => { } catch (error) {
useSettingsStore.getState().setQueryLabel(currentLabel) console.error('Error updating node ID in graph:', error)
}, 0) throw new Error('Failed to update node ID in graph')
}
} }
} }
} else if (name === 'description') { } else if (name === 'description') {
@@ -178,22 +200,45 @@ const EditablePropertyRow = ({
if (onValueChange) { if (onValueChange) {
onValueChange(editValue) onValueChange(editValue)
} }
} catch (error: any) { // Keep type as any to access potential response properties } catch (error: any) {
console.error('Error updating property:', error); console.error('Error updating property:', error);
// Attempt to extract a more specific error message // 尝试提取更具体的错误信息
let detailMessage = t('graphPanel.propertiesView.errors.updateFailed'); // Default message let detailMessage = t('graphPanel.propertiesView.errors.updateFailed');
if (error.response?.data?.detail) { if (error.response?.data?.detail) {
// Use the detailed message from the backend response if available
detailMessage = error.response.data.detail; detailMessage = error.response.data.detail;
} else if (error.response?.data?.message) {
detailMessage = error.response.data.message;
} else if (error.message) { } else if (error.message) {
// Use the error object's message if no backend detail
detailMessage = error.message; detailMessage = error.message;
} }
toast.error(detailMessage); // Show the determined error message // 记录详细的错误信息以便调试
console.error('Update failed:', {
entityType,
entityId,
propertyName: name,
newValue: editValue,
error: error.response?.data || error.message
});
toast.error(detailMessage, {
description: t('graphPanel.propertiesView.errors.tryAgainLater')
});
} finally { } finally {
// Update the value immediately in the UI
if (onValueChange) {
onValueChange(editValue);
}
// Trigger graph data refresh
useGraphStore.getState().setGraphDataFetchAttempted(false);
useGraphStore.getState().setLabelsFetchAttempted(false);
// Re-select the node to refresh properties panel
const currentNodeId = name === 'entity_id' ? editValue : (entityId || '');
useGraphStore.getState().setSelectedNode(null);
useGraphStore.getState().setSelectedNode(currentNodeId);
setIsSubmitting(false) setIsSubmitting(false)
setIsEditing(false) setIsEditing(false)
} }

View File

@@ -99,8 +99,11 @@ const GraphControl = ({ disableHoverEffect }: { disableHoverEffect?: boolean })
const events: Record<string, any> = { const events: Record<string, any> = {
enterNode: (event: NodeEvent) => { enterNode: (event: NodeEvent) => {
if (!isButtonPressed(event.event.original)) { if (!isButtonPressed(event.event.original)) {
const graph = sigma.getGraph()
if (graph.hasNode(event.node)) {
setFocusedNode(event.node) setFocusedNode(event.node)
} }
}
}, },
leaveNode: (event: NodeEvent) => { leaveNode: (event: NodeEvent) => {
if (!isButtonPressed(event.event.original)) { if (!isButtonPressed(event.event.original)) {
@@ -108,8 +111,11 @@ const GraphControl = ({ disableHoverEffect }: { disableHoverEffect?: boolean })
} }
}, },
clickNode: (event: NodeEvent) => { clickNode: (event: NodeEvent) => {
const graph = sigma.getGraph()
if (graph.hasNode(event.node)) {
setSelectedNode(event.node) setSelectedNode(event.node)
setSelectedEdge(null) setSelectedEdge(null)
}
}, },
clickStage: () => clearSelection() clickStage: () => clearSelection()
} }

View File

@@ -35,6 +35,32 @@
"common": { "common": {
"cancel": "إلغاء" "cancel": "إلغاء"
}, },
"graphPanel": {
"propertiesView": {
"errors": {
"duplicateName": "اسم العقدة موجود بالفعل",
"updateFailed": "فشل تحديث العقدة",
"tryAgainLater": "يرجى المحاولة مرة أخرى لاحقاً"
},
"success": {
"entityUpdated": "تم تحديث العقدة بنجاح",
"relationUpdated": "تم تحديث العلاقة بنجاح"
},
"node": {
"title": "عقدة",
"id": "المعرف",
"labels": "التسميات",
"degree": "الدرجة",
"properties": "الخصائص",
"relationships": "العلاقات (ضمن الرسم البياني الفرعي)",
"expandNode": "توسيع العقدة",
"pruneNode": "تقليم العقدة",
"deleteAllNodesError": "رفض حذف جميع العقد في الرسم البياني",
"nodesRemoved": "تم حذف {{count}} عقدة، بما في ذلك العقد اليتيمة",
"noNewNodes": "لم يتم العثور على عقد قابلة للتوسيع"
}
}
},
"documentPanel": { "documentPanel": {
"clearDocuments": { "clearDocuments": {
"button": "مسح", "button": "مسح",

View File

@@ -235,6 +235,15 @@
"vectorStorage": "Vector Storage" "vectorStorage": "Vector Storage"
}, },
"propertiesView": { "propertiesView": {
"errors": {
"duplicateName": "Node name already exists",
"updateFailed": "Failed to update node",
"tryAgainLater": "Please try again later"
},
"success": {
"entityUpdated": "Node updated successfully",
"relationUpdated": "Relation updated successfully"
},
"node": { "node": {
"title": "Node", "title": "Node",
"id": "ID", "id": "ID",

View File

@@ -35,6 +35,32 @@
"common": { "common": {
"cancel": "Annuler" "cancel": "Annuler"
}, },
"graphPanel": {
"propertiesView": {
"errors": {
"duplicateName": "Le nom du nœud existe déjà",
"updateFailed": "Échec de la mise à jour du nœud",
"tryAgainLater": "Veuillez réessayer plus tard"
},
"success": {
"entityUpdated": "Nœud mis à jour avec succès",
"relationUpdated": "Relation mise à jour avec succès"
},
"node": {
"title": "Nœud",
"id": "ID",
"labels": "Étiquettes",
"degree": "Degré",
"properties": "Propriétés",
"relationships": "Relations(dans le sous-graphe)",
"expandNode": "Développer le nœud",
"pruneNode": "Élaguer le nœud",
"deleteAllNodesError": "Refus de supprimer tous les nœuds du graphe",
"nodesRemoved": "{{count}} nœuds supprimés, y compris les nœuds orphelins",
"noNewNodes": "Aucun nœud extensible trouvé"
}
}
},
"documentPanel": { "documentPanel": {
"clearDocuments": { "clearDocuments": {
"button": "Effacer", "button": "Effacer",

View File

@@ -236,6 +236,15 @@
"vectorStorage": "向量存储" "vectorStorage": "向量存储"
}, },
"propertiesView": { "propertiesView": {
"errors": {
"duplicateName": "节点名称已存在",
"updateFailed": "更新节点失败",
"tryAgainLater": "请稍后重试"
},
"success": {
"entityUpdated": "节点更新成功",
"relationUpdated": "关系更新成功"
},
"node": { "node": {
"title": "节点", "title": "节点",
"id": "ID", "id": "ID",