1
- import React , { useRef , useState } from 'react'
1
+ import React , { useState } from 'react'
2
2
import { TextField , Button , Box , Typography , Table , TableHead , TableBody , TableRow , TableCell , Paper , IconButton , Dialog , DialogTitle , styled } from '@mui/material'
3
3
import apiClient from '../util/apiClient'
4
4
import { useMutation , useQuery } from '@tanstack/react-query'
5
5
import { CloudUpload , Settings } from '@mui/icons-material'
6
6
import Markdown from './Banner/Markdown'
7
+ import { useSnackbar } from 'notistack'
7
8
8
9
type RagResponse = {
9
10
id : string
@@ -67,17 +68,12 @@ const useUploadMutation = (index: RagIndexAttributes | null) => {
67
68
formData . append ( 'files' , file )
68
69
} )
69
70
70
- // Returns a stream
71
71
const response = await apiClient . put ( `/rag/indices/${ index . id } /upload` , formData , {
72
72
headers : {
73
73
'Content-Type' : 'multipart/form-data' ,
74
74
} ,
75
- responseType : 'stream' ,
76
-
77
-
78
75
} )
79
76
80
- console . log ( 'Upload response:' , response . data )
81
77
return response . data
82
78
} ,
83
79
} )
@@ -97,6 +93,7 @@ const VisuallyHiddenInput = styled('input')({
97
93
} )
98
94
99
95
const Rag : React . FC = ( ) => {
96
+ const { enqueueSnackbar } = useSnackbar ( )
100
97
const { data : indices , refetch } = useRagIndices ( )
101
98
const createIndexMutation = useCreateRagIndexMutation ( )
102
99
const deleteIndexMutation = useDeleteRagIndexMutation ( )
@@ -107,7 +104,6 @@ const Rag: React.FC = () => {
107
104
const [ response , setResponse ] = useState < RagResponse [ ] | null > ( null )
108
105
const uploadMutation = useUploadMutation ( selectedIndex )
109
106
const [ modalOpen , setModalOpen ] = useState ( false )
110
- const progressLogs = useRef < HTMLParagraphElement > ( )
111
107
112
108
const handleSubmit = async ( event : React . FormEvent ) => {
113
109
event . preventDefault ( )
@@ -122,45 +118,25 @@ const Rag: React.FC = () => {
122
118
setInputValue ( '' )
123
119
}
124
120
125
- // Processes the upload progress stream which returns JSON objects
126
- const processUploadProgressStream = ( stream ) => {
127
- stream . on ( 'data' , ( data : any ) => {
128
- const parsedData = JSON . parse ( data . toString ( ) )
129
- console . log ( 'Parsed data:' , parsedData )
130
- if ( parsedData . stage === 'done' ) {
131
- progressLogs . current . innerHTML += `Upload completed: ${ JSON . stringify ( parsedData ) } \n`
132
- } else if ( parsedData . error ) {
133
- progressLogs . current . innerHTML += `Error: ${ parsedData . error } \n`
134
- } else {
135
- progressLogs . current . innerHTML += `Progress: ${ JSON . stringify ( parsedData ) } \n`
136
- }
137
- } )
138
- stream . on ( 'end' , ( ) => {
139
- progressLogs . current . innerHTML += 'Upload stream ended.\n'
140
- } )
141
- stream . on ( 'error' , ( err : any ) => {
142
- progressLogs . current . innerHTML += `Error: ${ err } \n`
143
- } )
144
- stream . on ( 'close' , ( ) => {
145
- progressLogs . current . innerHTML += 'Upload stream closed.\n'
146
- } )
147
- }
148
-
149
121
return (
150
122
< Box sx = { { display : 'flex' , gap : 2 } } >
151
123
< Dialog open = { ! ! selectedIndex && modalOpen } onClose = { ( ) => setModalOpen ( false ) } >
152
124
< DialogTitle > Edit { selectedIndex ?. metadata ?. name } </ DialogTitle >
153
125
< Box sx = { { padding : 2 , display : 'flex' , gap : 2 } } >
154
- < Button component = "label" role = { undefined } variant = "contained" tabIndex = { - 1 } startIcon = { < CloudUpload /> } >
155
- Upload files
126
+ < Button component = "label" role = { undefined } variant = "contained" tabIndex = { - 1 } startIcon = { < CloudUpload /> } disabled = { uploadMutation . isPending } >
127
+ { uploadMutation . isPending ? 'Uploading...' : ' Upload Files' }
156
128
< VisuallyHiddenInput
157
129
type = "file"
158
130
onChange = { async ( event ) => {
159
131
const files = event . target . files
160
132
console . log ( 'Files selected:' , files )
161
133
if ( files && files . length > 0 ) {
162
- const stream = await uploadMutation . mutateAsync ( files )
163
- processUploadProgressStream ( stream )
134
+ await uploadMutation . mutateAsync ( files )
135
+ refetch ( )
136
+ setModalOpen ( false )
137
+ enqueueSnackbar ( 'Files uploaded successfully' , {
138
+ variant : 'success' ,
139
+ } )
164
140
}
165
141
} }
166
142
multiple
@@ -180,9 +156,6 @@ const Rag: React.FC = () => {
180
156
Delete Index
181
157
</ Button >
182
158
</ Box >
183
- < Box sx = { { padding : 2 } } >
184
- < p ref = { progressLogs } style = { { whiteSpace : 'pre-wrap' } } />
185
- </ Box >
186
159
</ Dialog >
187
160
< Box >
188
161
< Typography variant = "h4" mb = "1rem" >
0 commit comments