1
1
import { UploadFileType , UploadProps , UploadState , UploadStates , UploadTask , UploadType } from "@/src/types/shared/upload" ;
2
2
import { useDispatch , useSelector } from "react-redux" ;
3
3
import UploadField from "./helper-components/UploadField" ;
4
- import { useEffect , useState } from "react" ;
4
+ import { useCallback , useEffect , useState } from "react" ;
5
5
import UploadWrapper from "./helper-components/UploadWrapper" ;
6
6
import { selectUploadData , setImportOptions } from "@/src/reduxStore/states/upload" ;
7
7
import { useLazyQuery , useMutation } from "@apollo/client" ;
8
8
import { CREATE_PROJECT , DELETE_PROJECT , UPDATE_PROJECT_STATUS , UPDATE_PROJECT_TOKENIZER } from "@/src/services/gql/mutations/projects" ;
9
9
import { ProjectStatus } from "@/src/types/components/projects/projects-list" ;
10
10
import { timer } from "rxjs" ;
11
11
import { uploadFile } from "@/src/services/base/s3-service" ;
12
- import { CurrentPage } from "@/src/types/shared/general" ;
12
+ import { CurrentPage , CurrentPageSubKey } from "@/src/types/shared/general" ;
13
13
import { jsonCopy } from "@/submodules/javascript-functions/general" ;
14
14
import { useRouter } from "next/router" ;
15
15
import { extendAllProjects , removeFromAllProjectsById , selectAllProjects , selectProjectId } from "@/src/reduxStore/states/project" ;
@@ -53,7 +53,7 @@ export default function Upload(props: UploadProps) {
53
53
const [ getUploadTaskId ] = useLazyQuery ( GET_UPLOAD_TASK_BY_TASK_ID , { fetchPolicy : 'network-only' } ) ;
54
54
55
55
useEffect ( ( ) => {
56
- if ( ! props . uploadOptions . tokenizerValues ) return ;
56
+ if ( ! props . uploadOptions || ! props . uploadOptions . tokenizerValues ) return ;
57
57
const tokenizerValuesDisplay = [ ...props . uploadOptions . tokenizerValues ] ;
58
58
tokenizerValuesDisplay . forEach ( ( tokenizer : any , index : number ) => {
59
59
const tokenizerNameContainsBrackets = tokenizer . name . includes ( '(' ) && tokenizer . name . includes ( ')' ) ;
@@ -62,7 +62,7 @@ export default function Upload(props: UploadProps) {
62
62
tokenizerValuesDisplay [ index ] = tokenizerCopy ;
63
63
} ) ;
64
64
setPrepareTokenizedValues ( tokenizerValuesDisplay ) ;
65
- } , [ props . uploadOptions . tokenizerValues ] ) ;
65
+ } , [ props . uploadOptions ] ) ;
66
66
67
67
useEffect ( ( ) => {
68
68
if ( props . startUpload ) {
@@ -75,7 +75,7 @@ export default function Upload(props: UploadProps) {
75
75
props . isFileUploaded ( selectedFile != null ) ;
76
76
} , [ selectedFile ] ) ;
77
77
78
- function handleWebsocketNotification ( msgParts : string [ ] ) {
78
+ const handleWebsocketNotification = useCallback ( ( msgParts : string [ ] ) => {
79
79
const uploadTask = UploadHelper . getUploadTask ( ) ;
80
80
const projectId = UploadHelper . getProjectId ( ) ;
81
81
if ( ! uploadTask ) return ;
@@ -114,11 +114,11 @@ export default function Upload(props: UploadProps) {
114
114
} else {
115
115
console . log ( "unknown websocket message in part 3:" + msgParts [ 3 ] , "full message:" , msgParts )
116
116
}
117
- }
117
+ } , [ props . uploadOptions ?. deleteProjectOnFail , props . uploadOptions ?. reloadOnFinish ] ) ;
118
118
119
119
function submitUpload ( ) {
120
120
setSubmitted ( true ) ;
121
- if ( ! selectedFile ) return ;
121
+ if ( ! selectedFile || isProjectTitleDuplicate || isProjectTitleEmpty ) return ;
122
122
if ( uploadFileType == UploadFileType . RECORDS_NEW ) {
123
123
if ( projectTitle . trim ( ) == "" ) {
124
124
setIsProjectTitleEmpty ( true ) ;
@@ -219,7 +219,7 @@ export default function Upload(props: UploadProps) {
219
219
UploadHelper . setUploadTask ( task ) ;
220
220
if ( task . state == UploadStates . DONE || task . progress == 100 ) {
221
221
clearUploadTask ( ) ;
222
- if ( props . uploadOptions . reloadOnFinish ) location . reload ( ) ;
222
+ if ( props . uploadOptions && props . uploadOptions . reloadOnFinish ) location . reload ( ) ;
223
223
else setUploadStarted ( false ) ;
224
224
router . push ( '/projects/' + UploadHelper . getProjectId ( ) + '/settings' ) ;
225
225
}
@@ -253,11 +253,11 @@ export default function Upload(props: UploadProps) {
253
253
if ( findProjectName ) setIsProjectTitleDuplicate ( true ) ;
254
254
else setIsProjectTitleDuplicate ( false ) ;
255
255
}
256
-
256
+ useWebsocket ( CurrentPage . PROJECTS , handleWebsocketNotification , null , CurrentPageSubKey . FILE_UPLOAD ) ;
257
257
useWebsocket ( CurrentPage . UPLOAD_RECORDS , handleWebsocketNotification , UploadHelper . getProjectId ( ) ) ;
258
258
259
- return (
260
- < section className = { `${ ! props . uploadOptions . isModal ? 'p-4' : '' } ` } >
259
+ return < >
260
+ { props . uploadOptions && < section className = { `${ ! props . uploadOptions . isModal ? 'p-4' : '' } ` } >
261
261
{ uploadFileType == UploadFileType . PROJECT && ( < >
262
262
< UploadField isFileCleared = { selectedFile == null } uploadStarted = { uploadStarted } doingSomething = { doingSomething } progressState = { progressState } sendSelectedFile = { ( file ) => {
263
263
setSelectedFile ( file ) ;
@@ -270,7 +270,7 @@ export default function Upload(props: UploadProps) {
270
270
{ props . uploadOptions . showBadPasswordMsg && ( < div className = "text-red-700 text-xs mt-2 text-center" > Wrong password</ div > ) }
271
271
</ >
272
272
) }
273
- { uploadFileType == UploadFileType . RECORDS_NEW && ( < >
273
+ { uploadFileType == UploadFileType . RECORDS_NEW && ( < div className = "min-h-full h-[calc(100vh-7rem)] overflow-y-auto" >
274
274
< div className = "form-control" >
275
275
< label className = "text-gray-500 text-sm font-normal" > Project title</ label >
276
276
< div className = "flex flex-row" >
@@ -303,25 +303,25 @@ export default function Upload(props: UploadProps) {
303
303
isModal = { props . uploadOptions . isModal } submitUpload = { submitUpload } sendSelectedFile = { ( file ) => {
304
304
setSelectedFile ( file ) ;
305
305
} } setKey = { ( key ) => setKey ( key ) } />
306
- </ >
306
+ </ div >
307
307
) }
308
308
309
- { uploadFileType == UploadFileType . RECORDS_ADD && ( < >
309
+ { uploadFileType == UploadFileType . RECORDS_ADD && ( < div className = "min-h-full h-[calc(100vh-7rem)] overflow-y-auto" >
310
310
< div className = "text-lg leading-6 text-gray-900 font-medium inline-block" >
311
311
Record upload
312
312
</ div >
313
313
< div className = "text-sm text-gray-500 font-normal mt-2" > Add additional records to your project</ div >
314
314
< UploadWrapper uploadStarted = { uploadStarted } doingSomething = { doingSomething } progressState = { progressState } submitted = { submitted } isFileCleared = { selectedFile == null }
315
315
isModal = { props . uploadOptions . isModal } submitUpload = { submitUpload } sendSelectedFile = { ( file ) => {
316
316
setSelectedFile ( file ) ;
317
- } } setKey = { ( key ) => setKey ( key ) } /> </ > ) }
317
+ } } setKey = { ( key ) => setKey ( key ) } /> </ div > ) }
318
318
319
319
{ uploadFileType == UploadFileType . KNOWLEDGE_BASE && (
320
320
< UploadWrapper uploadStarted = { uploadStarted } doingSomething = { doingSomething } progressState = { progressState } submitted = { submitted } isFileCleared = { selectedFile == null }
321
321
isModal = { props . uploadOptions . isModal } submitUpload = { submitUpload } sendSelectedFile = { ( file ) => {
322
322
setSelectedFile ( file ) ;
323
323
} } setKey = { ( key ) => setKey ( key ) } /> )
324
324
}
325
- </ section >
326
- )
325
+ </ section > }
326
+ </ >
327
327
}
0 commit comments