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

Return markerProps from useStandaloneMapMarkers #292

Merged
merged 2 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion packages/libs/components/src/map/ChartMarker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
MarkerScaleDefault,
} from '../types/plots';

interface ChartMarkerProps
export interface ChartMarkerProps
extends BoundsDriftMarkerProps,
MarkerScaleAddon,
DependentAxisLogScaleAddon {
Expand Down
19 changes: 16 additions & 3 deletions packages/libs/eda/src/lib/map/analysis/MapAnalysis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ import { DraggablePanel } from '@veupathdb/coreui/dist/components/containers';
import { TabbedDisplayProps } from '@veupathdb/coreui/dist/components/grids/TabbedDisplay';
import { GeoConfig } from '../../core/types/geoConfig';
import Banner from '@veupathdb/coreui/dist/components/banners/Banner';
import DonutMarkerComponent from '@veupathdb/components/lib/map/DonutMarker';
import ChartMarkerComponent from '@veupathdb/components/lib/map/ChartMarker';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interested in the motivation for the rename...

Oh, I see, because of the DonutMarker SVG Icon component.

This could be a general "style guide" discussion at our next meeting maybe.


enum MapSideNavItemLabels {
Download = 'Download',
Expand Down Expand Up @@ -316,7 +318,7 @@ function MapAnalysisImpl(props: ImplProps) {
})();

const {
markers,
markersData,
pending,
error,
legendItems,
Expand All @@ -333,7 +335,18 @@ function MapAnalysisImpl(props: ImplProps) {
outputEntityId: outputEntity?.id,
//TO DO: maybe dependentAxisLogScale
});
const finalMarkers = useMemo(() => markers || [], [markers]);

const markers = useMemo(
() =>
markersData?.map((marker) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Purely style but markerProps is more readable?

markerType === 'pie' ? (
<DonutMarkerComponent {...marker} />
) : (
<ChartMarkerComponent {...marker} />
)
) || [],
[markersData, markerType]
);

const userLoggedIn = useWdkService(async (wdkService) => {
const user = await wdkService.getCurrentUser();
Expand Down Expand Up @@ -982,7 +995,7 @@ function MapAnalysisImpl(props: ImplProps) {
showSpinner={pending}
animation={defaultAnimation}
viewport={appState.viewport}
markers={finalMarkers}
markers={markers}
mouseMode={appState.mouseMode}
flyToMarkers={
markers && markers.length > 0 && willFlyTo && !pending
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { BoundsDriftMarkerProps } from '@veupathdb/components/lib/map/BoundsDriftMarker';
import { ReactElement, useCallback, useMemo } from 'react';
import { useCallback, useMemo } from 'react';
import { usePromise } from '../../../core/hooks/promise';
import { BoundsViewport } from '@veupathdb/components/lib/map/Types';
import { GeoConfig } from '../../../core/types/geoConfig';
Expand All @@ -17,8 +16,6 @@ import {
ColorPaletteDefault,
gradientSequentialColorscaleMap,
} from '@veupathdb/components/lib/types/plots';
import DonutMarker from '@veupathdb/components/lib/map/DonutMarker';
import ChartMarker from '@veupathdb/components/lib/map/ChartMarker';
import {
kFormatter,
mFormatter,
Expand All @@ -28,6 +25,8 @@ import { LegendItemsProps } from '@veupathdb/components/lib/components/plotContr
import { VariableDescriptor } from '../../../core/types/variable';
import { useDeepValue } from '../../../core/hooks/immutability';
import { UNSELECTED_DISPLAY_TEXT, UNSELECTED_TOKEN } from '../..';
import { DonutMarkerProps } from '@veupathdb/components/lib/map/DonutMarker';
import { ChartMarkerProps } from '@veupathdb/components/lib/map/ChartMarker';

/**
* Provides markers for use in the MapVEuMap component
Expand Down Expand Up @@ -55,7 +54,7 @@ export interface StandaloneMapMarkersProps {
// what this hook returns
interface MapMarkers {
/** the markers */
markers: ReactElement<BoundsDriftMarkerProps>[] | undefined;
markersData: DonutMarkerProps[] | ChartMarkerProps[] | undefined;
/** `totalVisibleEntityCount` tells you how many entities are visible at a given viewport. But not necessarily with data for the overlay variable. */
totalVisibleEntityCount: number | undefined;
/** This tells you how many entities are on screen that also have data for the overlay variable
Expand Down Expand Up @@ -140,7 +139,7 @@ export function useStandaloneMapMarkers(
? overlayConfig?.overlayValues.map((ov) => ov.binLabel)
: undefined;

const markerData = usePromise<StandaloneMapMarkersResponse | undefined>(
const rawMarkersData = usePromise<StandaloneMapMarkersResponse | undefined>(
useCallback(async () => {
// check all required vizConfigs are provided
if (
Expand Down Expand Up @@ -214,16 +213,16 @@ export function useStandaloneMapMarkers(
);

const totalVisibleEntityCount: number | undefined =
markerData.value?.mapElements.reduce((acc, curr) => {
rawMarkersData.value?.mapElements.reduce((acc, curr) => {
return acc + curr.entityCount;
}, 0);

// calculate minPos, max and sum for chart marker dependent axis
// assumes the value is a count! (so never negative)
const { valueMax, valueMinPos, countSum } = useMemo(
() =>
markerData.value
? markerData.value.mapElements
rawMarkersData.value
? rawMarkersData.value.mapElements
.flatMap((el) => el.overlayValues)
.reduce(
({ valueMax, valueMinPos, countSum }, elem) => ({
Expand All @@ -242,7 +241,7 @@ export function useStandaloneMapMarkers(
}
)
: { valueMax: undefined, valueMinPos: undefined, countSum: undefined },
[markerData]
[rawMarkersData]
);

const defaultDependentAxisRange = useDefaultAxisRange(
Expand All @@ -257,8 +256,8 @@ export function useStandaloneMapMarkers(
* Merge the overlay data into the basicMarkerData, if available,
* and create markers.
*/
const markers = useMemo(() => {
return markerData.value?.mapElements.map(
const finalMarkersData = useMemo(() => {
return rawMarkersData.value?.mapElements.map(
({
geoAggregateValue,
entityCount,
Expand Down Expand Up @@ -333,28 +332,24 @@ export function useStandaloneMapMarkers(

switch (markerType) {
case 'pie': {
return (
<DonutMarker
{...commonMarkerProps}
markerLabel={kFormatter(count)}
/>
);
return {
...commonMarkerProps,
markerLabel: kFormatter(count),
} as DonutMarkerProps;
}
default: {
return (
<ChartMarker
{...commonMarkerProps}
markerLabel={mFormatter(count)}
dependentAxisRange={defaultDependentAxisRange}
dependentAxisLogScale={dependentAxisLogScale}
/>
);
return {
...commonMarkerProps,
markerLabel: mFormatter(count),
dependentAxisRange: defaultDependentAxisRange,
dependentAxisLogScale,
} as ChartMarkerProps;
}
}
}
);
}, [
markerData.value?.mapElements,
rawMarkersData.value?.mapElements,
vocabulary,
markerType,
overlayType,
Expand Down Expand Up @@ -384,23 +379,23 @@ export function useStandaloneMapMarkers(
: undefined,
// has any geo-facet got an array of overlay data
// containing at least one element that satisfies label==label
hasData: markerData
? some(markerData.value?.mapElements, (el) =>
hasData: rawMarkersData
? some(rawMarkersData.value?.mapElements, (el) =>
el.overlayValues.some((ov) => ov.binLabel === label)
)
: false,
group: 1,
rank: 1,
}));
}, [markerData, vocabulary, overlayType]);
}, [rawMarkersData, vocabulary, overlayType]);

return {
markers,
markersData: finalMarkersData,
totalVisibleWithOverlayEntityCount: countSum,
totalVisibleEntityCount,
legendItems,
pending: markerData.pending,
error: markerData.error,
pending: rawMarkersData.pending,
error: rawMarkersData.error,
};
}

Expand Down