Skip to content

Commit

Permalink
Merge pull request #187 from NFDI4Chem/define-active-tab
Browse files Browse the repository at this point in the history
feat: set spectra active tab
  • Loading branch information
CS76 authored Jan 23, 2024
2 parents ca9ff18 + ce73266 commit 706b8fc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
13 changes: 8 additions & 5 deletions src/NMRiumWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,16 @@ export default function NMRiumWrapper() {
case 'nmrium':
setDate(loadData.data);
break;
case 'file':
loadSpectra({ files: loadData.data });
case 'file': {
const { data: files, activeTab } = loadData;
loadSpectra({ files, activeTab });
break;
case 'url':
loadSpectra({ urls: loadData.data });
}
case 'url': {
const { data: urls, activeTab } = loadData;
loadSpectra({ urls, activeTab });
break;

}
default: {
throw new Error(
`ERROR! Property 'type' accept only nmrium, url or file.`,
Expand Down
2 changes: 2 additions & 0 deletions src/demo/NMRiumWrapperDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export default function NMRiumWrapperDemo() {
events.trigger('load', {
data: files,
type: 'file',
activeTab: '13C',
});
});
}}
Expand All @@ -96,6 +97,7 @@ export default function NMRiumWrapperDemo() {
events.trigger('load', {
data: files,
type: 'file',
activeTab: '13C',
});
},
);
Expand Down
2 changes: 2 additions & 0 deletions src/events/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ type EventType =
type LoadData =
| {
data: string[];
activeTab?: string;
type: 'url';
}
| {
data: File[];
activeTab?: string;
type: 'file';
}
| {
Expand Down
28 changes: 21 additions & 7 deletions src/hooks/useLoadSpectra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ import {
NmriumState,
CURRENT_EXPORT_VERSION,
ParsingOptions,
ViewState,
} from 'nmr-load-save';
import { useCallback, useMemo, useState } from 'react';

import events from '../events';
import { getFileNameFromURL } from '../utilities/getFileNameFromURL';
import { isArrayOfString } from '../utilities/isArrayOfString';

type DeepPartial<T> = {
[K in keyof T]?: T[K] extends object ? DeepPartial<T[K]> : T[K];
};

const logger = new FifoLogger({
onChange: (log) => {
if (log && ['error', 'fatal'].includes(log.levelLabel) && log?.error) {
Expand Down Expand Up @@ -56,7 +61,9 @@ async function loadSpectraFromURLs(urls: string[]) {

type NMRiumData = NmriumState['data'];

type LoadOptions = { urls: string[] } | { files: File[] };
type LoadOptions =
| { urls: string[]; activeTab?: string }
| { files: File[]; activeTab?: string };

interface UseLoadSpectraResult {
data: { version: number; data: NMRiumData };
Expand All @@ -66,6 +73,7 @@ interface UseLoadSpectraResult {

export function useLoadSpectra(): UseLoadSpectraResult {
const [data, setData] = useState<NMRiumData>({ spectra: [], molecules: [] });
const [activeTab, setActiveTab] = useState<string>();
const [isLoading, setLoading] = useState<boolean>(false);

const load = useCallback(async (options: LoadOptions) => {
Expand All @@ -75,12 +83,14 @@ export function useLoadSpectra(): UseLoadSpectraResult {
if (isArrayOfString(options.urls)) {
const result = await loadSpectraFromURLs(options.urls);
setData(result as NMRiumData);
setActiveTab(options?.activeTab);
} 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);
setData(result as NMRiumData);
setActiveTab(options?.activeTab);
}
} catch (error: unknown) {
const loadError = error as Error;
Expand All @@ -92,12 +102,16 @@ export function useLoadSpectra(): UseLoadSpectraResult {
}
}, []);

return useMemo(
() => ({
data: { version: CURRENT_EXPORT_VERSION, data },
return useMemo(() => {
let view: DeepPartial<ViewState> = {};
if (activeTab) {
view = { spectra: { activeTab } };
}

return {
data: { version: CURRENT_EXPORT_VERSION, data, view },
load,
isLoading,
}),
[data, isLoading, load],
);
};
}, [activeTab, data, isLoading, load]);
}

0 comments on commit 706b8fc

Please sign in to comment.