Skip to content

Commit c4e7999

Browse files
fixed snap back
1 parent fd4bcae commit c4e7999

3 files changed

Lines changed: 25 additions & 4 deletions

File tree

frontend/plan/components/map/Map.tsx

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { Location } from "../../types";
1313
interface MapProps {
1414
locations: Location[];
1515
zoom: number;
16+
viewKey?: string;
1617
}
1718

1819
function toRadians(degrees: number): number {
@@ -87,11 +88,12 @@ function separateOverlappingPoints(points: Location[], offset = 0.0001) {
8788
return adjustedPoints;
8889
}
8990

90-
function Map({ locations, zoom }: MapProps) {
91+
function Map({ locations, zoom, viewKey }: MapProps) {
9192
const mapRef = useRef<MapRef | null>(null);
9293
const mapboxToken = process.env.NEXT_PUBLIC_MAPBOX_ACCESS_TOKEN;
9394
const mapboxStyleId = process.env.NEXT_PUBLIC_MAPBOX_STYLE_ID || "mapbox/streets-v12";
9495
const [cursor, setCursor] = useState<string>("");
96+
const [autoCenter, setAutoCenter] = useState(true);
9597
const [selected, setSelected] = useState<{
9698
longitude: number;
9799
latitude: number;
@@ -152,14 +154,20 @@ function Map({ locations, zoom }: MapProps) {
152154
return `${hours12}:${minutes.toString().padStart(2, "0")} ${period}`;
153155
};
154156

157+
// When the day/view changes, re-enable auto-centering.
155158
useEffect(() => {
156-
if (!mapRef.current) return;
159+
setAutoCenter(true);
160+
setSelected(null);
161+
}, [viewKey]);
162+
163+
useEffect(() => {
164+
if (!mapRef.current || !autoCenter) return;
157165
mapRef.current.flyTo({
158166
center: [center[1], center[0]],
159167
zoom,
160168
essential: true,
161169
});
162-
}, [center, zoom]);
170+
}, [autoCenter, center, zoom]);
163171

164172
if (!mapboxToken) {
165173
return (
@@ -200,6 +208,10 @@ function Map({ locations, zoom }: MapProps) {
200208
maxPitch={70}
201209
interactiveLayerIds={["pcp-course-markers"]}
202210
cursor={cursor}
211+
onDragStart={() => setAutoCenter(false)}
212+
onZoomStart={() => setAutoCenter(false)}
213+
onRotateStart={() => setAutoCenter(false)}
214+
onPitchStart={() => setAutoCenter(false)}
203215
onMouseMove={(e) => {
204216
const hovering = (e.features?.length || 0) > 0;
205217
setCursor(hovering ? "pointer" : "");
@@ -212,6 +224,14 @@ function Map({ locations, zoom }: MapProps) {
212224
}
213225
const [lng, lat] = f.geometry.coordinates as [number, number];
214226
const props = (f.properties || {}) as Record<string, unknown>;
227+
228+
// Center the map on the clicked marker (keep current zoom).
229+
setAutoCenter(false);
230+
mapRef.current?.flyTo({
231+
center: [lng, lat],
232+
essential: true,
233+
});
234+
215235
setSelected({
216236
longitude: lng,
217237
latitude: lat,

frontend/plan/components/map/MapTab.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ function MapTab({
139139
locData.lng != null
140140
)}
141141
zoom={14}
142+
viewKey={selectedDay}
142143
/>
143144
</MapContainer>
144145
<MapCourseItemcontainer>

frontend/plan/tsconfig.tsbuildinfo

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)