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

Wire marker selection to floaters #821

Merged
merged 23 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
d21ccf3
moved all marker selection logic out of MapAnalysis
bobular Feb 2, 2024
5e4dcf6
remove empty array default for selectedMarkers, remove SetStateAction…
bobular Feb 2, 2024
930c8ea
selected markers wired into donut mode
bobular Feb 2, 2024
fabc767
selected markers wired into bar and bubble
bobular Feb 2, 2024
f5d4b1a
and finally wire in the floaters - easy!
bobular Feb 2, 2024
0e4e94a
Merge remote-tracking branch 'origin/little-filter-refactor' into wir…
bobular Feb 2, 2024
56ed0b9
Merge remote-tracking branch 'origin/main' into wire-marker-selection…
bobular Feb 6, 2024
7e70ca3
missed one prop renaming
bobular Feb 6, 2024
3812c59
Merge remote-tracking branch 'origin/main' into wire-marker-selection…
bobular Feb 7, 2024
6d08637
prevented double-request for histograms by removing 'wait till distri…
bobular Feb 7, 2024
2a8b872
added basic snackbar notification if user selects a marker with no ac…
bobular Feb 7, 2024
eb3c244
indication of what's plotted for Histogram viz in Donut mode only at …
bobular Feb 7, 2024
d348008
change marker selection behaviour to require shift-click to add to se…
bobular Feb 8, 2024
753c227
relocated snackbars to top center
bobular Feb 8, 2024
eb56151
moved selected marker snackbar to a new hook and added shift-key snac…
bobular Feb 8, 2024
8c72316
refine snackbar logic
bobular Feb 8, 2024
a97f7a9
fixed the flashing popups after selecting a marker - yay
bobular Feb 8, 2024
cbdc210
improved floater subtitle verbiage; tidied up DonutMarkerMapType
bobular Feb 9, 2024
fc17a6d
make viz subtitle singular/plural specific
bobular Feb 9, 2024
8df8b6c
add snackbars to bar and bubble
bobular Feb 9, 2024
87b7ae9
fix a histogram runtime bug (double click on marker when another mark…
bobular Feb 9, 2024
f0b9859
rolled out plot subtitle to all visualisations; realised TitleOptions…
bobular Feb 9, 2024
c98f554
Merge remote-tracking branch 'origin/main' into wire-marker-selection…
bobular Feb 12, 2024
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
51 changes: 27 additions & 24 deletions packages/libs/components/src/map/BoundsDriftMarker.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useMap, Popup } from 'react-leaflet';
import { useRef, useEffect, useState } from 'react';
import { useRef, useEffect, useCallback } from 'react';
// use new ReactLeafletDriftMarker instead of DriftMarker
import ReactLeafletDriftMarker from 'react-leaflet-drift-marker';
import { MarkerProps, Bounds } from './Types';
Expand All @@ -15,7 +15,7 @@ export interface BoundsDriftMarkerProps extends MarkerProps {
// selectedMarkers state
selectedMarkers?: string[];
// selectedMarkers setState
setSelectedMarkers?: React.Dispatch<React.SetStateAction<string[]>>;
setSelectedMarkers?: (selectedMarkers: string[] | undefined) => void;
}

/**
Expand Down Expand Up @@ -281,30 +281,33 @@ export default function BoundsDriftMarker({
};

// add click events for highlighting markers
const handleClick = (e: LeafletMouseEvent) => {
// check the number of mouse click and enable function for single click only
if (e.originalEvent.detail === 1) {
if (setSelectedMarkers) {
if (selectedMarkers?.find((id) => id === props.id)) {
setSelectedMarkers((prevSelectedMarkers: string[]) =>
prevSelectedMarkers.filter((id: string) => id !== props.id)
);
} else {
// select
setSelectedMarkers((prevSelectedMarkers: string[]) => [
...prevSelectedMarkers,
props.id,
]);
const handleClick = useCallback(
Copy link
Member Author

@bobular bobular Feb 2, 2024

Choose a reason for hiding this comment

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

useCallback doesn't seem essential but seems sensible given it is debounced and this may avoid the debouncing being reset unnecessarily.

Especially as we are adding selectedMarkers to the function. Previously we were using "functional update" prevSelectedMarkers

(e: LeafletMouseEvent) => {
// check the number of mouse click and enable function for single click only
if (e.originalEvent.detail === 1) {
if (setSelectedMarkers) {
if (selectedMarkers?.find((id) => id === props.id)) {
setSelectedMarkers(
selectedMarkers.filter((id: string) => id !== props.id)
);
} else if (e.originalEvent.shiftKey) {
// add to selection if SHIFT key pressed
setSelectedMarkers([...(selectedMarkers ?? []), props.id]);
} else {
// replace selection
setSelectedMarkers([props.id]);
}
}
}

// Sometimes clicking throws off the popup's orientation, so reorient it
orientPopup(popupOrientationRef.current);
// Default popup behavior is to open on marker click
// Prevent by immediately closing it
e.target.closePopup();
}
};
// Sometimes clicking throws off the popup's orientation, so reorient it
orientPopup(popupOrientationRef.current);
// Default popup behavior is to open on marker click
// Prevent by immediately closing it
e.target.closePopup();
}
},
[setSelectedMarkers, selectedMarkers, props.id]
);

const handleDoubleClick = () => {
if (map) {
Expand Down
12 changes: 1 addition & 11 deletions packages/libs/components/src/map/MapVEuMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,6 @@ export interface MapVEuMapProps {
scrollingEnabled?: boolean;
/** pass default viewport */
defaultViewport?: Viewport;
/* selectedMarkers setState (for on-click reset) **/
setSelectedMarkers?: React.Dispatch<React.SetStateAction<string[]>>;
children?: React.ReactNode;
onMapClick?: () => void;
onMapDrag?: () => void;
Expand All @@ -179,7 +177,6 @@ function MapVEuMap(props: MapVEuMapProps, ref: Ref<PlotRef>) {
scrollingEnabled = true,
interactive = true,
defaultViewport,
setSelectedMarkers,
onMapClick,
onMapDrag,
onMapZoom,
Expand Down Expand Up @@ -253,7 +250,7 @@ function MapVEuMap(props: MapVEuMapProps, ref: Ref<PlotRef>) {
zoom={viewport.zoom}
minZoom={1}
zoomSnap={zoomSnap}
wheelPxPerZoomLevel={1000}
wheelPxPerZoomLevel={100}
// We add our own custom zoom control below
zoomControl={false}
style={{ height, width, ...style }}
Expand Down Expand Up @@ -296,7 +293,6 @@ function MapVEuMap(props: MapVEuMapProps, ref: Ref<PlotRef>) {
<MapVEuMapEvents
onViewportChanged={onViewportChanged}
onBaseLayerChanged={onBaseLayerChanged}
setSelectedMarkers={setSelectedMarkers}
onBoundsChanged={onBoundsChanged}
onMapClick={onMapClick}
onMapDrag={onMapDrag}
Expand All @@ -319,21 +315,17 @@ interface MapVEuMapEventsProps {
onViewportChanged: (viewport: Viewport) => void;
onBoundsChanged: (bondsViewport: BoundsViewport) => void;
onBaseLayerChanged?: (newBaseLayer: BaseLayerChoice) => void;
setSelectedMarkers?: React.Dispatch<React.SetStateAction<string[]>>;
onMapClick?: () => void;
onMapDrag?: () => void;
onMapZoom?: () => void;
}

const EMPTY_MARKERS: string[] = [];

// function to handle map events such as onViewportChanged and baselayerchange
function MapVEuMapEvents(props: MapVEuMapEventsProps) {
const {
onViewportChanged,
onBaseLayerChanged,
onBoundsChanged,
setSelectedMarkers,
onMapClick,
onMapDrag,
onMapZoom,
Expand Down Expand Up @@ -376,8 +368,6 @@ function MapVEuMapEvents(props: MapVEuMapEventsProps) {
},
// map click event: remove selected highlight markers
click: () => {
if (setSelectedMarkers != null) setSelectedMarkers(EMPTY_MARKERS);

if (onMapClick != null) onMapClick();
},
});
Expand Down
40 changes: 33 additions & 7 deletions packages/libs/components/src/map/SemanticMarkers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from 'react';
import { AnimationFunction, Bounds } from './Types';
import { BoundsDriftMarkerProps } from './BoundsDriftMarker';
import { useMap } from 'react-leaflet';
import { useMap, useMapEvents } from 'react-leaflet';
import { LatLngBounds } from 'leaflet';
import { debounce, isEqual } from 'lodash';

Expand All @@ -27,7 +27,7 @@ export interface SemanticMarkersProps {
/* selectedMarkers state **/
selectedMarkers?: string[];
/* selectedMarkers setState **/
setSelectedMarkers?: React.Dispatch<React.SetStateAction<string[]>>;
setSelectedMarkers?: (selectedMarkers: string[] | undefined) => void;
}

/**
Expand All @@ -48,6 +48,13 @@ export default function SemanticMarkers({
// react-leaflet v3
const map = useMap();

// cancel marker selection with a single click on the map
useMapEvents({
click: () => {
if (setSelectedMarkers != null) setSelectedMarkers(undefined);
},
});

const [prevRecenteredMarkers, setPrevRecenteredMarkers] =
useState<ReactElement<BoundsDriftMarkerProps>[]>(markers);

Expand Down Expand Up @@ -128,7 +135,13 @@ export default function SemanticMarkers({
});
// set them as current
// any marker that already existed will move to the modified position
setConsolidatedMarkers(animationValues.markers);
if (
!isEqual(
animationValues.markers.map(({ props }) => props),
consolidatedMarkers.map(({ props }) => props)
)
)
setConsolidatedMarkers(animationValues.markers);
// then set a timer to remove the old markers when zooming out
// or if zooming in, switch to just the new markers straight away
// (their starting position was set by `animationFunction`)
Expand All @@ -139,7 +152,13 @@ export default function SemanticMarkers({
);
} else {
/** First render of markers **/
setConsolidatedMarkers(recenteredMarkers);
if (
!isEqual(
recenteredMarkers.map(({ props }) => props),
consolidatedMarkers.map(({ props }) => props)
)
)
setConsolidatedMarkers(recenteredMarkers);
}

// To prevent infinite loops, especially when in "other worlds",
Expand Down Expand Up @@ -176,7 +195,14 @@ export default function SemanticMarkers({
);
}
}
}, [animation, map, markers, prevRecenteredMarkers, recenterMarkers]);
}, [
animation,
map,
markers,
prevRecenteredMarkers,
recenterMarkers,
consolidatedMarkers,
]);

// remove any selectedMarkers that no longer exist in the current markers
useEffect(() => {
Expand All @@ -188,7 +214,7 @@ export default function SemanticMarkers({
if (prunedSelectedMarkers.length < selectedMarkers.length)
setSelectedMarkers(prunedSelectedMarkers);
}
}, [consolidatedMarkers, selectedMarkers]);
}, [consolidatedMarkers, selectedMarkers, setSelectedMarkers]);

// add the selectedMarkers props and callback
// (and the scheduled-for-removal showPopup prop)
Expand All @@ -201,7 +227,7 @@ export default function SemanticMarkers({
setSelectedMarkers,
})
),
[consolidatedMarkers, selectedMarkers]
[consolidatedMarkers, selectedMarkers, setSelectedMarkers]
);

// this should use the unadulterated markers (which are always in the "main world")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default function geohashAnimation({
} else {
/** No difference in geohashes - Render markers as they are **/
zoomType = null;
consolidatedMarkers = [...markers];
consolidatedMarkers = markers;
}

return { zoomType: zoomType, markers: consolidatedMarkers };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ export const DonutMarkers: Story<MapVEuMapProps> = (args) => {
});

// make an array of objects state to list highlighted markers
const [selectedMarkers, setSelectedMarkers] = useState<string[]>([]);
const [selectedMarkers, setSelectedMarkers] =
useState<string[] | undefined>();

console.log('selectedMarkers =', selectedMarkers);

Expand All @@ -72,7 +73,6 @@ export const DonutMarkers: Story<MapVEuMapProps> = (args) => {
onViewportChanged={setViewport}
onBoundsChanged={handleViewportChanged}
zoomLevelToGeohashLevel={leafletZoomLevelToGeohashLevel}
setSelectedMarkers={setSelectedMarkers}
>
<SemanticMarkers
markers={markerElements}
Expand Down Expand Up @@ -110,7 +110,8 @@ export const ChartMarkers: Story<MapVEuMapProps> = (args) => {
const duration = defaultAnimationDuration;

// make an array of objects state to list highlighted markers
const [selectedMarkers, setSelectedMarkers] = useState<string[]>([]);
const [selectedMarkers, setSelectedMarkers] =
useState<string[] | undefined>();

console.log('selectedMarkers =', selectedMarkers);

Expand Down Expand Up @@ -142,7 +143,6 @@ export const ChartMarkers: Story<MapVEuMapProps> = (args) => {
onBoundsChanged={handleViewportChanged}
showGrid={true}
zoomLevelToGeohashLevel={leafletZoomLevelToGeohashLevel}
setSelectedMarkers={setSelectedMarkers}
>
<SemanticMarkers
markers={markerElements}
Expand Down
2 changes: 1 addition & 1 deletion packages/libs/eda/src/lib/core/components/layouts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface LayoutOptions {
}

export interface TitleOptions {
getPlotSubtitle?(config: unknown): ReactNode;
getPlotSubtitle?(config?: unknown): ReactNode;
}

export interface LegendOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ import {
barplotDefaultDependentAxisMinPos,
} from '../../../utils/axis-range-calculations';
import { createVisualizationPlugin } from '../VisualizationPlugin';
import { LayoutOptions } from '../../layouts/types';
import { LayoutOptions, TitleOptions } from '../../layouts/types';
import { OverlayOptions, RequestOptions } from '../options/types';
import { useDeepValue } from '../../../hooks/immutability';

Expand Down Expand Up @@ -138,6 +138,7 @@ export const barplotVisualization = createVisualizationPlugin({
interface Options
extends LayoutOptions,
OverlayOptions,
TitleOptions,
RequestOptions<BarplotConfig, {}, BarplotRequestParams> {}

function FullscreenComponent(props: VisualizationProps<Options>) {
Expand Down Expand Up @@ -902,6 +903,7 @@ function BarplotViz(props: VisualizationProps<Options>) {
.every((reqdVar) => !!(vizConfig as any)[reqdVar[0]]);

const LayoutComponent = options?.layoutComponent ?? PlotLayout;
const plotSubtitle = options?.getPlotSubtitle?.();
Copy link
Member Author

Choose a reason for hiding this comment

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

see standaloneVisPlugins.ts


return (
<div style={{ display: 'flex', flexDirection: 'column' }}>
Expand Down Expand Up @@ -935,7 +937,11 @@ function BarplotViz(props: VisualizationProps<Options>) {

<PluginError error={data.error} outputSize={outputSize} />
{!hideInputsAndControls && (
<OutputEntityTitle entity={outputEntity} outputSize={outputSize} />
<OutputEntityTitle
entity={outputEntity}
outputSize={outputSize}
subtitle={plotSubtitle}
/>
)}
<LayoutComponent
isFaceted={isFaceted(data.value)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,6 @@ function BoxplotViz(props: VisualizationProps<Options>) {
</>
);

// plot subtitle
const plotSubtitle = options?.getPlotSubtitle?.(
computation.descriptor.configuration
);
Expand Down
Loading
Loading