Skip to content

Commit 0867241

Browse files
committed
chore: improve types in component utilities
1 parent 23596f2 commit 0867241

6 files changed

Lines changed: 30 additions & 21 deletions

File tree

src/component/panels/MatrixGenerationPanel/MatrixGenerationPanelHeader.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { IoAnalytics } from 'react-icons/io5';
33
import { TbBrandGoogleAnalytics } from 'react-icons/tb';
44
import { TooltipHelpContent } from 'react-science/ui';
55

6+
import { isSpectrum1D } from '../../../data/data1d/Spectrum1D/index.ts';
67
import { useChartData } from '../../context/ChartContext.js';
78
import { usePreferences } from '../../context/PreferencesContext.js';
89
import { usePanelPreferences } from '../../hooks/usePanelPreferences.js';
@@ -41,7 +42,11 @@ export function MatrixGenerationPanelHeader(
4142
const signalProcessingFilterData = useHasSignalProcessingFilter();
4243

4344
function handleExportAsMatrix() {
44-
exportAsMatrix(data, spectraPreferences?.columns || [], 'Spectra Matrix');
45+
exportAsMatrix(
46+
data.filter(isSpectrum1D),
47+
spectraPreferences?.columns || [],
48+
'Spectra Matrix',
49+
);
4550
}
4651
function handleToggleStocsy() {
4752
dispatchPreferences({

src/component/utility/LocalStorage.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import dlv from 'dlv';
22
import lodashSet from 'lodash/set.js';
33
import { useCallback, useEffect, useMemo, useState } from 'react';
44

5-
export function useStateWithLocalStorage(localStorageKey: any, key?: string) {
5+
export function useStateWithLocalStorage(
6+
localStorageKey: string,
7+
key?: string,
8+
) {
69
const [value, setValue] = useState(
710
localStorage.getItem(localStorageKey) || '{}',
811
);
@@ -28,12 +31,12 @@ export function useStateWithLocalStorage(localStorageKey: any, key?: string) {
2831
}, [key, setData, value]);
2932
}
3033

31-
export function getLocalStorage(localStorageKey: any, isJson = true) {
34+
export function getLocalStorage(localStorageKey: string, isJson = true) {
3235
const settings = localStorage.getItem(localStorageKey);
3336
return settings && isJson ? JSON.parse(settings) : settings;
3437
}
3538

36-
export function storeData(localStorageKey: any, value: any) {
39+
export function storeData(localStorageKey: string, value: any) {
3740
localStorage.setItem(localStorageKey, value);
3841
}
3942

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export function convertPathArrayToString(value: any) {
1+
export function convertPathArrayToString(value: string | string[]) {
22
return Array.isArray(value) ? value.join('.') : value;
33
}

src/component/utility/export.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import type { ToastProps } from '@blueprintjs/core';
22
import type { SerializedStyles } from '@emotion/react';
33
import type {
44
JpathTableColumn,
5+
SerializedNmriumState,
56
SpectraTableColumn,
7+
Spectrum1D,
68
} from '@zakodium/nmrium-core';
79
import { BlobWriter, TextReader, ZipWriter } from '@zip.js/zip.js';
810
import dlv from 'dlv';
@@ -23,14 +25,14 @@ export const browserNotSupportedErrorToast: ToastProps = {
2325
* @param isCompressed
2426
*/
2527
async function exportAsJsonBlob(
26-
data: any,
28+
data: SerializedNmriumState,
2729
fileName = 'experiment',
2830
spaceIndent = 0,
2931
isCompressed = false,
3032
) {
3133
const fileData = JSON.stringify(
3234
data,
33-
(key, value: any) =>
35+
(key, value: unknown) =>
3436
ArrayBuffer.isView(value) ? Array.from(value as any) : value,
3537
spaceIndent,
3638
);
@@ -44,11 +46,10 @@ async function exportAsJsonBlob(
4446
}
4547

4648
function exportAsMatrix(
47-
data: any,
49+
data: Spectrum1D[],
4850
spectraColumns: SpectraTableColumn[],
4951
name: string,
5052
) {
51-
//columns labels
5253
const columnsLabels: string[] = [];
5354
// listed the spectra panel columns
5455
for (const col of spectraColumns) {
@@ -58,7 +59,7 @@ function exportAsMatrix(
5859
}
5960

6061
for (const value of data[0].data.x) {
61-
columnsLabels.push(value);
62+
columnsLabels.push(String(value));
6263
}
6364
let matrix = `${columnsLabels.join('\t')}\n`;
6465

@@ -76,7 +77,7 @@ function exportAsMatrix(
7677
}
7778
}
7879
for (const value of re) {
79-
cellsValues.push(value);
80+
cellsValues.push(String(value));
8081
}
8182
matrix += `${cellsValues.join('\t')}\n`;
8283
}
@@ -289,7 +290,7 @@ function transferToCanvas(offscreenCanvas: OffscreenCanvas) {
289290
// hack way to copy the image to the clipboard
290291
// TODO: remove when Firefox widely supports ClipboardItem
291292
// https://caniuse.com/mdn-api_clipboarditem
292-
function copyDataURLClipboardFireFox(image: any) {
293+
function copyDataURLClipboardFireFox(image: string) {
293294
const img = document.createElement('img');
294295
img.src = image;
295296

@@ -397,13 +398,13 @@ function parseDimension(value: string | null) {
397398

398399
function getBlob(targetElementID: string, options: GetBlobOptions): BlobObject {
399400
const { rootElement, css } = options;
400-
const _svg: any = (rootElement.getRootNode() as Document)
401+
const _svg = (rootElement.getRootNode() as Document)
401402
.querySelector(`#${targetElementID}`)
402-
?.cloneNode(true);
403+
?.cloneNode(true) as SVGElement;
403404

404-
const width = parseDimension(_svg?.getAttribute('width'));
405-
const height = parseDimension(_svg?.getAttribute('height'));
406-
const viewBox = _svg?.getAttribute('viewBox') || `0 0 ${width} ${height}`;
405+
const width = parseDimension(_svg.getAttribute('width'));
406+
const height = parseDimension(_svg.getAttribute('height'));
407+
const viewBox = _svg.getAttribute('viewBox') || `0 0 ${width} ${height}`;
407408

408409
for (const element of _svg.querySelectorAll('[data-no-export="true"]')) {
409410
element.remove();

src/component/utility/nucleusToString.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ export function is2DNucleus(nucleus: string) {
22
return nucleus.includes(',');
33
}
44

5-
export default function nucleusToString(nucleus: any) {
5+
export default function nucleusToString(nucleus: string | string[]) {
66
return typeof nucleus === 'string' ? nucleus : nucleus.join(',');
77
}

src/component/utility/stackOverlappingLabels.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function stackOverlappingLabelsArray<T extends Record<string, any>>(
2626

2727
return groups.flatMap((group) => {
2828
let i = 0;
29-
return group.map((item: any) => {
29+
return group.map((item) => {
3030
const stackIndex = i;
3131
if (item.assignment) {
3232
i++;
@@ -86,8 +86,8 @@ function stackOverlappingLabels<T extends Record<string, any>>(
8686
options: StackOverlappingLabelsOptions<T>,
8787
) {
8888
const { startPositionKey, labelWidthKey, padding = 0 } = options;
89-
const groups: any[][] = [];
90-
let currentGroup: any[] = [];
89+
const groups: T[][] = [];
90+
let currentGroup: T[] = [];
9191
let lastInPixel = 0;
9292

9393
for (const item of items) {

0 commit comments

Comments
 (0)