@garagem-ai/site-sdk 0.10.1 → 0.11.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/dist/react.mjs CHANGED
@@ -1429,7 +1429,8 @@ function emptyMapSearchResult(pageSize = MAP_SEARCH_DEFAULT_PAGE_SIZE) {
1429
1429
  page: 1,
1430
1430
  pageSize,
1431
1431
  total: 0,
1432
- totalPages: 0
1432
+ totalPages: 0,
1433
+ buckets: []
1433
1434
  };
1434
1435
  }
1435
1436
  function mapSearchPins(listings) {
@@ -1448,21 +1449,24 @@ function mapSearchPins(listings) {
1448
1449
  }
1449
1450
  return pins;
1450
1451
  }
1451
- function listingsResult(listings, meta, requestedPageSize) {
1452
+ function listingsResult(result, requestedPageSize) {
1453
+ const listings = result.listings ?? [];
1452
1454
  return {
1453
1455
  pins: mapSearchPins(listings),
1454
1456
  cards: listings.map((listing) => toListingCardModel(listing)),
1455
1457
  listings,
1456
- page: meta.page ?? 1,
1457
- pageSize: meta.limit ?? requestedPageSize,
1458
- total: meta.total ?? listings.length,
1459
- totalPages: meta.totalPages ?? 0
1458
+ page: result.page ?? 1,
1459
+ pageSize: result.limit ?? requestedPageSize,
1460
+ total: result.total ?? listings.length,
1461
+ totalPages: result.totalPages ?? 0,
1462
+ buckets: result.buckets ?? []
1460
1463
  };
1461
1464
  }
1462
1465
  function searchParams(area, options) {
1463
1466
  const params = {
1464
1467
  page: capMapPage(options.page),
1465
- limit: capMapPageSize(options.pageSize)
1468
+ limit: capMapPageSize(options.pageSize),
1469
+ buckets: true
1466
1470
  };
1467
1471
  if (options.q !== void 0) params.q = options.q;
1468
1472
  if (options.sort !== void 0) params.sort = options.sort;
@@ -1483,7 +1487,7 @@ async function runMapSearch(siteId, area, options) {
1483
1487
  publishableKey: options.publishableKey,
1484
1488
  next: options.next
1485
1489
  });
1486
- return listingsResult(result.listings ?? [], result, pageSize);
1490
+ return listingsResult(result, pageSize);
1487
1491
  }
1488
1492
  async function searchListingsInBounds(siteId, options) {
1489
1493
  if (!isMapBounds(options.bounds)) {
@@ -1508,63 +1512,63 @@ function clampZoom(zoom) {
1508
1512
  function mapClusterCellDegrees(zoom) {
1509
1513
  return 360 / 2 ** clampZoom(zoom) / 4;
1510
1514
  }
1511
- function usablePin(pin) {
1512
- return finitePair(pin.lat, pin.lng) !== null && !(pin.lat === 0 && pin.lng === 0);
1515
+ function usablePoint(point) {
1516
+ return finitePair(point.lat, point.lng) !== null && !(point.lat === 0 && point.lng === 0);
1513
1517
  }
1514
1518
  function groupPrecision(group) {
1515
1519
  let max = 0;
1516
- for (const pin of group) {
1520
+ for (const point of group) {
1517
1521
  max = Math.max(
1518
1522
  max,
1519
- coordinateDecimals(pin.lat),
1520
- coordinateDecimals(pin.lng)
1523
+ coordinateDecimals(point.lat),
1524
+ coordinateDecimals(point.lng)
1521
1525
  );
1522
1526
  }
1523
1527
  return max;
1524
1528
  }
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 [];
1529
+ function gridCells(points, zoom, weight) {
1550
1530
  const cell = mapClusterCellDegrees(zoom);
1551
1531
  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)}`;
1532
+ for (const point of points) {
1533
+ if (!usablePoint(point)) continue;
1534
+ const key = `${Math.floor(point.lat / cell)}:${Math.floor(point.lng / cell)}`;
1555
1535
  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)
1536
+ if (list) list.push(point);
1537
+ else groups.set(key, [point]);
1538
+ }
1539
+ return [...groups].map(([key, members]) => {
1540
+ const count = members.reduce((sum, point) => sum + weight(point), 0);
1541
+ const only = members.length === 1 ? members[0] : void 0;
1542
+ const center = only ?? coarsenCoordinates(
1543
+ {
1544
+ lat: members.reduce((sum, p) => sum + p.lat * weight(p), 0) / count,
1545
+ lng: members.reduce((sum, p) => sum + p.lng * weight(p), 0) / count
1546
+ },
1547
+ groupPrecision(members)
1564
1548
  );
1565
- }
1566
- clusters.sort((a, b) => a.id.localeCompare(b.id));
1567
- return clusters;
1549
+ return {
1550
+ id: only?.id ?? key,
1551
+ lat: center.lat,
1552
+ lng: center.lng,
1553
+ count,
1554
+ pinIds: [],
1555
+ pin: null,
1556
+ members
1557
+ };
1558
+ }).sort((a, b) => a.id.localeCompare(b.id));
1559
+ }
1560
+ function clusterPins(pins, zoom) {
1561
+ return gridCells(pins, zoom, () => 1).map(({ members, ...cluster }) => ({
1562
+ ...cluster,
1563
+ pinIds: members.map((pin) => pin.id),
1564
+ pin: members.length === 1 ? members[0] ?? null : null
1565
+ }));
1566
+ }
1567
+ function clusterBuckets(buckets, zoom) {
1568
+ const weighted = buckets.filter((bucket) => bucket.count > 0);
1569
+ return gridCells(weighted, zoom, (bucket) => bucket.count).map(
1570
+ ({ members: _members, ...cluster }) => cluster
1571
+ );
1568
1572
  }
1569
1573
  function describeArea(query) {
1570
1574
  if (query.polygon != null) {
@@ -1715,8 +1719,8 @@ function useMapSearch(options) {
1715
1719
  );
1716
1720
  }, [key]);
1717
1721
  const clusters = useMemo2(
1718
- () => clusterPins(result.pins, zoom),
1719
- [result.pins, zoom]
1722
+ () => result.buckets.length > 0 ? clusterBuckets(result.buckets, zoom) : clusterPins(result.pins, zoom),
1723
+ [result.buckets, result.pins, zoom]
1720
1724
  );
1721
1725
  return { ...result, clusters, loading, error };
1722
1726
  }