Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

use memoized 'available' selector for layer list

Jason Kent25da8b28e640
diff --git a/web/js/containers/sidebar/layer-list.js b/web/js/containers/sidebar/layer-list.js
index 0e744ef4e..001907e1c 100644
--- a/web/js/containers/sidebar/layer-list.js
+++ b/web/js/containers/sidebar/layer-list.js
@@ -8,7 +8,7 @@ import {
replaceSubGroup,
getZotsForActiveLayers,
getTitles,
- memoizeAvailable as availableSelector,
+ memoizedAvailable as availableSelector,
} from '../../modules/layers/selectors';
import { reorderLayers } from '../../modules/layers/actions';
diff --git a/web/js/modules/layers/selectors.js b/web/js/modules/layers/selectors.js
index 702750c3a..65c04101d 100644
--- a/web/js/modules/layers/selectors.js
+++ b/web/js/modules/layers/selectors.js
@@ -17,7 +17,7 @@ const getLayersState = ({ layers }) => layers;
const getActiveCompareState = ({ compare }) => (compare.isCompareA ? 'active' : 'activeB');
const getCurrentDate = ({ date, compare }) => date[compare.isCompareA ? 'selected' : 'selectedB'];
const getCurrentActiveLayers = ({ compare, layers }) => (compare.isCompareA ? layers.active : layers.activeB);
-const getConfigParameters = ({ config }) => config && config.parameters;
+const getConfigParameters = ({ config }) => (config ? config.parameters : {});
/**
* Return a map of active layers where key is layer id
@@ -239,7 +239,7 @@ function forGroup(group, spec = {}, activeLayers, state) {
* @param {*} activeLayers
* @param {*} config
*/
-function dateRange(id, layers, { parameters }) {
+function dateRange(id, layers, parameters = {}) {
const { debugGIBS, ignoreDateRange } = parameters;
if (debugGIBS || ignoreDateRange) {
return {
@@ -331,8 +331,8 @@ function dateRange(id, layers, { parameters }) {
* @param {*} layers
* @param {*} config
*/
-export function available(id, date, layers, config) {
- const range = dateRange(id, layers, config);
+function available(id, date, layers, parameters) {
+ const range = dateRange(id, layers, parameters);
if (range && (date < range.start || date > range.end)) {
return false;
}
@@ -345,90 +345,7 @@ export function available(id, date, layers, config) {
*/
export const memoizedAvailable = createSelector(
[getCurrentDate, getCurrentActiveLayers, getConfigParameters],
- (currentDate, activeLayers, parameters) => lodashMemoize((id) => {
- let dateRange;
- const { debugGIBS, ignoreDateRange } = parameters;
- if (debugGIBS || ignoreDateRange) {
- dateRange = {
- start: new Date(Date.UTC(1970, 0, 1)),
- end: util.now(),
- };
- }
- let min = Number.MAX_VALUE;
- let max = 0;
- let hasRange = false;
- const maxDates = [];
- // Use the minute ceiling of the current time so that we don't run into an issue where
- // seconds value of current appNow time is greater than a layer's available time range
- const minuteCeilingCurrentTime = util.now().setSeconds(59);
- const def = lodashFind(activeLayers, { id });
-
- if (!def) {
- return;
- }
- if (def.startDate) {
- hasRange = true;
- const start = util.parseDateUTC(def.startDate).getTime();
- min = Math.min(min, start);
- }
- // For now, we assume that any layer with an end date is
- // an ongoing product unless it is marked as inactive.
- if (def.futureLayer && def.endDate) {
- hasRange = true;
- max = util.parseDateUTC(def.endDate).getTime();
- maxDates.push(new Date(max));
- } else if (def.inactive && def.endDate) {
- hasRange = true;
- const end = util.parseDateUTC(def.endDate).getTime();
- max = Math.max(max, end);
- maxDates.push(new Date(max));
- } else if (def.endDate) {
- hasRange = true;
- max = minuteCeilingCurrentTime;
- maxDates.push(new Date(max));
- }
- // If there is a start date but no end date, this is a
- // product that is currently being created each day, set
- // the max day to today.
- if (def.futureLayer && def.futureTime && !def.endDate) {
- // Calculate endDate + parsed futureTime from layer JSON
- max = new Date();
- const { futureTime } = def;
- const dateType = futureTime.slice(-1);
- const dateInterval = futureTime.slice(0, -1);
-
- if (dateType === 'D') {
- max.setDate(max.getDate() + parseInt(dateInterval, 10));
- maxDates.push(new Date(max));
- } else if (dateType === 'M') {
- max.setMonth(max.getMonth() + parseInt(dateInterval, 10));
- maxDates.push(new Date(max));
- } else if (dateType === 'Y') {
- max.setYear(max.getYear() + parseInt(dateInterval, 10));
- maxDates.push(new Date(max));
- }
- } else if (def.startDate && !def.endDate) {
- max = minuteCeilingCurrentTime;
- maxDates.push(new Date(max));
- }
-
- if (hasRange) {
- if (max === 0) {
- max = minuteCeilingCurrentTime;
- maxDates.push(max);
- }
- const maxDate = Math.max.apply(max, maxDates);
- dateRange = {
- start: new Date(min),
- end: new Date(maxDate),
- };
- }
-
- if (hasRange && (currentDate < dateRange.start || currentDate > dateRange.end)) {
- return false;
- }
- return true;
- }),
+ (currentDate, activeLayers, parameters) => lodashMemoize((id) => available(id, currentDate, activeLayers, parameters)),
);
/**
@@ -440,10 +357,11 @@ export const memoizedAvailable = createSelector(
* @param {*} state
*/
export function isRenderable(id, activeLayers, date, state) {
+ const { parameters } = state.config || {};
const activeDateStr = state.compare.isCompareA ? 'selected' : 'selectedB';
date = date || state.date[activeDateStr];
const def = lodashFind(activeLayers, { id });
- const notAvailable = !available(id, date, activeLayers, state.config);
+ const notAvailable = !available(id, date, activeLayers, parameters);
if (!def || notAvailable || !def.visible || def.opacity === 0) {
return false;
@@ -461,7 +379,7 @@ export function isRenderable(id, activeLayers, date, state) {
if (
otherDef.visible
&& otherDef.opacity === 1.0
- && available(otherDef.id, date, activeLayers, state.config)
+ && available(otherDef.id, date, activeLayers, parameters)
) {
obscured = true;
return false;