Skip to content

Commit f58b9e1

Browse files
Abstract Away Faux Modules from MapBlock Component and make renderable for NextJs Instance Prototype (#1046)
**Related Ticket:** #1002 **These changes are RELATED TO THIS PR:** NASA-IMPACT/next-veda-ui#1 ### Description of Changes This makes the MapBlock component renderable by the NextJs instance by abstracting away the veda faux modules dependencies. This also exports the themeProvider and reactQueryProvider to be used by the nextJs instance. I've tried to keep these changes quite isolated to the problem we are trying to solve so its not too big of a PR. ### Notes & Questions About Changes * Should NOT BE BREAKING CHANGES. Should be able to merge without care of order of NextJs PR * See my comments in the code ### Validation / Testing * Regular build should work * Library build should work * The stories and mapblocks in the stories should render as usual in the deploy preview
2 parents 501f8be + 72193b0 commit f58b9e1

35 files changed

+225
-89
lines changed

app/scripts/components/analysis/results/timeseries-data.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import axios, { AxiosRequestConfig } from 'axios';
22
import { QueryClient } from '@tanstack/react-query';
33
import { FeatureCollection, Polygon } from 'geojson';
4-
import { DatasetLayer } from 'veda';
5-
64
import { MAX_QUERY_NUM } from '../constants';
75
import { getFilterPayload, combineFeatureCollection } from '../utils';
8-
import EventEmitter from './mini-events';
96
import { ConcurrencyManager, ConcurrencyManagerInstance } from './concurrency';
7+
import EventEmitter from './mini-events';
8+
import { DatasetLayer } from '$types/veda';
109
import { TimeDensity } from '$context/layer-data';
1110

1211
export const TIMESERIES_DATA_BASE_ID = 'analysis';

app/scripts/components/common/blocks/block-map.tsx

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import React, { useMemo, useState, useEffect } from 'react';
22
import styled from 'styled-components';
3-
// @NOTE: This should be replaced by types/veda once the changes are consolidated
4-
import { ProjectionOptions } from 'veda';
53
import { MapboxOptions } from 'mapbox-gl';
64
import {
75
convertProjectionToMapbox,
@@ -35,9 +33,9 @@ import {
3533
DatasetStatus
3634
} from '$components/exploration/types.d.ts';
3735

38-
import { reconcileDatasets } from '$components/exploration/data-utils';
39-
import { datasetLayers } from '$components/exploration/data-utils';
36+
import { reconcileDatasets, getDatasetLayers } from '$components/exploration/data-utils-no-faux-module';
4037
import { useReconcileWithStacMetadata } from '$components/exploration/hooks/use-stac-metadata-datasets';
38+
import { ProjectionOptions, VedaDatum, DatasetData } from '$types/veda';
4139

4240
export const mapHeight = '32rem';
4341
const Carto = styled.div`
@@ -111,6 +109,7 @@ function validateBlockProps(props: MapBlockProps) {
111109
}
112110

113111
interface MapBlockProps {
112+
datasets: VedaDatum<DatasetData>;
114113
dateTime?: string;
115114
compareDateTime?: string;
116115
center?: [number, number];
@@ -128,7 +127,6 @@ interface MapBlockProps {
128127
const getDataLayer = (layerIndex: number, layers: VizDataset[] | undefined): (VizDatasetSuccess | null) => {
129128
if (!layers || layers.length <= layerIndex) return null;
130129
const layer = layers[layerIndex];
131-
132130
// @NOTE: What to do when data returns ERROR
133131
if (layer.status !== DatasetStatus.SUCCESS) return null;
134132
return {
@@ -144,6 +142,7 @@ function MapBlock(props: MapBlockProps) {
144142
const generatedId = useMemo(() => `map-${++mapInstanceId}`, []);
145143

146144
const {
145+
datasets,
147146
layerId,
148147
dateTime,
149148
compareDateTime,
@@ -162,6 +161,8 @@ function MapBlock(props: MapBlockProps) {
162161
throw new HintedError('Malformed Map Block', errors);
163162
}
164163

164+
const datasetLayers = getDatasetLayers(datasets);
165+
165166
const layersToFetch = useMemo(() => {
166167
const [baseMapStaticData] = reconcileDatasets([layerId], datasetLayers, []);
167168
let totalLayers = [baseMapStaticData];
@@ -262,7 +263,7 @@ function MapBlock(props: MapBlockProps) {
262263
const providedLabel = compareDataLayer.data.mapLabel as string;
263264
return providedLabel;
264265
}
265-
266+
266267
// Default to date comparison.
267268
return selectedDatetime && compareToDate
268269
? formatCompareDate(
@@ -280,7 +281,6 @@ function MapBlock(props: MapBlockProps) {
280281
baseTimeDensity,
281282
compareTimeDensity
282283
]);
283-
284284
const initialPosition = useMemo(
285285
() => (center ? { lng: center[0], lat: center[1], zoom } : undefined),
286286
[center, zoom]

app/scripts/components/common/blocks/lazy-components.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
} from '$components/common/blocks/scrollytelling';
1717

1818
import { LoadingSkeleton } from '$components/common/loading-skeleton';
19+
import { veda_faux_module_datasets } from '$data-layer/datasets';
1920

2021
export function LazyChart(props) {
2122
return (
@@ -48,7 +49,7 @@ export function LazyMap(props) {
4849
offset={100}
4950
once
5051
>
51-
<Map {...props} />
52+
<Map {...props} datasets={veda_faux_module_datasets} />
5253
</LazyLoad>
5354
);
5455
}

app/scripts/components/common/blocks/scrollytelling/chapter.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { ReactNode } from 'react';
22
import styled from 'styled-components';
33
import { themeVal, media, multiply } from '@devseed-ui/theme-provider';
4-
import { ProjectionOptions } from 'veda';
4+
import { ProjectionOptions } from '$types/veda';
55

66
import { variableGlsp } from '$styles/variable-utils';
77
import { ContentBlockProse } from '$styles/content-block';

app/scripts/components/common/map/controls/aoi/custom-aoi-control.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,6 @@ export default function CustomAoIControl({
328328

329329
// Start/stop the drawing.
330330
useEffect(() => {
331-
// @ts-expect-error Property '_drawControl' does not exist on type 'Map'.
332331
// Property was added to access draw control.
333332
const mbDraw = main?._drawControl;
334333
if (!mbDraw) return;

app/scripts/components/common/map/controls/hooks/use-preset-aoi.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Feature, Polygon } from 'geojson';
22
import { useCallback, useEffect, useState } from 'react';
33
import axios from 'axios';
4-
54
import { getAoiAppropriateFeatures } from './use-custom-aoi';
65

76
const presetFilePath = `${process.env.PUBLIC_URL ?? ''}/public/geo-data/states/`;

app/scripts/components/common/map/controls/map-options/projections.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { MbProjectionOptions, ProjectionOptions } from 'veda';
2-
31
import { validateLat, validateLon } from '../../utils';
42
import { ProjectionListItem } from './types';
3+
import { MbProjectionOptions, ProjectionOptions } from '$types/veda';
54

65
// The id is internal to the app.
76
// The mbId is the projection name to use with mapbox. This is needed because

app/scripts/components/common/map/controls/map-options/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { MbProjectionOptions, ProjectionOptions } from 'veda';
21
import { BasemapId, Option } from './basemap';
2+
import { MbProjectionOptions, ProjectionOptions } from '$types/veda';
33

44
export interface MapOptionsProps {
55
onProjectionChange: (projection: ProjectionOptions) => void;

app/scripts/components/common/map/hooks/use-map-style.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { useContext } from "react";
2-
import { StylesContext } from "../styles";
2+
import { StylesContext, StylesContextType } from "../styles";
33
import useCustomMarker from "./use-custom-marker";
44
import useMaps from "./use-maps";
55

6-
export function useStylesContext() {
6+
export function useStylesContext(): StylesContextType {
77
return useContext(StylesContext);
88
}
99

app/scripts/components/common/map/hooks/use-maps.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import { useContext } from 'react';
22
import { useMap } from 'react-map-gl';
3-
import { MapsContext } from '../maps';
3+
import { MapsContext, MapsContextType } from '../maps';
44
import { useStylesContext } from './use-map-style';
55

6-
export function useMapsContext() {
6+
export function useMapsContext(): MapsContextType {
77
return useContext(MapsContext);
88
}
99

10-
export default function useMaps() {
10+
export default function useMaps(): Record<string, any | undefined> {
1111
const { mainId, comparedId } = useMapsContext();
1212
const { isCompared } = useStylesContext();
1313
const maps = useMap();
14-
const main = maps[mainId];
14+
const main = maps?.[mainId];
1515
const compared = maps[comparedId];
1616
const current = isCompared ? compared : main;
1717

app/scripts/components/common/map/layer-legend.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React, { ReactNode, Fragment, useState, useCallback } from 'react';
22
import styled from 'styled-components';
3-
import { LayerLegendCategorical, LayerLegendGradient } from 'veda';
43
import { AccordionFold, AccordionManager } from '@devseed-ui/accordion';
54
import {
65
glsp,
@@ -12,7 +11,6 @@ import { CollecticonCircleInformation } from '@devseed-ui/collecticons';
1211
import { Toolbar, ToolbarIconButton } from '@devseed-ui/toolbar';
1312
import { followCursor } from 'tippy.js';
1413
import { scaleLinear } from 'd3';
15-
1614
import { Tip } from '../tip';
1715
import {
1816
formatAsScientificNotation,
@@ -26,6 +24,7 @@ import {
2624
WidgetItemHeadline,
2725
WidgetItemHGroup
2826
} from '$styles/panel';
27+
import { LayerLegendCategorical, LayerLegendGradient } from '$types/veda';
2928

3029
interface LayerLegendCommonProps {
3130
id: string;

app/scripts/components/common/map/map-component.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import React, { useCallback, ReactElement, useMemo, Ref } from 'react';
22
import ReactMapGlMap, { LngLatBoundsLike, MapRef } from 'react-map-gl';
33
import { debounce } from 'lodash';
4-
import { ProjectionOptions } from 'veda';
5-
import 'mapbox-gl/dist/mapbox-gl.css';
6-
import 'mapbox-gl-compare/dist/mapbox-gl-compare.css';
74
import useMapStyle from './hooks/use-map-style';
85
import { useMapsContext } from './hooks/use-maps';
96
import { convertProjectionToMapbox } from './controls/map-options/projections';
7+
import { ProjectionOptions } from '$types/veda';
8+
import 'mapbox-gl/dist/mapbox-gl.css';
9+
import 'mapbox-gl-compare/dist/mapbox-gl-compare.css';
1010

1111
const maxMapBounds: LngLatBoundsLike = [
1212
[-540, -90], // SW

app/scripts/components/common/map/maps.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
iconDataURI
1616
} from '@devseed-ui/collecticons';
1717
import { themeVal } from '@devseed-ui/theme-provider';
18-
import { ProjectionOptions } from 'veda';
1918
import useDimensions from 'react-cool-dimensions';
2019
import 'mapbox-gl/dist/mapbox-gl.css';
2120
import 'mapbox-gl-compare/dist/mapbox-gl-compare.css';
@@ -27,6 +26,7 @@ import MapComponent from './map-component';
2726
import useMaps, { useMapsContext } from './hooks/use-maps';
2827
import { aoiCustomCursorStyle } from './controls/aoi/custom-aoi-control';
2928
import { COMPARE_CONTAINER_NAME, CONTROLS_CONTAINER_NAME } from '.';
29+
import { ProjectionOptions } from '$types/veda';
3030

3131
const chevronRightURI = () =>
3232
iconDataURI(CollecticonChevronRightSmall, {
@@ -202,7 +202,7 @@ export default function MapsContextWrapper(props: MapsContextWrapperProps) {
202202
);
203203
}
204204

205-
interface MapsContextType {
205+
export interface MapsContextType {
206206
initialViewState: any;
207207
setInitialViewState: (viewState: any) => void;
208208
mainId: string;

app/scripts/components/common/map/style-generators/cmr-timeseries.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React from 'react';
2-
32
import { BaseGeneratorParams } from '../types';
43
import { ZarrPaintLayer } from './zarr-timeseries';
54
import { useCMR } from './hooks';

app/scripts/components/common/map/style-generators/raster-timeseries.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -358,15 +358,13 @@ export function RasterTimeseries(props: RasterTimeseriesProps) {
358358
);
359359

360360
const tilejsonUrl = `${mosaicUrl}?${tileParams}`;
361-
362361
try {
363362
const tilejsonData = await requestQuickCache<any>({
364363
url: tilejsonUrl,
365364
method: 'GET',
366365
payload: null,
367366
controller
368367
});
369-
370368
const tileServerUrl = tilejsonData.tiles[0];
371369

372370
const wmtsBaseUrl = mosaicUrl.replace(
@@ -417,6 +415,7 @@ export function RasterTimeseries(props: RasterTimeseriesProps) {
417415
context: STATUS_KEY.StacSearch
418416
});
419417
}
418+
420419
LOG &&
421420
/* eslint-disable-next-line no-console */
422421
console.log(

app/scripts/components/common/map/style-generators/zarr-timeseries.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React, { useEffect, useMemo } from 'react';
22
import qs from 'qs';
33
import { RasterSource, RasterLayer } from 'mapbox-gl';
4-
54
import useMapStyle from '../hooks/use-map-style';
65
import useGeneratorParams from '../hooks/use-generator-params';
76
import { BaseGeneratorParams } from '../types';

app/scripts/components/common/map/styles.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
LayerOrderPosition
1515
} from './types';
1616

17-
interface StylesContextType {
17+
export interface StylesContextType {
1818
updateStyle: (params: GeneratorStyleParams) => void;
1919
style?: Style;
2020
updateMetaData?: (params: unknown) => void;

app/scripts/components/common/map/utils.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import axios, { Method } from 'axios';
2-
import { format } from 'date-fns';
2+
import format from 'date-fns/format';
33
import { Map as MapboxMap } from 'mapbox-gl';
44
import { MapRef } from 'react-map-gl';
5-
import { endOfDay, startOfDay } from 'date-fns';
5+
import startOfDay from 'date-fns/startOfDay';
6+
import endOfDay from 'date-fns/endOfDay';
67
import { Feature, MultiPolygon, Polygon } from 'geojson';
78
import { BBox } from '@turf/helpers';
9+
import { StacFeature } from './types';
810
import {
911
DatasetDatumFn,
1012
DatasetDatumFnResolverBag,
1113
DatasetDatumReturnType
12-
} from 'veda';
13-
14-
import { StacFeature } from './types';
14+
} from '$types/veda';
1515
import { TimeDensity } from '$context/layer-data';
1616
import { userTzDate2utcString } from '$utils/date';
1717
import { validateRangeNum } from '$utils/utils';

app/scripts/components/common/mapbox/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
iconDataURI
2424
} from '@devseed-ui/collecticons';
2525
import { themeVal } from '@devseed-ui/theme-provider';
26-
import { DatasetDatumFnResolverBag, ProjectionOptions } from 'veda';
26+
import { DatasetDatumFnResolverBag, ProjectionOptions, datasets } from 'veda';
2727

2828
import { AoiChangeListenerOverload, AoiState } from '../aoi/types';
2929
import MapMessage from '../map/map-message';
@@ -176,7 +176,7 @@ function MapboxMapComponent(
176176
compareInstance: mapCompareRef.current
177177
}));
178178

179-
const { baseLayer, compareLayer } = useDatasetAsyncLayer(datasetId, layerId);
179+
const { baseLayer, compareLayer } = useDatasetAsyncLayer(datasets, datasetId, layerId);
180180

181181
const shouldRenderCompare = isMapLoaded && isComparing;
182182

app/scripts/components/common/mapbox/layers/cmr-timeseries.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React from 'react';
22
import { Map as MapboxMap } from 'mapbox-gl';
3-
43
import { ZarrPaintLayer } from './zarr-timeseries';
54
import { ActionStatus } from '$utils/status';
65

app/scripts/components/common/mapbox/layers/utils.ts

+13-12
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,6 @@ import {
99
eachYearOfInterval
1010
} from 'date-fns';
1111
import { endOfDay, startOfDay } from 'date-fns';
12-
import {
13-
datasets,
14-
DatasetLayer,
15-
DatasetLayerCompareInternal,
16-
DatasetLayerCompareSTAC,
17-
DatasetDatumFn,
18-
DatasetDatumFnResolverBag,
19-
DatasetDatumReturnType,
20-
DatasetLayerCompareNormalized,
21-
DatasetLayerType
22-
} from 'veda';
2312
import {
2413
MapLayerRasterTimeseries,
2514
MapLayerRasterTimeseriesProps,
@@ -37,7 +26,18 @@ import {
3726
MapLayerCMRTimeseries,
3827
MapLayerCMRTimeseriesProps
3928
} from './cmr-timeseries';
40-
29+
import {
30+
DatasetLayer,
31+
DatasetLayerCompareInternal,
32+
DatasetLayerCompareSTAC,
33+
DatasetDatumFn,
34+
DatasetDatumFnResolverBag,
35+
DatasetDatumReturnType,
36+
DatasetLayerCompareNormalized,
37+
DatasetLayerType,
38+
VedaData,
39+
DatasetData,
40+
} from '$types/veda';
4141
import { userTzDate2utcString, utcString2userTzDate } from '$utils/date';
4242
import { AsyncDatasetLayer } from '$context/layer-data';
4343
import { S_FAILED, S_IDLE, S_LOADING, S_SUCCEEDED } from '$utils/status';
@@ -70,6 +70,7 @@ export const getLayerComponent = (
7070
* @returns object
7171
*/
7272
export const getCompareLayerData = (
73+
datasets: VedaData<DatasetData>,
7374
layerData: DatasetLayer | null
7475
): DatasetLayerCompareNormalized | null => {
7576
if (!layerData?.compare) return null;

app/scripts/components/common/mapbox/layers/vector-timeseries.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
import { Feature } from 'geojson';
1212
import { endOfDay, startOfDay } from 'date-fns';
1313
import centroid from '@turf/centroid';
14-
1514
import { requestQuickCache, useFitBbox, useLayerInteraction } from './utils';
1615
import { useMapStyle } from './styles';
1716
import { useCustomMarker } from './custom-marker';

app/scripts/components/common/mapbox/layers/zarr-timeseries.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React, { useEffect, useMemo } from 'react';
22
import qs from 'qs';
33
import { RasterSource, RasterLayer } from 'mapbox-gl';
4-
54
import { useMapStyle } from './styles';
65

76
import { ActionStatus } from '$utils/status';

0 commit comments

Comments
 (0)