Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

5.0 KB
Raw Blame
import { useState } from 'react';
import PropTypes from 'prop-types';
import { ListGroupItem, UncontrolledTooltip } from 'reactstrap';
import { connect } from 'react-redux';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { available, getActiveLayersMap } from '../../../../modules/layers/selectors';
import Checkbox from '../../../util/checkbox';
import {
  addLayer as addLayerAction,
  removeLayer as removeLayerAction,
} from '../../../../modules/layers/actions';
import SelectedDate from '../../../selected-date';
import { getSelectedDate } from '../../../../modules/date/selectors';
import { getLayerNoticesForLayer } from '../../../../modules/notifications/util';
import MonospaceDate from '../../../util/monospace-date';

/*
 * A scrollable list of layers
 * @class LayerList
 * @extends React.Component
 */
function MeasurementLayerRow (props) {
  const {
    isEnabled,
    isMobile,
    removeLayer,
    addLayer,
    layer,
    measurementId,
    title,
    selectedDate,
    layerNotices,
    isChartable,
  } = props;

  const [isHoveringChartingIcon, setIsHoveringChartingIcon] = useState(false);

  const layerIsUnavailable = !available(layer.id, selectedDate, [layer]);
  const listItemClass = layerIsUnavailable || layerNotices ? 'unavailable' : '';
  // Replace periods in id since period causes issue with tooltip targeting
  const itemElementId = `checkbox-case-${layer.id.split('.').join('-')}`;
  const checkboxId = `${layer.id.split('.').join('-')}-checkbox`;

  const onCheck = () => {
    if (isEnabled) {
      removeLayer(layer.id);
    } else {
      addLayer(layer.id);
    }
  };

  return (
    <ListGroupItem
      key={`${measurementId}-${layer.id}`}
      id={itemElementId}
      className={listItemClass}
    >
      <Checkbox
        id={checkboxId}
        name={title}
        onCheck={onCheck}
        checked={isEnabled}
        label={title}
        classNames="settings-check"
      >
        {layerIsUnavailable && (<FontAwesomeIcon icon="ban" id="availability-info" widthAuto />)}
        {layerNotices && (<FontAwesomeIcon icon="exclamation-triangle" id="notice-info" widthAuto />)}
        {(layerNotices || layerIsUnavailable || isHoveringChartingIcon) && (
          <UncontrolledTooltip
            id="center-align-tooltip"
            target={itemElementId}
            boundariesElement="window"
            className="zot-tooltip"
            placement="right"
            trigger="hover"
            autohide={isMobile}
            delay={isMobile ? { show: 300, hide: 300 } : { show: 50, hide: 300 }}
          >
            {layerIsUnavailable && (
              <div>
                This layer has no visible content on the selected date:
                {' '}
                <MonospaceDate>
                  <SelectedDate />
                </MonospaceDate>
              </div>
            )}
            {layerNotices && (<div dangerouslySetInnerHTML={{ __html: layerNotices }} />)}
            {isHoveringChartingIcon && (
              <div
                dangerouslySetInnerHTML={{
                  __html: 'Create time series charts or get statistics for this layer',
                }}
              />
            )}
          </UncontrolledTooltip>
        )}
        {isChartable && (
          <span
            className="chartable-icon-wrapper"
            onMouseEnter={() => setIsHoveringChartingIcon(true)}
            onMouseLeave={() => setIsHoveringChartingIcon(false)}
          >
            <i
              id={`${layer.id}-chartable-info`}
              className="layer-notice-icon chartable-icon"
            />
          </span>
        )}
      </Checkbox>
    </ListGroupItem>
  );
}

MeasurementLayerRow.propTypes = {
  addLayer: PropTypes.func,
  isChartable: PropTypes.bool,
  isEnabled: PropTypes.bool,
  isMobile: PropTypes.bool,
  layer: PropTypes.oneOfType([PropTypes.object, PropTypes.oneOf(['null'])]),
  layerNotices: PropTypes.string,
  measurementId: PropTypes.string,
  removeLayer: PropTypes.func,
  selectedDate: PropTypes.oneOfType([PropTypes.object, PropTypes.oneOf(['null'])]),
  title: PropTypes.string,
};

const mapStateToProps = (state, ownProps) => {
  const { notifications, screenSize } = state;
  const activeLayerMap = getActiveLayersMap(state);
  const { layer } = ownProps;
  const { id, colormapType, layerPeriod, disableCharting } = layer;
  const isChartable = Object.prototype.hasOwnProperty.call(layer, 'palette') && Object.prototype.hasOwnProperty.call(layer, 'colormapType') && colormapType === 'continuous' && layerPeriod === 'Daily' && !disableCharting;
  return {
    isEnabled: !!activeLayerMap[id],
    isMobile: screenSize.isMobile,
    selectedDate: getSelectedDate(state),
    layerNotices: getLayerNoticesForLayer(id, notifications),
    isChartable,
  };
};

const mapDispatchToProps = (dispatch) => ({
  addLayer: (id) => {
    dispatch(addLayerAction(id));
  },
  removeLayer: (id) => {
    dispatch(removeLayerAction(id));
  },
});

export default connect(
  mapStateToProps,
  mapDispatchToProps,
)(MeasurementLayerRow);