11import { useCallback , useMemo , useState } from 'react' ;
2- import { read } from 'nmr-load-save' ;
2+ import { read , readSource , Source } from 'nmr-load-save' ;
33import { Spectrum } from 'nmr-load-save/lib/types/Spectra/Spectrum' ;
4+ import { fileCollectionFromFiles } from 'filelist-utils' ;
45import events from '../events' ;
56import { isArrayOfString } from '../utilities/isArrayOfString' ;
6- import { loadFilesFromURLs } from '../utilities/loadFilesFromURLs' ;
7- import { createFileCollectionFromFiles } from '../utilities/createFileCollection' ;
7+ import { getFileNameFromURL } from '../utilities/getFileNameFromURL' ;
8+
9+ async function loadSpectraFromFiles ( files : File [ ] ) {
10+ // TODO use the new function from filelist-utils once they solve the problem of create filesCollection from files with empty webkitrelativepath
11+ const fileCollection = await fileCollectionFromFiles ( files ) ;
12+ const result = await read ( fileCollection ) ;
13+ // eslint-disable-next-line no-restricted-syntax
14+ for ( const spectrum of result . data . spectra ) {
15+ spectrum . source = { } as Source ;
16+ }
17+ return result ;
18+ }
19+
20+ async function loadSpectraFromURLs ( urls : string [ ] ) {
21+ const promises = urls . map ( ( url ) => {
22+ const refURL = new URL ( url ) ;
23+ let name = getFileNameFromURL ( url ) ;
24+ const hasExtension = name && name . indexOf ( '.' ) !== - 1 ;
25+ if ( ! hasExtension ) {
26+ name = `${ name } .zip` ;
27+ }
28+ return readSource ( {
29+ baseURL : refURL . origin ,
30+ files : [ { relativePath : refURL . pathname , name } ] ,
31+ } ) ;
32+ } , [ ] ) ;
33+ const results = await Promise . all ( promises ) ;
34+ const spectra : any [ ] = [ ] ;
35+ const molecules : any [ ] = [ ] ;
36+ // eslint-disable-next-line no-restricted-syntax
37+ for ( const result of results ) {
38+ spectra . push ( ...result . data . spectra ) ;
39+ molecules . push ( ...result . data . spectra ) ;
40+ }
41+ return { spectra, molecules } ;
42+ }
843
944export function useLoadSpectra ( ) {
1045 const [ data , setData ] = useState < {
@@ -17,21 +52,22 @@ export function useLoadSpectra() {
1752 async ( options : { urls : string [ ] } | { files : File [ ] } ) => {
1853 setLoading ( true ) ;
1954 try {
20- let inputFiles : File [ ] = [ ] ;
21-
2255 if ( 'urls' in options ) {
2356 if ( isArrayOfString ( options . urls ) ) {
24- inputFiles = await loadFilesFromURLs ( options . urls ) ;
57+ const { spectra, molecules } = await loadSpectraFromURLs (
58+ options . urls ,
59+ ) ;
60+ setData ( { spectra, molecules } ) ;
2561 } else {
26- throw new Error ( 'The input must be string[] ' ) ;
62+ throw new Error ( 'The input must be a valid urls array of string[]' ) ;
2763 }
2864 } else if ( 'files' in options ) {
29- inputFiles = options . files ;
65+ // TODO use the new function from filelist-utils once they solve the problem of create filesCollection from files with empty webkitrelativepath
66+ const {
67+ data : { spectra, molecules } ,
68+ } = await loadSpectraFromFiles ( options . files ) ;
69+ setData ( { spectra, molecules } ) ;
3070 }
31- // TODO use the new function from filelist-utils once they solve the problem of create filesCollection from files with empty webkitrelativepath
32- const fileCollection = await createFileCollectionFromFiles ( inputFiles ) ;
33- const { spectra, molecules } = await read ( fileCollection ) ;
34- setData ( { spectra, molecules } ) ;
3571 } catch ( error : any ) {
3672 events . trigger ( 'error' , error ) ;
3773 // eslint-disable-next-line no-console
0 commit comments