Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use VizDataset data type for block map, set a default value for interactive #1006

Merged
merged 8 commits into from
Jun 28, 2024
178 changes: 69 additions & 109 deletions app/scripts/components/common/blocks/block-map.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, { useMemo, useState, useEffect } from 'react';
import styled from 'styled-components';
import { DatasetDatumFnResolverBag, ProjectionOptions, datasets } from 'veda';
// @NOTE: This should be replaced by types/veda once the changes are consolidated
import { ProjectionOptions } from 'veda';
hanbyul-here marked this conversation as resolved.
Show resolved Hide resolved
import { MapboxOptions } from 'mapbox-gl';
import * as dateFns from 'date-fns';
import {
convertProjectionToMapbox,
projectionDefault,
Expand All @@ -15,7 +15,6 @@ import MapMessage from '../map/map-message';
import {
formatCompareDate,
formatSingleDate,
resolveConfigFunctions
} from '../map/utils';
import {
BasemapId,
Expand All @@ -30,10 +29,10 @@ import {
ScaleControl
} from '$components/common/map/controls';
import { Layer } from '$components/exploration/components/map/layer';
import { S_SUCCEEDED } from '$utils/status';
import {
TimelineDataset,
TimelineDatasetSuccess
VizDataset,
VizDatasetSuccess,
DatasetStatus
} from '$components/exploration/types.d.ts';

import { reconcileDatasets } from '$components/exploration/data-utils';
Expand Down Expand Up @@ -126,11 +125,25 @@ interface MapBlockProps {
layerId: string;
}

const getDataLayer = (layerIndex: number, layers: VizDataset[] | undefined): (VizDatasetSuccess | null) => {
if (!layers || layers.length <= layerIndex) return null;
const layer = layers[layerIndex];

// @NOTE: What to do when data returns ERROR
if (layer.status !== DatasetStatus.SUCCESS) return null;
return {
...layer,
settings: {
isVisible: true,
opacity: 100
}
};
};

function MapBlock(props: MapBlockProps) {
const generatedId = useMemo(() => `map-${++mapInstanceId}`, []);

const {
datasetId,
layerId,
dateTime,
compareDateTime,
Expand All @@ -144,36 +157,35 @@ function MapBlock(props: MapBlockProps) {
} = props;

const errors = validateBlockProps(props);

if (errors.length) {
throw new HintedError('Malformed Map Block', errors);
}

const [baseLayers, setBaseLayers] = useState<TimelineDataset[] | undefined>();
const [compareLayers, setCompareLayers] = useState<
TimelineDataset[] | undefined
>();

const [baseMapStaticData] = reconcileDatasets([layerId], datasetLayers, []);
const baseMapStaticCompareData = baseMapStaticData.data.compare;

let compareLayerId: undefined | string;
if (baseMapStaticCompareData && 'layerId' in baseMapStaticCompareData) {
compareLayerId = baseMapStaticCompareData.layerId;
}
const layersToFetch = useMemo(() => {
const [baseMapStaticData] = reconcileDatasets([layerId], datasetLayers, []);
let totalLayers = [baseMapStaticData];
const baseMapStaticCompareData = baseMapStaticData.data.compare;
if (baseMapStaticCompareData && 'layerId' in baseMapStaticCompareData) {
const compareLayerId = baseMapStaticCompareData.layerId;
const [compareMapStaticData] = reconcileDatasets(
compareLayerId ? [compareLayerId] : [],
datasetLayers,
[]
);
totalLayers = [...totalLayers, compareMapStaticData];
}
return totalLayers;
},[layerId]);

const [compareMapStaticData] = reconcileDatasets(
compareLayerId ? [compareLayerId] : [],
datasetLayers,
[]
);
const [layers, setLayers] = useState<VizDataset[]>(layersToFetch);

useReconcileWithStacMetadata([baseMapStaticData], setBaseLayers);
useReconcileWithStacMetadata([compareMapStaticData], setCompareLayers);
useReconcileWithStacMetadata(layers, setLayers);

const selectedDatetime = dateTime
const selectedDatetime: (Date | undefined) = dateTime
? utcString2userTzDate(dateTime)
: undefined;
const selectedCompareDatetime = compareDateTime
const selectedCompareDatetime: (Date | undefined) = compareDateTime
? utcString2userTzDate(compareDateTime)
: undefined;

Expand All @@ -197,57 +209,11 @@ function MapBlock(props: MapBlockProps) {

const [, setProjection] = useState(projectionStart);

const dataset = datasetId ? datasets[datasetId] : null;
const baseDataLayer: (VizDatasetSuccess | null) = useMemo(() => getDataLayer(0, layers), [layers]);
const compareDataLayer: (VizDatasetSuccess | null) = useMemo(() => getDataLayer(1, layers), [layers]);

const resolverBag = useMemo<DatasetDatumFnResolverBag>(
() => ({
datetime: selectedDatetime,
compareDatetime: selectedCompareDatetime,
dateFns
}),
[selectedDatetime, selectedCompareDatetime]
);

const [baseLayerResolvedData] = useMemo(() => {
if (!baseLayers || baseLayers.length !== 1) return [null, null];
const baseLayer = baseLayers[0];

if (baseLayer.status !== S_SUCCEEDED) return [null, null];

const bag = { ...resolverBag, raw: baseLayer.data };
const data = resolveConfigFunctions(baseLayer.data, bag);
return [data];
}, [baseLayers, resolverBag]);

const baseDataLayer: TimelineDataset | null = {
data: baseLayerResolvedData,
settings: {
isVisible: true,
opacity: 100
}
} as unknown as TimelineDataset;
const baseTimeDensity = baseLayerResolvedData?.timeDensity;

// Resolve data needed for the compare layer once it is loaded.
const [compareLayerResolvedData] = useMemo(() => {
if (!compareLayers || compareLayers.length !== 1) return [null, null];
const compareLayer = compareLayers[0];

if (compareLayer.status !== S_SUCCEEDED) return [null, null];
// Include access to raw data.
const bag = { ...resolverBag, raw: compareLayer.data };
const data = resolveConfigFunctions(compareLayer.data, bag);
return [data];
}, [compareLayers, resolverBag]);

const compareDataLayer: TimelineDataset | null = {
data: compareLayerResolvedData,
settings: {
isVisible: true,
opacity: 100
}
} as unknown as TimelineDataset;
const compareTimeDensity = compareLayerResolvedData?.timeDensity;
const baseTimeDensity = baseDataLayer?.data.timeDensity;
const compareTimeDensity = compareDataLayer?.data.timeDensity;

const mapOptions: Partial<MapboxOptions> = {
style: DEFAULT_MAP_STYLE_URL,
Expand Down Expand Up @@ -292,8 +258,8 @@ function MapBlock(props: MapBlockProps) {

const computedCompareLabel = useMemo(() => {
// Use a provided label if it exist.
if (compareLabel && compareLayerResolvedData) {
const providedLabel = compareLayerResolvedData.mapLabel as string;
if (compareLabel && compareDataLayer) {
const providedLabel = compareDataLayer.data.mapLabel as string;
return providedLabel;
}

Expand All @@ -308,7 +274,7 @@ function MapBlock(props: MapBlockProps) {
: null;
}, [
compareLabel,
compareLayerResolvedData,
compareDataLayer,
selectedDatetime,
compareToDate,
baseTimeDensity,
Expand All @@ -329,33 +295,32 @@ function MapBlock(props: MapBlockProps) {
}}
>
<Basemap basemapStyleId={mapBasemapId} />
{dataset && selectedDatetime && layerId && baseLayerResolvedData && (
{selectedDatetime && baseDataLayer && (
<Layer
key={baseLayerResolvedData.id}
id={`base-${baseLayerResolvedData.id}`}
dataset={baseDataLayer as unknown as TimelineDatasetSuccess}
key={baseDataLayer.data.id}
id={`base-${baseDataLayer.data.id}`}
dataset={baseDataLayer}
selectedDay={selectedDatetime}
/>
)}
{baseLayerResolvedData?.legend && (
{baseDataLayer?.data.legend && (
// Map overlay element
// Layer legend for the active layer.
// @NOTE: LayerLegendContainer is in old mapbox directory, may want to move this over to /map directory once old directory is deprecated
<LayerLegendContainer>
<LayerLegend
id={`base-${baseLayerResolvedData.id}`}
title={baseLayerResolvedData.name}
description={baseLayerResolvedData.description}
{...baseLayerResolvedData.legend}
id={`base-${baseDataLayer.data.id}`}
title={baseDataLayer.data.name}
description={baseDataLayer.data.description}
{...baseDataLayer.data.legend}
/>
{compareLayerResolvedData?.legend &&
{compareDataLayer?.data.legend &&
!!selectedCompareDatetime &&
baseLayerResolvedData.id !== compareLayerResolvedData.id && (
baseDataLayer.data.id !== compareDataLayer.data.id && (
<LayerLegend
id={`compare-${compareLayerResolvedData.id}`}
title={compareLayerResolvedData.name}
description={compareLayerResolvedData.description}
{...compareLayerResolvedData.legend}
id={`compare-${compareDataLayer.data.id}`}
title={compareDataLayer.data.name}
description={compareDataLayer.data.description}
{...compareDataLayer.data.legend}
/>
)}
</LayerLegendContainer>
Expand All @@ -364,19 +329,19 @@ function MapBlock(props: MapBlockProps) {
{selectedDatetime && selectedCompareDatetime ? (
<MapMessage
id='compare-message'
active={!!(selectedCompareDatetime && compareLayerResolvedData)}
active={!!(compareDataLayer && selectedCompareDatetime)}
>
{computedCompareLabel}
</MapMessage>
) : (
<MapMessage
id='single-map-message'
active={!!(selectedDatetime && baseLayerResolvedData)}
active={!!(selectedDatetime && baseDataLayer)}
>
{selectedDatetime &&
formatSingleDate(
selectedDatetime,
baseLayerResolvedData?.timeDensity
baseDataLayer?.data.timeDensity
)}
</MapMessage>
)}
Expand All @@ -387,16 +352,11 @@ function MapBlock(props: MapBlockProps) {
{selectedCompareDatetime && (
<Compare>
<Basemap basemapStyleId={mapBasemapId} />
{dataset &&
selectedCompareDatetime &&
layerId &&
compareLayerResolvedData && (
{compareDataLayer && (
<Layer
key={compareLayerResolvedData.id}
id={`compare-${compareLayerResolvedData.id}`}
dataset={
compareDataLayer as unknown as TimelineDatasetSuccess
}
key={compareDataLayer.data.id}
id={`compare-${compareDataLayer.data.id}`}
dataset={compareDataLayer}
selectedDay={selectedCompareDatetime}
/>
)}
Expand Down
2 changes: 1 addition & 1 deletion app/scripts/components/common/map/map-component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function MapComponent({
projection,
mapRef,
onMapLoad,
interactive
interactive = true
}: {
controls: ReactElement[];
isCompared?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ export function RasterTimeseries(props: RasterTimeseriesProps) {
'color: red;',
id
);
// Temporarily turning on log for debugging
/* eslint-disable-next-line no-console */
console.log(error);
return;
}
};
Expand Down
Loading