From 4ab3c1acadf5c2b917d18edaf4c43b86a8d6b328 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Gonz=C3=A1lez=20Mu=C3=B1oz?= Date: Wed, 12 Jun 2024 12:51:37 +0200 Subject: [PATCH] enables cog preview when available --- .../categories/category-layer/component.tsx | 2 +- .../preview-map/component.tsx | 82 ++++++++++++------- 2 files changed, 54 insertions(+), 30 deletions(-) diff --git a/client/src/containers/analysis-visualization/analysis-contextual-layer/categories/category-layer/component.tsx b/client/src/containers/analysis-visualization/analysis-contextual-layer/categories/category-layer/component.tsx index dbcb537f2..cff1cc118 100644 --- a/client/src/containers/analysis-visualization/analysis-contextual-layer/categories/category-layer/component.tsx +++ b/client/src/containers/analysis-visualization/analysis-contextual-layer/categories/category-layer/component.tsx @@ -45,7 +45,7 @@ const CategoryLayer = ({ }} /> {previewStatus === 'loading' && isPreviewActive ? ( - + ) : ( ; + onStatusChange?: Dispatch; } const INITIAL_PREVIEW_SETTINGS = { @@ -37,39 +39,61 @@ const PreviewMap = ({ selectedLayerId, selectedMaterialId, onStatusChange }: Pre }, }); - const { data, isFetching, status } = useH3Data({ + const { data: contextualLayers } = useContextualLayers(); + + const selectedLayer = useMemo(() => { + return contextualLayers + ?.flatMap((category) => category.layers) + .find((layer) => layer.id === selectedLayerId); + }, [contextualLayers, selectedLayerId]); + + const { + data: h3data, + isFetching, + status, + fetchStatus, + } = useH3Data({ id: selectedLayerId, params: { materialId: selectedMaterialId, year: materialYear, }, options: { - enabled: !!selectedLayerId && (selectedLayerId !== 'material' || !!materialYear), + enabled: + !!selectedLayerId && + (selectedLayerId !== 'material' || !!materialYear) && + !selectedLayer?.tilerUrl?.length, select: (response) => response.data, - staleTime: 10000, - // having placeholder data makes the status always be success placeholderData: undefined, }, }); useEffect(() => { + if (selectedLayer?.tilerUrl?.length) { + return onStatusChange?.('success'); + } onStatusChange?.(status); - }, [onStatusChange, status]); + }, [onStatusChange, status, fetchStatus, selectedLayer]); const PreviewLayer = useCallback(() => { - if (!data?.length) return null; + if (!selectedLayerId) return null; - return ( - > - id={PREVIEW_LAYER_ID} - type={H3HexagonLayer} - data={data} - getHexagon={(d) => d.h} - getFillColor={(d) => d.c} - getLineColor={(d) => d.c} - /> - ); - }, [data]); + if (selectedLayer?.tilerUrl?.length) { + return ; + } + if (h3data?.length) { + return ( + > + id={PREVIEW_LAYER_ID} + type={H3HexagonLayer} + data={h3data} + getHexagon={(d) => d.h} + getFillColor={(d) => d.c} + getLineColor={(d) => d.c} + /> + ); + } + }, [selectedLayerId, h3data, selectedLayer]); const layers = useMemo(() => [{ id: PREVIEW_LAYER_ID, layer: PreviewLayer }], [PreviewLayer]); @@ -79,7 +103,7 @@ const PreviewMap = ({ selectedLayerId, selectedMaterialId, onStatusChange }: Pre {() => } -
+