@@ -13,8 +13,9 @@ import {
13
13
import { useDropzone } from "react-dropzone" ;
14
14
import { Layout } from "@/components/Layout" ;
15
15
import { LoadingButton } from "@mui/lab" ;
16
- import { postDocumentToIndex } from "../api" ;
16
+ import { usePostDocumentToIndex } from "../api" ;
17
17
import { useAuth } from "@/utils/auth" ;
18
+ import { useShowIndexingStatusStore } from "../store/indexingStatusStore" ;
18
19
19
20
interface CustomError {
20
21
type : string ;
@@ -31,11 +32,13 @@ export const ImportFromPDFModal: React.FC<ImportFromPDFModalProps> = ({
31
32
onClose,
32
33
} ) => {
33
34
const { token } = useAuth ( ) ;
35
+ const { setIsOpen : setOpenIndexHistoryModal } = useShowIndexingStatusStore ( ) ;
34
36
const [ files , setFiles ] = useState < File [ ] > ( [ ] ) ;
35
37
const [ error , setError ] = useState < string > ( "" ) ;
36
38
const [ loading , setLoading ] = useState ( false ) ;
37
39
const [ importErrorMessages , setImportErrorMessages ] = useState < string [ ] > ( [ ] ) ;
38
40
const [ importSuccess , setImportSuccess ] = useState < boolean | null > ( null ) ;
41
+ const { mutate : postDocumentToIndex } = usePostDocumentToIndex ( token ! ) ;
39
42
40
43
useEffect ( ( ) => {
41
44
if ( ! open ) {
@@ -80,59 +83,36 @@ export const ImportFromPDFModal: React.FC<ImportFromPDFModalProps> = ({
80
83
setImportErrorMessages ( [ ] ) ;
81
84
// add artificial delay to show loading spinner (for UX)
82
85
await new Promise ( ( resolve ) => setTimeout ( resolve , 500 ) ) ;
83
- try {
84
- const response = await postDocumentToIndex ( files [ 0 ] , token ! ) ;
85
- if ( response . status === 200 ) {
86
- setImportSuccess ( true ) ;
87
- setFiles ( [ ] ) ;
88
- } else {
89
- console . error ( "Error uploading file:" , response . detail ) ;
90
- if ( response . detail . errors ) {
91
- const errorDescriptions = response . detail . errors . map (
92
- ( error : CustomError ) => error . description ,
93
- ) ;
94
- setImportErrorMessages ( errorDescriptions ) ;
95
- } else {
86
+ postDocumentToIndex (
87
+ { file : files [ 0 ] } ,
88
+ {
89
+ onSuccess : ( ) => {
90
+ setImportSuccess ( true ) ;
91
+ setFiles ( [ ] ) ;
92
+ } ,
93
+ onError : ( error ) => {
96
94
setImportErrorMessages ( [ "An unknown error occurred" ] ) ;
97
- }
98
- }
99
- } catch ( error ) {
100
- console . error ( "Error during import:" , error ) ;
101
- setImportErrorMessages ( [
102
- "An unexpected error occurred. Please try again later." ,
103
- ] ) ;
104
- } finally {
105
- setLoading ( false ) ;
106
- setFiles ( [ ] ) ;
107
- }
95
+ } ,
96
+ onSettled : ( ) => {
97
+ setLoading ( false ) ;
98
+ } ,
99
+ } ,
100
+ ) ;
108
101
} else {
109
- console . error ( "No file selected" ) ;
110
102
setImportErrorMessages ( [ "No file selected" ] ) ;
111
103
}
112
104
} ;
113
105
114
- // modal auto-close after successful import
115
- useEffect ( ( ) => {
116
- let timerId : NodeJS . Timeout ;
117
- if ( importSuccess ) {
118
- timerId = setTimeout ( ( ) => {
119
- onClose ( ) ;
120
- window . location . reload ( ) ;
121
- } , 1000 ) ;
122
- }
123
- return ( ) => clearTimeout ( timerId ) ;
124
- } , [ importSuccess ] ) ;
125
-
126
106
return (
127
107
< Dialog open = { open } onClose = { onClose } maxWidth = "sm" fullWidth >
128
108
< DialogTitle id = "import-pdf-dialog-title" >
129
- Import New Content From PDF
109
+ Generate New Content From PDF
130
110
</ DialogTitle >
131
111
< DialogContent >
132
112
< DialogContentText id = "import-pdf-dialog-description" >
133
113
< Typography >
134
- You can use this feature to import new contents from PDF files into AAQ. The
135
- system will automatically create cards from file content.
114
+ You can use this feature to generate new contents from PDF files into AAQ.
115
+ The system will automatically create cards from file content.
136
116
</ Typography >
137
117
< p >
138
118
⚠️ Maximum file size is 100MB.
@@ -203,23 +183,45 @@ export const ImportFromPDFModal: React.FC<ImportFromPDFModalProps> = ({
203
183
{ importSuccess && (
204
184
< >
205
185
< Layout . Spacer multiplier = { 2 } />
206
- < Alert variant = "standard" severity = "success" >
207
- File uploaded successfully!
186
+ < Alert
187
+ variant = "standard"
188
+ severity = "success"
189
+ action = {
190
+ < Box sx = { { display : "flex" , gap : 1 } } >
191
+ < Button
192
+ variant = "outlined"
193
+ color = "inherit"
194
+ size = "small"
195
+ onClick = { ( ) => {
196
+ onClose ( ) ;
197
+ setOpenIndexHistoryModal ( true ) ;
198
+ } }
199
+ >
200
+ Check Status
201
+ </ Button >
202
+ </ Box >
203
+ }
204
+ >
205
+ Card generation task successfully started!
208
206
</ Alert >
209
207
</ >
210
208
) }
211
209
</ DialogContent >
212
210
< DialogActions sx = { { marginBottom : 1 , marginRight : 1 } } >
213
211
< Button onClick = { onClose } > Cancel</ Button >
214
- < LoadingButton
215
- variant = "contained"
216
- disabled = { files . length === 0 || loading || ! ! error }
217
- autoFocus
218
- loading = { loading }
219
- onClick = { handleSubmit }
220
- >
221
- { importSuccess ? "Imported" : "Import" }
222
- </ LoadingButton >
212
+ { ! importSuccess && (
213
+ < >
214
+ < LoadingButton
215
+ variant = "contained"
216
+ disabled = { files . length === 0 || loading || ! ! error }
217
+ autoFocus
218
+ loading = { loading }
219
+ onClick = { handleSubmit }
220
+ >
221
+ Import
222
+ </ LoadingButton >
223
+ </ >
224
+ ) }
223
225
</ DialogActions >
224
226
</ Dialog >
225
227
) ;
0 commit comments