|
1 | 1 | import type { |
2 | 2 | CoreReadReturn, |
| 3 | + NmriumState, |
3 | 4 | ParsingOptions, |
4 | 5 | ViewState, |
5 | 6 | } from '@zakodium/nmrium-core'; |
@@ -82,22 +83,40 @@ export function useLoadSpectra(): UseLoadSpectraResult { |
82 | 83 | const load = useCallback(async (options: LoadOptions) => { |
83 | 84 | setLoading(true); |
84 | 85 | try { |
| 86 | + let loadedResult: CoreReadReturn; |
| 87 | + let resolvedActiveTab: string | undefined; |
| 88 | + |
85 | 89 | if ('nmrium' in options) { |
86 | | - const nmriumResult = await loadSpectraFromNMRium(options.nmrium); |
87 | | - setResult(nmriumResult); |
88 | | - setActiveTab( |
89 | | - options.activeTab ?? nmriumResult.state.view?.spectra?.activeTab, |
90 | | - ); |
| 90 | + loadedResult = await loadSpectraFromNMRium(options.nmrium); |
| 91 | + resolvedActiveTab = |
| 92 | + options.activeTab ?? loadedResult.state.view?.spectra?.activeTab; |
91 | 93 | } else if ('urls' in options) { |
92 | 94 | if (!isArrayOfString(options.urls)) { |
93 | 95 | throw new Error('The input must be a valid urls array of string[]'); |
94 | 96 | } |
95 | | - setResult(await loadSpectraFromURLs(options.urls)); |
96 | | - setActiveTab(options.activeTab); |
| 97 | + loadedResult = await loadSpectraFromURLs(options.urls); |
| 98 | + resolvedActiveTab = options.activeTab; |
97 | 99 | } else { |
98 | | - setResult(await loadSpectraFromFiles(options.files)); |
99 | | - setActiveTab(options.activeTab); |
| 100 | + loadedResult = await loadSpectraFromFiles(options.files); |
| 101 | + resolvedActiveTab = options.activeTab; |
100 | 102 | } |
| 103 | + |
| 104 | + setResult(loadedResult); |
| 105 | + setActiveTab(resolvedActiveTab); |
| 106 | + const state = { |
| 107 | + ...loadedResult.state, |
| 108 | + data: { |
| 109 | + spectra: [], |
| 110 | + molecules: [], |
| 111 | + ...loadedResult.state.data, |
| 112 | + actionType: 'INITIATE', |
| 113 | + }, |
| 114 | + }; |
| 115 | + |
| 116 | + events.trigger('data-change', { |
| 117 | + source: 'data', |
| 118 | + state: state as NmriumState, |
| 119 | + }); |
101 | 120 | } catch (error: unknown) { |
102 | 121 | events.trigger('error', error as Error); |
103 | 122 | // eslint-disable-next-line no-console |
|
0 commit comments