import { useContext, useEffect, useRef, useState } from "react"; import { InternalContext } from "../context/LolOpenLayersMapContext"; import { IFeatureSource } from "../sourceDefinition/LolOpenLayersSourceDef"; export interface MapLayerSelection { layerName: string; ids: number[]; features: IFeatureSource[]; } export type MapSelectHandler = (selected: MapLayerSelection[]) => void; export const useLolOpenLayersMapSelectEvent = (selected: MapSelectHandler) => { const [setupHandler, setSetupHandler] = useState(true); const mapSelectHandlerRef = useRef(undefined); const internalContext = useContext(InternalContext); useEffect(() => { mapSelectHandlerRef.current = selected; }, [selected]); useEffect(() => { if (!setupHandler || undefined === internalContext.ref()) return; internalContext.ref()!!.onSelect = (selected: MapLayerSelection[]) => { mapSelectHandlerRef.current?.(selected); }; setSetupHandler(false); }, []); };