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

Bug: direct link to plant location does not work (first time) #2350

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ const ImageSection = (props: ImageSectionProps) => {
const isEmbed = embed === 'true';

const handleBackButton = () => {
if (setPreventShallowPush) {
setPreventShallowPush(true);
}
if (setPreventShallowPush) setPreventShallowPush(true);

const previousPageRoute = sessionStorage.getItem('backNavigationUrl');
const defaultRoute = `/${locale}`;
Expand Down
6 changes: 5 additions & 1 deletion src/features/projectsV2/ProjectsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,8 @@
page !== 'project-details' ||
singleProject === null ||
selectedSite !== null ||
(requestedPlantLocation && requestedSite)
(requestedPlantLocation && requestedSite) ||
plantLocations === null
)
return;

Expand Down Expand Up @@ -353,21 +354,23 @@
selectedPlantLocation,
selectedSite,
hasNoSites,
plantLocations,
]);
useEffect(() => {
if (requestedPlantLocation && plantLocations === null) return;
if (
!router.isReady ||
page !== 'project-details' ||
singleProject === null ||
selectedPlantLocation !== null ||
preventShallowPush
)
return;
// Handle the case where a direct link requests a specific site (via URL query)
// This will update the site param based on the requestedSite. If the requested site ID is invalid,
// it falls back to the default (first) site.
if (requestedSite && selectedSite === null && !hasNoSites) {
const index = singleProject.sites?.findIndex(

Check notice on line 373 in src/features/projectsV2/ProjectsContext.tsx

View check run for this annotation

codefactor.io / CodeFactor

src/features/projectsV2/ProjectsContext.tsx#L359-L373

Complex Method
(site) => site.properties.id === requestedSite
);

Expand Down Expand Up @@ -398,6 +401,7 @@
requestedSite,
router.isReady,
selectedPlantLocation,
plantLocations,
preventShallowPush,
hasNoSites,
]);
Expand Down
25 changes: 19 additions & 6 deletions src/features/projectsV2/ProjectsMap/SingleProjectView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ interface Props {
}

const SingleProjectView = ({ mapRef }: Props) => {
const router = useRouter();
const { singleProject, selectedSite, selectedPlantLocation, plantLocations } =
useProjects();
const { ploc: requestedPlantLocation, site: requestedSite } = router.query;
if (singleProject === null) return null;

const { isSatelliteView, handleViewStateChange, setIsSatelliteView } =
useProjectsMap();
const router = useRouter();

const sitesGeojson = useMemo(() => {
return {
Expand All @@ -34,7 +34,6 @@ const SingleProjectView = ({ mapRef }: Props) => {
}, [singleProject?.sites]);
const hasNoSites = sitesGeojson.features.length === 0;
// Zoom to plant location

useEffect(() => {
if (!router.isReady || selectedPlantLocation === null) return;
const { geometry } = selectedPlantLocation;
Expand All @@ -57,11 +56,16 @@ const SingleProjectView = ({ mapRef }: Props) => {
zoomToLocation(handleViewStateChange, lon, lat, 20, 4000, mapRef);
}
}
}, [selectedPlantLocation, router.isReady]);
}, [selectedPlantLocation, router.isReady, requestedPlantLocation]);

// Zoom to project site
useEffect(() => {
if (!router.isReady || selectedPlantLocation !== null) return;
if (
!router.isReady ||
selectedPlantLocation !== null ||
Boolean(requestedPlantLocation)
)
return;
if (sitesGeojson.features.length > 0 && selectedSite !== null) {
zoomInToProjectSite(
mapRef,
Expand All @@ -72,6 +76,7 @@ const SingleProjectView = ({ mapRef }: Props) => {
);
} else {
const { lat: latitude, lon: longitude } = singleProject.coordinates;
if (!(singleProject.sites?.length === 0)) return;

if (typeof latitude === 'number' && typeof longitude === 'number') {
// Zoom into the project location that has no site
Expand All @@ -85,9 +90,17 @@ const SingleProjectView = ({ mapRef }: Props) => {
);
}
}
}, [selectedSite, sitesGeojson, router.isReady, selectedPlantLocation]);
}, [
selectedSite,
sitesGeojson,
router.isReady,
selectedPlantLocation,
requestedPlantLocation,
requestedSite,
]);

useEffect(() => {
if (plantLocations === null) return;
const hasNoPlantLocations = !plantLocations?.length;
const isSingleProjectLocation = hasNoPlantLocations && hasNoSites;
// Satellite view will be:
Expand Down
4 changes: 1 addition & 3 deletions src/utils/projectV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,12 @@ export const areMapCoordsEqual = (
export const getLocalizedPath = (path: string, locale: string): string => {
// Strip query parameters if present
const pathWithoutQuery = path.split('?')[0];

// Remove trailing slash if present
const cleanPath = pathWithoutQuery.endsWith('/')
? pathWithoutQuery.slice(0, -1)
: pathWithoutQuery;

// Handle root path special case
if (cleanPath === '' || cleanPath === '/') {
if (cleanPath === '' || cleanPath === '/' || cleanPath === `/${locale}`) {
return `/${locale}`;
}

Expand Down
Loading