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

Abstract Away Faux Modules from MapBlock Component and make renderable for NextJs Instance Prototype #1046

Merged
merged 9 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions app/scripts/components/analysis/results/timeseries-data.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import axios, { AxiosRequestConfig } from 'axios';
import { QueryClient } from '@tanstack/react-query';
import { FeatureCollection, Polygon } from 'geojson';
import { DatasetLayer } from 'veda';

import { MAX_QUERY_NUM } from '../constants';
import { getFilterPayload, combineFeatureCollection } from '../utils';
import EventEmitter from './mini-events';
import { ConcurrencyManager, ConcurrencyManagerInstance } from './concurrency';
import EventEmitter from './mini-events';
import { DatasetLayer } from '$types/veda';
import { TimeDensity } from '$context/layer-data';

export const TIMESERIES_DATA_BASE_ID = 'analysis';
Expand Down
14 changes: 7 additions & 7 deletions app/scripts/components/common/blocks/block-map.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import React, { useMemo, useState, useEffect } from 'react';
import styled from 'styled-components';
// @NOTE: This should be replaced by types/veda once the changes are consolidated
import { ProjectionOptions } from 'veda';
import { MapboxOptions } from 'mapbox-gl';
import {
convertProjectionToMapbox,
Expand Down Expand Up @@ -35,9 +33,9 @@ import {
DatasetStatus
} from '$components/exploration/types.d.ts';

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

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

interface MapBlockProps {
datasets: VedaDatum<DatasetData>;
dateTime?: string;
compareDateTime?: string;
center?: [number, number];
Expand All @@ -128,7 +127,6 @@ interface MapBlockProps {
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 {
Expand All @@ -144,6 +142,7 @@ function MapBlock(props: MapBlockProps) {
const generatedId = useMemo(() => `map-${++mapInstanceId}`, []);

const {
datasets,
layerId,
dateTime,
compareDateTime,
Expand All @@ -162,6 +161,8 @@ function MapBlock(props: MapBlockProps) {
throw new HintedError('Malformed Map Block', errors);
}

const datasetLayers = getDatasetLayers(datasets);

const layersToFetch = useMemo(() => {
const [baseMapStaticData] = reconcileDatasets([layerId], datasetLayers, []);
let totalLayers = [baseMapStaticData];
Expand Down Expand Up @@ -262,7 +263,7 @@ function MapBlock(props: MapBlockProps) {
const providedLabel = compareDataLayer.data.mapLabel as string;
return providedLabel;
}

// Default to date comparison.
return selectedDatetime && compareToDate
? formatCompareDate(
Expand All @@ -280,7 +281,6 @@ function MapBlock(props: MapBlockProps) {
baseTimeDensity,
compareTimeDensity
]);

const initialPosition = useMemo(
() => (center ? { lng: center[0], lat: center[1], zoom } : undefined),
[center, zoom]
Expand Down
3 changes: 2 additions & 1 deletion app/scripts/components/common/blocks/lazy-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from '$components/common/blocks/scrollytelling';

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

export function LazyChart(props) {
return (
Expand Down Expand Up @@ -48,7 +49,7 @@ export function LazyMap(props) {
offset={100}
once
>
<Map {...props} />
<Map {...props} datasets={veda_faux_module_datasets} />
</LazyLoad>
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { ReactNode } from 'react';
import styled from 'styled-components';
import { themeVal, media, multiply } from '@devseed-ui/theme-provider';
import { ProjectionOptions } from 'veda';
import { ProjectionOptions } from '$types/veda';

import { variableGlsp } from '$styles/variable-utils';
import { ContentBlockProse } from '$styles/content-block';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,6 @@ export default function CustomAoIControl({

// Start/stop the drawing.
useEffect(() => {
// @ts-expect-error Property '_drawControl' does not exist on type 'Map'.
// Property was added to access draw control.
const mbDraw = main?._drawControl;
if (!mbDraw) return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Feature, Polygon } from 'geojson';
import { useCallback, useEffect, useState } from 'react';
import axios from 'axios';

import { getAoiAppropriateFeatures } from './use-custom-aoi';

const presetFilePath = `${process.env.PUBLIC_URL ?? ''}/public/geo-data/states/`;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { MbProjectionOptions, ProjectionOptions } from 'veda';

import { validateLat, validateLon } from '../../utils';
import { ProjectionListItem } from './types';
import { MbProjectionOptions, ProjectionOptions } from '$types/veda';

// The id is internal to the app.
// The mbId is the projection name to use with mapbox. This is needed because
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MbProjectionOptions, ProjectionOptions } from 'veda';
import { BasemapId, Option } from './basemap';
import { MbProjectionOptions, ProjectionOptions } from '$types/veda';

export interface MapOptionsProps {
onProjectionChange: (projection: ProjectionOptions) => void;
Expand Down
4 changes: 2 additions & 2 deletions app/scripts/components/common/map/hooks/use-map-style.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useContext } from "react";
import { StylesContext } from "../styles";
import { StylesContext, StylesContextType } from "../styles";
import useCustomMarker from "./use-custom-marker";
import useMaps from "./use-maps";

export function useStylesContext() {
export function useStylesContext(): StylesContextType {
return useContext(StylesContext);
}

Expand Down
8 changes: 4 additions & 4 deletions app/scripts/components/common/map/hooks/use-maps.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { useContext } from 'react';
import { useMap } from 'react-map-gl';
import { MapsContext } from '../maps';
import { MapsContext, MapsContextType } from '../maps';
import { useStylesContext } from './use-map-style';

export function useMapsContext() {
export function useMapsContext(): MapsContextType {
return useContext(MapsContext);
}

export default function useMaps() {
export default function useMaps(): Record<string, any | undefined> {
const { mainId, comparedId } = useMapsContext();
const { isCompared } = useStylesContext();
const maps = useMap();
const main = maps[mainId];
const main = maps?.[mainId];
const compared = maps[comparedId];
const current = isCompared ? compared : main;

Expand Down
3 changes: 1 addition & 2 deletions app/scripts/components/common/map/layer-legend.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { ReactNode, Fragment, useState, useCallback } from 'react';
import styled from 'styled-components';
import { LayerLegendCategorical, LayerLegendGradient } from 'veda';
import { AccordionFold, AccordionManager } from '@devseed-ui/accordion';
import {
glsp,
Expand All @@ -12,7 +11,6 @@ import { CollecticonCircleInformation } from '@devseed-ui/collecticons';
import { Toolbar, ToolbarIconButton } from '@devseed-ui/toolbar';
import { followCursor } from 'tippy.js';
import { scaleLinear } from 'd3';

import { Tip } from '../tip';
import {
formatAsScientificNotation,
Expand All @@ -26,6 +24,7 @@ import {
WidgetItemHeadline,
WidgetItemHGroup
} from '$styles/panel';
import { LayerLegendCategorical, LayerLegendGradient } from '$types/veda';

interface LayerLegendCommonProps {
id: string;
Expand Down
6 changes: 3 additions & 3 deletions app/scripts/components/common/map/map-component.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React, { useCallback, ReactElement, useMemo, Ref } from 'react';
import ReactMapGlMap, { LngLatBoundsLike, MapRef } from 'react-map-gl';
import { debounce } from 'lodash';
import { ProjectionOptions } from 'veda';
import 'mapbox-gl/dist/mapbox-gl.css';
import 'mapbox-gl-compare/dist/mapbox-gl-compare.css';
import useMapStyle from './hooks/use-map-style';
import { useMapsContext } from './hooks/use-maps';
import { convertProjectionToMapbox } from './controls/map-options/projections';
import { ProjectionOptions } from '$types/veda';
import 'mapbox-gl/dist/mapbox-gl.css';
import 'mapbox-gl-compare/dist/mapbox-gl-compare.css';

const maxMapBounds: LngLatBoundsLike = [
[-540, -90], // SW
Expand Down
4 changes: 2 additions & 2 deletions app/scripts/components/common/map/maps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
iconDataURI
} from '@devseed-ui/collecticons';
import { themeVal } from '@devseed-ui/theme-provider';
import { ProjectionOptions } from 'veda';
import useDimensions from 'react-cool-dimensions';
import 'mapbox-gl/dist/mapbox-gl.css';
import 'mapbox-gl-compare/dist/mapbox-gl-compare.css';
Expand All @@ -27,6 +26,7 @@ import MapComponent from './map-component';
import useMaps, { useMapsContext } from './hooks/use-maps';
import { aoiCustomCursorStyle } from './controls/aoi/custom-aoi-control';
import { COMPARE_CONTAINER_NAME, CONTROLS_CONTAINER_NAME } from '.';
import { ProjectionOptions } from '$types/veda';

const chevronRightURI = () =>
iconDataURI(CollecticonChevronRightSmall, {
Expand Down Expand Up @@ -202,7 +202,7 @@ export default function MapsContextWrapper(props: MapsContextWrapperProps) {
);
}

interface MapsContextType {
export interface MapsContextType {
initialViewState: any;
setInitialViewState: (viewState: any) => void;
mainId: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react';

import { BaseGeneratorParams } from '../types';
import { ZarrPaintLayer } from './zarr-timeseries';
import { useCMR } from './hooks';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,15 +358,13 @@ export function RasterTimeseries(props: RasterTimeseriesProps) {
);

const tilejsonUrl = `${mosaicUrl}?${tileParams}`;

try {
const tilejsonData = await requestQuickCache<any>({
url: tilejsonUrl,
method: 'GET',
payload: null,
controller
});

const tileServerUrl = tilejsonData.tiles[0];

const wmtsBaseUrl = mosaicUrl.replace(
Expand Down Expand Up @@ -417,6 +415,7 @@ export function RasterTimeseries(props: RasterTimeseriesProps) {
context: STATUS_KEY.StacSearch
});
}

LOG &&
/* eslint-disable-next-line no-console */
console.log(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { useEffect, useMemo } from 'react';
import qs from 'qs';
import { RasterSource, RasterLayer } from 'mapbox-gl';

import useMapStyle from '../hooks/use-map-style';
import useGeneratorParams from '../hooks/use-generator-params';
import { BaseGeneratorParams } from '../types';
Expand Down
2 changes: 1 addition & 1 deletion app/scripts/components/common/map/styles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
LayerOrderPosition
} from './types';

interface StylesContextType {
export interface StylesContextType {
updateStyle: (params: GeneratorStyleParams) => void;
style?: Style;
updateMetaData?: (params: unknown) => void;
Expand Down
10 changes: 5 additions & 5 deletions app/scripts/components/common/map/utils.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import axios, { Method } from 'axios';
import { format } from 'date-fns';
import format from 'date-fns/format';
import { Map as MapboxMap } from 'mapbox-gl';
import { MapRef } from 'react-map-gl';
import { endOfDay, startOfDay } from 'date-fns';
import startOfDay from 'date-fns/startOfDay';
import endOfDay from 'date-fns/endOfDay';
Copy link
Collaborator Author

@sandrahoang686 sandrahoang686 Jul 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parcel build for library was unable to find these date-fns if it wasn't imported like this... So had to switch over to doing this. Seems to be an open issue with Parcel
parcel-bundler/parcel#9676

import { Feature, MultiPolygon, Polygon } from 'geojson';
import { BBox } from '@turf/helpers';
import { StacFeature } from './types';
import {
DatasetDatumFn,
DatasetDatumFnResolverBag,
DatasetDatumReturnType
} from 'veda';

import { StacFeature } from './types';
} from '$types/veda';
import { TimeDensity } from '$context/layer-data';
import { userTzDate2utcString } from '$utils/date';
import { validateRangeNum } from '$utils/utils';
Expand Down
4 changes: 2 additions & 2 deletions app/scripts/components/common/mapbox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
iconDataURI
} from '@devseed-ui/collecticons';
import { themeVal } from '@devseed-ui/theme-provider';
import { DatasetDatumFnResolverBag, ProjectionOptions } from 'veda';
import { DatasetDatumFnResolverBag, ProjectionOptions, datasets } from 'veda';

import { AoiChangeListenerOverload, AoiState } from '../aoi/types';
import MapMessage from '../map/map-message';
Expand Down Expand Up @@ -176,7 +176,7 @@ function MapboxMapComponent(
compareInstance: mapCompareRef.current
}));

const { baseLayer, compareLayer } = useDatasetAsyncLayer(datasetId, layerId);
const { baseLayer, compareLayer } = useDatasetAsyncLayer(datasets, datasetId, layerId);

const shouldRenderCompare = isMapLoaded && isComparing;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react';
import { Map as MapboxMap } from 'mapbox-gl';

import { ZarrPaintLayer } from './zarr-timeseries';
import { ActionStatus } from '$utils/status';

Expand Down
25 changes: 13 additions & 12 deletions app/scripts/components/common/mapbox/layers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,6 @@ import {
eachYearOfInterval
} from 'date-fns';
import { endOfDay, startOfDay } from 'date-fns';
import {
datasets,
DatasetLayer,
DatasetLayerCompareInternal,
DatasetLayerCompareSTAC,
DatasetDatumFn,
DatasetDatumFnResolverBag,
DatasetDatumReturnType,
DatasetLayerCompareNormalized,
DatasetLayerType
} from 'veda';
import {
MapLayerRasterTimeseries,
MapLayerRasterTimeseriesProps,
Expand All @@ -37,7 +26,18 @@ import {
MapLayerCMRTimeseries,
MapLayerCMRTimeseriesProps
} from './cmr-timeseries';

import {
DatasetLayer,
DatasetLayerCompareInternal,
DatasetLayerCompareSTAC,
DatasetDatumFn,
DatasetDatumFnResolverBag,
DatasetDatumReturnType,
DatasetLayerCompareNormalized,
DatasetLayerType,
VedaData,
DatasetData,
} from '$types/veda';
import { userTzDate2utcString, utcString2userTzDate } from '$utils/date';
import { AsyncDatasetLayer } from '$context/layer-data';
import { S_FAILED, S_IDLE, S_LOADING, S_SUCCEEDED } from '$utils/status';
Expand Down Expand Up @@ -70,6 +70,7 @@ export const getLayerComponent = (
* @returns object
*/
export const getCompareLayerData = (
datasets: VedaData<DatasetData>,
layerData: DatasetLayer | null
): DatasetLayerCompareNormalized | null => {
if (!layerData?.compare) return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
import { Feature } from 'geojson';
import { endOfDay, startOfDay } from 'date-fns';
import centroid from '@turf/centroid';

import { requestQuickCache, useFitBbox, useLayerInteraction } from './utils';
import { useMapStyle } from './styles';
import { useCustomMarker } from './custom-marker';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { useEffect, useMemo } from 'react';
import qs from 'qs';
import { RasterSource, RasterLayer } from 'mapbox-gl';

import { useMapStyle } from './styles';

import { ActionStatus } from '$utils/status';
Expand Down
Loading
Loading