Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/app/(forms)/profile/listings/[slug]/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default async function EditListingPage({ params }) {
.single();

const { data: listing } = await supabase
.from("listings")
.from("listings_private_data")
.select()
.eq("owner_id", user.id)
.match({ slug })
Expand Down
13 changes: 7 additions & 6 deletions src/components/ListingRead/ListingRead.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const ListingRead = memo(function Listing({
? listing.name
: listing.owner_first_name
: getListingDisplayName(listing, user);
const coordinates = listing?.coordinates;

if (!listing && presentation !== "demo") {
console.log("Listing not found");
Expand Down Expand Up @@ -158,15 +159,15 @@ const ListingRead = memo(function Listing({
<MapThumbnail
height="320px"
initialViewState={{
longitude: listing.longitude,
latitude: listing.latitude,
longitude: coordinates.longitude,
latitude: coordinates.latitude,
zoom: initialZoomLevel,
}}
interactive={false}
>
<Marker
longitude={listing.longitude}
latitude={listing.latitude}
longitude={coordinates.longitude}
latitude={coordinates.latitude}
anchor="center"
onClick={(event) => {
event.originalEvent.stopPropagation();
Expand Down Expand Up @@ -216,7 +217,7 @@ const ListingRead = memo(function Listing({
<Button
variant="secondary"
size="small"
href={`https://maps.apple.com/?ll=${listing.latitude},${listing.longitude}&q=${encodeURIComponent(
href={`https://maps.apple.com/?ll=${coordinates.latitude},${coordinates.longitude}&q=${encodeURIComponent(
listing.name
)}`}
target="_blank"
Expand All @@ -226,7 +227,7 @@ const ListingRead = memo(function Listing({
<Button
variant="secondary"
size="small"
href={`https://maps.google.com/?q=${listing.latitude},${listing.longitude}`}
href={`https://maps.google.com/?q=${coordinates.latitude},${coordinates.longitude}`}
target="_blank"
>
{t("Actions.openInGoogleMaps")}
Expand Down
11 changes: 1 addition & 10 deletions src/components/ListingWrite/ListingWrite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,7 @@ export default function ListingWrite({
);

const [coordinates, setCoordinates] = useState<Coordinates | null>(
initialListing
? {
latitude: initialListing.latitude,
longitude: initialListing.longitude,
}
: null
initialListing?.coordinates ?? null
);

const [areaName, setAreaName] = useState<string>(
Expand Down Expand Up @@ -264,10 +259,6 @@ export default function ListingWrite({
...(listingType !== "residential" && { name: validatedName }),
description,
location: `POINT(${selectedCoordinates.longitude} ${selectedCoordinates.latitude})`,
// Temporarily store the coordinates as longitude and latitude floats in the database as well
// ...because I can't get the geometry type to convert to to long and lat dynamically if a user goes direct to a listing, e.g. http://localhost:3000/map?listing=9xvN9zxH0rzZ
longitude: selectedCoordinates.longitude,
latitude: selectedCoordinates.latitude,
area_name: areaName,
country_code: countryCode,
accepted_items: acceptedItems.filter((item) => item.trim() !== ""),
Expand Down
132 changes: 92 additions & 40 deletions src/components/MapImmersive/MapImmersive.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,20 @@ const DEFAULT_COORDINATES = {
zoom: 9,
};

function getListingCoordinates(listing) {
return listing?.coordinates ?? null;
}

function hasValidCoordinates(listing) {
const coordinates = getListingCoordinates(listing);

return (
listing &&
!listing.error &&
typeof listing.latitude === "number" &&
typeof listing.longitude === "number" &&
Number.isFinite(listing.latitude) &&
Number.isFinite(listing.longitude)
typeof coordinates?.latitude === "number" &&
typeof coordinates?.longitude === "number" &&
Number.isFinite(coordinates.latitude) &&
Number.isFinite(coordinates.longitude)
);
}

Expand All @@ -86,7 +92,9 @@ export default function MapImmersive({
isDesktop,
countryCode,
}) {
const isFirstLoad = useRef(true);
const selectedListingCoordinates = getListingCoordinates(selectedListing);
const hasAppliedInitialPositionRef = useRef(false);
const centeredListingIdRef = useRef(null);
const [lastKnownPosition, setLastKnownPosition] = useState(null);
const [isListingInView, setIsListingInView] = useState(true);
const hasInitialPosition =
Expand All @@ -111,11 +119,15 @@ export default function MapImmersive({

// If there's a selected listing with valid coords, center on it instead of using IP location
if (hasValidCoordinates(selectedListing)) {
const coordinates = getListingCoordinates(selectedListing);

mapRef.current?.flyTo({
center: [selectedListing.longitude, selectedListing.latitude],
center: [coordinates.longitude, coordinates.latitude],
zoom: 12,
duration: 0,
});
hasAppliedInitialPositionRef.current = true;
centeredListingIdRef.current = selectedListing.id;
}

const bounds = mapRef.current.getMap().getBounds();
Expand All @@ -133,9 +145,10 @@ export default function MapImmersive({

// Check if selected listing is in view (only when it has valid coords)
if (hasValidCoordinates(selectedListing)) {
const coordinates = getListingCoordinates(selectedListing);
const isInView = bounds.contains([
selectedListing.longitude,
selectedListing.latitude,
coordinates.longitude,
coordinates.latitude,
]);
setIsListingInView(isInView);
}
Expand All @@ -144,8 +157,10 @@ export default function MapImmersive({
const handleFlyToListing = useCallback(() => {
if (!hasValidCoordinates(selectedListing) || !mapRef.current) return;

const coordinates = getListingCoordinates(selectedListing);

mapRef.current.flyTo({
center: [selectedListing.longitude, selectedListing.latitude],
center: [coordinates.longitude, coordinates.latitude],
duration: 1500,
});
}, [selectedListing]);
Expand All @@ -154,32 +169,66 @@ export default function MapImmersive({
let protocol = new Protocol();
maplibregl.addProtocol("pmtiles", protocol.tile);

// Only handle initial positioning on first load
if (isFirstLoad.current) {
isFirstLoad.current = false;

// If there's a selected listing in URL with valid coords, center on it
if (hasValidCoordinates(selectedListing)) {
mapRef.current?.flyTo({
center: [selectedListing.longitude, selectedListing.latitude],
zoom: 12,
duration: 0,
});
}
// If no listing but we have IP coordinates, use those
else if (initialCoordinates) {
mapRef.current?.flyTo({
center: [initialCoordinates.longitude, initialCoordinates.latitude],
zoom: initialCoordinates.zoom,
duration: 0,
});
}
}

return () => {
maplibregl.removeProtocol("pmtiles");
};
}, []); // Empty dependency array as before
}, []);

useEffect(() => {
if (
hasAppliedInitialPositionRef.current ||
hasValidCoordinates(selectedListing) ||
!initialCoordinates ||
!mapRef.current
) {
return;
}

hasAppliedInitialPositionRef.current = true;
mapRef.current.flyTo({
center: [initialCoordinates.longitude, initialCoordinates.latitude],
zoom: initialCoordinates.zoom,
duration: 0,
});
}, [initialCoordinates, mapRef, selectedListing]);
Comment on lines +177 to +193
Copy link

Copilot AI Apr 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isFirstLoad.current is only set to false when the "IP coordinates" flyTo runs. If the page loads with a selected listing (URL param) you never clear isFirstLoad, so later when the user clears selectedListing this effect can fire and unexpectedly recenter the map to initialCoordinates. Consider setting isFirstLoad.current = false unconditionally after the first mount/map load, and gating the initialCoordinates centering with a separate ref that only tracks whether the initialCoordinates centering has already run (rather than reusing isFirstLoad).

Copilot uses AI. Check for mistakes.

useEffect(() => {
if (
!mapRef.current ||
!hasValidCoordinates(selectedListing) ||
centeredListingIdRef.current === selectedListing.id
) {
return;
}

const coordinates = getListingCoordinates(selectedListing);
const map = mapRef.current.getMap();
const bounds = map.getBounds();
const isInView = bounds.contains([
coordinates.longitude,
coordinates.latitude,
]);

if (isInView) {
hasAppliedInitialPositionRef.current = true;
centeredListingIdRef.current = selectedListing.id;
setIsListingInView(true);
return;
}

mapRef.current.flyTo({
center: [coordinates.longitude, coordinates.latitude],
zoom: 12,
duration: 900,
});
hasAppliedInitialPositionRef.current = true;
centeredListingIdRef.current = selectedListing.id;
}, [
mapRef,
selectedListing,
selectedListingCoordinates?.latitude,
selectedListingCoordinates?.longitude,
]);
Comment on lines +195 to +231
Copy link

Copilot AI Apr 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hasCenteredSelectedListingRef is never reset when selectedListing changes, so this effect will only auto-center for the first listing that becomes selected during the component’s lifetime. If the user navigates to a different listing via URL/history (or selectedListing changes to a different id), the map won’t recenter. Consider tracking the last centered listing id/slug and resetting the ref when that identifier changes, or make the guard conditional on matching the currently selected listing.

Copilot uses AI. Check for mistakes.

// Set mapController to set relationship between MapSearch and MapImmersive
// Can't get this to work, perhaps delete all mapController and createMapLibreGlMapController code if I can't get it working
Expand All @@ -193,9 +242,11 @@ export default function MapImmersive({
// Update lastKnownPosition when we have a valid position
useEffect(() => {
if (hasValidCoordinates(selectedListing)) {
const coordinates = getListingCoordinates(selectedListing);

setLastKnownPosition({
latitude: selectedListing.latitude,
longitude: selectedListing.longitude,
latitude: coordinates.latitude,
longitude: coordinates.longitude,
});
} else if (initialCoordinates && !lastKnownPosition) {
setLastKnownPosition(initialCoordinates);
Expand All @@ -210,9 +261,10 @@ export default function MapImmersive({
}

const bounds = mapRef.current.getMap().getBounds();
const coordinates = getListingCoordinates(selectedListing);
const isInView = bounds.contains([
selectedListing.longitude,
selectedListing.latitude,
coordinates.longitude,
coordinates.latitude,
]);
console.log("isInView", isInView);
setIsListingInView(isInView);
Expand Down Expand Up @@ -269,13 +321,13 @@ export default function MapImmersive({
initialViewState={{
longitude:
(hasValidCoordinates(selectedListing)
? selectedListing.longitude
? selectedListingCoordinates.longitude
: null) ??
initialCoordinates?.longitude ??
DEFAULT_COORDINATES.longitude,
latitude:
(hasValidCoordinates(selectedListing)
? selectedListing.latitude
? selectedListingCoordinates.latitude
: null) ??
Comment on lines 321 to 331
Copy link

Copilot AI Apr 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You introduced getListingCoordinates() to centralize coordinate access, but initialViewState still reads selectedListing.coordinates.* directly. For consistency (and to reduce the chance of a future shape change missing one call site), consider using getListingCoordinates(selectedListing) here as well (or destructure once and reuse).

Copilot uses AI. Check for mistakes.
initialCoordinates?.latitude ??
DEFAULT_COORDINATES.latitude,
Expand Down Expand Up @@ -308,8 +360,8 @@ export default function MapImmersive({
.map((listing) => (
<DrawerTrigger key={listing.id}>
<Marker
longitude={listing.longitude}
latitude={listing.latitude}
longitude={listing.coordinates.longitude}
latitude={listing.coordinates.latitude}
anchor="center"
onClick={(event) => {
event.originalEvent.stopPropagation();
Expand Down
Loading
Loading