Skip to content

Commit

Permalink
Merge pull request #320 from Vizzuality/SKY30-481-fe-expanding-and-co…
Browse files Browse the repository at this point in the history
…llapsing-the-layers-panel-reset-which-layers-are-active

Only reset to the default layers when the tab changes
  • Loading branch information
clementprdhomme authored Oct 11, 2024
2 parents 0360014 + 81e1127 commit 19dcd5e
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions frontend/src/containers/map/sidebar/layers-panel/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ComponentProps, useCallback, useEffect, useMemo } from 'react';

import { useLocale, useTranslations } from 'next-intl';
import { usePreviousImmediate } from 'rooks';

import TooltipButton from '@/components/tooltip-button';
import { Label } from '@/components/ui/label';
Expand All @@ -20,6 +21,7 @@ const LayersPanel: FCWithMessages = (): JSX.Element => {
const [, setMapLayers] = useSyncMapLayers();
const [{ labels }, setMapSettings] = useSyncMapSettings();
const [{ tab }] = useSyncMapContentSettings();
const previousTab = usePreviousImmediate(tab);

const {
data: datasetsData,
Expand Down Expand Up @@ -122,23 +124,25 @@ const LayersPanel: FCWithMessages = (): JSX.Element => {

// Set map layers to the corresponding defaults when the user switches tabs
useEffect(() => {
let mapLayers = [];
switch (tab) {
case 'summary':
mapLayers = ['terrestrial', 'marine', 'basemap']?.reduce(
(ids, dataset) => [...ids, ...defaultLayersIds[dataset]],
[]
);
break;
case 'terrestrial':
mapLayers = defaultLayersIds.terrestrial;
break;
case 'marine':
mapLayers = defaultLayersIds.marine;
break;
if (tab !== previousTab && !!previousTab) {
let mapLayers = [];
switch (tab) {
case 'summary':
mapLayers = ['terrestrial', 'marine', 'basemap']?.reduce(
(ids, dataset) => [...ids, ...defaultLayersIds[dataset]],
[]
);
break;
case 'terrestrial':
mapLayers = defaultLayersIds.terrestrial;
break;
case 'marine':
mapLayers = defaultLayersIds.marine;
break;
}
setMapLayers(mapLayers);
}
setMapLayers(mapLayers);
}, [defaultLayersIds, setMapLayers, tab]);
}, [defaultLayersIds, setMapLayers, tab, previousTab]);

const handleLabelsChange = useCallback(
(active: Parameters<ComponentProps<typeof Switch>['onCheckedChange']>[0]) => {
Expand Down

0 comments on commit 19dcd5e

Please sign in to comment.