Skip to content

Commit

Permalink
refactor: remove some 'any' types (#2644)
Browse files Browse the repository at this point in the history
  • Loading branch information
wadjih-bencheikh18 authored Oct 23, 2023
1 parent 1f212f6 commit 1b27799
Show file tree
Hide file tree
Showing 19 changed files with 137 additions and 102 deletions.
2 changes: 1 addition & 1 deletion src/component/1d/ExclusionZonesAnnotations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import ExclusionZoneAnnotation from './ExclusionZoneAnnotation';
interface ExclusionZonesAnnotationsInnerProps {
displayerKey: string;
spectra: Spectrum1D[];
xDomains: any;
xDomains: Record<string, number[]>;
shiftY: number;
}

Expand Down
2 changes: 1 addition & 1 deletion src/component/1d/database/ResurrectedDatabaseRanges.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function ResurrectedDatabaseRanges() {

const { ranges = [] } = highlight.sourceData.extra || {};

const yDomain: any[] = [0, 0];
const yDomain: number[] = [0, 0];

const spectra = ranges.map((range) => {
const { from, to } = range;
Expand Down
25 changes: 18 additions & 7 deletions src/component/1d/multiplicityTree/MultiplicityTree.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @jsxImportSource @emotion/react */
import { css } from '@emotion/react';
import { Spectrum1D } from 'nmr-load-save';
import { Range, checkMultiplicity } from 'nmr-processing';
import { Range, Signal1D, checkMultiplicity } from 'nmr-processing';
import { CSSProperties, useMemo } from 'react';

import {
Expand All @@ -18,7 +18,7 @@ import { AssignmentActionsButtons } from '../ranges/AssignmentActionsButtons';
import LevelNode from './LevelNode';
import StringNode from './StringNode';
import TreeNodes from './TreeNodes';
import createTreeNodes from './buildTreeNode';
import createTreeNodes, { TreeNodeData } from './buildTreeNode';

const styles = {
cursor: 'default',
Expand Down Expand Up @@ -156,11 +156,21 @@ export function MultiplicityTree(props: MultiplicityTreeProps) {
</g>
);
}

interface TreeResult {
isMassive: boolean;
startX: number;
startY: number;
width: number;
height: number;
levelHeight: number;
nodes: TreeNodeData[];
signal: Signal1D;
range: Omit<Range, 'signals'>;
}
function getTree(range: Range, spectrum: Spectrum1D, scale) {
const SHIFT_X = 30;

const treeResult: any[] = [];
const treeResult: TreeResult[] = [];

const { signals = [], ...otherRangeProps } = range;
for (const signal of signals) {
Expand All @@ -169,7 +179,7 @@ function getTree(range: Range, spectrum: Spectrum1D, scale) {
let width = 0;
let height = 0;
let levelHeight = 0;
let nodes = [];
const nodes: TreeNodeData[] = [];
let startX = 0;

const { from, to } = range;
Expand All @@ -179,9 +189,10 @@ function getTree(range: Range, spectrum: Spectrum1D, scale) {
const jIndices = signal.multiplicity
.split('')
.map((_mult, i) => (hasCouplingConstant(_mult) ? i : undefined))
.filter((_i) => _i !== undefined);
.filter((i) => i !== undefined) as number[];

nodes = buildTreeNodesData(0, jIndices, [], signal.delta);
const treeNodes = buildTreeNodesData(0, jIndices, [], signal.delta);
if (treeNodes) nodes.push(...treeNodes);
}

// +2 because of multiplicity text and start level node before the actual tree starts
Expand Down
19 changes: 14 additions & 5 deletions src/component/1d/multiplicityTree/buildTreeNode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import lodashGet from 'lodash/get';
import { Spectrum1D } from 'nmr-load-save';
import { Signal1D } from 'nmr-processing';

import {
Expand All @@ -8,12 +9,20 @@ import {

import { TREE_LEVEL_COLORS } from './TreeColors';

function createTreeNodes(signal: Signal1D, spectrumData) {
export interface TreeNodeData {
startX: number;
_startX: number;
ratio: number;
multiplicityIndex: number;
color: string;
}

function createTreeNodes(signal: Signal1D, spectrumData: Spectrum1D) {
function buildTreeNodesData(
multiplicityIndex,
jIndices,
treeNodesData,
startX,
multiplicityIndex: number,
jIndices: number[],
treeNodesData: TreeNodeData[],
startX: number,
) {
if (!signal.multiplicity) {
return null;
Expand Down
32 changes: 18 additions & 14 deletions src/component/2d/Viewer2D.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Spectrum1D } from 'nmr-load-save';
import { useCallback, useEffect, useMemo, ReactNode, useRef } from 'react';
import { ResponsiveChart } from 'react-d3-utils';
import { assert } from 'react-science/ui';

import BrushXY, { BRUSH_TYPE } from '../1d-2d/tools/BrushXY';
import CrossLinePointer from '../1d-2d/tools/CrossLinePointer';
Expand Down Expand Up @@ -44,20 +46,22 @@ function Viewer2D({ emptyText = undefined }: Viewer2DProps) {
const dispatch = useDispatch();
const brushStartRef = useRef<{ x: number; y: number } | null>(null);

const spectrumData: any[] = useMemo(() => {
const spectrumData: Spectrum1D[] = useMemo(() => {
const nuclei = activeTab.split(',');

return nuclei.map((nucleus) => {
const spectra = activeSpectra[nucleus];
if (spectra?.length === 1) {
const id = spectra[0].id;
const spectrum = data.find(
(datum) => datum.id === id && !datum.info.isFid,
);
return spectrum;
}
return null;
});
return nuclei
.map((nucleus) => {
const spectra = activeSpectra[nucleus];
if (spectra?.length === 1) {
const id = spectra[0].id;
const spectrum = data.find(
(datum) => datum.id === id && !datum.info.isFid,
);
assert(spectrum, `Spectrum with id ${id} not found`);
return spectrum;
}
return null;
})
.filter((d) => d !== null) as Spectrum1D[];
}, [activeTab, data, activeSpectra]);

const DIMENSION = get2DDimensionLayout(state);
Expand Down Expand Up @@ -253,7 +257,7 @@ function Viewer2D({ emptyText = undefined }: Viewer2DProps) {
interface ViewerResponsiveWrapperProps {
width: number;
height: number;
children: any;
children: ReactNode;
}

export function ViewerResponsiveWrapper(props: ViewerResponsiveWrapperProps) {
Expand Down
4 changes: 2 additions & 2 deletions src/component/elements/ColorInput.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @jsxImportSource @emotion/react */
import { css } from '@emotion/react';
import { useCallback, memo, useState, useEffect } from 'react';
import { ColorPicker } from 'react-science/ui';
import { ColorPicker, ColorPickerProps } from 'react-science/ui';

const style = css`
display: flex;
Expand Down Expand Up @@ -67,7 +67,7 @@ const style = css`
export interface ColorInputProps {
value?: string;
name: string;
onColorChange?: (element: any) => void;
onColorChange?: ColorPickerProps['onChangeComplete'];
}

function ColorInput(props: ColorInputProps) {
Expand Down
44 changes: 17 additions & 27 deletions src/component/elements/ContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
useRef,
useEffect,
forwardRef,
MouseEvent,
} from 'react';
import { createPortal } from 'react-dom';

Expand Down Expand Up @@ -49,25 +50,28 @@ const styles = css`
border: none;
}
`;
interface ContextMenuItem {
onClick: <T>(data: T) => void;
interface ContextMenuItem<T> {
onClick: (data?: T) => void;
label: string;
}

export interface ContextMenuProps {
context: ContextMenuItem[] | null;
export interface ContextMenuProps<T = unknown> {
context: Array<ContextMenuItem<T>>;
}
// TODO: remove this hacky ref usage.

Check warning on line 61 in src/component/elements/ContextMenu.tsx

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

Unexpected 'todo' comment: 'TODO: remove this hacky ref usage.'
function ContextMenu({ context }: ContextMenuProps, ref: any) {
const [position, setPosition] = useState<{ left: any; top: any }>({
function ContextMenu<T = unknown>({ context }: ContextMenuProps<T>, ref: any) {
const [position, setPosition] = useState<{
left: string | number;
top: string | number;
}>({
left: 0,
top: 0,
});
const { rootRef, elementsWrapperRef } = useGlobal();
const [data, setData] = useState();
const [data, setData] = useState<T>();
const [isVisible, show] = useState<boolean>();
const [sourceElement, setSourceElement] = useState(null);
const root = useRef<any>();
const root = useRef<HTMLDivElement>();

useEffect(() => {
root.current = document.createElement('div');
Expand All @@ -94,37 +98,23 @@ function ContextMenu({ context }: ContextMenuProps, ref: any) {
const rootH = 0;

const right = screenW - clickX > rootW;
let left: any = !right;
let top: any = screenH - clickY > rootH;
const bottom = !top;
if (right) {
left = `${clickX + 5}px`;
}

if (left) {
left = `${clickX - rootW - 5}px`;
}
const bottom = screenH - clickY <= rootH;

if (top) {
top = `${clickY + 5}px`;
}

if (bottom) {
top = `${clickY - rootH - 5}px`;
}
const left = right ? `${clickX + 5}px` : `${clickX - rootW - 5}px`;
const top = bottom ? `${clickY - rootH - 5}px` : `${clickY + 5}px`;

setPosition({ left, top });
};

useImperativeHandle(ref, () => ({
handleContextMenu: (e, _data) => {
handleContextMenu: (e, _data: T) => {
setData(_data);
contextMenuHandler(e);
},
}));

const clickHandler = useCallback(
(event, click) => {
(event: MouseEvent, click: (data?: T) => void) => {
event.preventDefault();
click(data);
show(false);
Expand Down
6 changes: 3 additions & 3 deletions src/component/elements/EditableColumn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ function extractNumber(val: string | number, type: string) {

interface EditableColumnProps
extends Omit<InputProps, 'style' | 'value' | 'type'> {
onSave?: (element: any) => void;
onEditStart?: (element: any) => void;
onSave?: (element: KeyboardEvent<HTMLInputElement>) => void;
onEditStart?: (element: boolean) => void;
type?: 'number' | 'text';
editStatus?: boolean;
value: string | number;
style?: CSSProperties;
validate?: (value?: any) => boolean;
validate?: (value?: string | number) => boolean;
textOverFlowEllipses?: boolean;
}

Expand Down
2 changes: 1 addition & 1 deletion src/component/elements/InputRange.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const styles: Record<'container' | 'label', CSSProperties> = {
interface InputRangeProps {
name: string;
value?: number;
onChange: (element: any) => void;
onChange: (element: { value: number; name: string }) => void;
label?: string;
shortLabel?: string;
style?: CSSProperties;
Expand Down
24 changes: 16 additions & 8 deletions src/component/elements/MenuButton.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** @jsxImportSource @emotion/react */
import { css } from '@emotion/react';
import { SerializedStyles, css } from '@emotion/react';
import { ReactNode, useState, useCallback, useRef } from 'react';

import ToolTip from './ToolTip/ToolTip';
Expand Down Expand Up @@ -70,14 +70,24 @@ function MenuItem({ icon, label, onClick }: MenuItemProps) {
</button>
);
}
export interface BoundingBox {
x: number;
y: number;
width: number;
height: number;
}

interface MenuListProps {
items: Array<MenuItemProps & { id: string }>;
onClick: (element: MenuItemProps & { id: string }) => void;
boxBounding: any;
boxBounding?: BoundingBox;
}

function MenuList({ items, boxBounding, onClick }: MenuListProps) {
function MenuList({
items,
boxBounding = { x: 0, y: 0, width: 0, height: 0 },
onClick,
}: MenuListProps) {
return (
<div
className="menu"
Expand All @@ -94,13 +104,11 @@ function MenuList({ items, boxBounding, onClick }: MenuListProps) {
);
}

interface MenuButtonProps {
style?: any;
component: any;
interface MenuButtonProps extends Pick<MenuListProps, 'items' | 'onClick'> {
style?: SerializedStyles;
component: ReactNode;
toolTip: string;
className?: string;
items: any[];
onClick: (element: any) => void;
}

export default function MenuButton({
Expand Down
6 changes: 3 additions & 3 deletions src/component/elements/NextPrev.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/** @jsxImportSource @emotion/react */
import { css } from '@emotion/react';
import {
ReactNode,
useState,
Children,
useMemo,
useEffect,
useCallback,
memo,
CSSProperties,
ReactElement,
} from 'react';
import { FaAngleLeft } from 'react-icons/fa';
import { useMeasure } from 'react-use';
Expand Down Expand Up @@ -63,7 +63,7 @@ function Arrow({ direction, onClick, style = {} }: ArrowProps) {
const transition = 0.45;

interface NextPrevProps {
children: ReactNode;
children: ReactElement | ReactElement[];
loop?: boolean;
defaultIndex?: number;
onChange?: (element: number) => void;
Expand All @@ -86,7 +86,7 @@ function NextPrev(props: NextPrevProps) {

const Sliders = useMemo(
() =>
Children.map(children, (child: any) => {
Children.map(children, (child: ReactElement) => {
return (
<div
key={child.key}
Expand Down
6 changes: 3 additions & 3 deletions src/component/elements/NumberInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export interface NumberInputProps
extends Omit<React.InputHTMLAttributes<HTMLInputElement>, 'style'> {
pattern?: string;
name?: string;
step?: any;
min?: any;
max?: any;
step?: string;
min?: number | string;
max?: number | string;
value?: number;
style?: CSSProperties;
autoSelect?: boolean;
Expand Down
Loading

0 comments on commit 1b27799

Please sign in to comment.