import L from 'leaflet'; import React, { useCallback, useEffect, useRef } from 'react'; import { defineMessages, FormattedMessage, useIntl } from 'react-intl'; import { Button, Modal, Stack } from 'soapbox/components/ui'; import { useAppSelector } from 'soapbox/hooks'; import { makeGetStatus } from 'soapbox/selectors'; import type { Status as StatusEntity } from 'soapbox/types/entities'; import 'leaflet/dist/leaflet.css'; L.Icon.Default.mergeOptions({ iconRetinaUrl: require('leaflet/dist/images/marker-icon-2x.png'), iconUrl: require('leaflet/dist/images/marker-icon.png'), shadowUrl: require('leaflet/dist/images/marker-shadow.png'), }); interface IEventMapModal { onClose: (type: string) => void, statusId: string, } const messages = defineMessages({ osmAttribution: { id: 'event_map.osm_attribution', defaultMessage: '© OpenStreetMap Contributors' }, }); const EventMapModal: React.FC = ({ onClose, statusId }) => { const intl = useIntl(); const getStatus = useCallback(makeGetStatus(), []); const status = useAppSelector(state => getStatus(state, { id: statusId })) as StatusEntity; const location = status.event!.location!; const map = useRef(); useEffect(() => { const latlng: [number, number] = [+location.get('latitude'), +location.get('longitude')]; map.current = L.map('event-map').setView(latlng, 15); L.marker(latlng, { title: location.get('name'), }).addTo(map.current); L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', { attribution: intl.formatMessage(messages.osmAttribution), }).addTo(map.current); return () => { map.current?.remove(); }; }, []); const onClickClose = () => { onClose('EVENT_MAP'); }; const onClickNavigate = () => { window.open(`https://www.openstreetmap.org/directions?from=&to=${location.get('latitude')},${location.get('longitude')}#map=14/${location.get('latitude')}/${location.get('longitude')}`, '_blank'); }; return ( } onClose={onClickClose} width='2xl' >
); }; export default EventMapModal;