diff --git a/src/hooks/useLoadSpectra.ts b/src/hooks/useLoadSpectra.ts index 08c6d29..30e42fa 100644 --- a/src/hooks/useLoadSpectra.ts +++ b/src/hooks/useLoadSpectra.ts @@ -44,36 +44,41 @@ async function loadSpectraFromURLs(urls: string[]) { type NMRiumData = NmriumState['data']; -export function useLoadSpectra() { +type LoadOptions = { urls: string[] } | { files: File[] }; + +interface UseLoadSpectraResult { + data: { version: number; data: NMRiumData }; + load: (options: LoadOptions) => void; + isLoading: boolean; +} + +export function useLoadSpectra(): UseLoadSpectraResult { const [data, setData] = useState({ spectra: [], molecules: [] }); const [isLoading, setLoading] = useState(false); - const load = useCallback( - async (options: { urls: string[] } | { files: File[] }) => { - setLoading(true); - try { - if ('urls' in options) { - if (isArrayOfString(options.urls)) { - const result = await loadSpectraFromURLs(options.urls); - setData(result as NMRiumData); - } else { - throw new Error('The input must be a valid urls array of string[]'); - } - } else if ('files' in options) { - const result = await loadSpectraFromFiles(options.files); + const load = useCallback(async (options: LoadOptions) => { + setLoading(true); + try { + if ('urls' in options) { + if (isArrayOfString(options.urls)) { + const result = await loadSpectraFromURLs(options.urls); setData(result as NMRiumData); + } else { + throw new Error('The input must be a valid urls array of string[]'); } - } catch (error: unknown) { - const loadError = error as Error; - events.trigger('error', loadError); - // eslint-disable-next-line no-console - console.log(error); - } finally { - setLoading(false); + } else if ('files' in options) { + const result = await loadSpectraFromFiles(options.files); + setData(result as NMRiumData); } - }, - [], - ); + } catch (error: unknown) { + const loadError = error as Error; + events.trigger('error', loadError); + // eslint-disable-next-line no-console + console.log(error); + } finally { + setLoading(false); + } + }, []); return useMemo( () => ({