yangdx commited on
Commit
5edb4ed
·
1 Parent(s): 20592d9

Refactor graph label handling to extract labels directly from graph data

Browse files

- Remove redundant label caching logic
- Add graphLabels state to graph store

lightrag_webui/src/components/graph/GraphLabels.tsx CHANGED
@@ -1,35 +1,15 @@
1
  import { useCallback } from 'react'
2
  import { AsyncSelect } from '@/components/ui/AsyncSelect'
3
- import { getGraphLabels } from '@/api/lightrag'
4
  import { useSettingsStore } from '@/stores/settings'
5
  import { useGraphStore } from '@/stores/graph'
6
  import { labelListLimit } from '@/lib/constants'
7
  import MiniSearch from 'minisearch'
8
 
9
- const lastGraph: any = {
10
- graph: null,
11
- searchEngine: null,
12
- labels: []
13
- }
14
-
15
  const GraphLabels = () => {
16
  const label = useSettingsStore.use.queryLabel()
17
- const graph = useGraphStore.use.sigmaGraph()
18
-
19
- const getSearchEngine = useCallback(async () => {
20
- if (lastGraph.graph == graph) {
21
- return {
22
- labels: lastGraph.labels,
23
- searchEngine: lastGraph.searchEngine
24
- }
25
- }
26
- const labels = ['*'].concat(await getGraphLabels())
27
-
28
- // Ensure query label exists
29
- if (!labels.includes(useSettingsStore.getState().queryLabel)) {
30
- useSettingsStore.getState().setQueryLabel(labels[0])
31
- }
32
 
 
33
  // Create search engine
34
  const searchEngine = new MiniSearch({
35
  idField: 'id',
@@ -44,22 +24,18 @@ const GraphLabels = () => {
44
  })
45
 
46
  // Add documents
47
- const documents = labels.map((str, index) => ({ id: index, value: str }))
48
  searchEngine.addAll(documents)
49
 
50
- lastGraph.graph = graph
51
- lastGraph.searchEngine = searchEngine
52
- lastGraph.labels = labels
53
-
54
  return {
55
- labels,
56
  searchEngine
57
  }
58
- }, [graph])
59
 
60
  const fetchData = useCallback(
61
  async (query?: string): Promise<string[]> => {
62
- const { labels, searchEngine } = await getSearchEngine()
63
 
64
  let result: string[] = labels
65
  if (query) {
 
1
  import { useCallback } from 'react'
2
  import { AsyncSelect } from '@/components/ui/AsyncSelect'
 
3
  import { useSettingsStore } from '@/stores/settings'
4
  import { useGraphStore } from '@/stores/graph'
5
  import { labelListLimit } from '@/lib/constants'
6
  import MiniSearch from 'minisearch'
7
 
 
 
 
 
 
 
8
  const GraphLabels = () => {
9
  const label = useSettingsStore.use.queryLabel()
10
+ const graphLabels = useGraphStore.use.graphLabels()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
+ const getSearchEngine = useCallback(() => {
13
  // Create search engine
14
  const searchEngine = new MiniSearch({
15
  idField: 'id',
 
24
  })
25
 
26
  // Add documents
27
+ const documents = graphLabels.map((str, index) => ({ id: index, value: str }))
28
  searchEngine.addAll(documents)
29
 
 
 
 
 
30
  return {
31
+ labels: graphLabels,
32
  searchEngine
33
  }
34
+ }, [graphLabels])
35
 
36
  const fetchData = useCallback(
37
  async (query?: string): Promise<string[]> => {
38
+ const { labels, searchEngine } = getSearchEngine()
39
 
40
  let result: string[] = labels
41
  if (query) {
lightrag_webui/src/hooks/useLightragGraph.tsx CHANGED
@@ -205,6 +205,21 @@ const useLightrangeGraph = () => {
205
  state.setSigmaGraph(createSigmaGraph(data))
206
  data?.buildDynamicMap()
207
  state.setRawGraph(data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
  })
209
  }
210
  } else {
 
205
  state.setSigmaGraph(createSigmaGraph(data))
206
  data?.buildDynamicMap()
207
  state.setRawGraph(data)
208
+
209
+ // Extract labels from graph data
210
+ if (data) {
211
+ const labelSet = new Set<string>(['*'])
212
+ for (const node of data.nodes) {
213
+ if (node.labels && Array.isArray(node.labels)) {
214
+ for (const label of node.labels) {
215
+ labelSet.add(label)
216
+ }
217
+ }
218
+ }
219
+ state.setGraphLabels(Array.from(labelSet).sort())
220
+ } else {
221
+ state.setGraphLabels(['*'])
222
+ }
223
  })
224
  }
225
  } else {
lightrag_webui/src/stores/graph.ts CHANGED
@@ -65,6 +65,7 @@ interface GraphState {
65
 
66
  rawGraph: RawGraph | null
67
  sigmaGraph: DirectedGraph | null
 
68
 
69
  moveToSelectedNode: boolean
70
 
@@ -79,6 +80,7 @@ interface GraphState {
79
 
80
  setRawGraph: (rawGraph: RawGraph | null) => void
81
  setSigmaGraph: (sigmaGraph: DirectedGraph | null) => void
 
82
  }
83
 
84
  const useGraphStoreBase = create<GraphState>()((set) => ({
@@ -91,6 +93,7 @@ const useGraphStoreBase = create<GraphState>()((set) => ({
91
 
92
  rawGraph: null,
93
  sigmaGraph: null,
 
94
 
95
  setSelectedNode: (nodeId: string | null, moveToSelectedNode?: boolean) =>
96
  set({ selectedNode: nodeId, moveToSelectedNode }),
@@ -112,6 +115,7 @@ const useGraphStoreBase = create<GraphState>()((set) => ({
112
  focusedEdge: null,
113
  rawGraph: null,
114
  sigmaGraph: null,
 
115
  moveToSelectedNode: false
116
  }),
117
 
@@ -121,6 +125,8 @@ const useGraphStoreBase = create<GraphState>()((set) => ({
121
  }),
122
 
123
  setSigmaGraph: (sigmaGraph: DirectedGraph | null) => set({ sigmaGraph }),
 
 
124
 
125
  setMoveToSelectedNode: (moveToSelectedNode?: boolean) => set({ moveToSelectedNode })
126
  }))
 
65
 
66
  rawGraph: RawGraph | null
67
  sigmaGraph: DirectedGraph | null
68
+ graphLabels: string[]
69
 
70
  moveToSelectedNode: boolean
71
 
 
80
 
81
  setRawGraph: (rawGraph: RawGraph | null) => void
82
  setSigmaGraph: (sigmaGraph: DirectedGraph | null) => void
83
+ setGraphLabels: (labels: string[]) => void
84
  }
85
 
86
  const useGraphStoreBase = create<GraphState>()((set) => ({
 
93
 
94
  rawGraph: null,
95
  sigmaGraph: null,
96
+ graphLabels: [],
97
 
98
  setSelectedNode: (nodeId: string | null, moveToSelectedNode?: boolean) =>
99
  set({ selectedNode: nodeId, moveToSelectedNode }),
 
115
  focusedEdge: null,
116
  rawGraph: null,
117
  sigmaGraph: null,
118
+ graphLabels: [],
119
  moveToSelectedNode: false
120
  }),
121
 
 
125
  }),
126
 
127
  setSigmaGraph: (sigmaGraph: DirectedGraph | null) => set({ sigmaGraph }),
128
+
129
+ setGraphLabels: (labels: string[]) => set({ graphLabels: labels }),
130
 
131
  setMoveToSelectedNode: (moveToSelectedNode?: boolean) => set({ moveToSelectedNode })
132
  }))