-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
107 lines (94 loc) · 3.08 KB
/
App.tsx
File metadata and controls
107 lines (94 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import React, { useState, useEffect } from 'react';
import ChatInterface from './components/ChatInterface';
import { MapChunk, Coordinates, PlaceDetails } from './types';
import { getDirections, RouteData } from './services/mapService';
const App: React.FC = () => {
const [mapChunks, setMapChunks] = useState<MapChunk[]>([]);
const [location, setLocation] = useState<Coordinates | undefined>(undefined);
const [locationError, setLocationError] = useState<string | null>(null);
// Lifted State
const [selectedPlace, setSelectedPlace] = useState<PlaceDetails | null>(null);
const [routeData, setRouteData] = useState<RouteData | null>(null);
const [favorites, setFavorites] = useState<PlaceDetails[]>(() => {
const saved = localStorage.getItem('favorites');
return saved ? JSON.parse(saved) : [];
});
useEffect(() => {
localStorage.setItem('favorites', JSON.stringify(favorites));
}, [favorites]);
const toggleFavorite = (place: PlaceDetails) => {
setFavorites(prev => {
const exists = prev.some(p => p.name === place.name); // Simple check by name for now
if (exists) {
return prev.filter(p => p.name !== place.name);
} else {
return [...prev, place];
}
});
};
const isFavorite = (place: PlaceDetails) => {
return favorites.some(p => p.name === place.name);
};
// Initialize Location Watch on Mount
useEffect(() => {
if (!("geolocation" in navigator)) {
setLocationError("Unsupported");
return;
}
const watchId = navigator.geolocation.watchPosition(
(position) => {
setLocation({
latitude: position.coords.latitude,
longitude: position.coords.longitude
});
setLocationError(null);
},
(error) => {
console.warn("Location access denied or failed", error);
setLocationError("Access denied");
},
{
enableHighAccuracy: true,
timeout: 10000,
maximumAge: 0
}
);
return () => {
navigator.geolocation.clearWatch(watchId);
};
}, []);
const handleNavigate = async (place: PlaceDetails) => {
if (!location) {
alert("Please enable location services to use navigation.");
return;
}
const route = await getDirections(location, {
latitude: place.geometry.location.lat,
longitude: place.geometry.location.lng
});
if (route) {
setRouteData(route);
} else {
alert("Could not find a route.");
}
};
return (
<div className="relative w-full h-[100dvh] overflow-hidden bg-gray-100 font-sans">
<ChatInterface
onMapChunksUpdate={setMapChunks}
userLocation={location}
locationError={locationError}
selectedPlace={selectedPlace}
onNavigate={() => selectedPlace && handleNavigate(selectedPlace)}
// Pass map state to ChatInterface
mapChunks={mapChunks}
routeData={routeData}
onSelectPlace={setSelectedPlace}
favorites={favorites}
onToggleFavorite={toggleFavorite}
isFavorite={isFavorite}
/>
</div>
);
};
export default App;