Interactive interface for browsing global, full-resolution satellite imagery
show total selected granule size
Jason Kentf2f716ed8cdd
diff --git a/web/css/smart-handoff.css b/web/css/smart-handoff.cssindex 9e3611171..2d41ec3c7 100644--- a/web/css/smart-handoff.css+++ b/web/css/smart-handoff.css@@ -120,13 +120,32 @@ label[for='chk-crop-toggle'] span {text-decoration: underline;}}--.granule-count span {- color: #fff;- font-size: 16px;- font-weight: 900;- margin: 14px 0 10px;+.granule-count-header {+ text-align: center;+}+.granule-count-info {+ margin: 0 30px;+ text-align: center;+ span {+ color: #fff;+ font-size: 16px;+ font-weight: 900;+ margin: 14px 5px 5px;+ display: inline-block;+ text-align: center;+ &.help-link {+ text-align: left;+ display: inline;+ }+ &.granule-size {+ font-weight: 300;+ font-size: 14px;+ line-height: 14px;+ vertical-align: bottom;+ }+ }}+.loading-granule-count {font-size: 30px;}@@ -135,11 +154,11 @@ label[for='chk-crop-toggle'] span {overflow: hidden;display: inline-block;vertical-align: bottom;- -moz-animation: ellipsis steps(4, end) 900ms infinite;- -o-animation: ellipsis steps(4, end) 900ms infinite;- -ms-animation: ellipsis steps(4, end) 900ms infinite;- -webkit-animation: ellipsis steps(4, end) 900ms infinite;- animation: ellipsis steps(4, end) 900ms infinite;+ -moz-animation: ellipsis steps(4, end) 600ms infinite;+ -o-animation: ellipsis steps(4, end) 600ms infinite;+ -ms-animation: ellipsis steps(4, end) 600ms infinite;+ -webkit-animation: ellipsis steps(4, end) 600ms infinite;+ animation: ellipsis steps(4, end) 600ms infinite;content: '\2026'; /* ascii code for the ellipsis character */width: 0;}@@ -160,11 +179,11 @@ label[for='chk-crop-toggle'] span {}.fade-in {- animation: fadein ease 1s;- -webkit-animation: fadein ease 1s;- -moz-animation: fadein ease 1s;- -o-animation: fadein ease 1s;- -ms-animation: fadein ease 1s;+ animation: fadein ease 0.25s;+ -webkit-animation: fadein ease 0.25s;+ -moz-animation: fadein ease 0.25s;+ -o-animation: fadein ease 0.25s;+ -ms-animation: fadein ease 0.25s;}/* Overrides react image crop component */diff --git a/web/js/components/smart-handoffs/granule-count.js b/web/js/components/smart-handoffs/granule-count.jsindex cfa110612..7943254c8 100644--- a/web/js/components/smart-handoffs/granule-count.js+++ b/web/js/components/smart-handoffs/granule-count.js@@ -1,9 +1,11 @@-import React, { useState, useEffect } from 'react';+import React, { useEffect, useState } from 'react';import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';import { get as lodashGet } from 'lodash';import PropTypes from 'prop-types';import util from '../../util/util';+const granulesBaseUrl = 'https://cmr.earthdata.nasa.gov/search/granules.json';+export default function GranuleCount (props) {const {currentExtent,@@ -12,83 +14,128 @@ export default function GranuleCount (props) {selectedLayer,selectedCollection,selectedDate,- showBoundingBox,} = props;- const [isLoading, setLoading] = useState(false);- const [selectedGranules, setSelectedGranules] = useState(0);- const [totalGranules, setTotalGranules] = useState(0);+ if (!selectedCollection) return null;- const updateGranules = async () => {- const { southWest, northEast } = currentExtent;- if (!selectedLayer) return;+ const [state, setState] = useState({+ isLoading: false,+ selectedGranules: 0,+ totalGranules: 0,+ granuleDownloadSize: 0,+ });+ const {+ isLoading, totalGranules, selectedGranules, granuleDownloadSize,+ } = state;++ const requestGranules = async (url) => {+ const granulesResponse = await fetch(url, { timeout: 5000 });+ const result = await granulesResponse.json();+ return lodashGet(result, 'feed.entry', []);+ };++ const getDownloadSize = (entries) => entries.reduce(+ (prev, curr) => prev + parseInt(curr.granule_size, 10), 0,+ );+ const updateGranules = async () => {+ const { southWest, northEast } = currentExtent || {};+ let newTotalGranules = 0;+ let newSelectedGranules;+ let newGranuleDownloadSize = 0;const { dateRanges } = selectedLayer;const params = {- include_granule_counts: true,- concept_id: selectedCollection.value,+ collection_concept_id: selectedCollection.value,+ pageSize: 500,};-if (dateRanges) {const startDate = `${selectedDate}T00:00:00.000Z`;const endDate = `${selectedDate}T23:59:59.999Z`;params.temporal = `${startDate},${endDate}`;}- let granuleRequestUrl = `https://cmr.earthdata.nasa.gov/search/collections.json${util.toQueryString(params)}`;+ const granulesRequestUrl = granulesBaseUrl + util.toQueryString(params);- if (!totalGranules) {- // Gets the total amount of granules that the layer has- const totalGranuleResponse = await fetch(granuleRequestUrl, { timeout: 5000 });- const totalResult = await totalGranuleResponse.json();- const newTotalGranules = lodashGet(totalResult, 'feed.entry[0].granule_count', 0);- setTotalGranules(newTotalGranules);+ if (currentExtent) {+ const bboxRequestUrl = `${granulesRequestUrl}&bounding_box=${southWest},${northEast}`;+ const selectedEntries = await requestGranules(bboxRequestUrl);+ newSelectedGranules = selectedEntries.length;+ newGranuleDownloadSize = getDownloadSize(selectedEntries);}- // Gets the total subset of granules that are within the defining bounding box- if (showBoundingBox && southWest && northEast) {- granuleRequestUrl += `&bounding_box=${southWest},${northEast}`;- const selectedGranulesResponse = await fetch(granuleRequestUrl, { timeout: 5000 });- const selectedResult = await selectedGranulesResponse.json();- const newSelectedGranules = lodashGet(selectedResult, 'feed.entry[0].granule_count', 0);- setSelectedGranules(newSelectedGranules);+ const totalEntries = await requestGranules(granulesRequestUrl);+ newTotalGranules = totalEntries.length;+ if (newGranuleDownloadSize === 0) {+ newGranuleDownloadSize = getDownloadSize(totalEntries);}- setLoading(false);++ setState({+ isLoading: false,+ totalGranules: newTotalGranules,+ selectedGranules: newSelectedGranules,+ granuleDownloadSize: newGranuleDownloadSize,+ });};/*** Fetch granule data for the specified selected layer/collection*/useEffect(() => {- setTotalGranules(0);- setLoading(true);+ setState({+ isLoading: true,+ totalGranules: 0,+ selectedGranules: 0,+ });updateGranules();- }, [currentExtent, selectedCollection, displayDate]);+ }, [currentExtent, selectedCollection, selectedDate]);++ const granulesExist = !isLoading && totalGranules !== 0;- return !selectedCollection ? null : (+ const renderLoading = () => (+ <span className="loading-granule-count" />+ );++ const renderDownloadSize = () => {+ const printSize = (s) => Number(s).toFixed(0).toLocaleString();+ let sizeText;+ if (granuleDownloadSize > 1000) {+ sizeText = `${printSize(granuleDownloadSize / 1000)} GB`;+ } else {+ sizeText = `${printSize(granuleDownloadSize)} MB`;+ }+ return (+ <>+ { granulesExist && (+ <span className="granule-size fade-in">{`(${sizeText})`}</span>+ )}+ <span className="help-link" onClick={showGranuleHelpModal}>+ <FontAwesomeIcon icon="question-circle" />+ </span>+ </>+ );+ };++ return (<div className="granule-count">- <h1>+ <div className="granule-count-header">Available granules for{` ${displayDate}: `}+ </div>- { !isLoading && totalGranules === 0 && (- <span className="fade-in constant-width">NONE</span>- )}+ <div className="granule-count-info">- { !isLoading && totalGranules !== 0 && (- <span className="fade-in constant-width">- {showBoundingBox && `${selectedGranules} of `}- {totalGranules}- </span>+ {!isLoading && (+ <>+ <span className="fade-in">+ {granulesExist && selectedGranules && `${selectedGranules} of `}+ {granulesExist ? totalGranules : 'NONE'}+ </span>+ </>)}- { isLoading && (- <span className="loading-granule-count fade-in constant-width" />- )}- <span className="help-link" onClick={showGranuleHelpModal}>- <FontAwesomeIcon icon="question-circle" />- </span>- </h1>+ {!isLoading ? renderDownloadSize() : renderLoading()}++ </div></div>);}@@ -99,6 +146,5 @@ GranuleCount.propTypes = {selectedLayer: PropTypes.object,selectedDate: PropTypes.string,selectedCollection: PropTypes.object,- showBoundingBox: PropTypes.bool,showGranuleHelpModal: PropTypes.func,};diff --git a/web/js/containers/sidebar/smart-handoff.js b/web/js/containers/sidebar/smart-handoff.jsindex 1ee4ae707..e6646f143 100644--- a/web/js/containers/sidebar/smart-handoff.js+++ b/web/js/containers/sidebar/smart-handoff.js@@ -57,7 +57,7 @@ class SmartHandoff extends Component {y2: screenHeight / 2 + 100,},showBoundingBox: false,- currentExtent: {},+ currentExtent: undefined,coordinates: {},};@@ -66,7 +66,7 @@ class SmartHandoff extends Component {this.onCheckboxToggle = this.onCheckboxToggle.bind(this);this.onClickDownload = this.onClickDownload.bind(this);this.updateExtent = this.updateExtent.bind(this);- this.debouncedUpdateExtent = lodashDebounce(this.updateExtent, 250);+ this.debouncedUpdateExtent = lodashDebounce(this.updateExtent, 500, { leading: true });}componentDidUpdate(prevProps) {@@ -155,7 +155,7 @@ class SmartHandoff extends Component {};if (selectedCollection && extent) {- this.debouncedUpdateExtent(coordinates, boundaries, extent);+ this.debouncedUpdateExtent(coordinates, newBoundaries, extent);}}@@ -417,11 +417,10 @@ class SmartHandoff extends Component {{this.renderCropBox()}<GranuleCountdisplayDate={displayDate}- currentExtent={currentExtent}+ currentExtent={showBoundingBox && currentExtent}selectedDate={selectedDate}selectedLayer={selectedLayer}selectedCollection={selectedCollection}- showBoundingBox={showBoundingBox}showGranuleHelpModal={showGranuleHelpModal}/><Button