@@ -70,8 +70,10 @@ const CustomAPIFileInput = () => {
7070 }
7171 ) ;
7272
73- const { data : existIdList = [ ] } = useRequest2 (
74- ( ) => getApiDatasetFileListExistId ( { datasetId : datasetDetail . _id } ) ,
73+ const { data : existIdList = new Set ( ) } = useRequest2 (
74+ async ( ) => {
75+ return new Set < string > ( await getApiDatasetFileListExistId ( { datasetId : datasetDetail . _id } ) ) ;
76+ } ,
7577 {
7678 manual : false
7779 }
@@ -89,35 +91,41 @@ const CustomAPIFileInput = () => {
8991 const allFiles : APIFileItem [ ] = [ ] ;
9092
9193 for ( const file of files ) {
92- if ( file . type === 'folder' ) {
94+ if ( sources . some ( ( item ) => item . apiFileId === file . id ) ) {
95+ allFiles . push ( file ) ;
96+ continue ;
97+ }
98+
99+ if ( file . hasChild ) {
93100 const folderFiles = await getApiDatasetFileList ( {
94101 datasetId : datasetDetail . _id ,
95102 parentId : file ?. id
96103 } ) ;
97104
98105 const subFiles = await getFilesRecursively ( folderFiles ) ;
99106 allFiles . push ( ...subFiles ) ;
100- } else {
101- allFiles . push ( file ) ;
102107 }
108+ allFiles . push ( file ) ;
103109 }
104110
105111 return allFiles ;
106112 } ;
107113
108114 const allFiles = await getFilesRecursively ( selectFiles ) ;
115+ const uniqueFiles = allFiles . filter (
116+ ( item , index , array ) =>
117+ ! existIdList . has ( item . id ) && array . findIndex ( ( file ) => file . id === item . id ) === index
118+ ) ;
109119
110120 setSources (
111- allFiles
112- . filter ( ( item ) => ! existIdList . includes ( item . id ) )
113- . map ( ( item ) => ( {
114- id : item . id ,
115- apiFileId : item . id ,
116- apiFile : item ,
117- createStatus : 'waiting' ,
118- sourceName : item . name ,
119- icon : getSourceNameIcon ( { sourceName : item . name } ) as any
120- } ) )
121+ uniqueFiles . map ( ( item ) => ( {
122+ id : item . id ,
123+ apiFileId : item . id ,
124+ apiFile : item ,
125+ createStatus : 'waiting' ,
126+ sourceName : item . name ,
127+ icon : getSourceNameIcon ( { sourceName : item . name } ) as any
128+ } ) )
121129 ) ;
122130 } ,
123131 {
@@ -147,15 +155,24 @@ const CustomAPIFileInput = () => {
147155 [ selectFiles ]
148156 ) ;
149157
150- const handleSelectAll = useCallback ( ( ) => {
151- const isAllSelected = fileList . length === selectFiles . length ;
158+ const isAllSelected = useMemo ( ( ) => {
159+ return fileList . every (
160+ ( item ) => existIdList . has ( item . id ) || selectFiles . some ( ( file ) => file . id === item . id )
161+ ) ;
162+ } , [ fileList , selectFiles , existIdList ] ) ;
152163
164+ const handleSelectAll = useCallback ( ( ) => {
153165 if ( isAllSelected ) {
154- setSelectFiles ( [ ] ) ;
166+ setSelectFiles ( ( state ) =>
167+ state . filter ( ( file ) => ! fileList . find ( ( item ) => item . id === file . id ) )
168+ ) ;
155169 } else {
156- setSelectFiles ( fileList ) ;
170+ setSelectFiles ( ( state ) => [
171+ ...state . filter ( ( file ) => ! fileList . find ( ( item ) => item . id === file . id ) ) ,
172+ ...fileList . filter ( ( item ) => ! existIdList . has ( item . id ) )
173+ ] ) ;
157174 }
158- } , [ fileList , selectFiles ] ) ;
175+ } , [ isAllSelected , fileList , existIdList ] ) ;
159176
160177 return (
161178 < MyBox isLoading = { loading } position = "relative" h = "full" >
@@ -193,23 +210,22 @@ const CustomAPIFileInput = () => {
193210 fontSize = { 'sm' }
194211 fontWeight = { 'medium' }
195212 color = { 'myGray.900' }
196- onClick = { ( e ) => {
197- if ( ! ( e . target as HTMLElement ) . closest ( '.checkbox' ) ) {
198- handleSelectAll ( ) ;
199- }
200- } }
213+ // onClick={(e) => {
214+ // if (!(e.target as HTMLElement).closest('.checkbox')) {
215+ // handleSelectAll();
216+ // }
217+ // }}
201218 >
202219 < Checkbox
203220 className = "checkbox"
204221 mr = { 2 }
205- isChecked = { fileList . length === selectFiles . length }
222+ isChecked = { isAllSelected }
206223 onChange = { handleSelectAll }
207224 />
208225 { t ( 'common:Select_all' ) }
209226 </ Flex >
210227 { fileList . map ( ( item ) => {
211- const isFolder = item . type === 'folder' ;
212- const isExists = existIdList . includes ( item . id ) ;
228+ const isExists = existIdList . has ( item . id ) ;
213229 const isChecked = isExists || selectFiles . some ( ( file ) => file . id === item . id ) ;
214230
215231 return (
@@ -243,9 +259,9 @@ const CustomAPIFileInput = () => {
243259 />
244260 < MyIcon
245261 name = {
246- ! isFolder
247- ? ( getSourceNameIcon ( { sourceName : item . name } ) as any )
248- : 'common/folderFill'
262+ item . type === 'folder'
263+ ? 'common/folderFill'
264+ : ( getSourceNameIcon ( { sourceName : item . name } ) as any )
249265 }
250266 w = { '18px' }
251267 mr = { 1.5 }
0 commit comments