Skip to content

Commit

Permalink
debug client: dynamically update interactive layers
Browse files Browse the repository at this point in the history
  • Loading branch information
flaktack committed Nov 7, 2024
1 parent 4dee5af commit 26f78f5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
14 changes: 13 additions & 1 deletion client/src/components/MapView/LayerControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { IControl, Map, TypedStyleLayer } from 'maplibre-gl';

type LayerControlProps = {
position: ControlPosition;
setInteractiveLayerIds: (interactiveLayerIds: string[]) => void;
};

/**
Expand All @@ -15,6 +16,12 @@ type LayerControlProps = {
class LayerControl implements IControl {
private readonly container: HTMLDivElement = document.createElement('div');

private readonly setInteractiveLayerIds: (interactiveLayerIds: string[]) => void;

constructor(setInteractiveLayerIds: (interactiveLayerIds: string[]) => void) {
this.setInteractiveLayerIds = setInteractiveLayerIds;
}

onAdd(map: Map) {
this.container.className = 'maplibregl-ctrl maplibregl-ctrl-group layer-select';

Expand Down Expand Up @@ -62,6 +69,10 @@ class LayerControl implements IControl {
return this.container;
}

private updateInteractiveLayerIds(map: Map) {
this.setInteractiveLayerIds(map.getLayersOrder().filter((layerId) => this.layerVisible(map, { id: layerId })));
}

private buildLayerDiv(layer: TypedStyleLayer, map: Map) {
const layerDiv = document.createElement('div');
layerDiv.className = 'layer';
Expand All @@ -77,6 +88,7 @@ class LayerControl implements IControl {
} else {
map.setLayoutProperty(layer.id, 'visibility', 'none');
}
this.updateInteractiveLayerIds(map);
};
input.checked = this.layerVisible(map, layer);
input.className = 'layer';
Expand Down Expand Up @@ -124,7 +136,7 @@ class LayerControl implements IControl {
}

export default function DebugLayerControl(props: LayerControlProps) {
useControl(() => new LayerControl(), {
useControl(() => new LayerControl(props.setInteractiveLayerIds), {
position: props.position,
});

Expand Down
5 changes: 3 additions & 2 deletions client/src/components/MapView/MapView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export function MapView({
const onMapDoubleClick = useMapDoubleClick({ tripQueryVariables, setTripQueryVariables });
const [showContextPopup, setShowContextPopup] = useState<LngLat | null>(null);
const [showPropsPopup, setShowPropsPopup] = useState<PopupData | null>(null);
const [interactiveLayerIds, setInteractiveLayerIds] = useState<string[]>([]);
const [cursor, setCursor] = useState<string>('auto');
const onMouseEnter = useCallback(() => setCursor('pointer'), []);
const onMouseLeave = useCallback(() => setCursor('auto'), []);
Expand Down Expand Up @@ -78,7 +79,7 @@ export function MapView({
}}
// it's unfortunate that you have to list these layers here.
// maybe there is a way around it: https://github.com/visgl/react-map-gl/discussions/2343
interactiveLayerIds={['regular-stop', 'area-stop', 'group-stop', 'parking-vertex', 'vertex', 'edge', 'link']}
interactiveLayerIds={interactiveLayerIds}
cursor={cursor}
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
Expand All @@ -97,7 +98,7 @@ export function MapView({
setTripQueryVariables={setTripQueryVariables}
loading={loading}
/>
<DebugLayerControl position="top-right" />
<DebugLayerControl position="top-right" setInteractiveLayerIds={setInteractiveLayerIds} />
{tripQueryResult?.trip.tripPatterns.length && (
<LegLines tripPattern={tripQueryResult.trip.tripPatterns[selectedTripPatternIndex] as TripPattern} />
)}
Expand Down

0 comments on commit 26f78f5

Please sign in to comment.