From 53b6be2237f647a1a5864da9646af55d069af4c5 Mon Sep 17 00:00:00 2001 From: Vitor George Date: Wed, 18 Dec 2024 11:42:26 -0300 Subject: [PATCH 1/4] refactor(config): evolve EnvConfigProvider into VedaUIProvider - Transform environment config into comprehensive Veda UI configuration - Add framework-agnostic navigation handling - Extract environment and navigation into separate type-safe interfaces - Add useVedaUI hook with context validation - Improve TypeScript types and error handling --- .../components/common/blocks/block-map.tsx | 6 +- .../common/blocks/scrollytelling/index.tsx | 5 +- .../components/common/map/map-component.tsx | 12 +--- .../common/map/style-generators/basemap.tsx | 6 +- .../exploration/components/map/index.tsx | 7 +- .../exploration/components/map/layer.tsx | 7 +- .../hooks/use-analysis-data-request.ts | 7 +- app/scripts/context/env-config.tsx | 27 ------- app/scripts/context/veda-ui-provider.tsx | 72 +++++++++++++++++++ app/scripts/index.ts | 4 +- app/scripts/main.tsx | 6 +- 11 files changed, 97 insertions(+), 62 deletions(-) delete mode 100644 app/scripts/context/env-config.tsx create mode 100644 app/scripts/context/veda-ui-provider.tsx diff --git a/app/scripts/components/common/blocks/block-map.tsx b/app/scripts/components/common/blocks/block-map.tsx index 35f8db89a..1b6d7a175 100644 --- a/app/scripts/components/common/blocks/block-map.tsx +++ b/app/scripts/components/common/blocks/block-map.tsx @@ -1,4 +1,4 @@ -import React, { useMemo, useState, useEffect, useContext } from 'react'; +import React, { useMemo, useState, useEffect } from 'react'; import styled from 'styled-components'; import { MapboxOptions } from 'mapbox-gl'; import * as dateFns from 'date-fns'; @@ -37,7 +37,7 @@ import { } 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'; -import { EnvConfigContext } from '$context/env-config'; +import { useVedaUI } from '$context/veda-ui-provider'; export const mapHeight = '32rem'; const Carto = styled.div` @@ -185,7 +185,7 @@ function MapBlock(props: MapBlockProps) { const [layers, setLayers] = useState(layersToFetch); - const { envApiStacEndpoint } = useContext(EnvConfigContext); + const { envApiStacEndpoint } = useVedaUI(); useReconcileWithStacMetadata(layers, setLayers, envApiStacEndpoint); diff --git a/app/scripts/components/common/blocks/scrollytelling/index.tsx b/app/scripts/components/common/blocks/scrollytelling/index.tsx index 111a2d13c..b36b7bda8 100644 --- a/app/scripts/components/common/blocks/scrollytelling/index.tsx +++ b/app/scripts/components/common/blocks/scrollytelling/index.tsx @@ -2,7 +2,6 @@ import React, { Children, ReactElement, useCallback, - useContext, useEffect, useMemo, useRef, @@ -52,7 +51,7 @@ import { formatSingleDate, reconcileVizDataset } from '$components/common/map/utils'; -import { EnvConfigContext } from '$context/env-config'; +import { useVedaUI } from '$context/veda-ui-provider'; type ResolvedScrollyMapLayer = { vizDataset: VizDatasetSuccess; @@ -260,7 +259,7 @@ const MAP_OPTIONS = { function Scrollytelling(props) { const { children } = props; - const { envApiStacEndpoint } = useContext(EnvConfigContext); + const { envApiStacEndpoint } = useVedaUI(); const { isHeaderHidden, headerHeight, wrapperHeight } = useSlidingStickyHeaderProps(); diff --git a/app/scripts/components/common/map/map-component.tsx b/app/scripts/components/common/map/map-component.tsx index a421e34de..c7fad3e25 100644 --- a/app/scripts/components/common/map/map-component.tsx +++ b/app/scripts/components/common/map/map-component.tsx @@ -1,10 +1,4 @@ -import React, { - useCallback, - ReactElement, - useMemo, - Ref, - useContext -} from 'react'; +import React, { useCallback, ReactElement, useMemo, Ref } from 'react'; import ReactMapGlMap, { LngLatBoundsLike, MapRef } from 'react-map-gl'; import { debounce } from 'lodash'; import useMapStyle from './hooks/use-map-style'; @@ -13,7 +7,7 @@ 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'; -import { EnvConfigContext } from '$context/env-config'; +import { useVedaUI } from '$context/veda-ui-provider'; const maxMapBounds: LngLatBoundsLike = [ [-540, -90], // SW @@ -35,7 +29,7 @@ export default function MapComponent({ onMapLoad?: () => void; interactive?: boolean; }) { - const { envMapboxToken } = useContext(EnvConfigContext); + const { envMapboxToken } = useVedaUI(); const { initialViewState, setInitialViewState, mainId, comparedId } = useMapsContext(); diff --git a/app/scripts/components/common/map/style-generators/basemap.tsx b/app/scripts/components/common/map/style-generators/basemap.tsx index c3805f996..7eafc6398 100644 --- a/app/scripts/components/common/map/style-generators/basemap.tsx +++ b/app/scripts/components/common/map/style-generators/basemap.tsx @@ -1,6 +1,6 @@ import { useQuery } from '@tanstack/react-query'; import { AnySourceImpl, Layer, Style } from 'mapbox-gl'; -import { useContext, useEffect, useMemo, useState } from 'react'; +import { useEffect, useMemo, useState } from 'react'; import { BasemapId, getStyleUrl, @@ -9,7 +9,7 @@ import { } from '../controls/map-options/basemap'; import { ExtendedLayer } from '../types'; import useMapStyle from '../hooks/use-map-style'; -import { EnvConfigContext } from '$context/env-config'; +import { useVedaUI } from '$context/veda-ui-provider'; interface BasemapProps { basemapStyleId?: BasemapId; @@ -33,7 +33,7 @@ export function Basemap({ labelsOption = true, boundariesOption = true }: BasemapProps) { - const { envMapboxToken } = useContext(EnvConfigContext); + const { envMapboxToken } = useVedaUI(); const { updateStyle } = useMapStyle(); const [baseStyle, setBaseStyle] = useState