choizhang commited on
Commit
d7362f8
·
1 Parent(s): 48b5396

feat(webui): Add attribute editing dialog box and optimize editable attribute row component

Browse files
lightrag/api/routers/graph_routes.py CHANGED
@@ -95,7 +95,6 @@ def create_graph_routes(rag, api_key: Optional[str] = None):
95
  Dict: Updated entity information
96
  """
97
  try:
98
- print(request.entity_name, request.updated_data, request.allow_rename)
99
  result = await rag.aedit_entity(
100
  entity_name=request.entity_name,
101
  updated_data=request.updated_data,
 
95
  Dict: Updated entity information
96
  """
97
  try:
 
98
  result = await rag.aedit_entity(
99
  entity_name=request.entity_name,
100
  updated_data=request.updated_data,
lightrag_webui/src/components/graph/EditablePropertyRow.tsx CHANGED
@@ -1,12 +1,12 @@
1
- import { useState, useEffect, useRef } from 'react'
 
2
  import { useTranslation } from 'react-i18next'
3
  import Text from '@/components/ui/Text'
4
- import Input from '@/components/ui/Input'
5
  import { toast } from 'sonner'
6
  import { updateEntity, updateRelation, checkEntityNameExists } from '@/api/lightrag'
7
  import { useGraphStore } from '@/stores/graph'
8
  import { PencilIcon } from 'lucide-react'
9
- import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/components/ui/Tooltip'
10
 
11
  /**
12
  * Interface for the EditablePropertyRow component props
@@ -44,7 +44,6 @@ const EditablePropertyRow = ({
44
  name,
45
  value: initialValue,
46
  onClick,
47
- tooltip,
48
  entityId,
49
  entityType,
50
  sourceId,
@@ -55,11 +54,11 @@ const EditablePropertyRow = ({
55
  // Component state
56
  const { t } = useTranslation()
57
  const [isEditing, setIsEditing] = useState(false)
58
- const [editValue, setEditValue] = useState('')
59
  const [isSubmitting, setIsSubmitting] = useState(false)
60
  const [currentValue, setCurrentValue] = useState(initialValue)
61
  const inputRef = useRef<HTMLInputElement>(null)
62
 
 
63
  /**
64
  * Update currentValue when initialValue changes from parent
65
  */
@@ -72,7 +71,6 @@ const EditablePropertyRow = ({
72
  */
73
  useEffect(() => {
74
  if (isEditing) {
75
- setEditValue(String(currentValue))
76
  // Focus the input element when entering edit mode with a small delay
77
  // to ensure the input is rendered before focusing
78
  setTimeout(() => {
@@ -82,7 +80,7 @@ const EditablePropertyRow = ({
82
  }
83
  }, 50)
84
  }
85
- }, [isEditing, currentValue])
86
 
87
  /**
88
  * Get translated property name from i18n
@@ -95,25 +93,19 @@ const EditablePropertyRow = ({
95
  }
96
 
97
  /**
98
- * Handle double-click event to enter edit mode
99
  */
100
- const handleDoubleClick = () => {
101
  if (isEditable && !isEditing) {
102
  setIsEditing(true)
103
  }
104
  }
105
 
106
  /**
107
- * Handle keyboard events in the input field
108
- * - Enter: Save changes
109
- * - Escape: Cancel editing
110
  */
111
- const handleKeyDown = (e: React.KeyboardEvent) => {
112
- if (e.key === 'Enter') {
113
- handleSave()
114
- } else if (e.key === 'Escape') {
115
- setIsEditing(false)
116
- }
117
  }
118
 
119
  /**
@@ -300,12 +292,12 @@ const EditablePropertyRow = ({
300
  * Save changes to the property value
301
  * Updates both the API and the graph visualization
302
  */
303
- const handleSave = async () => {
304
  // Prevent duplicate submissions
305
  if (isSubmitting) return
306
 
307
  // Skip if value hasn't changed
308
- if (editValue === String(currentValue)) {
309
  setIsEditing(false)
310
  return
311
  }
@@ -315,44 +307,44 @@ const EditablePropertyRow = ({
315
  try {
316
  // Handle node property updates
317
  if (entityType === 'node' && entityId) {
318
- let updatedData = { [name]: editValue }
319
 
320
  // Special handling for entity_id (name) changes
321
  if (name === 'entity_id') {
322
  // Check if the new name already exists
323
- const exists = await checkEntityNameExists(editValue)
324
  if (exists) {
325
  toast.error(t('graphPanel.propertiesView.errors.duplicateName'))
326
  setIsSubmitting(false)
327
  return
328
  }
329
  // For entity_id, we update entity_name in the API
330
- updatedData = { 'entity_name': editValue }
331
  }
332
 
333
  // Update entity in API
334
  await updateEntity(entityId, updatedData, true)
335
  // Update graph visualization
336
- await updateGraphNode(entityId, name, editValue)
337
  toast.success(t('graphPanel.propertiesView.success.entityUpdated'))
338
  }
339
  // Handle edge property updates
340
  else if (entityType === 'edge' && sourceId && targetId) {
341
- const updatedData = { [name]: editValue }
342
  // Update relation in API
343
  await updateRelation(sourceId, targetId, updatedData)
344
  // Update graph visualization
345
- await updateGraphEdge(sourceId, targetId, name, editValue)
346
  toast.success(t('graphPanel.propertiesView.success.relationUpdated'))
347
  }
348
 
349
  // Update local state
350
  setIsEditing(false)
351
- setCurrentValue(editValue)
352
 
353
  // Notify parent component if callback provided
354
  if (onValueChange) {
355
- onValueChange(editValue)
356
  }
357
  } catch (error) {
358
  console.error('Error updating property:', error)
@@ -364,58 +356,43 @@ const EditablePropertyRow = ({
364
 
365
  /**
366
  * Render the property row with edit functionality
367
- * Shows property name, edit icon, and either the editable input or the current value
368
  */
369
  return (
370
- <div className="flex items-center gap-1" onDoubleClick={handleDoubleClick}>
371
  {/* Property name with translation */}
372
  <span className="text-primary/60 tracking-wide whitespace-nowrap">
373
  {getPropertyNameTranslation(name)}
374
  </span>
375
 
376
- {/* Edit icon with tooltip */}
377
- <TooltipProvider delayDuration={200}>
378
- <Tooltip>
379
- <TooltipTrigger asChild>
380
- <div>
381
- <PencilIcon
382
- className="h-3 w-3 text-gray-500 hover:text-gray-700 cursor-pointer"
383
- onClick={() => setIsEditing(true)}
384
- />
385
- </div>
386
- </TooltipTrigger>
387
- <TooltipContent side="top">
388
- {t('graphPanel.propertiesView.doubleClickToEdit')}
389
- </TooltipContent>
390
- </Tooltip>
391
- </TooltipProvider>:
392
-
393
- {/* Conditional rendering based on edit state */}
394
- {isEditing ? (
395
- // Input field for editing
396
- <Input
397
- ref={inputRef}
398
- type="text"
399
- value={editValue}
400
- onChange={(e) => setEditValue(e.target.value)}
401
- onKeyDown={handleKeyDown}
402
- onBlur={handleSave}
403
- className="h-6 text-xs"
404
- disabled={isSubmitting}
405
  />
406
- ) : (
407
- // Text display when not editing
408
- <div className="flex items-center gap-1">
409
- <Text
410
- className="hover:bg-primary/20 rounded p-1 overflow-hidden text-ellipsis"
411
- tooltipClassName="max-w-80"
412
- text={currentValue}
413
- tooltip={tooltip || (typeof currentValue === 'string' ? currentValue : JSON.stringify(currentValue, null, 2))}
414
- side="left"
415
- onClick={onClick}
416
- />
417
- </div>
418
- )}
419
  </div>
420
  )
421
  }
 
1
+ import { useState, useEffect } from 'react'
2
+ import { useRef } from 'react'
3
  import { useTranslation } from 'react-i18next'
4
  import Text from '@/components/ui/Text'
 
5
  import { toast } from 'sonner'
6
  import { updateEntity, updateRelation, checkEntityNameExists } from '@/api/lightrag'
7
  import { useGraphStore } from '@/stores/graph'
8
  import { PencilIcon } from 'lucide-react'
9
+ import PropertyEditDialog from './PropertyEditDialog'
10
 
11
  /**
12
  * Interface for the EditablePropertyRow component props
 
44
  name,
45
  value: initialValue,
46
  onClick,
 
47
  entityId,
48
  entityType,
49
  sourceId,
 
54
  // Component state
55
  const { t } = useTranslation()
56
  const [isEditing, setIsEditing] = useState(false)
 
57
  const [isSubmitting, setIsSubmitting] = useState(false)
58
  const [currentValue, setCurrentValue] = useState(initialValue)
59
  const inputRef = useRef<HTMLInputElement>(null)
60
 
61
+
62
  /**
63
  * Update currentValue when initialValue changes from parent
64
  */
 
71
  */
72
  useEffect(() => {
73
  if (isEditing) {
 
74
  // Focus the input element when entering edit mode with a small delay
75
  // to ensure the input is rendered before focusing
76
  setTimeout(() => {
 
80
  }
81
  }, 50)
82
  }
83
+ }, [isEditing])
84
 
85
  /**
86
  * Get translated property name from i18n
 
93
  }
94
 
95
  /**
96
+ * Handle edit icon click to open dialog
97
  */
98
+ const handleEditClick = () => {
99
  if (isEditable && !isEditing) {
100
  setIsEditing(true)
101
  }
102
  }
103
 
104
  /**
105
+ * Handle dialog close without saving
 
 
106
  */
107
+ const handleCancel = () => {
108
+ setIsEditing(false)
 
 
 
 
109
  }
110
 
111
  /**
 
292
  * Save changes to the property value
293
  * Updates both the API and the graph visualization
294
  */
295
+ const handleSave = async (value: string) => {
296
  // Prevent duplicate submissions
297
  if (isSubmitting) return
298
 
299
  // Skip if value hasn't changed
300
+ if (value === String(currentValue)) {
301
  setIsEditing(false)
302
  return
303
  }
 
307
  try {
308
  // Handle node property updates
309
  if (entityType === 'node' && entityId) {
310
+ let updatedData = { [name]: value }
311
 
312
  // Special handling for entity_id (name) changes
313
  if (name === 'entity_id') {
314
  // Check if the new name already exists
315
+ const exists = await checkEntityNameExists(value)
316
  if (exists) {
317
  toast.error(t('graphPanel.propertiesView.errors.duplicateName'))
318
  setIsSubmitting(false)
319
  return
320
  }
321
  // For entity_id, we update entity_name in the API
322
+ updatedData = { 'entity_name': value }
323
  }
324
 
325
  // Update entity in API
326
  await updateEntity(entityId, updatedData, true)
327
  // Update graph visualization
328
+ await updateGraphNode(entityId, name, value)
329
  toast.success(t('graphPanel.propertiesView.success.entityUpdated'))
330
  }
331
  // Handle edge property updates
332
  else if (entityType === 'edge' && sourceId && targetId) {
333
+ const updatedData = { [name]: value }
334
  // Update relation in API
335
  await updateRelation(sourceId, targetId, updatedData)
336
  // Update graph visualization
337
+ await updateGraphEdge(sourceId, targetId, name, value)
338
  toast.success(t('graphPanel.propertiesView.success.relationUpdated'))
339
  }
340
 
341
  // Update local state
342
  setIsEditing(false)
343
+ setCurrentValue(value)
344
 
345
  // Notify parent component if callback provided
346
  if (onValueChange) {
347
+ onValueChange(value)
348
  }
349
  } catch (error) {
350
  console.error('Error updating property:', error)
 
356
 
357
  /**
358
  * Render the property row with edit functionality
359
+ * Shows property name, edit icon, and the current value
360
  */
361
  return (
362
+ <div className="flex items-center gap-1">
363
  {/* Property name with translation */}
364
  <span className="text-primary/60 tracking-wide whitespace-nowrap">
365
  {getPropertyNameTranslation(name)}
366
  </span>
367
 
368
+ {/* Edit icon without tooltip */}
369
+ <div>
370
+ <PencilIcon
371
+ className="h-3 w-3 text-gray-500 hover:text-gray-700 cursor-pointer"
372
+ onClick={handleEditClick}
373
+ />
374
+ </div>:
375
+
376
+ {/* Text display */}
377
+ <div className="flex items-center gap-1">
378
+ <Text
379
+ className="hover:bg-primary/20 rounded p-1 overflow-hidden text-ellipsis"
380
+ tooltipClassName="max-w-80"
381
+ text={currentValue}
382
+ side="left"
383
+ onClick={onClick}
 
 
 
 
 
 
 
 
 
 
 
 
 
384
  />
385
+ </div>
386
+
387
+ {/* Edit dialog */}
388
+ <PropertyEditDialog
389
+ isOpen={isEditing}
390
+ onClose={handleCancel}
391
+ onSave={handleSave}
392
+ propertyName={name}
393
+ initialValue={String(currentValue)}
394
+ isSubmitting={isSubmitting}
395
+ />
 
 
396
  </div>
397
  )
398
  }
lightrag_webui/src/components/graph/PropertyEditDialog.tsx ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { useState, useEffect } from 'react'
2
+ import { useTranslation } from 'react-i18next'
3
+ import {
4
+ Dialog,
5
+ DialogContent,
6
+ DialogHeader,
7
+ DialogTitle,
8
+ DialogFooter
9
+ } from '@/components/ui/Dialog'
10
+ import Button from '@/components/ui/Button'
11
+ import Input from '@/components/ui/Input'
12
+
13
+ interface PropertyEditDialogProps {
14
+ isOpen: boolean
15
+ onClose: () => void
16
+ onSave: (value: string) => void
17
+ propertyName: string
18
+ initialValue: string
19
+ isSubmitting?: boolean
20
+ }
21
+
22
+ /**
23
+ * Dialog component for editing property values
24
+ * Provides a modal with a title, multi-line text input, and save/cancel buttons
25
+ */
26
+ const PropertyEditDialog = ({
27
+ isOpen,
28
+ onClose,
29
+ onSave,
30
+ propertyName,
31
+ initialValue,
32
+ isSubmitting = false
33
+ }: PropertyEditDialogProps) => {
34
+ const { t } = useTranslation()
35
+ const [value, setValue] = useState('')
36
+
37
+ // Initialize value when dialog opens
38
+ useEffect(() => {
39
+ if (isOpen) {
40
+ setValue(initialValue)
41
+ }
42
+ }, [isOpen, initialValue])
43
+
44
+ // Get translated property name
45
+ const getPropertyNameTranslation = (name: string) => {
46
+ const translationKey = `graphPanel.propertiesView.node.propertyNames.${name}`
47
+ const translation = t(translationKey)
48
+ return translation === translationKey ? name : translation
49
+ }
50
+
51
+ const handleSave = () => {
52
+ if (value.trim() !== '') {
53
+ onSave(value)
54
+ onClose()
55
+ }
56
+ }
57
+
58
+ return (
59
+ <Dialog open={isOpen} onOpenChange={(open) => !open && onClose()}>
60
+ <DialogContent className="sm:max-w-md" aria-describedby="property-edit-description">
61
+ <DialogHeader>
62
+ <DialogTitle>
63
+ {t('graphPanel.propertiesView.editProperty', {
64
+ property: getPropertyNameTranslation(propertyName)
65
+ })}
66
+ </DialogTitle>
67
+ <p id="property-edit-description" className="text-sm text-muted-foreground">
68
+ {t('graphPanel.propertiesView.editPropertyDescription')}
69
+ </p>
70
+ </DialogHeader>
71
+
72
+ {/* Multi-line text input using textarea */}
73
+ <div className="grid gap-4 py-4">
74
+ <textarea
75
+ value={value}
76
+ onChange={(e) => setValue(e.target.value)}
77
+ rows={5}
78
+ className="border-input focus-visible:ring-ring flex w-full rounded-md border bg-transparent px-3 py-2 text-sm shadow-sm transition-colors focus-visible:ring-1 focus-visible:outline-none disabled:cursor-not-allowed disabled:opacity-50"
79
+ disabled={isSubmitting}
80
+ />
81
+ </div>
82
+
83
+ <DialogFooter>
84
+ <Button
85
+ type="button"
86
+ variant="outline"
87
+ onClick={onClose}
88
+ disabled={isSubmitting}
89
+ >
90
+ {t('common.cancel')}
91
+ </Button>
92
+ <Button
93
+ type="button"
94
+ onClick={handleSave}
95
+ disabled={isSubmitting}
96
+ >
97
+ {t('common.save')}
98
+ </Button>
99
+ </DialogFooter>
100
+ </DialogContent>
101
+ </Dialog>
102
+ )
103
+ }
104
+
105
+ export default PropertyEditDialog
lightrag_webui/src/locales/ar.json CHANGED
@@ -33,7 +33,8 @@
33
  "guestMode": "وضع بدون تسجيل دخول"
34
  },
35
  "common": {
36
- "cancel": "إلغاء"
 
37
  },
38
  "documentPanel": {
39
  "clearDocuments": {
@@ -236,12 +237,14 @@
236
  "vectorStorage": "تخزين المتجهات"
237
  },
238
  "propertiesView": {
 
 
239
  "errors": {
240
  "duplicateName": "اسم العقدة موجود بالفعل",
241
  "updateFailed": "فشل تحديث العقدة",
242
  "tryAgainLater": "يرجى المحاولة مرة أخرى لاحقًا"
243
  },
244
- "doubleClickToEdit": "انقر نقرًا مزدوجًا للتعديل",
245
  "success": {
246
  "entityUpdated": "تم تحديث العقدة بنجاح",
247
  "relationUpdated": "تم تحديث العلاقة بنجاح"
 
33
  "guestMode": "وضع بدون تسجيل دخول"
34
  },
35
  "common": {
36
+ "cancel": "إلغاء",
37
+ "save": "حفظ"
38
  },
39
  "documentPanel": {
40
  "clearDocuments": {
 
237
  "vectorStorage": "تخزين المتجهات"
238
  },
239
  "propertiesView": {
240
+ "editProperty": "تعديل {{property}}",
241
+ "editPropertyDescription": "قم بتحرير قيمة الخاصية في منطقة النص أدناه.",
242
  "errors": {
243
  "duplicateName": "اسم العقدة موجود بالفعل",
244
  "updateFailed": "فشل تحديث العقدة",
245
  "tryAgainLater": "يرجى المحاولة مرة أخرى لاحقًا"
246
  },
247
+
248
  "success": {
249
  "entityUpdated": "تم تحديث العقدة بنجاح",
250
  "relationUpdated": "تم تحديث العلاقة بنجاح"
lightrag_webui/src/locales/en.json CHANGED
@@ -33,7 +33,8 @@
33
  "guestMode": "Login Free"
34
  },
35
  "common": {
36
- "cancel": "Cancel"
 
37
  },
38
  "documentPanel": {
39
  "clearDocuments": {
@@ -235,12 +236,14 @@
235
  "vectorStorage": "Vector Storage"
236
  },
237
  "propertiesView": {
 
 
238
  "errors": {
239
  "duplicateName": "Node name already exists",
240
  "updateFailed": "Failed to update node",
241
  "tryAgainLater": "Please try again later"
242
  },
243
- "doubleClickToEdit": "Double click to edit",
244
  "success": {
245
  "entityUpdated": "Node updated successfully",
246
  "relationUpdated": "Relation updated successfully"
 
33
  "guestMode": "Login Free"
34
  },
35
  "common": {
36
+ "cancel": "Cancel",
37
+ "save": "Save"
38
  },
39
  "documentPanel": {
40
  "clearDocuments": {
 
236
  "vectorStorage": "Vector Storage"
237
  },
238
  "propertiesView": {
239
+ "editProperty": "Edit {{property}}",
240
+ "editPropertyDescription": "Edit the property value in the text area below.",
241
  "errors": {
242
  "duplicateName": "Node name already exists",
243
  "updateFailed": "Failed to update node",
244
  "tryAgainLater": "Please try again later"
245
  },
246
+
247
  "success": {
248
  "entityUpdated": "Node updated successfully",
249
  "relationUpdated": "Relation updated successfully"
lightrag_webui/src/locales/fr.json CHANGED
@@ -33,7 +33,8 @@
33
  "guestMode": "Mode sans connexion"
34
  },
35
  "common": {
36
- "cancel": "Annuler"
 
37
  },
38
  "documentPanel": {
39
  "clearDocuments": {
@@ -236,6 +237,8 @@
236
  "vectorStorage": "Stockage vectoriel"
237
  },
238
  "propertiesView": {
 
 
239
  "errors": {
240
  "duplicateName": "Le nom du nœud existe déjà",
241
  "updateFailed": "Échec de la mise à jour du nœud",
 
33
  "guestMode": "Mode sans connexion"
34
  },
35
  "common": {
36
+ "cancel": "Annuler",
37
+ "save": "Sauvegarder"
38
  },
39
  "documentPanel": {
40
  "clearDocuments": {
 
237
  "vectorStorage": "Stockage vectoriel"
238
  },
239
  "propertiesView": {
240
+ "editProperty": "Modifier {{property}}",
241
+ "editPropertyDescription": "Modifiez la valeur de la propriété dans la zone de texte ci-dessous.",
242
  "errors": {
243
  "duplicateName": "Le nom du nœud existe déjà",
244
  "updateFailed": "Échec de la mise à jour du nœud",
lightrag_webui/src/locales/zh.json CHANGED
@@ -33,7 +33,8 @@
33
  "guestMode": "无需登陆"
34
  },
35
  "common": {
36
- "cancel": "取消"
 
37
  },
38
  "documentPanel": {
39
  "clearDocuments": {
@@ -236,12 +237,14 @@
236
  "vectorStorage": "向量存储"
237
  },
238
  "propertiesView": {
 
 
239
  "errors": {
240
  "duplicateName": "节点名称已存在",
241
  "updateFailed": "更新节点失败",
242
  "tryAgainLater": "请稍后重试"
243
  },
244
- "doubleClickToEdit": "双击编辑",
245
  "success": {
246
  "entityUpdated": "节点更新成功",
247
  "relationUpdated": "关系更新成功"
 
33
  "guestMode": "无需登陆"
34
  },
35
  "common": {
36
+ "cancel": "取消",
37
+ "save": "保存"
38
  },
39
  "documentPanel": {
40
  "clearDocuments": {
 
237
  "vectorStorage": "向量存储"
238
  },
239
  "propertiesView": {
240
+ "editProperty": "编辑{{property}}",
241
+ "editPropertyDescription": "在下方文本区域编辑属性值。",
242
  "errors": {
243
  "duplicateName": "节点名称已存在",
244
  "updateFailed": "更新节点失败",
245
  "tryAgainLater": "请稍后重试"
246
  },
247
+
248
  "success": {
249
  "entityUpdated": "节点更新成功",
250
  "relationUpdated": "关系更新成功"