-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
GoogleMaps.tsx
130 lines (119 loc) · 3.88 KB
/
GoogleMaps.tsx
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
import { Group as TweenGroup } from '@tweenjs/tween.js'
import { useEventEmitter, useMap } from 'ahooks'
import { type EventEmitter } from 'ahooks/lib/useEventEmitter'
import { createContext, type ReactNode, useEffect, useMemo, useRef, useState } from 'react'
export const GoogleMapContext = createContext<GoogleMapContext | null>(null)
GoogleMapContext.displayName = 'GoogleMapContext'
export const GoogleMapsProvider = ({ children }: { children: ReactNode }) => {
const [map, setMap] = useState<google.maps.Map>()
const [infoWindow, setInfoWindow] = useState<google.maps.InfoWindow>()
const [isReady, setIsReady] = useState(false)
const [markers, marker] = useMap<string, google.maps.marker.AdvancedMarkerElement>()
const eventEmitter = useEventEmitter<boolean>()
const mapEvents = useMemo(
() => ({
ready: eventEmitter,
initialPropsSet: eventEmitter,
}),
[eventEmitter]
)
const mapEventRef = useRef<MapEvents>(mapEvents)
const initialCamera = useMemo(() => {
return {
center: map?.getCenter()?.toJSON() ?? { lat: 39.8283, lng: -98.5795 },
zoom: map?.getZoom() ?? 4,
}
}, [map])
const cameraRef = useRef<google.maps.CameraOptions>(initialCamera)
const tweenGroup = useMemo(() => new TweenGroup(), [])
mapEventRef.current.ready.useSubscription((val) => {
setIsReady(val)
})
useEffect(() => {
if (map && infoWindow) {
const infoListener = google.maps.event.addListener(infoWindow, 'closeclick', () => {
if (cameraRef.current.center) {
map.panTo(cameraRef.current.center)
}
})
const mapListener = google.maps.event.addListener(map, 'click', () => {
infoWindow.close()
if (cameraRef.current.center) {
map.panTo(cameraRef.current.center)
}
})
markers.forEach((singleMarker) => (singleMarker.map = map))
return () => {
google.maps.event.removeListener(infoListener)
google.maps.event.removeListener(mapListener)
}
}
return () => void 0
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [map, infoWindow])
const mapIsReady = typeof map !== 'undefined' && typeof infoWindow !== 'undefined'
const contextValue: ContextValue<boolean> = useMemo(
() =>
mapIsReady && isReady
? {
map,
infoWindow,
setMap,
setInfoWindow,
mapEvents,
marker,
markers,
isReady,
tweenGroup,
camera: cameraRef.current,
}
: {
setMap,
setInfoWindow,
mapEvents,
marker,
markers,
tweenGroup,
isReady: false,
map: undefined,
infoWindow: undefined,
camera: cameraRef.current,
},
[infoWindow, map, mapEvents, mapIsReady, marker, markers, isReady, tweenGroup]
)
return <GoogleMapContext.Provider value={contextValue}>{children}</GoogleMapContext.Provider>
}
export interface MapEvents {
ready: EventEmitter<boolean>
initialPropsSet: EventEmitter<boolean>
}
export interface MarkerState {
set: (key: string, value: google.maps.marker.AdvancedMarkerElement) => void
setAll: (value: Iterable<readonly [string, google.maps.marker.AdvancedMarkerElement]>) => void
get: (key: string) => google.maps.marker.AdvancedMarkerElement | undefined
remove: (key: string) => void
reset: () => void
}
interface GoogleMapContextBase {
setMap: (map: google.maps.Map) => void
setInfoWindow: (infoWindow: google.maps.InfoWindow) => void
mapEvents: MapEvents
camera: google.maps.CameraOptions
marker: MarkerState
markers: Map<string, google.maps.marker.AdvancedMarkerElement>
tweenGroup: TweenGroup
}
interface GoogleMapReadyContext extends GoogleMapContextBase {
map: google.maps.Map
infoWindow: google.maps.InfoWindow
isReady: true
}
interface GoogleMapNotReadyContext extends GoogleMapContextBase {
map: undefined
infoWindow: undefined
isReady: false
}
type GoogleMapContext = GoogleMapReadyContext | GoogleMapNotReadyContext
type ContextValue<TReady extends boolean> = TReady extends true
? GoogleMapReadyContext
: GoogleMapNotReadyContext