yangdx commited on
Commit
eef0ccc
·
1 Parent(s): 292edcb

Refactoring clear document UI

Browse files
lightrag_webui/src/api/lightrag.ts CHANGED
@@ -382,6 +382,14 @@ export const clearDocuments = async (): Promise<DocActionResponse> => {
382
  return response.data
383
  }
384
 
 
 
 
 
 
 
 
 
385
  export const getAuthStatus = async (): Promise<AuthStatusResponse> => {
386
  try {
387
  // Add a timeout to the request to prevent hanging
 
382
  return response.data
383
  }
384
 
385
+ export const clearCache = async (modes?: string[]): Promise<{
386
+ status: 'success' | 'fail'
387
+ message: string
388
+ }> => {
389
+ const response = await axiosInstance.post('/documents/clear_cache', { modes })
390
+ return response.data
391
+ }
392
+
393
  export const getAuthStatus = async (): Promise<AuthStatusResponse> => {
394
  try {
395
  // Add a timeout to the request to prevent hanging
lightrag_webui/src/components/documents/ClearDocumentsDialog.tsx CHANGED
@@ -1,4 +1,4 @@
1
- import { useState, useCallback } from 'react'
2
  import Button from '@/components/ui/Button'
3
  import {
4
  Dialog,
@@ -6,24 +6,79 @@ import {
6
  DialogDescription,
7
  DialogHeader,
8
  DialogTitle,
9
- DialogTrigger
 
10
  } from '@/components/ui/Dialog'
 
 
11
  import { toast } from 'sonner'
12
  import { errorMessage } from '@/lib/utils'
13
- import { clearDocuments } from '@/api/lightrag'
 
14
 
15
- import { EraserIcon } from 'lucide-react'
16
  import { useTranslation } from 'react-i18next'
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  export default function ClearDocumentsDialog() {
19
  const { t } = useTranslation()
20
  const [open, setOpen] = useState(false)
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  const handleClear = useCallback(async () => {
 
 
23
  try {
 
24
  const result = await clearDocuments()
 
 
 
 
 
 
 
 
 
 
 
25
  if (result.status === 'success') {
26
  toast.success(t('documentPanel.clearDocuments.success'))
 
 
 
 
 
 
 
 
 
27
  setOpen(false)
28
  } else {
29
  toast.error(t('documentPanel.clearDocuments.failed', { message: result.message }))
@@ -31,7 +86,7 @@ export default function ClearDocumentsDialog() {
31
  } catch (err) {
32
  toast.error(t('documentPanel.clearDocuments.error', { error: errorMessage(err) }))
33
  }
34
- }, [setOpen, t])
35
 
36
  return (
37
  <Dialog open={open} onOpenChange={setOpen}>
@@ -42,12 +97,58 @@ export default function ClearDocumentsDialog() {
42
  </DialogTrigger>
43
  <DialogContent className="sm:max-w-xl" onCloseAutoFocus={(e) => e.preventDefault()}>
44
  <DialogHeader>
45
- <DialogTitle>{t('documentPanel.clearDocuments.title')}</DialogTitle>
46
- <DialogDescription>{t('documentPanel.clearDocuments.confirm')}</DialogDescription>
 
 
 
 
 
 
 
 
 
 
47
  </DialogHeader>
48
- <Button variant="destructive" onClick={handleClear}>
49
- {t('documentPanel.clearDocuments.confirmButton')}
50
- </Button>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  </DialogContent>
52
  </Dialog>
53
  )
 
1
+ import { useState, useCallback, useEffect } from 'react'
2
  import Button from '@/components/ui/Button'
3
  import {
4
  Dialog,
 
6
  DialogDescription,
7
  DialogHeader,
8
  DialogTitle,
9
+ DialogTrigger,
10
+ DialogFooter
11
  } from '@/components/ui/Dialog'
12
+ import Input from '@/components/ui/Input'
13
+ import Checkbox from '@/components/ui/Checkbox'
14
  import { toast } from 'sonner'
15
  import { errorMessage } from '@/lib/utils'
16
+ import { clearDocuments, clearCache, getDocuments } from '@/api/lightrag'
17
+ import { useBackendState } from '@/stores/state'
18
 
19
+ import { EraserIcon, AlertTriangleIcon } from 'lucide-react'
20
  import { useTranslation } from 'react-i18next'
21
 
22
+ // 简单的Label组件
23
+ const Label = ({
24
+ htmlFor,
25
+ className,
26
+ children,
27
+ ...props
28
+ }: React.LabelHTMLAttributes<HTMLLabelElement>) => (
29
+ <label
30
+ htmlFor={htmlFor}
31
+ className={className}
32
+ {...props}
33
+ >
34
+ {children}
35
+ </label>
36
+ )
37
+
38
  export default function ClearDocumentsDialog() {
39
  const { t } = useTranslation()
40
  const [open, setOpen] = useState(false)
41
+ const [confirmText, setConfirmText] = useState('')
42
+ const [clearCacheOption, setClearCacheOption] = useState(false)
43
+ const isConfirmEnabled = confirmText.toLowerCase() === 'yes'
44
+ const check = useBackendState.use.check()
45
+
46
+ // 重置状态当对话框关闭时
47
+ useEffect(() => {
48
+ if (!open) {
49
+ setConfirmText('')
50
+ setClearCacheOption(false)
51
+ }
52
+ }, [open])
53
 
54
  const handleClear = useCallback(async () => {
55
+ if (!isConfirmEnabled) return
56
+
57
  try {
58
+ // 清空文档
59
  const result = await clearDocuments()
60
+
61
+ // 如果选择了清空缓存,则清空缓存
62
+ if (clearCacheOption) {
63
+ try {
64
+ await clearCache()
65
+ toast.success(t('documentPanel.clearDocuments.cacheCleared'))
66
+ } catch (cacheErr) {
67
+ toast.error(t('documentPanel.clearDocuments.cacheClearFailed', { error: errorMessage(cacheErr) }))
68
+ }
69
+ }
70
+
71
  if (result.status === 'success') {
72
  toast.success(t('documentPanel.clearDocuments.success'))
73
+
74
+ // 刷新文档列表和后端状态
75
+ try {
76
+ await getDocuments()
77
+ check()
78
+ } catch (refreshErr) {
79
+ console.error('Error refreshing documents:', refreshErr)
80
+ }
81
+
82
  setOpen(false)
83
  } else {
84
  toast.error(t('documentPanel.clearDocuments.failed', { message: result.message }))
 
86
  } catch (err) {
87
  toast.error(t('documentPanel.clearDocuments.error', { error: errorMessage(err) }))
88
  }
89
+ }, [isConfirmEnabled, clearCacheOption, setOpen, t, check])
90
 
91
  return (
92
  <Dialog open={open} onOpenChange={setOpen}>
 
97
  </DialogTrigger>
98
  <DialogContent className="sm:max-w-xl" onCloseAutoFocus={(e) => e.preventDefault()}>
99
  <DialogHeader>
100
+ <DialogTitle className="flex items-center gap-2 text-red-500 dark:text-red-400 font-bold">
101
+ <AlertTriangleIcon className="h-5 w-5" />
102
+ {t('documentPanel.clearDocuments.title')}
103
+ </DialogTitle>
104
+ <DialogDescription className="pt-2">
105
+ <div className="text-red-500 dark:text-red-400 font-semibold mb-4">
106
+ {t('documentPanel.clearDocuments.warning')}
107
+ </div>
108
+ <div className="mb-4">
109
+ {t('documentPanel.clearDocuments.confirm')}
110
+ </div>
111
+ </DialogDescription>
112
  </DialogHeader>
113
+
114
+ <div className="space-y-4">
115
+ <div className="space-y-2">
116
+ <Label htmlFor="confirm-text" className="text-sm font-medium">
117
+ {t('documentPanel.clearDocuments.confirmPrompt')}
118
+ </Label>
119
+ <Input
120
+ id="confirm-text"
121
+ value={confirmText}
122
+ onChange={(e: React.ChangeEvent<HTMLInputElement>) => setConfirmText(e.target.value)}
123
+ placeholder={t('documentPanel.clearDocuments.confirmPlaceholder')}
124
+ className="w-full"
125
+ />
126
+ </div>
127
+
128
+ <div className="flex items-center space-x-2">
129
+ <Checkbox
130
+ id="clear-cache"
131
+ checked={clearCacheOption}
132
+ onCheckedChange={(checked: boolean | 'indeterminate') => setClearCacheOption(checked === true)}
133
+ />
134
+ <Label htmlFor="clear-cache" className="text-sm font-medium cursor-pointer">
135
+ {t('documentPanel.clearDocuments.clearCache')}
136
+ </Label>
137
+ </div>
138
+ </div>
139
+
140
+ <DialogFooter>
141
+ <Button variant="outline" onClick={() => setOpen(false)}>
142
+ {t('common.cancel')}
143
+ </Button>
144
+ <Button
145
+ variant="destructive"
146
+ onClick={handleClear}
147
+ disabled={!isConfirmEnabled}
148
+ >
149
+ {t('documentPanel.clearDocuments.confirmButton')}
150
+ </Button>
151
+ </DialogFooter>
152
  </DialogContent>
153
  </Dialog>
154
  )
lightrag_webui/src/locales/ar.json CHANGED
@@ -32,14 +32,23 @@
32
  "authDisabled": "تم تعطيل المصادقة. استخدام وضع بدون تسجيل دخول.",
33
  "guestMode": "وضع بدون تسجيل دخول"
34
  },
 
 
 
35
  "documentPanel": {
36
  "clearDocuments": {
37
  "button": "مسح",
38
  "tooltip": "مسح المستندات",
39
  "title": "مسح المستندات",
 
40
  "confirm": "هل تريد حقًا مسح جميع المستندات؟",
 
 
 
41
  "confirmButton": "نعم",
42
  "success": "تم مسح المستندات بنجاح",
 
 
43
  "failed": "فشل مسح المستندات:\n{{message}}",
44
  "error": "فشل مسح المستندات:\n{{error}}"
45
  },
 
32
  "authDisabled": "تم تعطيل المصادقة. استخدام وضع بدون تسجيل دخول.",
33
  "guestMode": "وضع بدون تسجيل دخول"
34
  },
35
+ "common": {
36
+ "cancel": "إلغاء"
37
+ },
38
  "documentPanel": {
39
  "clearDocuments": {
40
  "button": "مسح",
41
  "tooltip": "مسح المستندات",
42
  "title": "مسح المستندات",
43
+ "warning": "تحذير: سيؤدي هذا الإجراء إلى حذف جميع المستندات بشكل دائم ولا يمكن التراجع عنه!",
44
  "confirm": "هل تريد حقًا مسح جميع المستندات؟",
45
+ "confirmPrompt": "اكتب 'yes' لتأكيد هذا الإجراء",
46
+ "confirmPlaceholder": "اكتب yes للتأكيد",
47
+ "clearCache": "مسح ذاكرة التخزين المؤقت أيضًا",
48
  "confirmButton": "نعم",
49
  "success": "تم مسح المستندات بنجاح",
50
+ "cacheCleared": "تم مسح ذاكرة التخزين المؤقت بنجاح",
51
+ "cacheClearFailed": "فشل مسح ذاكرة التخزين المؤقت:\n{{error}}",
52
  "failed": "فشل مسح المستندات:\n{{message}}",
53
  "error": "فشل مسح المستندات:\n{{error}}"
54
  },
lightrag_webui/src/locales/en.json CHANGED
@@ -32,14 +32,23 @@
32
  "authDisabled": "Authentication is disabled. Using login free mode.",
33
  "guestMode": "Login Free"
34
  },
 
 
 
35
  "documentPanel": {
36
  "clearDocuments": {
37
  "button": "Clear",
38
  "tooltip": "Clear documents",
39
  "title": "Clear Documents",
 
40
  "confirm": "Do you really want to clear all documents?",
 
 
 
41
  "confirmButton": "YES",
42
  "success": "Documents cleared successfully",
 
 
43
  "failed": "Clear Documents Failed:\n{{message}}",
44
  "error": "Clear Documents Failed:\n{{error}}"
45
  },
 
32
  "authDisabled": "Authentication is disabled. Using login free mode.",
33
  "guestMode": "Login Free"
34
  },
35
+ "common": {
36
+ "cancel": "Cancel"
37
+ },
38
  "documentPanel": {
39
  "clearDocuments": {
40
  "button": "Clear",
41
  "tooltip": "Clear documents",
42
  "title": "Clear Documents",
43
+ "warning": "WARNING: This action will permanently delete all documents and cannot be undone!",
44
  "confirm": "Do you really want to clear all documents?",
45
+ "confirmPrompt": "Type 'yes' to confirm this action",
46
+ "confirmPlaceholder": "Type yes to confirm",
47
+ "clearCache": "Also clear cache",
48
  "confirmButton": "YES",
49
  "success": "Documents cleared successfully",
50
+ "cacheCleared": "Cache cleared successfully",
51
+ "cacheClearFailed": "Failed to clear cache:\n{{error}}",
52
  "failed": "Clear Documents Failed:\n{{message}}",
53
  "error": "Clear Documents Failed:\n{{error}}"
54
  },
lightrag_webui/src/locales/fr.json CHANGED
@@ -32,14 +32,23 @@
32
  "authDisabled": "L'authentification est désactivée. Utilisation du mode sans connexion.",
33
  "guestMode": "Mode sans connexion"
34
  },
 
 
 
35
  "documentPanel": {
36
  "clearDocuments": {
37
  "button": "Effacer",
38
  "tooltip": "Effacer les documents",
39
  "title": "Effacer les documents",
 
40
  "confirm": "Voulez-vous vraiment effacer tous les documents ?",
 
 
 
41
  "confirmButton": "OUI",
42
  "success": "Documents effacés avec succès",
 
 
43
  "failed": "Échec de l'effacement des documents :\n{{message}}",
44
  "error": "Échec de l'effacement des documents :\n{{error}}"
45
  },
 
32
  "authDisabled": "L'authentification est désactivée. Utilisation du mode sans connexion.",
33
  "guestMode": "Mode sans connexion"
34
  },
35
+ "common": {
36
+ "cancel": "Annuler"
37
+ },
38
  "documentPanel": {
39
  "clearDocuments": {
40
  "button": "Effacer",
41
  "tooltip": "Effacer les documents",
42
  "title": "Effacer les documents",
43
+ "warning": "ATTENTION : Cette action supprimera définitivement tous les documents et ne peut pas être annulée !",
44
  "confirm": "Voulez-vous vraiment effacer tous les documents ?",
45
+ "confirmPrompt": "Tapez 'yes' pour confirmer cette action",
46
+ "confirmPlaceholder": "Tapez yes pour confirmer",
47
+ "clearCache": "Effacer également le cache",
48
  "confirmButton": "OUI",
49
  "success": "Documents effacés avec succès",
50
+ "cacheCleared": "Cache effacé avec succès",
51
+ "cacheClearFailed": "Échec de l'effacement du cache :\n{{error}}",
52
  "failed": "Échec de l'effacement des documents :\n{{message}}",
53
  "error": "Échec de l'effacement des documents :\n{{error}}"
54
  },
lightrag_webui/src/locales/zh.json CHANGED
@@ -32,14 +32,23 @@
32
  "authDisabled": "认证已禁用,使用无需登陆模式。",
33
  "guestMode": "无需登陆"
34
  },
 
 
 
35
  "documentPanel": {
36
  "clearDocuments": {
37
  "button": "清空",
38
  "tooltip": "清空文档",
39
  "title": "清空文档",
 
40
  "confirm": "确定要清空所有文档吗?",
 
 
 
41
  "confirmButton": "确定",
42
  "success": "文档清空成功",
 
 
43
  "failed": "清空文档失败:\n{{message}}",
44
  "error": "清空文档失败:\n{{error}}"
45
  },
 
32
  "authDisabled": "认证已禁用,使用无需登陆模式。",
33
  "guestMode": "无需登陆"
34
  },
35
+ "common": {
36
+ "cancel": "取消"
37
+ },
38
  "documentPanel": {
39
  "clearDocuments": {
40
  "button": "清空",
41
  "tooltip": "清空文档",
42
  "title": "清空文档",
43
+ "warning": "警告:此操作将永久删除所有文档,无法恢复!",
44
  "confirm": "确定要清空所有文档吗?",
45
+ "confirmPrompt": "请输入 yes 确认操作",
46
+ "confirmPlaceholder": "输入 yes 确认",
47
+ "clearCache": "同时清空缓存",
48
  "confirmButton": "确定",
49
  "success": "文档清空成功",
50
+ "cacheCleared": "缓存清空成功",
51
+ "cacheClearFailed": "清空缓存失败:\n{{error}}",
52
  "failed": "清空文档失败:\n{{message}}",
53
  "error": "清空文档失败:\n{{error}}"
54
  },