Skip to content

Commit

Permalink
refactor: useLoadSpectra function return type
Browse files Browse the repository at this point in the history
  • Loading branch information
hamed-musallam committed Nov 20, 2023
1 parent 62b4c1e commit 7a0ec41
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions src/hooks/useLoadSpectra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<NMRiumData>({ spectra: [], molecules: [] });
const [isLoading, setLoading] = useState<boolean>(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(
() => ({
Expand Down

0 comments on commit 7a0ec41

Please sign in to comment.