@@ -6,13 +6,18 @@ import {
66 NmriumState ,
77 CURRENT_EXPORT_VERSION ,
88 ParsingOptions ,
9+ ViewState ,
910} from 'nmr-load-save' ;
1011import { useCallback , useMemo , useState } from 'react' ;
1112
1213import events from '../events' ;
1314import { getFileNameFromURL } from '../utilities/getFileNameFromURL' ;
1415import { isArrayOfString } from '../utilities/isArrayOfString' ;
1516
17+ type DeepPartial < T > = {
18+ [ K in keyof T ] ?: T [ K ] extends object ? DeepPartial < T [ K ] > : T [ K ] ;
19+ } ;
20+
1621const logger = new FifoLogger ( {
1722 onChange : ( log ) => {
1823 if ( log && [ 'error' , 'fatal' ] . includes ( log . levelLabel ) && log ?. error ) {
@@ -56,7 +61,9 @@ async function loadSpectraFromURLs(urls: string[]) {
5661
5762type NMRiumData = NmriumState [ 'data' ] ;
5863
59- type LoadOptions = { urls : string [ ] } | { files : File [ ] } ;
64+ type LoadOptions =
65+ | { urls : string [ ] ; activeTab ?: string }
66+ | { files : File [ ] ; activeTab ?: string } ;
6067
6168interface UseLoadSpectraResult {
6269 data : { version : number ; data : NMRiumData } ;
@@ -66,6 +73,7 @@ interface UseLoadSpectraResult {
6673
6774export function useLoadSpectra ( ) : UseLoadSpectraResult {
6875 const [ data , setData ] = useState < NMRiumData > ( { spectra : [ ] , molecules : [ ] } ) ;
76+ const [ activeTab , setActiveTab ] = useState < string > ( ) ;
6977 const [ isLoading , setLoading ] = useState < boolean > ( false ) ;
7078
7179 const load = useCallback ( async ( options : LoadOptions ) => {
@@ -75,12 +83,14 @@ export function useLoadSpectra(): UseLoadSpectraResult {
7583 if ( isArrayOfString ( options . urls ) ) {
7684 const result = await loadSpectraFromURLs ( options . urls ) ;
7785 setData ( result as NMRiumData ) ;
86+ setActiveTab ( options ?. activeTab ) ;
7887 } else {
7988 throw new Error ( 'The input must be a valid urls array of string[]' ) ;
8089 }
8190 } else if ( 'files' in options ) {
8291 const result = await loadSpectraFromFiles ( options . files ) ;
8392 setData ( result as NMRiumData ) ;
93+ setActiveTab ( options ?. activeTab ) ;
8494 }
8595 } catch ( error : unknown ) {
8696 const loadError = error as Error ;
@@ -92,12 +102,16 @@ export function useLoadSpectra(): UseLoadSpectraResult {
92102 }
93103 } , [ ] ) ;
94104
95- return useMemo (
96- ( ) => ( {
97- 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 } ,
98113 load,
99114 isLoading,
100- } ) ,
101- [ data , isLoading , load ] ,
102- ) ;
115+ } ;
116+ } , [ activeTab , data , isLoading , load ] ) ;
103117}
0 commit comments