@@ -5,19 +5,7 @@ import { notification } from 'antd';
55import { ColumnType } from 'antd/lib/table/interface' ;
66import * as bodybuilder from 'bodybuilder' ;
77import json2csv , { Parser } from 'json2csv' ;
8- import {
9- get ,
10- has ,
11- isArray ,
12- isNil ,
13- isString ,
14- pick ,
15- sum ,
16- sumBy ,
17- toNumber ,
18- uniq ,
19- uniqBy ,
20- } from 'lodash' ;
8+ import { isArray , isNil , isString , pick , sumBy , toNumber } from 'lodash' ;
219import * as React from 'react' ;
2210import { useQuery } from 'react-query' ;
2311import {
@@ -48,6 +36,7 @@ import { isNumeric, parseJsonMaybe } from '../utils/index';
4836import { addColumnsForES , rowRender } from '../utils/parseESResults' ;
4937import { sparqlQueryExecutor } from '../utils/querySparqlView' ;
5038import { CartContext } from './useDataCart' ;
39+ import PromisePool from '@supercharge/promise-pool' ;
5140
5241export const EXPORT_CSV_FILENAME = 'nexus-query-result.csv' ;
5342export const CSV_MEDIATYPE = 'text/csv' ;
@@ -415,6 +404,52 @@ const getTotalContentSize = (rows: TDataSource[]) => {
415404 return size ;
416405} ;
417406
407+ export const fetchResourceForDownload = async (
408+ selfUrl : string ,
409+ nexus : ReturnType < typeof useNexusContext >
410+ ) : Promise < Resource > => {
411+ let compactResource ;
412+ let expandedResource ;
413+
414+ try {
415+ compactResource = await nexus . httpGet ( {
416+ path : selfUrl ,
417+ headers : { Accept : 'application/json' } ,
418+ } ) ;
419+
420+ const receivedExpandedId =
421+ isValidUrl ( compactResource [ '@id' ] ) &&
422+ compactResource [ '@id' ] ?. startsWith ( 'http' ) ;
423+
424+ if ( receivedExpandedId ) {
425+ return compactResource ;
426+ }
427+
428+ expandedResource = await nexus . httpGet ( {
429+ path : `${ selfUrl } ?format=expanded` ,
430+ headers : { Accept : 'application/json' } ,
431+ } ) ;
432+
433+ return {
434+ ...compactResource ,
435+ [ '@id' ] :
436+ expandedResource ?. [ 0 ] ?. [ '@id' ] ??
437+ expandedResource [ '@id' ] ??
438+ compactResource [ '@id' ] ,
439+ } ;
440+ } catch ( err ) {
441+ notification . warning ( {
442+ message : < > Could not fetch a resource with id for download { selfUrl } </ > ,
443+ description : < em > { ( err as any ) ?. reason ?? ( err as any ) ?. [ '@type' ] } </ em > ,
444+ key : selfUrl ,
445+ } ) ;
446+ // @ts -ignore TODO: Remove when we supprot es2022 in ts.
447+ throw new Error ( `Could not select resource ${ selfUrl } for download` , {
448+ cause : err ,
449+ } ) ;
450+ }
451+ } ;
452+
418453export const useAccessDataForTable = (
419454 orgLabel : string ,
420455 projectLabel : string ,
@@ -446,34 +481,31 @@ export const useAccessDataForTable = (
446481 let rowsForLS = dataPanelLS ?. selectedRows || [ ] ;
447482
448483 if ( selected ) {
449- rowKeysForLS = [
450- ...rowKeysForLS ,
451- ...changedRows . map ( row => getStudioRowKey ( row ) ) ,
452- ] ;
453-
454- const futureResources = changedRows . map ( row =>
455- fetchResourceForDownload ( getStudioRowKey ( row ) )
456- ) ;
457-
458- Promise . allSettled ( futureResources )
459- . then ( result => {
460- result . forEach ( res => {
461- if ( res . status === 'fulfilled' ) {
462- const localStorageResource = toLocalStorageResources (
463- res . value ,
464- 'studios'
465- ) ;
466- rowsForLS = [ ...rowsForLS , ...localStorageResource ] ;
467- }
468- } ) ;
484+ const { results, errors } = await PromisePool . withConcurrency ( 4 )
485+ . for ( changedRows )
486+ . handleError ( async err => {
487+ console . log (
488+ '@@error in selecting multiple resources for download in studios' ,
489+ err
490+ ) ;
469491 return ;
470492 } )
471- . then ( ( ) => {
472- saveSelectedRowsToLocalStorage ( rowKeysForLS , rowsForLS ) ;
473- } )
474- . catch ( err => {
475- console . log ( '@@error' , err ) ;
493+ . process ( async row => {
494+ const fetchedRow = await fetchResourceForDownload (
495+ getStudioRowKey ( row ) ,
496+ nexus
497+ ) ;
498+ const localStorageResources = toLocalStorageResources (
499+ fetchedRow ,
500+ 'studios'
501+ ) ;
502+ rowKeysForLS . push ( getStudioRowKey ( row ) ) ;
503+
504+ return localStorageResources ;
476505 } ) ;
506+
507+ rowsForLS = [ ...rowsForLS , ...results . flat ( ) ] ;
508+ saveSelectedRowsToLocalStorage ( rowKeysForLS , rowsForLS ) ;
477509 } else {
478510 const rowKeysToRemove = changedRows . map ( row => getStudioRowKey ( row ) ) ;
479511
@@ -500,7 +532,7 @@ export const useAccessDataForTable = (
500532 let localStorageRows = dataPanelLS ?. selectedRows || [ ] ;
501533
502534 if ( selected ) {
503- const deltaResource = await fetchResourceForDownload ( recordKey ) ;
535+ const deltaResource = await fetchResourceForDownload ( recordKey , nexus ) ;
504536 const localStorageResource = toLocalStorageResources (
505537 deltaResource ,
506538 'studios'
@@ -515,53 +547,6 @@ export const useAccessDataForTable = (
515547 saveSelectedRowsToLocalStorage ( localStorageRowKeys , localStorageRows ) ;
516548 } ;
517549
518- const fetchResourceForDownload = async (
519- selfUrl : string
520- ) : Promise < Resource > => {
521- let compactResource ;
522- let expandedResource ;
523-
524- try {
525- compactResource = await nexus . httpGet ( {
526- path : selfUrl ,
527- headers : { Accept : 'application/json' } ,
528- } ) ;
529-
530- const receivedExpandedId =
531- isValidUrl ( compactResource [ '@id' ] ) &&
532- ! isUrlCurieFormat ( compactResource [ '@id' ] ) ;
533-
534- if ( receivedExpandedId ) {
535- return compactResource ;
536- }
537-
538- expandedResource = await nexus . httpGet ( {
539- path : `${ selfUrl } ?format=expanded` ,
540- headers : { Accept : 'application/json' } ,
541- } ) ;
542-
543- return {
544- ...compactResource ,
545- [ '@id' ] :
546- expandedResource ?. [ 0 ] ?. [ '@id' ] ??
547- expandedResource [ '@id' ] ??
548- compactResource [ '@id' ] ,
549- } ;
550- } catch ( err ) {
551- notification . warning ( {
552- message : (
553- < div > Could not fetch a resource with id for download { selfUrl } </ div >
554- ) ,
555- description : < em > { ( err as any ) ?. reason ?? ( err as any ) ?. [ '@type' ] } </ em > ,
556- key : selfUrl ,
557- } ) ;
558- // @ts -ignore TODO: Remove when we supprot es2022 in ts.
559- throw new Error ( `Could not select resource ${ selfUrl } for download` , {
560- cause : err ,
561- } ) ;
562- }
563- } ;
564-
565550 const saveSelectedRowsToLocalStorage = (
566551 rowKeys : React . Key [ ] ,
567552 rows : TDataSource [ ]
0 commit comments