Refactor graph label handling to extract labels directly from graph data
- Remove redundant label caching logic - Add graphLabels state to graph store
This commit is contained in:
@@ -1,35 +1,15 @@
|
|||||||
import { useCallback } from 'react'
|
import { useCallback } from 'react'
|
||||||
import { AsyncSelect } from '@/components/ui/AsyncSelect'
|
import { AsyncSelect } from '@/components/ui/AsyncSelect'
|
||||||
import { getGraphLabels } from '@/api/lightrag'
|
|
||||||
import { useSettingsStore } from '@/stores/settings'
|
import { useSettingsStore } from '@/stores/settings'
|
||||||
import { useGraphStore } from '@/stores/graph'
|
import { useGraphStore } from '@/stores/graph'
|
||||||
import { labelListLimit } from '@/lib/constants'
|
import { labelListLimit } from '@/lib/constants'
|
||||||
import MiniSearch from 'minisearch'
|
import MiniSearch from 'minisearch'
|
||||||
|
|
||||||
const lastGraph: any = {
|
|
||||||
graph: null,
|
|
||||||
searchEngine: null,
|
|
||||||
labels: []
|
|
||||||
}
|
|
||||||
|
|
||||||
const GraphLabels = () => {
|
const GraphLabels = () => {
|
||||||
const label = useSettingsStore.use.queryLabel()
|
const label = useSettingsStore.use.queryLabel()
|
||||||
const graph = useGraphStore.use.sigmaGraph()
|
const graphLabels = useGraphStore.use.graphLabels()
|
||||||
|
|
||||||
const getSearchEngine = useCallback(async () => {
|
|
||||||
if (lastGraph.graph == graph) {
|
|
||||||
return {
|
|
||||||
labels: lastGraph.labels,
|
|
||||||
searchEngine: lastGraph.searchEngine
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const labels = ['*'].concat(await getGraphLabels())
|
|
||||||
|
|
||||||
// Ensure query label exists
|
|
||||||
if (!labels.includes(useSettingsStore.getState().queryLabel)) {
|
|
||||||
useSettingsStore.getState().setQueryLabel(labels[0])
|
|
||||||
}
|
|
||||||
|
|
||||||
|
const getSearchEngine = useCallback(() => {
|
||||||
// Create search engine
|
// Create search engine
|
||||||
const searchEngine = new MiniSearch({
|
const searchEngine = new MiniSearch({
|
||||||
idField: 'id',
|
idField: 'id',
|
||||||
@@ -44,22 +24,18 @@ const GraphLabels = () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// Add documents
|
// Add documents
|
||||||
const documents = labels.map((str, index) => ({ id: index, value: str }))
|
const documents = graphLabels.map((str, index) => ({ id: index, value: str }))
|
||||||
searchEngine.addAll(documents)
|
searchEngine.addAll(documents)
|
||||||
|
|
||||||
lastGraph.graph = graph
|
|
||||||
lastGraph.searchEngine = searchEngine
|
|
||||||
lastGraph.labels = labels
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
labels,
|
labels: graphLabels,
|
||||||
searchEngine
|
searchEngine
|
||||||
}
|
}
|
||||||
}, [graph])
|
}, [graphLabels])
|
||||||
|
|
||||||
const fetchData = useCallback(
|
const fetchData = useCallback(
|
||||||
async (query?: string): Promise<string[]> => {
|
async (query?: string): Promise<string[]> => {
|
||||||
const { labels, searchEngine } = await getSearchEngine()
|
const { labels, searchEngine } = getSearchEngine()
|
||||||
|
|
||||||
let result: string[] = labels
|
let result: string[] = labels
|
||||||
if (query) {
|
if (query) {
|
||||||
|
@@ -205,6 +205,21 @@ const useLightrangeGraph = () => {
|
|||||||
state.setSigmaGraph(createSigmaGraph(data))
|
state.setSigmaGraph(createSigmaGraph(data))
|
||||||
data?.buildDynamicMap()
|
data?.buildDynamicMap()
|
||||||
state.setRawGraph(data)
|
state.setRawGraph(data)
|
||||||
|
|
||||||
|
// Extract labels from graph data
|
||||||
|
if (data) {
|
||||||
|
const labelSet = new Set<string>(['*'])
|
||||||
|
for (const node of data.nodes) {
|
||||||
|
if (node.labels && Array.isArray(node.labels)) {
|
||||||
|
for (const label of node.labels) {
|
||||||
|
labelSet.add(label)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
state.setGraphLabels(Array.from(labelSet).sort())
|
||||||
|
} else {
|
||||||
|
state.setGraphLabels(['*'])
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@@ -65,6 +65,7 @@ interface GraphState {
|
|||||||
|
|
||||||
rawGraph: RawGraph | null
|
rawGraph: RawGraph | null
|
||||||
sigmaGraph: DirectedGraph | null
|
sigmaGraph: DirectedGraph | null
|
||||||
|
graphLabels: string[]
|
||||||
|
|
||||||
moveToSelectedNode: boolean
|
moveToSelectedNode: boolean
|
||||||
|
|
||||||
@@ -79,6 +80,7 @@ interface GraphState {
|
|||||||
|
|
||||||
setRawGraph: (rawGraph: RawGraph | null) => void
|
setRawGraph: (rawGraph: RawGraph | null) => void
|
||||||
setSigmaGraph: (sigmaGraph: DirectedGraph | null) => void
|
setSigmaGraph: (sigmaGraph: DirectedGraph | null) => void
|
||||||
|
setGraphLabels: (labels: string[]) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
const useGraphStoreBase = create<GraphState>()((set) => ({
|
const useGraphStoreBase = create<GraphState>()((set) => ({
|
||||||
@@ -91,6 +93,7 @@ const useGraphStoreBase = create<GraphState>()((set) => ({
|
|||||||
|
|
||||||
rawGraph: null,
|
rawGraph: null,
|
||||||
sigmaGraph: null,
|
sigmaGraph: null,
|
||||||
|
graphLabels: [],
|
||||||
|
|
||||||
setSelectedNode: (nodeId: string | null, moveToSelectedNode?: boolean) =>
|
setSelectedNode: (nodeId: string | null, moveToSelectedNode?: boolean) =>
|
||||||
set({ selectedNode: nodeId, moveToSelectedNode }),
|
set({ selectedNode: nodeId, moveToSelectedNode }),
|
||||||
@@ -112,6 +115,7 @@ const useGraphStoreBase = create<GraphState>()((set) => ({
|
|||||||
focusedEdge: null,
|
focusedEdge: null,
|
||||||
rawGraph: null,
|
rawGraph: null,
|
||||||
sigmaGraph: null,
|
sigmaGraph: null,
|
||||||
|
graphLabels: [],
|
||||||
moveToSelectedNode: false
|
moveToSelectedNode: false
|
||||||
}),
|
}),
|
||||||
|
|
||||||
@@ -122,6 +126,8 @@ const useGraphStoreBase = create<GraphState>()((set) => ({
|
|||||||
|
|
||||||
setSigmaGraph: (sigmaGraph: DirectedGraph | null) => set({ sigmaGraph }),
|
setSigmaGraph: (sigmaGraph: DirectedGraph | null) => set({ sigmaGraph }),
|
||||||
|
|
||||||
|
setGraphLabels: (labels: string[]) => set({ graphLabels: labels }),
|
||||||
|
|
||||||
setMoveToSelectedNode: (moveToSelectedNode?: boolean) => set({ moveToSelectedNode })
|
setMoveToSelectedNode: (moveToSelectedNode?: boolean) => set({ moveToSelectedNode })
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user