Skip to content

Commit 7a0ec41

Browse files
refactor: useLoadSpectra function return type
1 parent 62b4c1e commit 7a0ec41

1 file changed

Lines changed: 29 additions & 24 deletions

File tree

src/hooks/useLoadSpectra.ts

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,36 +44,41 @@ async function loadSpectraFromURLs(urls: string[]) {
4444

4545
type NMRiumData = NmriumState['data'];
4646

47-
export function useLoadSpectra() {
47+
type LoadOptions = { urls: string[] } | { files: File[] };
48+
49+
interface UseLoadSpectraResult {
50+
data: { version: number; data: NMRiumData };
51+
load: (options: LoadOptions) => void;
52+
isLoading: boolean;
53+
}
54+
55+
export function useLoadSpectra(): UseLoadSpectraResult {
4856
const [data, setData] = useState<NMRiumData>({ spectra: [], molecules: [] });
4957
const [isLoading, setLoading] = useState<boolean>(false);
5058

51-
const load = useCallback(
52-
async (options: { urls: string[] } | { files: File[] }) => {
53-
setLoading(true);
54-
try {
55-
if ('urls' in options) {
56-
if (isArrayOfString(options.urls)) {
57-
const result = await loadSpectraFromURLs(options.urls);
58-
setData(result as NMRiumData);
59-
} else {
60-
throw new Error('The input must be a valid urls array of string[]');
61-
}
62-
} else if ('files' in options) {
63-
const result = await loadSpectraFromFiles(options.files);
59+
const load = useCallback(async (options: LoadOptions) => {
60+
setLoading(true);
61+
try {
62+
if ('urls' in options) {
63+
if (isArrayOfString(options.urls)) {
64+
const result = await loadSpectraFromURLs(options.urls);
6465
setData(result as NMRiumData);
66+
} else {
67+
throw new Error('The input must be a valid urls array of string[]');
6568
}
66-
} catch (error: unknown) {
67-
const loadError = error as Error;
68-
events.trigger('error', loadError);
69-
// eslint-disable-next-line no-console
70-
console.log(error);
71-
} finally {
72-
setLoading(false);
69+
} else if ('files' in options) {
70+
const result = await loadSpectraFromFiles(options.files);
71+
setData(result as NMRiumData);
7372
}
74-
},
75-
[],
76-
);
73+
} catch (error: unknown) {
74+
const loadError = error as Error;
75+
events.trigger('error', loadError);
76+
// eslint-disable-next-line no-console
77+
console.log(error);
78+
} finally {
79+
setLoading(false);
80+
}
81+
}, []);
7782

7883
return useMemo(
7984
() => ({

0 commit comments

Comments
 (0)