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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
import React from 'react'
import {
DEFAULT_GEOLOCATION_CONFIG,
DEFAULT_GEOLOCATION_STATUS,
} from '#/state/geolocation/const'
import {onGeolocationConfigUpdate} from '#/state/geolocation/events'
import {logger} from '#/state/geolocation/logger'
import {
type DeviceLocation,
type GeolocationStatus,
} from '#/state/geolocation/types'
import {useSyncedDeviceGeolocation} from '#/state/geolocation/useSyncedDeviceGeolocation'
import {
computeGeolocationStatus,
mergeGeolocation,
} from '#/state/geolocation/util'
import {type Device, device} from '#/storage'
export * from '#/state/geolocation/config'
export * from '#/state/geolocation/types'
export * from '#/state/geolocation/util'
type DeviceGeolocationContext = {
deviceGeolocation: DeviceLocation | undefined
}
type DeviceGeolocationAPIContext = {
setDeviceGeolocation(deviceGeolocation: DeviceLocation): void
}
type GeolocationConfigContext = {
config: Device['geolocation']
}
type GeolocationStatusContext = {
/**
* Merged geolocation from config and device GPS (if available).
*/
location: DeviceLocation
/**
* Computed geolocation status based on the merged location and config.
*/
status: GeolocationStatus
}
const DeviceGeolocationContext = React.createContext<DeviceGeolocationContext>({
deviceGeolocation: undefined,
})
DeviceGeolocationContext.displayName = 'DeviceGeolocationContext'
const DeviceGeolocationAPIContext =
React.createContext<DeviceGeolocationAPIContext>({
setDeviceGeolocation: () => {},
})
DeviceGeolocationAPIContext.displayName = 'DeviceGeolocationAPIContext'
const GeolocationConfigContext = React.createContext<GeolocationConfigContext>({
config: DEFAULT_GEOLOCATION_CONFIG,
})
GeolocationConfigContext.displayName = 'GeolocationConfigContext'
const GeolocationStatusContext = React.createContext<GeolocationStatusContext>({
location: {
countryCode: undefined,
regionCode: undefined,
},
status: DEFAULT_GEOLOCATION_STATUS,
})
GeolocationStatusContext.displayName = 'GeolocationStatusContext'
/**
* Provider of geolocation config and computed geolocation status.
*/
export function GeolocationStatusProvider({
children,
}: {
children: React.ReactNode
}) {
const {deviceGeolocation} = React.useContext(DeviceGeolocationContext)
const [config, setConfig] = React.useState(() => {
const initial = device.get(['geolocation']) || DEFAULT_GEOLOCATION_CONFIG
return initial
})
React.useEffect(() => {
return onGeolocationConfigUpdate(config => {
setConfig(config!)
})
}, [])
const configContext = React.useMemo(() => ({config}), [config])
const statusContext = React.useMemo(() => {
if (deviceGeolocation?.countryCode) {
logger.debug('has device geolocation available')
}
const geolocation = mergeGeolocation(deviceGeolocation, config)
const status = computeGeolocationStatus(geolocation, config)
// ensure this remains debug and never leaves device
logger.debug('result', {deviceGeolocation, geolocation, status, config})
return {location: geolocation, status}
}, [config, deviceGeolocation])
return (
<GeolocationConfigContext.Provider value={configContext}>
<GeolocationStatusContext.Provider value={statusContext}>
{children}
</GeolocationStatusContext.Provider>
</GeolocationConfigContext.Provider>
)
}
/**
* Provider of providers. Provides device geolocation data to lower-level
* `GeolocationStatusProvider`, and device geolocation APIs to children.
*/
export function Provider({children}: {children: React.ReactNode}) {
const [deviceGeolocation, setDeviceGeolocation] = useSyncedDeviceGeolocation()
const handleSetDeviceGeolocation = React.useCallback(
(location: DeviceLocation) => {
logger.debug('setting device geolocation')
setDeviceGeolocation({
countryCode: location.countryCode ?? undefined,
regionCode: location.regionCode ?? undefined,
})
},
[setDeviceGeolocation],
)
return (
<DeviceGeolocationAPIContext.Provider
value={React.useMemo(
() => ({setDeviceGeolocation: handleSetDeviceGeolocation}),
[handleSetDeviceGeolocation],
)}>
<DeviceGeolocationContext.Provider
value={React.useMemo(() => ({deviceGeolocation}), [deviceGeolocation])}>
<GeolocationStatusProvider>{children}</GeolocationStatusProvider>
</DeviceGeolocationContext.Provider>
</DeviceGeolocationAPIContext.Provider>
)
}
export function useDeviceGeolocationApi() {
return React.useContext(DeviceGeolocationAPIContext)
}
export function useGeolocationConfig() {
return React.useContext(GeolocationConfigContext)
}
export function useGeolocationStatus() {
return React.useContext(GeolocationStatusContext)
}
|