Skip to content
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
7 changes: 6 additions & 1 deletion src/NMRiumWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default function NMRiumWrapper() {
const { workspace, preferences, defaultEmptyMessage, customWorkspaces } =
usePreferences();

const { load: loadSpectra, data, isLoading } = useLoadSpectra();
const { load: loadSpectra, data, isLoading, setActiveTab } = useLoadSpectra();

const dataChangeHandler = useCallback<NMRiumChangeCb>((state, source) => {
// avoid triggering data-change event for SET_2D_LEVEL action, This should be handled internally in NMRium
Expand All @@ -48,6 +48,11 @@ export default function NMRiumWrapper() {
}
break;
}
case 'selectTab': {
const { tab } = request.params;
setActiveTab({ tab: tab.toUpperCase() });
break;
}
default: {
throw new Error(
`ERROR! Property 'type' accepts only 'exportViewerAsBlob'.`,
Expand Down
12 changes: 8 additions & 4 deletions src/events/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ type LoadData =
activeTab?: string;
};

interface ActionRequest {
type: 'exportSpectraViewerAsBlob';
// params?: any;
}
type ActionRequest =
| {
type: 'exportSpectraViewerAsBlob';
}
| {
type: 'selectTab';
params: { tab: string };
};

interface ActionResponse {
type: 'exportSpectraViewerAsBlob';
Expand Down
13 changes: 8 additions & 5 deletions src/hooks/useLoadSpectra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ interface UseLoadSpectraResult {
data: NMRiumData | null;
load: (options: LoadOptions) => Promise<void>;
isLoading: boolean;
setActiveTab: (input: { tab: string }) => void;
}

const core = init();
Expand Down Expand Up @@ -77,7 +78,7 @@ async function loadSpectraFromURLs(urls: string[]): Promise<CoreReadReturn> {

export function useLoadSpectra(): UseLoadSpectraResult {
const [result, setResult] = useState<CoreReadReturn | null>(null);
const [activeTab, setActiveTab] = useState<string | undefined>();
const [activeTab, setActiveTab] = useState<{ tab: string } | undefined>();
const [isLoading, setLoading] = useState(false);

const load = useCallback(async (options: LoadOptions) => {
Expand All @@ -102,7 +103,7 @@ export function useLoadSpectra(): UseLoadSpectraResult {
}

setResult(loadedResult);
setActiveTab(resolvedActiveTab);
setActiveTab({ tab: resolvedActiveTab ?? '' });
const state = {
...loadedResult.state,
data: {
Expand All @@ -127,7 +128,9 @@ export function useLoadSpectra(): UseLoadSpectraResult {
}, []);

return useMemo(() => {
const view = { spectra: { activeTab } } as unknown as ViewState;
const view = {
spectra: { activeTab: activeTab?.tab },
} as unknown as ViewState;

const data: NMRiumData | null = result
? {
Expand All @@ -140,6 +143,6 @@ export function useLoadSpectra(): UseLoadSpectraResult {
}
: null;

return { data, load, isLoading };
}, [activeTab, result, isLoading, load]);
return { data, load, isLoading, setActiveTab };
}, [activeTab, result, isLoading, load, setActiveTab]);
}
Loading