forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.tsx
More file actions
133 lines (122 loc) 路 3.84 KB
/
Copy pathMap.tsx
File metadata and controls
133 lines (122 loc) 路 3.84 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
'use client';
import { MapContainer, TileLayer, Marker, Popup, useMap } from 'react-leaflet';
import 'leaflet/dist/leaflet.css';
import { icon } from 'leaflet';
import { useState, useEffect } from 'react';
import { Plus } from 'lucide-react';
import AddGistModal from './AddGistModal';
import { motion } from 'framer-motion';
export interface Gist {
id: number;
content: string;
lat: number;
lng: number;
}
const greenIcon = icon({
iconUrl:
'https://raw.githubusercontent.com/pointhi/leaflet-color-markers/master/img/marker-icon-2x-green.png',
shadowUrl:
'https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.7/images/marker-shadow.png',
iconSize: [25, 41],
iconAnchor: [12, 41],
popupAnchor: [1, -34],
shadowSize: [41, 41],
});
const blueIcon = icon({
iconUrl:
'https://raw.githubusercontent.com/pointhi/leaflet-color-markers/master/img/marker-icon-2x-blue.png',
shadowUrl:
'https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.7/images/marker-shadow.png',
iconSize: [25, 41],
iconAnchor: [12, 41],
popupAnchor: [1, -34],
shadowSize: [41, 41],
});
function ChangeView({
center,
zoom,
}: {
center: [number, number];
zoom: number;
}) {
const map = useMap();
useEffect(() => {
map.flyTo(center, zoom);
}, [center]);
return null;
}
export default function Map() {
const [position, setPosition] = useState<[number, number]>([6.5244, 3.3792]);
const [gists, setGists] = useState<Gist[]>([
{
id: 1,
content: 'Amazing suya spot just opened here!',
lat: 6.52,
lng: 3.37,
},
{
id: 2,
content: 'Heads up, major traffic on this bridge.',
lat: 6.53,
lng: 3.38,
},
]);
const [isModalOpen, setIsModalOpen] = useState(false);
useEffect(() => {
navigator.geolocation.getCurrentPosition(
(pos) => {
const { latitude, longitude } = pos.coords;
setPosition([latitude, longitude]);
},
(err) => {
console.warn(`Geolocation Error (${err.code}): ${err.message}`);
}
);
}, []);
const handleAddGist = (content: string) => {
const newGist: Gist = {
id: Date.now(),
content,
lat: position[0],
lng: position[1],
};
setGists((prevGists) => [...prevGists, newGist]);
};
return (
<div className="relative h-full w-full">
<MapContainer center={position} zoom={14} className="h-full w-full">
<ChangeView center={position} zoom={14} />
<TileLayer
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
/>
<Marker position={position} icon={greenIcon}>
<Popup>Your Current Location</Popup>
</Marker>
{gists.map((gist) => (
<Marker key={gist.id} position={[gist.lat, gist.lng]} icon={blueIcon}>
<Popup>{gist.content}</Popup>
</Marker>
))}
</MapContainer>
<motion.button
onClick={() => setIsModalOpen(true)}
// 3. Animation props for pulsing effect
animate={{ scale: [1, 1.1, 1] }}
transition={{ duration: 1.5, repeat: Infinity, ease: 'easeInOut' }}
className="absolute top-1/2 left-8 -translate-y-1/2 z-[1000] flex items-center justify-center w-16 h-16 bg-gradient-to-r from-purple-600 via-blue-600 to-navy-400
bg-[size:200%_auto]
hover:bg-[position:100%_center]
transition-all duration-500 ease-in-out disabled:bg-blue-400 disabled:cursor-not-allowed text-white rounded-full shadow-lg hover:bg-blue-700"
aria-label="Add new gist"
>
<Plus size={32} />
</motion.button>
<AddGistModal
isOpen={isModalOpen}
onClose={() => setIsModalOpen(false)}
onAddGist={handleAddGist}
/>
</div>
);
}