yangdx commited on
Commit
ebd81d2
·
1 Parent(s): f57fcd2

Handle empty graph data and add empty graph hints

Browse files

- Add empty data handling flag
- Display "Graph Is Empty" node

lightrag_webui/src/hooks/useLightragGraph.tsx CHANGED
@@ -226,6 +226,8 @@ const useLightrangeGraph = () => {
226
  // Use ref to track if data has been loaded and initial load
227
  const dataLoadedRef = useRef(false)
228
  const initialLoadRef = useRef(false)
 
 
229
 
230
  const getNode = useCallback(
231
  (nodeId: string) => {
@@ -258,11 +260,16 @@ const useLightrangeGraph = () => {
258
 
259
  // Data fetching logic
260
  useEffect(() => {
261
- // Skip if fetch is already in progress or no query label
262
- if (fetchInProgressRef.current || !queryLabel) {
263
  return
264
  }
265
 
 
 
 
 
 
266
  // Only fetch data when graphDataFetchAttempted is false (avoids re-fetching on vite dev mode)
267
  if (!isFetching && !useGraphStore.getState().graphDataFetchAttempted) {
268
  // Set flags
@@ -280,15 +287,27 @@ const useLightrangeGraph = () => {
280
  })
281
  }
282
 
283
- console.log('Fetching graph data...')
284
 
285
  // Use a local copy of the parameters
286
  const currentQueryLabel = queryLabel
287
  const currentMaxQueryDepth = maxQueryDepth
288
  const currentMinDegree = minDegree
289
 
290
- // Fetch graph data
291
- fetchGraph(currentQueryLabel, currentMaxQueryDepth, currentMinDegree).then((data) => {
 
 
 
 
 
 
 
 
 
 
 
 
292
  const state = useGraphStore.getState()
293
 
294
  // Reset state
@@ -296,23 +315,36 @@ const useLightrangeGraph = () => {
296
 
297
  // Check if data is empty or invalid
298
  if (!data || !data.nodes || data.nodes.length === 0) {
299
- // Create an empty graph
300
  const emptyGraph = new DirectedGraph();
301
 
302
- // Set empty graph to store
 
 
 
 
 
 
 
 
 
 
 
303
  state.setSigmaGraph(emptyGraph);
304
  state.setRawGraph(null);
305
 
306
- // Show "Graph Is Empty" placeholder
307
  state.setGraphIsEmpty(true);
308
 
309
- // Clear current label
310
- useSettingsStore.getState().setQueryLabel('');
 
 
311
 
312
  // Clear last successful query label to ensure labels are fetched next time
313
  state.setLastSuccessfulQueryLabel('');
314
 
315
- console.log('Graph data is empty or invalid, set empty graph');
316
  } else {
317
  // Create and set new graph
318
  const newSigmaGraph = createSigmaGraph(data);
@@ -337,6 +369,11 @@ const useLightrangeGraph = () => {
337
  initialLoadRef.current = true
338
  fetchInProgressRef.current = false
339
  state.setIsFetching(false)
 
 
 
 
 
340
  }).catch((error) => {
341
  console.error('Error fetching graph data:', error)
342
 
 
226
  // Use ref to track if data has been loaded and initial load
227
  const dataLoadedRef = useRef(false)
228
  const initialLoadRef = useRef(false)
229
+ // Use ref to track if empty data has been handled
230
+ const emptyDataHandledRef = useRef(false)
231
 
232
  const getNode = useCallback(
233
  (nodeId: string) => {
 
260
 
261
  // Data fetching logic
262
  useEffect(() => {
263
+ // Skip if fetch is already in progress
264
+ if (fetchInProgressRef.current) {
265
  return
266
  }
267
 
268
+ // Empty queryLabel should be only handle once(avoid infinite loop)
269
+ if (!queryLabel && emptyDataHandledRef.current) {
270
+ return;
271
+ }
272
+
273
  // Only fetch data when graphDataFetchAttempted is false (avoids re-fetching on vite dev mode)
274
  if (!isFetching && !useGraphStore.getState().graphDataFetchAttempted) {
275
  // Set flags
 
287
  })
288
  }
289
 
290
+ console.log('Preparing graph data...')
291
 
292
  // Use a local copy of the parameters
293
  const currentQueryLabel = queryLabel
294
  const currentMaxQueryDepth = maxQueryDepth
295
  const currentMinDegree = minDegree
296
 
297
+ // Declare a variable to store data promise
298
+ let dataPromise;
299
+
300
+ // 1. If query label is not empty, use fetchGraph
301
+ if (currentQueryLabel) {
302
+ dataPromise = fetchGraph(currentQueryLabel, currentMaxQueryDepth, currentMinDegree);
303
+ } else {
304
+ // 2. If query label is empty, set data to null
305
+ console.log('Query label is empty, show empty graph')
306
+ dataPromise = Promise.resolve(null);
307
+ }
308
+
309
+ // 3. Process data
310
+ dataPromise.then((data) => {
311
  const state = useGraphStore.getState()
312
 
313
  // Reset state
 
315
 
316
  // Check if data is empty or invalid
317
  if (!data || !data.nodes || data.nodes.length === 0) {
318
+ // Create a graph with a single "Graph Is Empty" node
319
  const emptyGraph = new DirectedGraph();
320
 
321
+ // Add a single node with "Graph Is Empty" label
322
+ emptyGraph.addNode('empty-graph-node', {
323
+ label: t('graphPanel.emptyGraph'),
324
+ color: '#cccccc', // gray color
325
+ x: 0.5,
326
+ y: 0.5,
327
+ size: 15,
328
+ borderColor: Constants.nodeBorderColor,
329
+ borderSize: 0.2
330
+ });
331
+
332
+ // Set graph to store
333
  state.setSigmaGraph(emptyGraph);
334
  state.setRawGraph(null);
335
 
336
+ // Still mark graph as empty for other logic
337
  state.setGraphIsEmpty(true);
338
 
339
+ // Only clear current label if it's not already empty
340
+ if (currentQueryLabel) {
341
+ useSettingsStore.getState().setQueryLabel('');
342
+ }
343
 
344
  // Clear last successful query label to ensure labels are fetched next time
345
  state.setLastSuccessfulQueryLabel('');
346
 
347
+ console.log('Graph data is empty, created graph with empty graph node');
348
  } else {
349
  // Create and set new graph
350
  const newSigmaGraph = createSigmaGraph(data);
 
369
  initialLoadRef.current = true
370
  fetchInProgressRef.current = false
371
  state.setIsFetching(false)
372
+
373
+ // Mark empty data as handled if data is empty and query label is empty
374
+ if ((!data || !data.nodes || data.nodes.length === 0) && !currentQueryLabel) {
375
+ emptyDataHandledRef.current = true;
376
+ }
377
  }).catch((error) => {
378
  console.error('Error fetching graph data:', error)
379
 
lightrag_webui/src/locales/en.json CHANGED
@@ -201,7 +201,8 @@
201
  "placeholder": "Search labels...",
202
  "andOthers": "And {count} others",
203
  "refreshTooltip": "Reload graph data"
204
- }
 
205
  },
206
  "retrievePanel": {
207
  "chatMessage": {
 
201
  "placeholder": "Search labels...",
202
  "andOthers": "And {count} others",
203
  "refreshTooltip": "Reload graph data"
204
+ },
205
+ "emptyGraph": "Graph Is Empty"
206
  },
207
  "retrievePanel": {
208
  "chatMessage": {
lightrag_webui/src/locales/zh.json CHANGED
@@ -198,7 +198,8 @@
198
  "placeholder": "搜索标签...",
199
  "andOthers": "还有 {count} 个",
200
  "refreshTooltip": "重新加载图形数据"
201
- }
 
202
  },
203
  "retrievePanel": {
204
  "chatMessage": {
 
198
  "placeholder": "搜索标签...",
199
  "andOthers": "还有 {count} 个",
200
  "refreshTooltip": "重新加载图形数据"
201
+ },
202
+ "emptyGraph": "图谱数据为空"
203
  },
204
  "retrievePanel": {
205
  "chatMessage": {