Remove grapOperation.ts
This commit is contained in:
@@ -2,7 +2,7 @@ import { useState, useEffect } from 'react'
|
|||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
import { toast } from 'sonner'
|
import { toast } from 'sonner'
|
||||||
import { updateEntity, updateRelation, checkEntityNameExists } from '@/api/lightrag'
|
import { updateEntity, updateRelation, checkEntityNameExists } from '@/api/lightrag'
|
||||||
import { updateGraphNode, updateGraphEdge } from '@/utils/graphOperations'
|
import { useGraphStore } from '@/stores/graph'
|
||||||
import { PropertyName, EditIcon, PropertyValue } from './PropertyRowComponents'
|
import { PropertyName, EditIcon, PropertyValue } from './PropertyRowComponents'
|
||||||
import PropertyEditDialog from './PropertyEditDialog'
|
import PropertyEditDialog from './PropertyEditDialog'
|
||||||
|
|
||||||
@@ -85,12 +85,22 @@ const EditablePropertyRow = ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
await updateEntity(entityId, updatedData, true)
|
await updateEntity(entityId, updatedData, true)
|
||||||
await updateGraphNode(nodeId, entityId, name, value)
|
try {
|
||||||
|
await useGraphStore.getState().updateNodeAndSelect(nodeId, entityId, name, value)
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error updating node in graph:', error)
|
||||||
|
throw new Error('Failed to update node in graph')
|
||||||
|
}
|
||||||
toast.success(t('graphPanel.propertiesView.success.entityUpdated'))
|
toast.success(t('graphPanel.propertiesView.success.entityUpdated'))
|
||||||
} else if (entityType === 'edge' && sourceId && targetId && edgeId && dynamicId) {
|
} else if (entityType === 'edge' && sourceId && targetId && edgeId && dynamicId) {
|
||||||
const updatedData = { [name]: value }
|
const updatedData = { [name]: value }
|
||||||
await updateRelation(sourceId, targetId, updatedData)
|
await updateRelation(sourceId, targetId, updatedData)
|
||||||
await updateGraphEdge(edgeId, dynamicId, sourceId, targetId, name, value)
|
try {
|
||||||
|
await useGraphStore.getState().updateEdgeAndSelect(edgeId, dynamicId, sourceId, targetId, name, value)
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Error updating edge ${sourceId}->${targetId} in graph:`, error)
|
||||||
|
throw new Error('Failed to update edge in graph')
|
||||||
|
}
|
||||||
toast.success(t('graphPanel.propertiesView.success.relationUpdated'))
|
toast.success(t('graphPanel.propertiesView.success.relationUpdated'))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -136,7 +136,7 @@ interface GraphState {
|
|||||||
// Version counter to trigger data refresh
|
// Version counter to trigger data refresh
|
||||||
graphDataVersion: number
|
graphDataVersion: number
|
||||||
incrementGraphDataVersion: () => void
|
incrementGraphDataVersion: () => void
|
||||||
|
|
||||||
// Methods for updating graph elements and UI state together
|
// Methods for updating graph elements and UI state together
|
||||||
updateNodeAndSelect: (nodeId: string, entityId: string, propertyName: string, newValue: string) => Promise<void>
|
updateNodeAndSelect: (nodeId: string, entityId: string, propertyName: string, newValue: string) => Promise<void>
|
||||||
updateEdgeAndSelect: (edgeId: string, dynamicId: string, sourceId: string, targetId: string, propertyName: string, newValue: string) => Promise<void>
|
updateEdgeAndSelect: (edgeId: string, dynamicId: string, sourceId: string, targetId: string, propertyName: string, newValue: string) => Promise<void>
|
||||||
@@ -252,40 +252,40 @@ const useGraphStoreBase = create<GraphState>()((set, get) => ({
|
|||||||
// Get current state
|
// Get current state
|
||||||
const state = get()
|
const state = get()
|
||||||
const { sigmaGraph, rawGraph } = state
|
const { sigmaGraph, rawGraph } = state
|
||||||
|
|
||||||
// Validate graph state
|
// Validate graph state
|
||||||
if (!sigmaGraph || !rawGraph || !sigmaGraph.hasNode(nodeId)) {
|
if (!sigmaGraph || !rawGraph || !sigmaGraph.hasNode(nodeId)) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const nodeAttributes = sigmaGraph.getNodeAttributes(nodeId)
|
const nodeAttributes = sigmaGraph.getNodeAttributes(nodeId)
|
||||||
|
|
||||||
console.log('updateNodeAndSelect', nodeId, entityId, propertyName, newValue)
|
console.log('updateNodeAndSelect', nodeId, entityId, propertyName, newValue)
|
||||||
|
|
||||||
// For entity_id changes (node renaming) with NetworkX graph storage
|
// For entity_id changes (node renaming) with NetworkX graph storage
|
||||||
if ((nodeId === entityId) && (propertyName === 'entity_id')) {
|
if ((nodeId === entityId) && (propertyName === 'entity_id')) {
|
||||||
// Create new node with updated ID but same attributes
|
// Create new node with updated ID but same attributes
|
||||||
sigmaGraph.addNode(newValue, { ...nodeAttributes, label: newValue })
|
sigmaGraph.addNode(newValue, { ...nodeAttributes, label: newValue })
|
||||||
|
|
||||||
const edgesToUpdate: EdgeToUpdate[] = []
|
const edgesToUpdate: EdgeToUpdate[] = []
|
||||||
|
|
||||||
// Process all edges connected to this node
|
// Process all edges connected to this node
|
||||||
sigmaGraph.forEachEdge(nodeId, (edge, attributes, source, target) => {
|
sigmaGraph.forEachEdge(nodeId, (edge, attributes, source, target) => {
|
||||||
const otherNode = source === nodeId ? target : source
|
const otherNode = source === nodeId ? target : source
|
||||||
const isOutgoing = source === nodeId
|
const isOutgoing = source === nodeId
|
||||||
|
|
||||||
// Get original edge dynamic ID for later reference
|
// Get original edge dynamic ID for later reference
|
||||||
const originalEdgeDynamicId = edge
|
const originalEdgeDynamicId = edge
|
||||||
const edgeIndexInRawGraph = rawGraph.edgeDynamicIdMap[originalEdgeDynamicId]
|
const edgeIndexInRawGraph = rawGraph.edgeDynamicIdMap[originalEdgeDynamicId]
|
||||||
|
|
||||||
// Create new edge with updated node reference
|
// Create new edge with updated node reference
|
||||||
const newEdgeId = sigmaGraph.addEdge(
|
const newEdgeId = sigmaGraph.addEdge(
|
||||||
isOutgoing ? newValue : otherNode,
|
isOutgoing ? newValue : otherNode,
|
||||||
isOutgoing ? otherNode : newValue,
|
isOutgoing ? otherNode : newValue,
|
||||||
attributes
|
attributes
|
||||||
)
|
)
|
||||||
|
|
||||||
// Track edges that need updating in the raw graph
|
// Track edges that need updating in the raw graph
|
||||||
if (edgeIndexInRawGraph !== undefined) {
|
if (edgeIndexInRawGraph !== undefined) {
|
||||||
edgesToUpdate.push({
|
edgesToUpdate.push({
|
||||||
@@ -294,14 +294,14 @@ const useGraphStoreBase = create<GraphState>()((set, get) => ({
|
|||||||
edgeIndex: edgeIndexInRawGraph
|
edgeIndex: edgeIndexInRawGraph
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove the old edge
|
// Remove the old edge
|
||||||
sigmaGraph.dropEdge(edge)
|
sigmaGraph.dropEdge(edge)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Remove the old node after all edges are processed
|
// Remove the old node after all edges are processed
|
||||||
sigmaGraph.dropNode(nodeId)
|
sigmaGraph.dropNode(nodeId)
|
||||||
|
|
||||||
// Update node reference in raw graph data
|
// Update node reference in raw graph data
|
||||||
const nodeIndex = rawGraph.nodeIdMap[nodeId]
|
const nodeIndex = rawGraph.nodeIdMap[nodeId]
|
||||||
if (nodeIndex !== undefined) {
|
if (nodeIndex !== undefined) {
|
||||||
@@ -311,7 +311,7 @@ const useGraphStoreBase = create<GraphState>()((set, get) => ({
|
|||||||
delete rawGraph.nodeIdMap[nodeId]
|
delete rawGraph.nodeIdMap[nodeId]
|
||||||
rawGraph.nodeIdMap[newValue] = nodeIndex
|
rawGraph.nodeIdMap[newValue] = nodeIndex
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update all edge references in raw graph data
|
// Update all edge references in raw graph data
|
||||||
edgesToUpdate.forEach(({ originalDynamicId, newEdgeId, edgeIndex }) => {
|
edgesToUpdate.forEach(({ originalDynamicId, newEdgeId, edgeIndex }) => {
|
||||||
if (rawGraph.edges[edgeIndex]) {
|
if (rawGraph.edges[edgeIndex]) {
|
||||||
@@ -322,14 +322,14 @@ const useGraphStoreBase = create<GraphState>()((set, get) => ({
|
|||||||
if (rawGraph.edges[edgeIndex].target === nodeId) {
|
if (rawGraph.edges[edgeIndex].target === nodeId) {
|
||||||
rawGraph.edges[edgeIndex].target = newValue
|
rawGraph.edges[edgeIndex].target = newValue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update dynamic ID mappings
|
// Update dynamic ID mappings
|
||||||
rawGraph.edges[edgeIndex].dynamicId = newEdgeId
|
rawGraph.edges[edgeIndex].dynamicId = newEdgeId
|
||||||
delete rawGraph.edgeDynamicIdMap[originalDynamicId]
|
delete rawGraph.edgeDynamicIdMap[originalDynamicId]
|
||||||
rawGraph.edgeDynamicIdMap[newEdgeId] = edgeIndex
|
rawGraph.edgeDynamicIdMap[newEdgeId] = edgeIndex
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// Update selected node in store
|
// Update selected node in store
|
||||||
set({ selectedNode: newValue, moveToSelectedNode: true })
|
set({ selectedNode: newValue, moveToSelectedNode: true })
|
||||||
} else {
|
} else {
|
||||||
@@ -342,7 +342,7 @@ const useGraphStoreBase = create<GraphState>()((set, get) => ({
|
|||||||
sigmaGraph.setNodeAttribute(String(nodeId), 'label', newValue)
|
sigmaGraph.setNodeAttribute(String(nodeId), 'label', newValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Trigger a re-render by incrementing the version counter
|
// Trigger a re-render by incrementing the version counter
|
||||||
set((state) => ({ graphDataVersion: state.graphDataVersion + 1 }))
|
set((state) => ({ graphDataVersion: state.graphDataVersion + 1 }))
|
||||||
}
|
}
|
||||||
@@ -351,17 +351,17 @@ const useGraphStoreBase = create<GraphState>()((set, get) => ({
|
|||||||
throw new Error('Failed to update node in graph')
|
throw new Error('Failed to update node in graph')
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
updateEdgeAndSelect: async (edgeId: string, dynamicId: string, sourceId: string, targetId: string, propertyName: string, newValue: string) => {
|
updateEdgeAndSelect: async (edgeId: string, dynamicId: string, sourceId: string, targetId: string, propertyName: string, newValue: string) => {
|
||||||
// Get current state
|
// Get current state
|
||||||
const state = get()
|
const state = get()
|
||||||
const { sigmaGraph, rawGraph } = state
|
const { sigmaGraph, rawGraph } = state
|
||||||
|
|
||||||
// Validate graph state
|
// Validate graph state
|
||||||
if (!sigmaGraph || !rawGraph) {
|
if (!sigmaGraph || !rawGraph) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const edgeIndex = rawGraph.edgeIdMap[String(edgeId)]
|
const edgeIndex = rawGraph.edgeIdMap[String(edgeId)]
|
||||||
if (edgeIndex !== undefined && rawGraph.edges[edgeIndex]) {
|
if (edgeIndex !== undefined && rawGraph.edges[edgeIndex]) {
|
||||||
@@ -370,10 +370,10 @@ const useGraphStoreBase = create<GraphState>()((set, get) => ({
|
|||||||
sigmaGraph.setEdgeAttribute(dynamicId, 'label', newValue)
|
sigmaGraph.setEdgeAttribute(dynamicId, 'label', newValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Trigger a re-render by incrementing the version counter
|
// Trigger a re-render by incrementing the version counter
|
||||||
set((state) => ({ graphDataVersion: state.graphDataVersion + 1 }))
|
set((state) => ({ graphDataVersion: state.graphDataVersion + 1 }))
|
||||||
|
|
||||||
// Update selected edge in store to ensure UI reflects changes
|
// Update selected edge in store to ensure UI reflects changes
|
||||||
set({ selectedEdge: dynamicId })
|
set({ selectedEdge: dynamicId })
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@@ -1,41 +0,0 @@
|
|||||||
import { useGraphStore } from '@/stores/graph'
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Update node in the graph visualization
|
|
||||||
* This function is now a wrapper around the store's updateNodeAndSelect method
|
|
||||||
*
|
|
||||||
* @param nodeId - ID of the node to update
|
|
||||||
* @param entityId - ID of the entity
|
|
||||||
* @param propertyName - Name of the property being updated
|
|
||||||
* @param newValue - New value for the property
|
|
||||||
*/
|
|
||||||
export const updateGraphNode = async (nodeId: string, entityId: string, propertyName: string, newValue: string) => {
|
|
||||||
try {
|
|
||||||
// Call the store method that handles both data update and UI state
|
|
||||||
await useGraphStore.getState().updateNodeAndSelect(nodeId, entityId, propertyName, newValue)
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error updating node in graph:', error)
|
|
||||||
throw new Error('Failed to update node in graph')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Update edge in the graph visualization
|
|
||||||
* This function is now a wrapper around the store's updateEdgeAndSelect method
|
|
||||||
*
|
|
||||||
* @param edgeId - ID of the edge
|
|
||||||
* @param dynamicId - Dynamic ID of the edge in sigma graph
|
|
||||||
* @param sourceId - ID of the source node
|
|
||||||
* @param targetId - ID of the target node
|
|
||||||
* @param propertyName - Name of the property being updated
|
|
||||||
* @param newValue - New value for the property
|
|
||||||
*/
|
|
||||||
export const updateGraphEdge = async (edgeId: string, dynamicId: string, sourceId: string, targetId: string, propertyName: string, newValue: string) => {
|
|
||||||
try {
|
|
||||||
// Call the store method that handles both data update and UI state
|
|
||||||
await useGraphStore.getState().updateEdgeAndSelect(edgeId, dynamicId, sourceId, targetId, 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