/* eslint-disable react/jsx-indent-props */ /* eslint-disable import/no-absolute-path */ /* eslint-disable import/no-absolute-path */ import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { withTranslation } from 'react-i18next'; import GoogleMutantLayer from '/imports/ui/components/Maps/GoogleMutantLayer'; import Gkeys from '/imports/startup/client/Gkeys'; import { TileLayer, LayersControl } from 'react-leaflet'; const { BaseLayer } = LayersControl; const defOpacity = 0.7; // Cuánto se espera a que Google conteste antes de montar el control solo con // OpenStreetMap. Si el script de Google no llega (sin red hacia Google, una // extensión que lo bloquea, un despliegue sin clave), el mapa tiene que salir // igual: las capas de Google son un extra. const GMAPS_WAIT_MS = 5000; class DefMapLayers extends Component { constructor(props) { super(props); this.state = { gkey: null, // El control de capas se monta UNA vez, con la lista de capas ya cerrada. // Añadirle capas después —que es lo que pasaba al resolverse la clave de // Google— hacía que react-leaflet se dejara por el camino la capa base // marcada, y el mapa se quedaba en gris: sin teselas, sin fuegos y con el // "Actualizando…" eterno. Solo se veía en despliegues CON clave de Google // (staging y producción), nunca en desarrollo, que no la tiene. ready: false }; } componentDidMount() { this.mounted = true; const settle = (key) => { if (!this.mounted || this.state.ready) return; this.setState({ gkey: key || null, ready: true }); }; Gkeys.load((err, key) => settle(key)); this.waitTimer = setTimeout(() => settle(null), GMAPS_WAIT_MS); } componentWillUnmount() { this.mounted = false; clearTimeout(this.waitTimer); } render() { const { t } = this.props; if (!this.state.ready) return null; const osmgraylayer = ( ); const osmlayer = ( ); return ( {osmlayer} {osmgraylayer} {/* React.Fragment does not work here */} { this.state.gkey && } { this.state.gkey && } { this.state.gkey && } ); } } DefMapLayers.propTypes = { t: PropTypes.func.isRequired, gray: PropTypes.bool, osmcolor: PropTypes.bool, terrain: PropTypes.bool, satellite: PropTypes.bool }; DefMapLayers.defaultProps = { gray: false, osmcolor: false, terrain: false, satellite: false }; export default withTranslation()(DefMapLayers);