1+ import { FifoLogger } from 'fifo-logger' ;
12import { fileCollectionFromFiles } from 'filelist-utils' ;
23import {
34 read ,
45 readFromWebSource ,
56 NmriumState ,
67 CURRENT_EXPORT_VERSION ,
78 ParsingOptions ,
9+ ViewState ,
810} from 'nmr-load-save' ;
911import { useCallback , useMemo , useState } from 'react' ;
1012
1113import events from '../events' ;
1214import { getFileNameFromURL } from '../utilities/getFileNameFromURL' ;
1315import { isArrayOfString } from '../utilities/isArrayOfString' ;
1416
17+ type DeepPartial < T > = {
18+ [ K in keyof T ] ?: T [ K ] extends object ? DeepPartial < T [ K ] > : T [ K ] ;
19+ } ;
20+
21+ const logger = new FifoLogger ( {
22+ onChange : ( log ) => {
23+ if ( log && [ 'error' , 'fatal' ] . includes ( log . levelLabel ) && log ?. error ) {
24+ events . trigger ( 'error' , log . error ) ;
25+ // eslint-disable-next-line no-console
26+ console . log ( log . error ) ;
27+ }
28+ } ,
29+ } ) ;
30+
1531const PARSING_OPTIONS : Partial < ParsingOptions > = {
1632 onLoadProcessing : { autoProcessing : true } ,
1733 sourceSelector : { general : { dataSelection : 'preferFT' } } ,
34+ logger,
1835} ;
1936
2037async function loadSpectraFromFiles ( files : File [ ] ) {
@@ -44,7 +61,9 @@ async function loadSpectraFromURLs(urls: string[]) {
4461
4562type NMRiumData = NmriumState [ 'data' ] ;
4663
47- type LoadOptions = { urls : string [ ] } | { files : File [ ] } ;
64+ type LoadOptions =
65+ | { urls : string [ ] ; activeTab ?: string }
66+ | { files : File [ ] ; activeTab ?: string } ;
4867
4968interface UseLoadSpectraResult {
5069 data : { version : number ; data : NMRiumData } ;
@@ -54,6 +73,7 @@ interface UseLoadSpectraResult {
5473
5574export function useLoadSpectra ( ) : UseLoadSpectraResult {
5675 const [ data , setData ] = useState < NMRiumData > ( { spectra : [ ] , molecules : [ ] } ) ;
76+ const [ activeTab , setActiveTab ] = useState < string > ( ) ;
5777 const [ isLoading , setLoading ] = useState < boolean > ( false ) ;
5878
5979 const load = useCallback ( async ( options : LoadOptions ) => {
@@ -63,12 +83,14 @@ export function useLoadSpectra(): UseLoadSpectraResult {
6383 if ( isArrayOfString ( options . urls ) ) {
6484 const result = await loadSpectraFromURLs ( options . urls ) ;
6585 setData ( result as NMRiumData ) ;
86+ setActiveTab ( options ?. activeTab ) ;
6687 } else {
6788 throw new Error ( 'The input must be a valid urls array of string[]' ) ;
6889 }
6990 } else if ( 'files' in options ) {
7091 const result = await loadSpectraFromFiles ( options . files ) ;
7192 setData ( result as NMRiumData ) ;
93+ setActiveTab ( options ?. activeTab ) ;
7294 }
7395 } catch ( error : unknown ) {
7496 const loadError = error as Error ;
@@ -80,12 +102,16 @@ export function useLoadSpectra(): UseLoadSpectraResult {
80102 }
81103 } , [ ] ) ;
82104
83- return useMemo (
84- ( ) => ( {
85- data : { version : CURRENT_EXPORT_VERSION , data } ,
105+ return useMemo ( ( ) => {
106+ let view : DeepPartial < ViewState > = { } ;
107+ if ( activeTab ) {
108+ view = { spectra : { activeTab } } ;
109+ }
110+
111+ return {
112+ data : { version : CURRENT_EXPORT_VERSION , data, view } ,
86113 load,
87114 isLoading,
88- } ) ,
89- [ data , isLoading , load ] ,
90- ) ;
115+ } ;
116+ } , [ activeTab , data , isLoading , load ] ) ;
91117}
0 commit comments