Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: set spectra active tab #187

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]);
}
Loading