@garagem-ai/site-sdk 0.8.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +162 -5
- package/dist/index.d.mts +130 -43
- package/dist/index.d.ts +130 -43
- package/dist/index.js +744 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +705 -2
- package/dist/index.mjs.map +1 -1
- package/dist/react.d.mts +24 -2
- package/dist/react.d.ts +24 -2
- package/dist/react.js +499 -12
- package/dist/react.js.map +1 -1
- package/dist/react.mjs +498 -12
- package/dist/react.mjs.map +1 -1
- package/dist/{types-photo-search-CmGRj6my.d.mts → types-photo-search-D88oevZv.d.mts} +375 -1
- package/dist/{types-photo-search-CmGRj6my.d.ts → types-photo-search-D88oevZv.d.ts} +375 -1
- package/package.json +1 -1
- package/src/fetchers.ts +4 -1
- package/src/index.ts +89 -0
- package/src/map-search.ts +487 -0
- package/src/maps.ts +743 -0
- package/src/react/index.ts +2 -0
- package/src/react/use-map-search.ts +101 -0
- package/src/studio-contract.ts +46 -0
- package/src/types-maps.ts +275 -0
- package/src/types.ts +12 -0
package/dist/react.mjs
CHANGED
|
@@ -4,6 +4,17 @@
|
|
|
4
4
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
5
5
|
import { usePathname, useRouter } from "next/navigation";
|
|
6
6
|
|
|
7
|
+
// src/listing-path.ts
|
|
8
|
+
function listingPublicPath(listing) {
|
|
9
|
+
const composed = listing.path?.trim() ?? "";
|
|
10
|
+
if (composed.startsWith("/") && !composed.startsWith("//")) return composed;
|
|
11
|
+
const identity = listing.listingCode?.trim() || listing.id?.trim() || "";
|
|
12
|
+
if (!identity || identity === "undefined" || identity === "null") {
|
|
13
|
+
throw new TypeError("listingPublicPath: listing has no listingCode or id");
|
|
14
|
+
}
|
|
15
|
+
return `/imoveis/id-${identity}`;
|
|
16
|
+
}
|
|
17
|
+
|
|
7
18
|
// src/internal/sdk-http.ts
|
|
8
19
|
var PUBLISHABLE_KEY_HEADER = "X-Garagem-Publishable-Key";
|
|
9
20
|
function publishableKeyHeaders(publishableKey) {
|
|
@@ -163,7 +174,7 @@ async function getSearchUi(options) {
|
|
|
163
174
|
);
|
|
164
175
|
}
|
|
165
176
|
async function queryListings(options) {
|
|
166
|
-
const { params, baseUrl, fetch: fetchImpl, next } = options;
|
|
177
|
+
const { params, baseUrl, fetch: fetchImpl, next, signal } = options;
|
|
167
178
|
const siteId = await coerceSiteId(options.siteId);
|
|
168
179
|
const base = normalizeBaseUrl(baseUrl);
|
|
169
180
|
const url = `${base}/api/sites/${encodeURIComponent(siteId)}/search/listings`;
|
|
@@ -176,7 +187,8 @@ async function queryListings(options) {
|
|
|
176
187
|
"Content-Type": "application/json"
|
|
177
188
|
},
|
|
178
189
|
body: JSON.stringify(params ?? {}),
|
|
179
|
-
next
|
|
190
|
+
next,
|
|
191
|
+
signal
|
|
180
192
|
},
|
|
181
193
|
fetchImpl ?? fetch,
|
|
182
194
|
options.publishableKey
|
|
@@ -630,6 +642,19 @@ function pathFiltersToQueryFilters(pathFilters) {
|
|
|
630
642
|
}
|
|
631
643
|
|
|
632
644
|
// src/presentation.ts
|
|
645
|
+
function formatListingPrice(amount, locale = "pt-BR", currency = "BRL") {
|
|
646
|
+
if (amount == null) return "Sob consulta";
|
|
647
|
+
try {
|
|
648
|
+
return new Intl.NumberFormat(locale, {
|
|
649
|
+
style: "currency",
|
|
650
|
+
currency,
|
|
651
|
+
minimumFractionDigits: 0,
|
|
652
|
+
maximumFractionDigits: 0
|
|
653
|
+
}).format(amount);
|
|
654
|
+
} catch {
|
|
655
|
+
return `${currency} ${amount}`;
|
|
656
|
+
}
|
|
657
|
+
}
|
|
633
658
|
function parseGroupedInteger(value) {
|
|
634
659
|
if (typeof value === "number") {
|
|
635
660
|
return Number.isFinite(value) ? Math.trunc(value) : void 0;
|
|
@@ -639,6 +664,9 @@ function parseGroupedInteger(value) {
|
|
|
639
664
|
const amount = Number.parseInt(digits, 10);
|
|
640
665
|
return Number.isFinite(amount) ? amount : void 0;
|
|
641
666
|
}
|
|
667
|
+
function listingBusinessLabel(businessType) {
|
|
668
|
+
return businessType === "rental" ? "Aluguel" : "Venda";
|
|
669
|
+
}
|
|
642
670
|
|
|
643
671
|
// src/search-engine.ts
|
|
644
672
|
var PATH_FILTER_IDS = {
|
|
@@ -1236,9 +1264,466 @@ function usePublicSearch(options) {
|
|
|
1236
1264
|
};
|
|
1237
1265
|
}
|
|
1238
1266
|
|
|
1239
|
-
// src/react/use-
|
|
1267
|
+
// src/react/use-map-search.ts
|
|
1240
1268
|
import { useEffect as useEffect2, useMemo as useMemo2, useRef as useRef2, useState as useState2 } from "react";
|
|
1241
1269
|
|
|
1270
|
+
// src/listing-card-model.ts
|
|
1271
|
+
function listingLocationLabel(location) {
|
|
1272
|
+
return [location.neighborhood, location.city, location.state].map((part) => part ? String(part).trim() : "").filter(Boolean).join(", ");
|
|
1273
|
+
}
|
|
1274
|
+
function toListingCardModel(listing) {
|
|
1275
|
+
const isPrivate = listing.priceVisibility === "private";
|
|
1276
|
+
const area = listing.property?.usableArea ?? listing.property?.totalArea;
|
|
1277
|
+
return {
|
|
1278
|
+
href: listingPublicPath(listing),
|
|
1279
|
+
title: listing.title,
|
|
1280
|
+
businessType: listing.businessType,
|
|
1281
|
+
businessLabel: listingBusinessLabel(listing.businessType),
|
|
1282
|
+
priceVisibility: listing.priceVisibility,
|
|
1283
|
+
price: listing.price,
|
|
1284
|
+
priceLabel: isPrivate ? "Sob consulta" : formatListingPrice(listing.price, "pt-BR", listing.currency ?? "BRL"),
|
|
1285
|
+
pricePeriod: listing.businessType === "rental" && !isPrivate && listing.price != null ? "/m\xEAs" : null,
|
|
1286
|
+
locationLabel: listingLocationLabel(listing.location || {}),
|
|
1287
|
+
propertyType: listing.property?.type ?? null,
|
|
1288
|
+
coverSrc: listing.coverImage?.src ?? null,
|
|
1289
|
+
coverAlt: listing.coverImage?.alt ?? listing.title,
|
|
1290
|
+
bedrooms: listing.property?.bedrooms ?? null,
|
|
1291
|
+
bathrooms: listing.property?.bathrooms ?? null,
|
|
1292
|
+
parkingSpaces: listing.property?.parkingSpaces ?? null,
|
|
1293
|
+
areaM2: area ?? null
|
|
1294
|
+
};
|
|
1295
|
+
}
|
|
1296
|
+
|
|
1297
|
+
// src/maps.ts
|
|
1298
|
+
function finite(value) {
|
|
1299
|
+
return typeof value === "number" && Number.isFinite(value) ? value : null;
|
|
1300
|
+
}
|
|
1301
|
+
function listingCoordinates(listing) {
|
|
1302
|
+
const raw = listing?.location?.coordinates;
|
|
1303
|
+
const lat = finite(raw?.lat);
|
|
1304
|
+
const lng = finite(raw?.lng);
|
|
1305
|
+
if (lat == null || lng == null) return null;
|
|
1306
|
+
if (lat === 0 && lng === 0) return null;
|
|
1307
|
+
if (Math.abs(lat) > 90 || Math.abs(lng) > 180) return null;
|
|
1308
|
+
return { lat, lng };
|
|
1309
|
+
}
|
|
1310
|
+
function coordinateDecimals(value) {
|
|
1311
|
+
if (!Number.isFinite(value)) return 0;
|
|
1312
|
+
const text = Math.abs(value).toString();
|
|
1313
|
+
if (text.includes("e")) {
|
|
1314
|
+
const fixed = Math.abs(value).toFixed(12).replace(/0+$/, "");
|
|
1315
|
+
const dot2 = fixed.indexOf(".");
|
|
1316
|
+
return dot2 === -1 ? 0 : fixed.length - dot2 - 1;
|
|
1317
|
+
}
|
|
1318
|
+
const dot = text.indexOf(".");
|
|
1319
|
+
return dot === -1 ? 0 : text.length - dot - 1;
|
|
1320
|
+
}
|
|
1321
|
+
function roundTo(value, decimals) {
|
|
1322
|
+
const factor = 10 ** decimals;
|
|
1323
|
+
return Math.round(value * factor) / factor;
|
|
1324
|
+
}
|
|
1325
|
+
function coarsenCoordinates(coordinates, maxDecimals) {
|
|
1326
|
+
if (maxDecimals == null || !Number.isFinite(maxDecimals)) return coordinates;
|
|
1327
|
+
const limit = Math.max(0, Math.floor(maxDecimals));
|
|
1328
|
+
return {
|
|
1329
|
+
lat: coordinateDecimals(coordinates.lat) > limit ? roundTo(coordinates.lat, limit) : coordinates.lat,
|
|
1330
|
+
lng: coordinateDecimals(coordinates.lng) > limit ? roundTo(coordinates.lng, limit) : coordinates.lng
|
|
1331
|
+
};
|
|
1332
|
+
}
|
|
1333
|
+
function createTravelTimeCache(options = {}) {
|
|
1334
|
+
const maxEntries = Math.max(1, Math.floor(options.maxEntries ?? 200));
|
|
1335
|
+
const ttlMs = Math.max(0, options.ttlMs ?? 15 * 60 * 1e3);
|
|
1336
|
+
const entries = /* @__PURE__ */ new Map();
|
|
1337
|
+
return {
|
|
1338
|
+
get(key) {
|
|
1339
|
+
const hit = entries.get(key);
|
|
1340
|
+
if (!hit) return void 0;
|
|
1341
|
+
if (hit.expiresAt <= Date.now()) {
|
|
1342
|
+
entries.delete(key);
|
|
1343
|
+
return void 0;
|
|
1344
|
+
}
|
|
1345
|
+
entries.delete(key);
|
|
1346
|
+
entries.set(key, hit);
|
|
1347
|
+
return hit.value;
|
|
1348
|
+
},
|
|
1349
|
+
set(key, value) {
|
|
1350
|
+
entries.delete(key);
|
|
1351
|
+
entries.set(key, { value, expiresAt: Date.now() + ttlMs });
|
|
1352
|
+
while (entries.size > maxEntries) {
|
|
1353
|
+
const oldest = entries.keys().next().value;
|
|
1354
|
+
if (oldest === void 0) break;
|
|
1355
|
+
entries.delete(oldest);
|
|
1356
|
+
}
|
|
1357
|
+
},
|
|
1358
|
+
clear() {
|
|
1359
|
+
entries.clear();
|
|
1360
|
+
},
|
|
1361
|
+
get size() {
|
|
1362
|
+
return entries.size;
|
|
1363
|
+
}
|
|
1364
|
+
};
|
|
1365
|
+
}
|
|
1366
|
+
var defaultTravelTimeCache = createTravelTimeCache();
|
|
1367
|
+
|
|
1368
|
+
// src/map-search.ts
|
|
1369
|
+
var MAP_SEARCH_MAX_PAGE_SIZE = 100;
|
|
1370
|
+
var MAP_SEARCH_DEFAULT_PAGE_SIZE = 100;
|
|
1371
|
+
var MAP_SEARCH_MAX_PAGE = 1e4;
|
|
1372
|
+
var MAP_VIEWPORT_DEBOUNCE_MS = 400;
|
|
1373
|
+
var MAP_POLYGON_MAX_VERTICES = 64;
|
|
1374
|
+
function capMapPageSize(value) {
|
|
1375
|
+
if (value == null || !Number.isFinite(value) || value <= 0) {
|
|
1376
|
+
return MAP_SEARCH_DEFAULT_PAGE_SIZE;
|
|
1377
|
+
}
|
|
1378
|
+
return Math.min(MAP_SEARCH_MAX_PAGE_SIZE, Math.floor(value));
|
|
1379
|
+
}
|
|
1380
|
+
function capMapPage(value) {
|
|
1381
|
+
if (value == null || !Number.isFinite(value) || value <= 1) return 1;
|
|
1382
|
+
return Math.min(MAP_SEARCH_MAX_PAGE, Math.floor(value));
|
|
1383
|
+
}
|
|
1384
|
+
function finitePair(lat, lng) {
|
|
1385
|
+
if (typeof lat !== "number" || typeof lng !== "number") return null;
|
|
1386
|
+
if (!Number.isFinite(lat) || !Number.isFinite(lng)) return null;
|
|
1387
|
+
if (lat < -90 || lat > 90 || lng < -180 || lng > 180) return null;
|
|
1388
|
+
return [lat, lng];
|
|
1389
|
+
}
|
|
1390
|
+
function isMapBounds(bounds) {
|
|
1391
|
+
if (!bounds) return false;
|
|
1392
|
+
const north = finitePair(bounds.north, bounds.east);
|
|
1393
|
+
const south = finitePair(bounds.south, bounds.west);
|
|
1394
|
+
if (!north || !south) return false;
|
|
1395
|
+
return bounds.north > bounds.south && bounds.east > bounds.west;
|
|
1396
|
+
}
|
|
1397
|
+
function isMapPolygon(polygon) {
|
|
1398
|
+
if (!Array.isArray(polygon)) return false;
|
|
1399
|
+
if (polygon.length < 3 || polygon.length > MAP_POLYGON_MAX_VERTICES + 1) {
|
|
1400
|
+
return false;
|
|
1401
|
+
}
|
|
1402
|
+
let unique = 0;
|
|
1403
|
+
let prev = null;
|
|
1404
|
+
for (const point of polygon) {
|
|
1405
|
+
if (!Array.isArray(point)) return false;
|
|
1406
|
+
const pair = finitePair(point[0], point[1]);
|
|
1407
|
+
if (!pair) return false;
|
|
1408
|
+
if (prev && prev[0] === pair[0] && prev[1] === pair[1]) continue;
|
|
1409
|
+
prev = pair;
|
|
1410
|
+
unique += 1;
|
|
1411
|
+
}
|
|
1412
|
+
if (unique >= 2 && polygon.length >= 2) {
|
|
1413
|
+
const first = finitePair(polygon[0]?.[0], polygon[0]?.[1]);
|
|
1414
|
+
const last = finitePair(
|
|
1415
|
+
polygon[polygon.length - 1]?.[0],
|
|
1416
|
+
polygon[polygon.length - 1]?.[1]
|
|
1417
|
+
);
|
|
1418
|
+
if (first && last && first[0] === last[0] && first[1] === last[1] && unique > 1) {
|
|
1419
|
+
unique -= 1;
|
|
1420
|
+
}
|
|
1421
|
+
}
|
|
1422
|
+
return unique >= 3 && unique <= MAP_POLYGON_MAX_VERTICES;
|
|
1423
|
+
}
|
|
1424
|
+
function emptyMapSearchResult(pageSize = MAP_SEARCH_DEFAULT_PAGE_SIZE) {
|
|
1425
|
+
return {
|
|
1426
|
+
pins: [],
|
|
1427
|
+
cards: [],
|
|
1428
|
+
listings: [],
|
|
1429
|
+
page: 1,
|
|
1430
|
+
pageSize,
|
|
1431
|
+
total: 0,
|
|
1432
|
+
totalPages: 0
|
|
1433
|
+
};
|
|
1434
|
+
}
|
|
1435
|
+
function mapSearchPins(listings) {
|
|
1436
|
+
const pins = [];
|
|
1437
|
+
for (const listing of listings) {
|
|
1438
|
+
const coords = listingCoordinates(listing);
|
|
1439
|
+
if (!coords || !listing.id) continue;
|
|
1440
|
+
const src = listing.coverImage?.src;
|
|
1441
|
+
pins.push({
|
|
1442
|
+
id: listing.id,
|
|
1443
|
+
lat: coords.lat,
|
|
1444
|
+
lng: coords.lng,
|
|
1445
|
+
price: listing.price ?? null,
|
|
1446
|
+
coverImage: src ? src : null
|
|
1447
|
+
});
|
|
1448
|
+
}
|
|
1449
|
+
return pins;
|
|
1450
|
+
}
|
|
1451
|
+
function listingsResult(listings, meta, requestedPageSize) {
|
|
1452
|
+
return {
|
|
1453
|
+
pins: mapSearchPins(listings),
|
|
1454
|
+
cards: listings.map((listing) => toListingCardModel(listing)),
|
|
1455
|
+
listings,
|
|
1456
|
+
page: meta.page ?? 1,
|
|
1457
|
+
pageSize: meta.limit ?? requestedPageSize,
|
|
1458
|
+
total: meta.total ?? listings.length,
|
|
1459
|
+
totalPages: meta.totalPages ?? 0
|
|
1460
|
+
};
|
|
1461
|
+
}
|
|
1462
|
+
function searchParams(area, options) {
|
|
1463
|
+
const params = {
|
|
1464
|
+
page: capMapPage(options.page),
|
|
1465
|
+
limit: capMapPageSize(options.pageSize)
|
|
1466
|
+
};
|
|
1467
|
+
if (options.q !== void 0) params.q = options.q;
|
|
1468
|
+
if (options.sort !== void 0) params.sort = options.sort;
|
|
1469
|
+
if (options.location !== void 0) params.location = options.location;
|
|
1470
|
+
if (options.filters) params.filters = options.filters;
|
|
1471
|
+
if (area.polygon) params.polygon = area.polygon;
|
|
1472
|
+
else if (area.bounds) params.bounds = area.bounds;
|
|
1473
|
+
return params;
|
|
1474
|
+
}
|
|
1475
|
+
async function runMapSearch(siteId, area, options) {
|
|
1476
|
+
const pageSize = capMapPageSize(options.pageSize);
|
|
1477
|
+
const result = await queryListings({
|
|
1478
|
+
siteId,
|
|
1479
|
+
params: searchParams(area, options),
|
|
1480
|
+
baseUrl: options.baseUrl,
|
|
1481
|
+
fetch: options.fetch,
|
|
1482
|
+
signal: options.signal,
|
|
1483
|
+
publishableKey: options.publishableKey,
|
|
1484
|
+
next: options.next
|
|
1485
|
+
});
|
|
1486
|
+
return listingsResult(result.listings ?? [], result, pageSize);
|
|
1487
|
+
}
|
|
1488
|
+
async function searchListingsInBounds(siteId, options) {
|
|
1489
|
+
if (!isMapBounds(options.bounds)) {
|
|
1490
|
+
throw new TypeError(
|
|
1491
|
+
"searchListingsInBounds: bounds must be a finite rectangle with north > south and east > west (no antimeridian)."
|
|
1492
|
+
);
|
|
1493
|
+
}
|
|
1494
|
+
return runMapSearch(siteId, { bounds: options.bounds }, options);
|
|
1495
|
+
}
|
|
1496
|
+
async function searchListingsInPolygon(siteId, options) {
|
|
1497
|
+
if (!isMapPolygon(options.polygon)) {
|
|
1498
|
+
throw new TypeError(
|
|
1499
|
+
`searchListingsInPolygon: polygon must be 3\u2013${MAP_POLYGON_MAX_VERTICES} finite [lat, lng] points.`
|
|
1500
|
+
);
|
|
1501
|
+
}
|
|
1502
|
+
return runMapSearch(siteId, { polygon: options.polygon }, options);
|
|
1503
|
+
}
|
|
1504
|
+
function clampZoom(zoom) {
|
|
1505
|
+
if (!Number.isFinite(zoom)) return 0;
|
|
1506
|
+
return Math.min(22, Math.max(0, zoom));
|
|
1507
|
+
}
|
|
1508
|
+
function mapClusterCellDegrees(zoom) {
|
|
1509
|
+
return 360 / 2 ** clampZoom(zoom) / 4;
|
|
1510
|
+
}
|
|
1511
|
+
function usablePin(pin) {
|
|
1512
|
+
return finitePair(pin.lat, pin.lng) !== null && !(pin.lat === 0 && pin.lng === 0);
|
|
1513
|
+
}
|
|
1514
|
+
function groupPrecision(group) {
|
|
1515
|
+
let min = 12;
|
|
1516
|
+
for (const pin of group) {
|
|
1517
|
+
min = Math.min(
|
|
1518
|
+
min,
|
|
1519
|
+
coordinateDecimals(pin.lat),
|
|
1520
|
+
coordinateDecimals(pin.lng)
|
|
1521
|
+
);
|
|
1522
|
+
}
|
|
1523
|
+
return min;
|
|
1524
|
+
}
|
|
1525
|
+
function multiCluster(key, group) {
|
|
1526
|
+
const lat = group.reduce((sum, pin) => sum + pin.lat, 0) / group.length;
|
|
1527
|
+
const lng = group.reduce((sum, pin) => sum + pin.lng, 0) / group.length;
|
|
1528
|
+
const center = coarsenCoordinates({ lat, lng }, groupPrecision(group));
|
|
1529
|
+
return {
|
|
1530
|
+
id: key,
|
|
1531
|
+
lat: center.lat,
|
|
1532
|
+
lng: center.lng,
|
|
1533
|
+
count: group.length,
|
|
1534
|
+
pinIds: group.map((pin) => pin.id),
|
|
1535
|
+
pin: null
|
|
1536
|
+
};
|
|
1537
|
+
}
|
|
1538
|
+
function singleCluster(pin) {
|
|
1539
|
+
return {
|
|
1540
|
+
id: pin.id,
|
|
1541
|
+
lat: pin.lat,
|
|
1542
|
+
lng: pin.lng,
|
|
1543
|
+
count: 1,
|
|
1544
|
+
pinIds: [pin.id],
|
|
1545
|
+
pin
|
|
1546
|
+
};
|
|
1547
|
+
}
|
|
1548
|
+
function clusterPins(pins, zoom) {
|
|
1549
|
+
if (pins.length === 0) return [];
|
|
1550
|
+
const cell = mapClusterCellDegrees(zoom);
|
|
1551
|
+
const groups = /* @__PURE__ */ new Map();
|
|
1552
|
+
for (const pin of pins) {
|
|
1553
|
+
if (!usablePin(pin)) continue;
|
|
1554
|
+
const key = `${Math.floor(pin.lat / cell)}:${Math.floor(pin.lng / cell)}`;
|
|
1555
|
+
const list = groups.get(key);
|
|
1556
|
+
if (list) list.push(pin);
|
|
1557
|
+
else groups.set(key, [pin]);
|
|
1558
|
+
}
|
|
1559
|
+
const clusters = [];
|
|
1560
|
+
for (const [key, group] of groups) {
|
|
1561
|
+
const only = group[0];
|
|
1562
|
+
clusters.push(
|
|
1563
|
+
group.length === 1 && only ? singleCluster(only) : multiCluster(key, group)
|
|
1564
|
+
);
|
|
1565
|
+
}
|
|
1566
|
+
clusters.sort((a, b) => a.id.localeCompare(b.id));
|
|
1567
|
+
return clusters;
|
|
1568
|
+
}
|
|
1569
|
+
function describeArea(query) {
|
|
1570
|
+
if (query.polygon != null) {
|
|
1571
|
+
if (!isMapPolygon(query.polygon)) {
|
|
1572
|
+
return {
|
|
1573
|
+
kind: "invalid",
|
|
1574
|
+
message: `Map search polygon must be 3\u2013${MAP_POLYGON_MAX_VERTICES} [lat, lng] points.`
|
|
1575
|
+
};
|
|
1576
|
+
}
|
|
1577
|
+
return { kind: "polygon", polygon: query.polygon };
|
|
1578
|
+
}
|
|
1579
|
+
if (query.bounds != null) {
|
|
1580
|
+
if (!isMapBounds(query.bounds)) {
|
|
1581
|
+
return {
|
|
1582
|
+
kind: "invalid",
|
|
1583
|
+
message: "Map search bounds must be a finite rectangle with north > south and east > west."
|
|
1584
|
+
};
|
|
1585
|
+
}
|
|
1586
|
+
return { kind: "bounds", bounds: query.bounds };
|
|
1587
|
+
}
|
|
1588
|
+
return { kind: "empty" };
|
|
1589
|
+
}
|
|
1590
|
+
function isAbort(error, signal) {
|
|
1591
|
+
if (signal.aborted) return true;
|
|
1592
|
+
return error instanceof Error && error.name === "AbortError";
|
|
1593
|
+
}
|
|
1594
|
+
function asError(error) {
|
|
1595
|
+
return error instanceof Error ? error : new Error(String(error));
|
|
1596
|
+
}
|
|
1597
|
+
function requestFromSubscription(query, signal) {
|
|
1598
|
+
const {
|
|
1599
|
+
siteId: _siteId,
|
|
1600
|
+
bounds: _bounds,
|
|
1601
|
+
polygon: _polygon,
|
|
1602
|
+
...request
|
|
1603
|
+
} = query;
|
|
1604
|
+
return { ...request, signal };
|
|
1605
|
+
}
|
|
1606
|
+
async function fetchArea(query, area, signal) {
|
|
1607
|
+
const request = requestFromSubscription(query, signal);
|
|
1608
|
+
if (area.kind === "polygon") {
|
|
1609
|
+
return searchListingsInPolygon(query.siteId, {
|
|
1610
|
+
...request,
|
|
1611
|
+
polygon: area.polygon
|
|
1612
|
+
});
|
|
1613
|
+
}
|
|
1614
|
+
return searchListingsInBounds(query.siteId, {
|
|
1615
|
+
...request,
|
|
1616
|
+
bounds: area.bounds
|
|
1617
|
+
});
|
|
1618
|
+
}
|
|
1619
|
+
function mapSearchSubscriptionKey(query) {
|
|
1620
|
+
return JSON.stringify({
|
|
1621
|
+
siteId: query.siteId,
|
|
1622
|
+
baseUrl: query.baseUrl ?? "",
|
|
1623
|
+
bounds: query.bounds ?? null,
|
|
1624
|
+
polygon: query.polygon ?? null,
|
|
1625
|
+
q: query.q ?? null,
|
|
1626
|
+
sort: query.sort ?? null,
|
|
1627
|
+
location: query.location ?? null,
|
|
1628
|
+
filters: query.filters ?? null,
|
|
1629
|
+
publishableKey: query.publishableKey ?? null,
|
|
1630
|
+
next: query.next ?? null,
|
|
1631
|
+
page: query.page ?? 1,
|
|
1632
|
+
pageSize: query.pageSize ?? MAP_SEARCH_DEFAULT_PAGE_SIZE
|
|
1633
|
+
});
|
|
1634
|
+
}
|
|
1635
|
+
function subscribeMapSearch(query, listener, options) {
|
|
1636
|
+
const area = describeArea(query);
|
|
1637
|
+
if (area.kind === "empty") {
|
|
1638
|
+
listener({ type: "empty" });
|
|
1639
|
+
return () => {
|
|
1640
|
+
};
|
|
1641
|
+
}
|
|
1642
|
+
if (area.kind === "invalid") {
|
|
1643
|
+
listener({ type: "error", error: new TypeError(area.message) });
|
|
1644
|
+
return () => {
|
|
1645
|
+
};
|
|
1646
|
+
}
|
|
1647
|
+
const controller = new AbortController();
|
|
1648
|
+
const debounceMs = options?.debounceMs ?? MAP_VIEWPORT_DEBOUNCE_MS;
|
|
1649
|
+
const timer = setTimeout(() => {
|
|
1650
|
+
listener({ type: "loading" });
|
|
1651
|
+
void fetchArea(query, area, controller.signal).then(
|
|
1652
|
+
(result) => {
|
|
1653
|
+
if (controller.signal.aborted) return;
|
|
1654
|
+
listener({ type: "ready", result });
|
|
1655
|
+
},
|
|
1656
|
+
(error) => {
|
|
1657
|
+
if (isAbort(error, controller.signal)) return;
|
|
1658
|
+
listener({ type: "error", error: asError(error) });
|
|
1659
|
+
}
|
|
1660
|
+
);
|
|
1661
|
+
}, debounceMs);
|
|
1662
|
+
return () => {
|
|
1663
|
+
clearTimeout(timer);
|
|
1664
|
+
controller.abort();
|
|
1665
|
+
};
|
|
1666
|
+
}
|
|
1667
|
+
|
|
1668
|
+
// src/react/use-map-search.ts
|
|
1669
|
+
var IDLE = emptyMapSearchResult();
|
|
1670
|
+
function areaKey(options) {
|
|
1671
|
+
return JSON.stringify({
|
|
1672
|
+
search: mapSearchSubscriptionKey(options),
|
|
1673
|
+
enabled: options.enabled !== false,
|
|
1674
|
+
debounceMs: options.debounceMs ?? MAP_VIEWPORT_DEBOUNCE_MS
|
|
1675
|
+
});
|
|
1676
|
+
}
|
|
1677
|
+
function useMapSearch(options) {
|
|
1678
|
+
const [result, setResult] = useState2(IDLE);
|
|
1679
|
+
const [loading, setLoading] = useState2(false);
|
|
1680
|
+
const [error, setError] = useState2(null);
|
|
1681
|
+
const optionsRef = useRef2(options);
|
|
1682
|
+
optionsRef.current = options;
|
|
1683
|
+
const key = areaKey(options);
|
|
1684
|
+
const zoom = options.zoom ?? 14;
|
|
1685
|
+
useEffect2(() => {
|
|
1686
|
+
const current = optionsRef.current;
|
|
1687
|
+
if (current.enabled === false) {
|
|
1688
|
+
setLoading(false);
|
|
1689
|
+
return;
|
|
1690
|
+
}
|
|
1691
|
+
return subscribeMapSearch(
|
|
1692
|
+
current,
|
|
1693
|
+
(event) => {
|
|
1694
|
+
if (event.type === "empty") {
|
|
1695
|
+
setResult(emptyMapSearchResult(current.pageSize));
|
|
1696
|
+
setLoading(false);
|
|
1697
|
+
setError(null);
|
|
1698
|
+
return;
|
|
1699
|
+
}
|
|
1700
|
+
if (event.type === "loading") {
|
|
1701
|
+
setLoading(true);
|
|
1702
|
+
setError(null);
|
|
1703
|
+
return;
|
|
1704
|
+
}
|
|
1705
|
+
if (event.type === "error") {
|
|
1706
|
+
setLoading(false);
|
|
1707
|
+
setError(event.error);
|
|
1708
|
+
return;
|
|
1709
|
+
}
|
|
1710
|
+
setResult(event.result);
|
|
1711
|
+
setLoading(false);
|
|
1712
|
+
setError(null);
|
|
1713
|
+
},
|
|
1714
|
+
{ debounceMs: current.debounceMs }
|
|
1715
|
+
);
|
|
1716
|
+
}, [key]);
|
|
1717
|
+
const clusters = useMemo2(
|
|
1718
|
+
() => clusterPins(result.pins, zoom),
|
|
1719
|
+
[result.pins, zoom]
|
|
1720
|
+
);
|
|
1721
|
+
return { ...result, clusters, loading, error };
|
|
1722
|
+
}
|
|
1723
|
+
|
|
1724
|
+
// src/react/use-public-photo-search.ts
|
|
1725
|
+
import { useEffect as useEffect3, useMemo as useMemo3, useRef as useRef3, useState as useState3 } from "react";
|
|
1726
|
+
|
|
1242
1727
|
// src/fetchers-photo-search.ts
|
|
1243
1728
|
var PHOTO_SEARCH_MIN_QUERY_LENGTH = 3;
|
|
1244
1729
|
var EMPTY_RESULT = {
|
|
@@ -1352,17 +1837,17 @@ function usePublicPhotoSearch(options) {
|
|
|
1352
1837
|
} = options;
|
|
1353
1838
|
const query = (options.query ?? "").trim();
|
|
1354
1839
|
const searchable = enabled && query.length >= PHOTO_SEARCH_MIN_QUERY_LENGTH;
|
|
1355
|
-
const siteIdRef =
|
|
1840
|
+
const siteIdRef = useRef3(siteId);
|
|
1356
1841
|
siteIdRef.current = siteId;
|
|
1357
1842
|
const siteIdToken = typeof siteId === "string" ? siteId : null;
|
|
1358
|
-
const fetchRef =
|
|
1843
|
+
const fetchRef = useRef3(fetchImpl);
|
|
1359
1844
|
fetchRef.current = fetchImpl;
|
|
1360
|
-
const requestRef =
|
|
1361
|
-
const [result, setResult] =
|
|
1362
|
-
const [answered, setAnswered] =
|
|
1363
|
-
const [loading, setLoading] =
|
|
1364
|
-
const [error, setError] =
|
|
1365
|
-
|
|
1845
|
+
const requestRef = useRef3(0);
|
|
1846
|
+
const [result, setResult] = useState3(EMPTY);
|
|
1847
|
+
const [answered, setAnswered] = useState3("");
|
|
1848
|
+
const [loading, setLoading] = useState3(searchable);
|
|
1849
|
+
const [error, setError] = useState3(null);
|
|
1850
|
+
useEffect3(() => {
|
|
1366
1851
|
const requestId = ++requestRef.current;
|
|
1367
1852
|
if (!searchable) {
|
|
1368
1853
|
setResult(EMPTY);
|
|
@@ -1406,7 +1891,7 @@ function usePublicPhotoSearch(options) {
|
|
|
1406
1891
|
if (timer != null) clearTimeout(timer);
|
|
1407
1892
|
};
|
|
1408
1893
|
}, [searchable, query, limit, page, baseUrl, siteIdToken, debounceMs]);
|
|
1409
|
-
const listings =
|
|
1894
|
+
const listings = useMemo3(
|
|
1410
1895
|
() => applyMatchedPhotoCover(
|
|
1411
1896
|
result.items.map((item) => item.listing),
|
|
1412
1897
|
result.items
|
|
@@ -1427,6 +1912,7 @@ function usePublicPhotoSearch(options) {
|
|
|
1427
1912
|
}
|
|
1428
1913
|
export {
|
|
1429
1914
|
PHOTO_SEARCH_DEBOUNCE_MS,
|
|
1915
|
+
useMapSearch,
|
|
1430
1916
|
usePublicPhotoSearch,
|
|
1431
1917
|
usePublicSearch
|
|
1432
1918
|
};
|