import { memo } from "preact/compat"; import { useMemo, useRef, useState } from "preact/hooks"; import BaseLayer from "../BaseLayer"; import FeaturesInfosListener from "../FeaturesInfosListener"; import I18n from "../I18n"; import LayoutState from "../LayoutState"; import LinesNetworkPlanLayer from "../LinesNetworkPlanLayer"; import LinesNetworkPlanLayerHighlight from "../LinesNetworkPlanLayerHighlight"; import MapDispatchEvents from "../MapDispatchEvents"; import MapLayout from "../MapLayout"; import MapsetLayer from "../MapsetLayer"; import NotificationsLayer from "../NotificationsLayer"; import Permalink from "../Permalink"; import RealtimeLayer from "../RealtimeLayer"; import SingleClickListener from "../SingleClickListener"; import StationsLayer from "../StationsLayer"; import useInitialLayersVisiblity from "../utils/hooks/useInitialLayersVisiblity"; import { MapContext } from "../utils/hooks/useMapContext"; import WindowMessageListener from "../WindowMessageListener"; import MobilityMapAttributes from "./MobilityMapAttributes"; // @ts-expect-error bad type definition import tailwind from "../style.css"; // @ts-expect-error bad type definition import style from "./index.css"; import type { MapsetLayer as MtbMapsetLayer } from "mobility-toolbox-js/ol"; import type { MaplibreLayer, MaplibreStyleLayer, MocoLayer, RealtimeLayer as MtbRealtimeLayer, } from "mobility-toolbox-js/ol"; import type { LayerGetFeatureInfoResponse, RealtimeStation, RealtimeStationId, RealtimeTrainId, SituationType, } from "mobility-toolbox-js/types"; import type { Feature, Map as OlMap } from "ol"; import type { MobilityMapAttributeName } from "./MobilityMapAttributes"; export type MobilityMapProps = Record< MobilityMapAttributeName, null | string | undefined >; function MobilityMap(props: MobilityMapProps) { const eventNodeRef = useRef(); const [baseLayer, setBaseLayer] = useState(); const [isFollowing, setIsFollowing] = useState(false); const [isTracking, setIsTracking] = useState(false); const [isEmbed, setIsEmbed] = useState(false); const [hasGeolocation, setHasGeolocation] = useState(false); const [hasLnp, setHasLnp] = useState(false); const [hasDetails, setHasDetails] = useState(false); const [hasMapset, setHasMapset] = useState(false); const [hasNotification, setHasNotification] = useState(false); const [hasPermalink, setHasPermalink] = useState(false); const [hasPrint, setHasPrint] = useState(false); const [hasRealtime, setHasRealtime] = useState(false); const [hasSearch, setHasSearch] = useState(false); const [hasStations, setHasStations] = useState(false); const [hasToolbar, setHasToolbar] = useState(false); const [hasShare, setHasShare] = useState(false); const [hasLayerTree, setHasLayerTree] = useState(false); const [isOverlayOpen, setIsOverlayOpen] = useState(false); const [isExportMenuOpen, setIsExportMenuOpen] = useState(false); const [isShareMenuOpen, setIsShareMenuOpen] = useState(false); const [isLayerTreeOpen, setIsLayerTreeOpen] = useState(false); const [isSearchOpen, setIsSearchOpen] = useState(false); const [stationsLayer, setStationsLayer] = useState(); const [station, setStation] = useState(); const [realtimeLayer, setRealtimeLayer] = useState(); const [notificationsLayer, setNotificationsLayer] = useState(); const [mapsetLayer, setMapsetLayer] = useState(); const [linesNetworkPlanLayer, setLinesNetworkPlanLayer] = useState(); const [map, setMap] = useState(); const [stationId, setStationId] = useState(); const [trainId, setTrainId] = useState(); const [linesIds, setLinesIds] = useState(); const [notificationId, setNotificationId] = useState(); const [notificationSupportedLanguages, setNotificationSupportedLanguages] = useState(); const [featuresInfos, setFeaturesInfos] = useState< LayerGetFeatureInfoResponse[] >([]); const [featuresInfosHovered, setFeaturesInfosHovered] = useState< LayerGetFeatureInfoResponse[] >([]); const [selectedFeature, setSelectedFeature] = useState(null); const [selectedFeatures, setSelectedFeatures] = useState([]); const [permalinkUrlSearchParams, setPermalinkUrlSearchParams] = useState(); const [previewNotifications, setPreviewNotifications] = useState(); const { lang, layers, permalinktemplate } = props; // Apply initial visibility of layers useInitialLayersVisiblity(map, layers, permalinktemplate); // Object representing all the web-component attributes and the map context values. const mapContextValue = useMemo(() => { return { // MobilityMapProps ...props, baseLayer, featuresInfos, featuresInfosHovered, hasDetails, hasGeolocation, hasLayerTree, hasLnp, hasMapset, hasNotification, hasPermalink, hasPrint, hasRealtime, hasSearch, hasShare, hasStations, hasToolbar, isEmbed, isExportMenuOpen, isFollowing, isLayerTreeOpen, isOverlayOpen, isSearchOpen, isShareMenuOpen, isTracking, linesIds, linesNetworkPlanLayer, map, mapsetLayer, notificationId, notificationsLayer, // MapContextProps notificationSupportedLanguages, permalinkUrlSearchParams, previewNotifications, realtimeLayer, selectedFeature, selectedFeatures, setBaseLayer, setFeaturesInfos, setFeaturesInfosHovered, setHasDetails, setHasGeolocation, setHasLayerTree, setHasLnp, setHasMapset, setHasNotification, setHasPermalink, setHasPrint, setHasRealtime, setHasSearch, setHasShare, setHasStations, setHasToolbar, setIsEmbed, setIsExportMenuOpen, setIsFollowing, setIsLayerTreeOpen, setIsOverlayOpen, setIsSearchOpen, setIsShareMenuOpen, setIsTracking, setLinesIds, setLinesNetworkPlanLayer, setMap, setMapsetLayer, setNotificationId, setNotificationsLayer, setNotificationSupportedLanguages, setPermalinkUrlSearchParams, setPreviewNotifications, setRealtimeLayer, setSelectedFeature, setSelectedFeatures, setStation, setStationId, setStationsLayer, setTrainId, station, stationId, stationsLayer, trainId, }; }, [ props, baseLayer, featuresInfos, featuresInfosHovered, hasDetails, hasGeolocation, hasLayerTree, hasLnp, hasMapset, hasNotification, hasPermalink, hasPrint, hasRealtime, hasSearch, hasShare, hasStations, hasToolbar, isEmbed, isExportMenuOpen, isFollowing, isLayerTreeOpen, isOverlayOpen, isSearchOpen, isShareMenuOpen, isTracking, linesIds, linesNetworkPlanLayer, map, mapsetLayer, notificationId, notificationSupportedLanguages, notificationsLayer, permalinkUrlSearchParams, previewNotifications, realtimeLayer, selectedFeature, selectedFeatures, station, stationId, stationsLayer, trainId, ]); return ( {/* There is a bug in tailwindcss@4 , variables are not imported in the shadow dom see https://github.com/tailwindlabs/tailwindcss/issues/15005*/} {/* Layers */} {hasNotification && } {hasRealtime && } {hasStations && } {hasLnp && } {hasLnp && } {hasMapset && } {/* Layout */}
); } const MemoMobilityMap = memo(MobilityMap); // We creates a wrapper to inject the default props values from MobilityMapAttributes. const defaultProps: Partial = {}; Object.entries(MobilityMapAttributes).forEach(([key]) => { defaultProps[key] = MobilityMapAttributes[key].defaultValue || null; }); function MobilityMapWithDefaultProps(props: MobilityMapProps) { return ; } export default memo(MobilityMapWithDefaultProps);