Skip to content

Commit

Permalink
Update handleHover data type check to be more readable
Browse files Browse the repository at this point in the history
Removes the redundant `dataType` param and just does type checking
directly on the `data` param as discussed in review

<#1910 (comment)>
  • Loading branch information
joverlee521 committed Nov 22, 2024
1 parent 9a60742 commit 435b3f8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
23 changes: 16 additions & 7 deletions src/components/measurements/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { CSSProperties, MutableRefObject, useCallback, useRef, useEffect, useMemo, useState } from "react";
import { useSelector } from "react-redux";
import { isEqual, orderBy } from "lodash";
import { measurementIdSymbol, NODE_VISIBLE } from "../../util/globals";
import { NODE_VISIBLE } from "../../util/globals";
import { getColorByTitle, getTipColorAttribute } from "../../util/colorHelpers";
import { determineLegendMatch } from "../../util/tipRadiusHelpers";
import ErrorBoundary from "../../util/errorBoundary";
Expand All @@ -28,8 +28,18 @@ import {
import { RootState } from "../../store";
import { MeasurementFilters } from "../../reducers/controls";
import { Visibility } from "../../reducers/tree/types";
import { Measurement } from "../../reducers/measurements/types";
import { Measurement, isMeasurement } from "../../reducers/measurements/types";

interface MeanAndStandardDeviation {
mean: number
standardDeviation: number | undefined
}
function isMeanAndStandardDeviation(x: any): x is MeanAndStandardDeviation {
return (
typeof x.mean === "number" &&
(typeof x.standardDeviation === "number" || x.standardDeviation === undefined)
)
}
interface TreeStrainVisibility {
[strain: string]: Visibility
}
Expand Down Expand Up @@ -205,8 +215,7 @@ const MeasurementsPlot = ({height, width, showLegend, setPanelTitle}) => {
});
// Cache handleHover function to avoid extra useEffect calls
const handleHover = useCallback((
data: Measurement | { mean: number, standardDeviation: number },
dataType: "measurement" | "mean",
data: Measurement | MeanAndStandardDeviation,
mouseX: number,
mouseY: number,
colorByAttr: string = null
Expand All @@ -217,7 +226,7 @@ const MeasurementsPlot = ({height, width, showLegend, setPanelTitle}) => {
const hoverTitle = colorByAttr !== null ? `Color by ${getColorByTitle(colorings, colorBy)} : ${colorByAttr}` : null;
// Create a Map of data to save order of fields
const newData = new Map();
if (dataType === "measurement" && measurementIdSymbol in data) {
if (isMeasurement(data)) {
// Handle single measurement data
// Filter out internal auspice fields (i.e. measurementsJitter and measurementsId)
const displayFields = Object.keys(data).filter((field) => fields.has(field));
Expand All @@ -227,13 +236,13 @@ const MeasurementsPlot = ({height, width, showLegend, setPanelTitle}) => {
orderedFields.forEach((field) => {
newData.set(fields.get(field).title, data[field]);
});
} else if (dataType === "mean" && !(measurementIdSymbol in data)) {
} else if (isMeanAndStandardDeviation(data)) {
// Handle mean and standard deviation data
newData.set("mean", data.mean.toFixed(2));
newData.set("standard deviation", data.standardDeviation ? data.standardDeviation.toFixed(2) : "N/A");
} else {
// Catch unknown data types
console.error(`"Unknown data type for hover panel: ${dataType}`);
console.error(`"Unknown data type for hover panel: ${JSON.stringify(data)}`);
// Display provided data without extra ordering or parsing
Object.entries(data).forEach(([key, value]) => newData.set(key, value));
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/measurements/measurementsD3.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ export const addHoverPanelToMeasurementsAndMeans = (ref, handleHover, treeStrain
}

// sets hover data state to trigger the hover panel display
handleHover(d, dataType, clientX, clientY, colorByAttr);
handleHover(d, clientX, clientY, colorByAttr);
})
.on("mouseout.hoverPanel", () => handleHover(null));
};
Expand Down
9 changes: 9 additions & 0 deletions src/reducers/measurements/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ export function asMeasurement(x: Partial<Measurement>): Measurement {
throw new Error("Measurement is partial.");
}

export function isMeasurement(x: any): x is Measurement {
try {
asMeasurement(x);
return true;
} catch {
return false;
}
}

export interface Collection {
// TODO: Convert this to MeasurementsControlState during parseMeasurementsJSON
display_defaults?: JsonCollectionDisplayDefaults
Expand Down

0 comments on commit 435b3f8

Please sign in to comment.