Skip to content

Commit

Permalink
Only reset to default layers when the tab changes
Browse files Browse the repository at this point in the history
  • Loading branch information
clementprdhomme committed Oct 11, 2024
1 parent 0360014 commit 81e1127
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 81e1127

Please sign in to comment.