@@ -24,7 +24,11 @@ import {
24
24
TextFilter ,
25
25
} from '@cloudscape-design/components' ;
26
26
import SpaceBetween from '@cloudscape-design/components/space-between' ;
27
- import { useDeleteRagDocumentsMutation , useListRagDocumentsQuery } from '../../shared/reducers/rag.reducer' ;
27
+ import {
28
+ useDeleteRagDocumentsMutation ,
29
+ useLazyDownloadRagDocumentQuery ,
30
+ useListRagDocumentsQuery ,
31
+ } from '../../shared/reducers/rag.reducer' ;
28
32
import Table from '@cloudscape-design/components/table' ;
29
33
import ButtonDropdown from '@cloudscape-design/components/button-dropdown' ;
30
34
import { DEFAULT_PREFERENCES , PAGE_SIZE_OPTIONS , TABLE_DEFINITION , TABLE_PREFERENCES } from './DocumentLibraryConfig' ;
@@ -35,6 +39,7 @@ import { selectCurrentUserIsAdmin, selectCurrentUsername } from '../../shared/re
35
39
import { RagDocument } from '../types' ;
36
40
import { setConfirmationModal } from '../../shared/reducers/modal.reducer' ;
37
41
import { useLocalStorage } from '../../shared/hooks/use-local-storage' ;
42
+ import { downloadFile } from '../../shared/util/downloader' ;
38
43
39
44
type DocumentLibraryComponentProps = {
40
45
repositoryId ?: string ;
@@ -54,9 +59,8 @@ function disabledDeleteReason (selectedItems: ReadonlyArray<RagDocument>) {
54
59
55
60
export function DocumentLibraryComponent ( { repositoryId } : DocumentLibraryComponentProps ) : ReactElement {
56
61
const { data : allDocs , isFetching } = useListRagDocumentsQuery ( { repositoryId } , { refetchOnMountOrArgChange : 5 } ) ;
57
- const [ deleteMutation , {
58
- isLoading : isDeleteLoading ,
59
- } ] = useDeleteRagDocumentsMutation ( ) ;
62
+ const [ deleteMutation , { isLoading : isDeleteLoading } ] = useDeleteRagDocumentsMutation ( ) ;
63
+
60
64
const currentUser = useAppSelector ( selectCurrentUsername ) ;
61
65
const isAdmin = useAppSelector ( selectCurrentUserIsAdmin ) ;
62
66
const [ preferences , setPreferences ] = useLocalStorage ( 'DocumentRagPreferences' , DEFAULT_PREFERENCES ) ;
@@ -84,13 +88,18 @@ export function DocumentLibraryComponent ({ repositoryId }: DocumentLibraryCompo
84
88
selection : { trackBy : 'document_id' } ,
85
89
} ,
86
90
) ;
87
-
91
+ const [ getDownloadUrl , { isFetching : isDownloading } ] = useLazyDownloadRagDocumentQuery ( ) ;
88
92
const actionItems : ButtonDropdownProps . Item [ ] = [
89
93
{
90
94
id : 'rm' ,
91
95
text : 'Delete' ,
92
96
disabled : ! canDeleteAll ( collectionProps . selectedItems , currentUser , isAdmin ) ,
93
97
disabledReason : disabledDeleteReason ( collectionProps . selectedItems ) ,
98
+ } , {
99
+ id : 'download' ,
100
+ text : 'Download' ,
101
+ disabled : collectionProps . selectedItems . length > 1 ,
102
+ disabledReason : 'Only one file can be downloaded at a time' ,
94
103
} ,
95
104
] ;
96
105
const handleAction = async ( e : any ) => {
@@ -109,6 +118,12 @@ export function DocumentLibraryComponent ({ repositoryId }: DocumentLibraryCompo
109
118
) ;
110
119
break ;
111
120
}
121
+ case 'download' : {
122
+ const { document_id, document_name } = collectionProps . selectedItems [ 0 ] ;
123
+ const resp = await getDownloadUrl ( { documentId : document_id , repositoryId } ) ;
124
+ downloadFile ( resp . data , document_name ) ;
125
+ break ;
126
+ }
112
127
default :
113
128
console . error ( 'Action not implemented' , e . detail . id ) ;
114
129
}
@@ -150,8 +165,9 @@ export function DocumentLibraryComponent ({ repositoryId }: DocumentLibraryCompo
150
165
>
151
166
< ButtonDropdown
152
167
items = { actionItems }
153
- loading = { isDeleteLoading }
154
- onItemClick = { ( e ) => handleAction ( e ) }
168
+ loading = { isDeleteLoading || isDownloading }
169
+ disabled = { collectionProps . selectedItems . length === 0 }
170
+ onItemClick = { async ( e ) => handleAction ( e ) }
155
171
>
156
172
Actions
157
173
</ ButtonDropdown >
0 commit comments