Skip to content

Commit f36ccf7

Browse files
committed
feat: optimize TimeTravel mounting and display logic
- mount TimeTravel only when selectedTab = timeTravel for the first time - unmount TimeTravel only while moving off the single project page - prevent unnecessary reloads of tiles in comparison maps
1 parent 5206872 commit f36ccf7

File tree

5 files changed

+46
-13
lines changed

5 files changed

+46
-13
lines changed

src/features/projectsV2/ProjectsMap/ProjectMapTabs/ProjectMapTabs.module.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
left: 10px;
1414
top: 10px;
1515
margin: 0 auto;
16-
z-index: 10;
16+
z-index: 40;
1717
color: $dark;
1818
}
1919
.singleTabOption {

src/features/projectsV2/ProjectsMap/ProjectSiteDropDown/SiteDropdown.module.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ $layoutPaddingSide: 20px;
1414
cursor: pointer;
1515
gap: 18px;
1616
position: absolute;
17-
z-index: 2;
17+
z-index: 40;
1818
top: calc($layoutPaddingTop + 10px);
1919
right: calc($layoutPaddingSide + 10px);
2020
}
@@ -61,7 +61,7 @@ $layoutPaddingSide: 20px;
6161
width: 350px;
6262
font-size: $fontXSmall;
6363
position: absolute;
64-
z-index: 4;
64+
z-index: 50;
6565
top: 84px;
6666
right: 30px;
6767
max-height: 58vh;

src/features/projectsV2/ProjectsMap/TimeTravel/TimeTravel.module.scss

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
position: absolute;
55
inset: 0;
66
background: rgba(0, 0, 0, 0.5);
7+
z-index: 10;
78
:global(.maplibregl-compare) {
9+
z-index: 30;
810
:global(.compare-swiper-vertical) {
911
background-color: $primaryColor;
1012
}
@@ -14,21 +16,21 @@
1416
.comparisonMap {
1517
position: absolute;
1618
inset: 0;
17-
z-index: 1;
19+
z-index: 20;
1820
}
1921

2022
.beforeDropdown {
2123
position: absolute;
2224
top: 78px;
2325
left: 10px;
24-
z-index: 2;
26+
z-index: 40;
2527
}
2628

2729
.afterDropdown {
2830
position: absolute;
2931
top: 78px;
3032
right: 10px;
31-
z-index: 2;
33+
z-index: 40;
3234
}
3335

3436
.loadingOverlay {
@@ -39,5 +41,18 @@
3941
justify-content: center;
4042
background: rgba(0, 0, 0, 0.5);
4143
color: white;
42-
z-index: 2;
44+
z-index: 50;
45+
}
46+
47+
.hidden {
48+
opacity: 0;
49+
visibility: hidden;
50+
pointer-events: none;
51+
}
52+
53+
.visible {
54+
opacity: 1;
55+
visibility: visible;
56+
pointer-events: auto;
57+
transition: opacity 0.3s ease;
4358
}

src/features/projectsV2/ProjectsMap/TimeTravel/index.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,13 @@ type MapErrorCode = (typeof MAP_ERROR_CODES)[keyof typeof MAP_ERROR_CODES];
4444

4545
interface Props {
4646
sitesGeoJson: FeatureCollection<Polygon | MultiPolygon, ProjectSite>;
47+
isVisible: boolean;
4748
}
4849

49-
export default function TimeTravel({ sitesGeoJson }: Props): ReactElement {
50+
export default function TimeTravel({
51+
sitesGeoJson,
52+
isVisible,
53+
}: Props): ReactElement {
5054
const { viewState: mainMapViewState } = useProjectsMap();
5155
const { setErrors } = useContext(ErrorHandlingContext);
5256

@@ -448,7 +452,7 @@ export default function TimeTravel({ sitesGeoJson }: Props): ReactElement {
448452
};
449453

450454
return (
451-
<>
455+
<div className={`${isVisible ? styles.visible : styles.hidden}`}>
452456
<TimeTravelDropdown
453457
defaultYear={selectedYearBefore}
454458
defaultSource={selectedSourceBefore}
@@ -486,6 +490,6 @@ export default function TimeTravel({ sitesGeoJson }: Props): ReactElement {
486490
className={styles.comparisonMap}
487491
></div>
488492
</div>
489-
</>
493+
</div>
490494
);
491495
}

src/features/projectsV2/ProjectsMap/index.tsx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ function ProjectsMap(props: ProjectsMapProps) {
6363
selectedSamplePlantLocation,
6464
} = useProjects();
6565
const [selectedTab, setSelectedTab] = useState<SelectedTab | null>(null);
66+
const [wasTimeTravelMounted, setWasTimeTravelMounted] = useState(false);
6667

6768
const sitesGeoJson = useMemo(() => {
6869
return {
@@ -77,9 +78,16 @@ function ProjectsMap(props: ProjectsMapProps) {
7778
setSelectedTab('field');
7879
} else {
7980
setSelectedTab(null);
81+
setWasTimeTravelMounted(false);
8082
}
8183
}, [props.page]);
8284

85+
useEffect(() => {
86+
if (selectedTab === 'timeTravel') {
87+
setWasTimeTravelMounted(true);
88+
}
89+
}, [selectedTab]);
90+
8391
useDebouncedEffect(
8492
() => {
8593
const map = mapRef.current;
@@ -214,9 +222,15 @@ function ProjectsMap(props: ProjectsMapProps) {
214222
{shouldShowMapTabs && (
215223
<MapTabs selectedTab={selectedTab} setSelectedTab={setSelectedTab} />
216224
)}
217-
{selectedTab === 'timeTravel' && (
218-
<TimeTravel sitesGeoJson={sitesGeoJson} />
219-
)}
225+
{shouldShowSingleProjectsView &&
226+
(selectedTab === 'timeTravel' || wasTimeTravelMounted) && (
227+
<div>
228+
<TimeTravel
229+
sitesGeoJson={sitesGeoJson}
230+
isVisible={selectedTab === 'timeTravel'}
231+
/>
232+
</div>
233+
)}
220234
<Map
221235
{...viewState}
222236
{...mapState}

0 commit comments

Comments
 (0)