UNPKG

@hisptz/react-ui

Version:

A collection of reusable complex DHIS2 react ui components.

44 lines 1.16 kB
import { geoJSON } from "leaflet"; import { useEffect, useMemo } from "react"; import { useMapOrganisationUnit } from "../components/MapProvider/hooks"; import { useElementSize, useMediaQuery } from "usehooks-ts"; import { isEmpty } from "lodash"; import { useMap } from "react-leaflet"; export function useMapBounds() { const { orgUnits } = useMapOrganisationUnit(); const geoJSONObject = useMemo(() => geoJSON({ type: "FeatureCollection", features: orgUnits === null || orgUnits === void 0 ? void 0 : orgUnits.map(orgUnit => orgUnit.geoJSON) }), [orgUnits]); const center = useMemo(() => { return geoJSONObject.getBounds().getCenter(); }, [orgUnits]); const bounds = useMemo(() => { return geoJSONObject.getBounds(); }, [orgUnits]); return { center, bounds }; } export function useCenterMap(_ref) { let { bounds } = _ref; const map = useMap(); const [ref, { width, height }] = useElementSize(); useEffect(() => { if (!isEmpty(bounds)) { map.fitBounds(bounds); } }, [width, height]); return ref; } export function usePrintMedia() { return useMediaQuery("@media print"); }