Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release: Refactor Project Details map for performance & other improvements #2082

Merged
merged 19 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
9e65ac6
Code Refactor to use GeoJSON filters for rendering map layers
shyambhongle May 20, 2024
87a44d1
Date difference layer added
shyambhongle May 21, 2024
b175005
Refactored code for better readability
shyambhongle May 27, 2024
17e5987
refactor: code formatting and TS warning fixes
mohitb35 May 30, 2024
fbc3ca4
refactor: rename map layers for point/polygon
mohitb35 May 30, 2024
678e14c
revert: reverses logic change in sample tree interactivity
mohitb35 May 30, 2024
7c9f1b7
doc: adds reminder to remove deprecated `onStateChange`
mohitb35 May 30, 2024
f075cbf
Merge pull request #2071 from Plant-for-the-Planet-org/hotfix/refactor
mohitb35 May 30, 2024
a78c0d0
fix:zoom and hover issue
shyambhongle Jun 10, 2024
a72926d
fix: site zoom fix
shyambhongle Jun 10, 2024
d99ce2b
fix: ImageSlider Crash
shyambhongle Jun 13, 2024
ff609c4
refactor: minor
shyambhongle Jun 13, 2024
a213327
refactor: corrects slider interval, minor changes to imports from rea…
mohitb35 Jun 14, 2024
d69117f
fix: resolves bugs with site selection logic
mohitb35 Jun 14, 2024
4f87a4c
Merge pull request #2085 from Plant-for-the-Planet-org/hotfix/polygon…
mohitb35 Jun 14, 2024
1ec9eb7
removed twitter link from footer
norbertschuler Jun 18, 2024
9274b01
fix: removes references to trilliontrees twitter handle
mohitb35 Jun 18, 2024
9ff0d9c
fix: explicitly sets button type for share to twitter functionality
mohitb35 Jun 18, 2024
ee395d5
Merge pull request #2092 from Plant-for-the-Planet-org/feature/remove…
norbertschuler Jun 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/features/common/types/plantLocation.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,34 +39,34 @@ export interface PlantLocationBase {
statusReason: string | null; // currently always null. Should we do something here?
}
export interface PlantLocationSingle extends PlantLocationBase {
type: PlantLocationType;
type: 'single';
scientificName: string | null;
scientificSpecies: string | null;
tag: string | null;
measurements: Measurements;
originalGeometry: Point;
geometry: GeometryOfSinglePlant;
geometry: Point;
sampleTrees: SamplePlantLocation[];
}

export interface PlantLocationMulti extends PlantLocationBase {
type: PlantLocationType;
type: 'multi';
nextMeasurementDate: DateString | null;
plantDateStart: DateString | null;
plantDateEnd: DateString | null;
sampleTreeCount: number;
samplePlantLocations: SamplePlantLocation[];
plantedSpecies: PlantedSpecies[];
originalGeometry: Polygon;
geometry: Geometry;
geometry: Point | Polygon;
sampleTrees: SamplePlantLocation[];
}

export type PlantLocation = PlantLocationSingle | PlantLocationMulti;

export interface SamplePlantLocation
extends Omit<PlantLocationBase, 'plantProject'> {
type: Type;
type: 'sample';
/** parent plant location */
parent: string;
/** tpo profile id */
Expand Down
78 changes: 38 additions & 40 deletions src/features/projects/components/ProjectsMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import LayerDisabled from '../../../../public/assets/images/icons/LayerDisabled'
import { useTranslations } from 'next-intl';
import { ParamsContext } from '../../common/Layout/QueryParamsContext';
import { PopupData } from './maps/Markers';
import { PlantLocation } from '../../common/types/plantLocation';

interface ShowDetailsProps {
coordinates: [number, number] | null;
Expand All @@ -33,7 +34,6 @@ export default function ProjectsMap(): ReactElement {
defaultMapCenter,
defaultZoom,
zoomLevel,
plIds,
setHoveredPl,
plantLocations,
setSelectedPl,
Expand Down Expand Up @@ -85,49 +85,46 @@ export default function ProjectsMap(): ReactElement {
setViewPort,
};

const handlePlantLocationSelection = (
plantLocations: PlantLocation[] | null,
e: MapEvent
) => {
if (!plantLocations || !e || !e.features || !e.features[0]) {
return;
}

const { id } = e.features[0].properties;
const selectedElement = plantLocations.find(
(location) => location.id === id
);

if (selectedElement) {
setSelectedPl(selectedElement);
}
};

const onMapClick = (e: MapEvent) => {
setSamplePlantLocation(null);
setPopupData({ show: false });
setIsPolygonMenuOpen(false);
setFilterOpen(false);
if (e.features && e.features?.length !== 0) {
if (e.features[0].layer?.source) {
for (const key in plantLocations) {
if (Object.prototype.hasOwnProperty.call(plantLocations, key)) {
const element = plantLocations[Number(key)];
if (element.id === e.features[0].layer?.source) {
setSelectedPl(element);

break;
}
}
}
//router.replace(`/${project.slug}/${e.features[0].layer?.source}`);
}
}
handlePlantLocationSelection(plantLocations, e);
};

const onMapHover = (e: MapEvent) => {
if (e.features && e.features?.length !== 0) {
if (!hoveredPl || hoveredPl.type !== 'sample') {
if (e.features[0].layer?.source && plantLocations) {
for (const key in plantLocations) {
if (Object.prototype.hasOwnProperty.call(plantLocations, key)) {
const element = plantLocations[key];
if (element.id === e.features[0].layer?.source) {
setHoveredPl(element);
// setSelectedPl(element);
break;
}
}
}
}
if (plantLocations && e && e.features && e.features[0]) {
const activeElement = e.features[0];
const activePlantLocation = plantLocations.find(
(obj) => obj.id === activeElement.properties.id
);
if (activePlantLocation) {
setHoveredPl(activePlantLocation);
setShowDetails({ coordinates: e.lngLat, show: true });
}
setShowDetails({ coordinates: e.lngLat, show: true });
} else {
setShowDetails({ ...showDetails, show: false });
setHoveredPl(null);
return;
}
setShowDetails({ ...showDetails, show: false });
setHoveredPl(null);
};

React.useEffect(() => {
Expand All @@ -147,6 +144,10 @@ export default function ProjectsMap(): ReactElement {
}
}, [showProjectList]);

const handleOnLoad = () => {
setLoaded(true);
};

return (
<div
className={
Expand All @@ -158,11 +159,12 @@ export default function ProjectsMap(): ReactElement {
{...mapState}
{...viewport}
onViewportChange={_onViewportChange}
// TODO: onStateChange is deprecated, and does not work any more. _onStateChange does not seem to be called in any scenario while debugging the code. However this is left in to avoid breaking the code unintentionally. NOTE: replacing with onViewStateChange does not work as expected, the map stops zooming in to a clicked plant location, or to the site after switching between the Field data and Time travel map tabs.
onStateChange={_onStateChange}
onClick={onMapClick}
onHover={onMapHover}
onLoad={() => setLoaded(true)}
interactiveLayerIds={plIds ? plIds : undefined}
onLoad={handleOnLoad}
interactiveLayerIds={['polygon-layer', 'point-layer']}
>
{zoomLevel === 1 && searchedProject && showProjects && (
<Home {...homeProps} />
Expand Down Expand Up @@ -216,7 +218,3 @@ export default function ProjectsMap(): ReactElement {
</div>
);
}
interface DetailsType {
coordinates: number[];
show: boolean;
}
Loading
Loading