22import "mapbox-gl/dist/mapbox-gl.css" ;
33
44import { Building } from "@common/types" ;
5- import mbxDirections from "@mapbox/mapbox-sdk/services/directions" ;
65import Box from "@mui/material/Box" ;
76import { DarkModeContext } from "app/clientLayout" ;
8- import { useSearchParams } from "next/navigation" ;
97import React , { useContext , useEffect , useRef , useState } from "react" ;
108import {
11- Layer ,
129 LngLatBoundsLike ,
1310 Map ,
1411 MapRef ,
1512 Marker ,
1613 Source ,
14+ Layer ,
1715} from "react-map-gl/mapbox" ;
1816import { useDebounceValue } from "usehooks-ts" ;
19- import BuildingDrawer from "views/BuildingDrawer" ;
2017
2118import { MAPBOX_ACCESS_TOKEN } from "../config" ;
2219import useBuildings from "../hooks/useBuildings" ;
@@ -69,12 +66,18 @@ if (!MAPBOX_ACCESS_TOKEN) {
6966}
7067
7168export const MapComponent = ( ) => {
69+ // Use debounce to allow moving from marker to popup without popup hiding
70+ const [ currentHover , setCurrentHover ] = useState < Building | null > ( null ) ;
71+ const [ debouncedCurrentHover ] = useDebounceValue ( currentHover , 50 ) ;
72+ const [ routeGeoJSON , setRouteGeoJSON ] = useState < any | null > ( null ) ;
73+ const [ distances , setDistances ] = useState < number [ ] > ( [ ] ) ;
74+ const [ roomIdToFocus , setRoomIdToFocus ] = useState < string > ( "" ) ;
75+
76+ const mapRef = useRef < MapRef > ( null ) ;
77+
7278 const { buildings } = useBuildings ( ) ;
7379 const { isDarkMode } = useContext ( DarkModeContext ) ;
7480 const { userLat, userLng } = useUserLocation ( ) ;
75-
76- const [ roomIdToFocus , setRoomIdToFocus ] = useState < string > ( "" ) ;
77-
7881 const { room } = useRoom ( roomIdToFocus ) ;
7982
8083 // TODO check if I need to only call this once? this is currently getting called every user change
@@ -87,32 +90,25 @@ export const MapComponent = () => {
8790 } , [ ] ) ;
8891
8992 useEffect ( ( ) => {
93+ console . log ( "HERE" ) ;
9094 if ( ! userLat || ! userLng || ! roomIdToFocus ) return ;
91- if ( ! room ) return ;
92-
93- // -33.917347,151.2286926
95+ console . log ( "test1" ) ;
96+ // Only set route once
97+ if ( routeGeoJSON && routeGeoJSON . geometry ) return ;
98+ console . log ( "tes2" ) ;
99+ console . log ( geometry ) ;
94100
95101 setRouteGeoJSON ( {
96102 type : "Feature" ,
97103 properties : { } ,
98104 geometry,
99105 } ) ;
100- } , [ userLat , userLng , roomIdToFocus ] ) ;
101-
102- const mapRef = useRef < MapRef > ( null ) ;
103-
104- // Use debounce to allow moving from marker to popup without popup hiding
105- const [ currentHover , setCurrentHover ] = useState < Building | null > ( null ) ;
106- const [ debouncedCurrentHover ] = useDebounceValue ( currentHover , 50 ) ;
107-
108- const [ routeGeoJSON , setRouteGeoJSON ] = useState < any | null > ( null ) ;
106+ } , [ userLat , userLng , roomIdToFocus , geometry ] ) ;
109107
110108 const style = isDarkMode
111109 ? "mapbox://styles/bengodw/cmcimql2101qo01sp7dricgzq"
112110 : "mapbox://styles/bengodw/cmcimp1tz002p01rcfzbd8btn" ;
113111
114- const [ distances , setDistances ] = useState < number [ ] > ( [ ] ) ;
115-
116112 useEffect ( ( ) => {
117113 if ( buildings && userLat && userLng && isInBounds ( userLat , userLng ) ) {
118114 setDistances (
@@ -123,19 +119,6 @@ export const MapComponent = () => {
123119 }
124120 } , [ buildings , userLat , userLng ] ) ;
125121
126- // // TODO refactor to fetch room coordinates
127- // const handleMarkerClick = async (building: Building) => {
128- // if (!userLat || !userLng) return;
129-
130- // const { geometry } = await useMapboxNavigation(userLat, userLng, building);
131-
132- // setRouteGeoJSON({
133- // type: "Feature",
134- // properties: {},
135- // geometry,
136- // });
137- // };
138-
139122 return (
140123 < div style = { { height : "100%" , position : "relative" } } >
141124 < Map
@@ -167,16 +150,18 @@ export const MapComponent = () => {
167150 </ Marker >
168151 ) }
169152
170- < RoomMapMarker
171- roomId = { roomIdToFocus }
172- roomLocation = { ( lat , long ) => {
173- mapRef . current ?. flyTo ( {
174- center : [ long , lat ] ,
175- zoom : 19.5 ,
176- duration : 1000 ,
177- } ) ;
178- } }
179- />
153+ { roomIdToFocus && (
154+ < RoomMapMarker
155+ roomId = { roomIdToFocus }
156+ roomLocation = { ( lat , long ) => {
157+ mapRef . current ?. flyTo ( {
158+ center : [ long , lat ] ,
159+ zoom : 19.5 ,
160+ duration : 1000 ,
161+ } ) ;
162+ } }
163+ />
164+ ) }
180165
181166 { routeGeoJSON && (
182167 < >
@@ -190,7 +175,7 @@ export const MapComponent = () => {
190175 "line-join" : "round" ,
191176 } }
192177 paint = { {
193- "line-color" : "#1DB954 " ,
178+ "line-color" : "#1976d2 " ,
194179 "line-width" : 4 ,
195180 } }
196181 />
0 commit comments