Skip to content

Commit 6144db3

Browse files
committed
update types for mapbox navigation
1 parent b7f7190 commit 6144db3

2 files changed

Lines changed: 25 additions & 26 deletions

File tree

frontend/components/Map.tsx

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -73,37 +73,31 @@ export const MapComponent = () => {
7373
const { isDarkMode } = useContext(DarkModeContext);
7474
const { userLat, userLng } = useUserLocation();
7575

76-
const [roomIdToFocus, setRoomIdToFocus] = useState<string | undefined>(
77-
undefined
78-
);
76+
const [roomIdToFocus, setRoomIdToFocus] = useState<string>("");
77+
78+
const { room } = useRoom(roomIdToFocus);
7979

80-
const [initialUserCoordinate, setIntialUserCoordinate] = useState<{
81-
lat: number;
82-
lng: number;
83-
} | null>(null);
80+
// TODO check if I need to only call this once? this is currently getting called every user change
81+
const { geometry } = useMapboxNavigation(userLat, userLng, room);
8482

8583
useEffect(() => {
8684
// Run only on client side, no Suspense issues
8785
const params = new URLSearchParams(window.location.search);
88-
setRoomIdToFocus(params.get("roomId") ?? undefined);
86+
setRoomIdToFocus(params.get("roomId") ?? "");
8987
}, []);
9088

91-
// TODO refactor hook usage here
9289
useEffect(() => {
9390
if (!userLat || !userLng || !roomIdToFocus) return;
94-
const { room } = useRoom(roomIdToFocus);
95-
if (!initialUserCoordinate) {
96-
if (!room) return;
97-
const { geometry } = useMapboxNavigation(userLat, userLng, room);
98-
99-
setRouteGeoJSON({
100-
type: "Feature",
101-
properties: {},
102-
geometry,
103-
});
104-
setIntialUserCoordinate({ lat: userLat, lng: userLng });
105-
}
106-
}, [userLat, userLng, roomIdToFocus, initialUserCoordinate]);
91+
if (!room) return;
92+
93+
// -33.917347,151.2286926
94+
95+
setRouteGeoJSON({
96+
type: "Feature",
97+
properties: {},
98+
geometry,
99+
});
100+
}, [userLat, userLng, roomIdToFocus]);
107101

108102
const mapRef = useRef<MapRef>(null);
109103

frontend/hooks/useMapboxNavigation.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const fetchRoute = async (
1414
}
1515

1616
const directionsClient = mbxDirections({ accessToken: MAPBOX_ACCESS_TOKEN });
17-
17+
console.log(userLat, userLng);
1818
try {
1919
const response = await directionsClient
2020
.getDirections({
@@ -30,16 +30,21 @@ const fetchRoute = async (
3030
return response.body.routes[0].geometry;
3131
// TODO proper typing here
3232
} catch (error: any) {
33-
console.error("Route fetch error:", error.response?.body || error.message);
33+
console.log(error);
3434
throw error;
3535
}
3636
};
3737

38-
const useMapboxNavigation = (userLat: number, userLng: number, room: Room) => {
38+
const useMapboxNavigation = (
39+
userLat: number | undefined,
40+
userLng: number | undefined,
41+
room: Room | undefined
42+
) => {
3943
/** TODO add proper types */
4044

45+
console.log("TEST", room);
4146
const { data, error } = useSWRImmutable(
42-
[userLat, userLng, room.lat, room.long],
47+
[userLat, userLng, room ? room.lat : null, room ? room.long : null],
4348
fetchRoute
4449
);
4550

0 commit comments

Comments
 (0)