mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-05 10:53:59 +00:00
refactor: separate installation and proximity context components
This commit is contained in:
1 parent
287493dfb7
commit
3b77277883
39 files changed
+4151
-2632
No files matched your search
@@ -0,0 +1,78 @@
|
||||
/** Read mapped installations and explicit nearby-place searches through fixed endpoints. */
|
||||
export function createInstallationSource({
|
||||
fetchImpl = (...args) => globalThis.fetch(...args),
|
||||
} = {}) {
|
||||
return {
|
||||
async getMappedSites(box, { exact = false, signal } = {}) {
|
||||
const { south, west, north, east } = box || {};
|
||||
if (
|
||||
![south, west, north, east].every(Number.isFinite) ||
|
||||
south < -90 ||
|
||||
north > 90 ||
|
||||
west < -180 ||
|
||||
east > 180 ||
|
||||
north <= south ||
|
||||
east <= west ||
|
||||
north - south > 10 ||
|
||||
east - west > 10
|
||||
)
|
||||
throw new TypeError('A bounded installation viewport is required');
|
||||
signal?.throwIfAborted();
|
||||
const query = new URLSearchParams(
|
||||
Object.entries({ south, west, north, east }).map(([key, value]) => [
|
||||
key,
|
||||
value.toFixed(5),
|
||||
]),
|
||||
);
|
||||
if (exact) query.set('exact', '1');
|
||||
const response = await fetchImpl(`/api/military-installations?${query}`, {
|
||||
signal,
|
||||
});
|
||||
const body = await response.json();
|
||||
signal?.throwIfAborted();
|
||||
if (!response.ok)
|
||||
throw Object.assign(
|
||||
new Error(body?.error || `Installation feed HTTP ${response.status}`),
|
||||
{
|
||||
failureReason: ['rate_limited', 'timeout', 'query_failed'].includes(
|
||||
body?.reason,
|
||||
)
|
||||
? body.reason
|
||||
: 'unavailable',
|
||||
},
|
||||
);
|
||||
if (!Array.isArray(body?.elements))
|
||||
throw new Error('Malformed installation snapshot');
|
||||
return body;
|
||||
},
|
||||
async searchNearby({ latitude, longitude, radiusM }, { signal } = {}) {
|
||||
if (
|
||||
![latitude, longitude, radiusM].every(Number.isFinite) ||
|
||||
Math.abs(latitude) > 90 ||
|
||||
Math.abs(longitude) > 180 ||
|
||||
radiusM < 1000 ||
|
||||
radiusM > 50000
|
||||
)
|
||||
throw new TypeError('Invalid nearby installation search');
|
||||
signal?.throwIfAborted();
|
||||
const response = await fetchImpl(
|
||||
`/api/google/text-search?${new URLSearchParams({
|
||||
q: 'military installation',
|
||||
lat: latitude.toFixed(5),
|
||||
lon: longitude.toFixed(5),
|
||||
radiusM: String(radiusM),
|
||||
})}`,
|
||||
{ signal },
|
||||
);
|
||||
const payload = await response.json();
|
||||
signal?.throwIfAborted();
|
||||
if (!response.ok)
|
||||
throw new Error(
|
||||
payload?.error || `Google Places HTTP ${response.status}`,
|
||||
);
|
||||
if (!Array.isArray(payload?.places))
|
||||
throw new Error('Malformed nearby-place snapshot');
|
||||
return payload;
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user