@findlane/browser 0.33.0 → 0.35.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/index.d.ts +38 -3
- package/dist/index.js +1 -1
- package/dist/map.d.ts +10 -2
- package/dist/map.js +18 -1
- package/dist/react/use-map-search.d.ts +10 -5
- package/dist/react/use-map-search.js +32 -11
- package/package.json +7 -4
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export { imageWidth, imageWidths } from "./images.js";
|
|
2
2
|
export { placeholderCss, placeholderUrl, revealCss } from "./placeholder.js";
|
|
3
|
-
export { tilesFor } from "./map.js";
|
|
3
|
+
export { clusterFor, tilesFor } from "./map.js";
|
|
4
4
|
export type SearchRequest = {
|
|
5
5
|
q?: string;
|
|
6
6
|
/** A sort ID from the index settings, "relevance", or "distance" (with `near`). */
|
|
@@ -59,7 +59,7 @@ export type SearchRequest = {
|
|
|
59
59
|
* fields, as `facetStats` does. */
|
|
60
60
|
clusters?: {
|
|
61
61
|
cells: MapCells;
|
|
62
|
-
stats?:
|
|
62
|
+
stats?: ClusterStat[];
|
|
63
63
|
};
|
|
64
64
|
/** With `tile`: the page is each cell's best records (`perCell`, 1 by default, up to 10), by the search's ranking, rather than the
|
|
65
65
|
* top of one list, so hits spread over the map. It takes no `offset`, and `distinct` stays off. */
|
|
@@ -95,6 +95,24 @@ export type SearchRequest = {
|
|
|
95
95
|
fields?: string[];
|
|
96
96
|
apply?: "never" | "whole" | "exact";
|
|
97
97
|
};
|
|
98
|
+
/** Advanced syntax (quotes and minus) for this search (0.35.0), over the index's `advancedSyntax`: `false` searches quotes and minus
|
|
99
|
+
* signs as punctuation, as a page whose shoppers type product names with quotes in them may want; `true` reads both. */
|
|
100
|
+
advancedSyntax?: AdvancedSyntax;
|
|
101
|
+
/** With advanced syntax: `false` keeps a phrase that finds nothing as it is. By default a search a phrase finds
|
|
102
|
+
* nothing for is searched again with its words loose, every page alike, and `query.relaxed` says so (0.35.0). */
|
|
103
|
+
relax?: boolean;
|
|
104
|
+
};
|
|
105
|
+
/** Advanced syntax (0.35.0): `true` reads both, `false` neither, or `{ phrases, exclude }` one of them. */
|
|
106
|
+
export type AdvancedSyntax = boolean | {
|
|
107
|
+
phrases?: boolean;
|
|
108
|
+
exclude?: boolean;
|
|
109
|
+
};
|
|
110
|
+
/** What a search with quotes or minus searched (0.35.0); see SearchResponse's `query`. */
|
|
111
|
+
export type SearchedQuery = {
|
|
112
|
+
words: string[];
|
|
113
|
+
phrases: string[];
|
|
114
|
+
excluded: string[];
|
|
115
|
+
relaxed: boolean;
|
|
98
116
|
};
|
|
99
117
|
/** A subindex's own search, in a search that asks for it. */
|
|
100
118
|
export type SubindexSearch = Omit<SearchRequest, "subindexes">;
|
|
@@ -115,6 +133,13 @@ export type GeoBox = {
|
|
|
115
133
|
east: number;
|
|
116
134
|
west: number;
|
|
117
135
|
};
|
|
136
|
+
/** A map library's own bounds object, which `tilesFor` and `useMapSearch` also read: MapLibre's, Mapbox's or Leaflet's. */
|
|
137
|
+
export type MapBounds = {
|
|
138
|
+
getNorth(): number;
|
|
139
|
+
getSouth(): number;
|
|
140
|
+
getEast(): number;
|
|
141
|
+
getWest(): number;
|
|
142
|
+
};
|
|
118
143
|
/** A map tile, numbered as MapLibre and other web maps number them. */
|
|
119
144
|
export type MapTile = {
|
|
120
145
|
z: number;
|
|
@@ -123,8 +148,14 @@ export type MapTile = {
|
|
|
123
148
|
};
|
|
124
149
|
/** Cells a side of a map tile. */
|
|
125
150
|
export type MapCells = 1 | 2 | 4 | 8 | 16;
|
|
151
|
+
/** A number field in clusters' stats, by its name. With `gt`, values at or below it are left out of the stat, such as a price of 0
|
|
152
|
+
* that means no price (0.34.0). Those records stay in the cluster's `count` and on the map. */
|
|
153
|
+
export type ClusterStat = string | {
|
|
154
|
+
field: string;
|
|
155
|
+
gt?: number;
|
|
156
|
+
};
|
|
126
157
|
/** A group of matches: how many, their average place (where a bubble sits), and the box to zoom to on a click. A box with no size
|
|
127
|
-
* means every record is at one place, such as flats in one building. */
|
|
158
|
+
* means every record is at one place, such as flats in one building. `stats` leaves out a field no record has a value of. */
|
|
128
159
|
export type Cluster = {
|
|
129
160
|
count: number;
|
|
130
161
|
lat: number;
|
|
@@ -223,6 +254,10 @@ export type SearchResponse<T> = {
|
|
|
223
254
|
/** With `facetMatches`: the facet values the query names, best first, at most 10 (0.30.0). Empty when it
|
|
224
255
|
* names none or the index has no facet matches setting. */
|
|
225
256
|
facetMatches?: FacetMatch[];
|
|
257
|
+
/** With quotes or minus in the query, on an index that reads them (0.35.0): what was searched. `words` outside quotes,
|
|
258
|
+
* `phrases` in quotes, what a minus `excluded`, each as its words joined by spaces; `relaxed` when a phrase found
|
|
259
|
+
* nothing and its words were searched loose: say so, as in "No exact matches for "red dress"". Left out otherwise. */
|
|
260
|
+
query?: SearchedQuery;
|
|
226
261
|
};
|
|
227
262
|
/** A facet value a query names (0.30.0), such as the category Prótein for "prót":
|
|
228
263
|
* - `values`: the field's values it stands for, as facet counts write them; several when values share a label,
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { defaultImageOrigin, imageHostsHeader, imageHostsOf, imageOriginHeader, imageOriginOf, optimizedImageUrl, preconnect, rememberedImages, rememberImages } from "./images.js";
|
|
2
2
|
export { imageWidth, imageWidths } from "./images.js";
|
|
3
3
|
export { placeholderCss, placeholderUrl, revealCss } from "./placeholder.js";
|
|
4
|
-
export { tilesFor } from "./map.js";
|
|
4
|
+
export { clusterFor, tilesFor } from "./map.js";
|
|
5
5
|
/** A query suggestion's text in parts: what was typed (`typed: true`) and the rest. */
|
|
6
6
|
export function suggestionParts(suggestion) {
|
|
7
7
|
const [start, end] = suggestion.typed ?? [0, 0];
|
package/dist/map.d.ts
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
|
-
import type { GeoBox, MapTile } from "./index.js";
|
|
1
|
+
import type { Cluster, GeoBox, MapBounds, MapTile } from "./index.js";
|
|
2
|
+
/** A box from a plain one or a map library's own bounds object (MapLibre, Mapbox, Leaflet). */
|
|
3
|
+
export declare function plainBox(box: GeoBox | MapBounds): GeoBox;
|
|
2
4
|
/** The fixed tiles covering a map's view, from its bounds and zoom, for `tile` in a search. Everyone looking at the
|
|
3
5
|
* same area asks for the same tiles, so they're answered from the cache where a box almost never would be. They're
|
|
4
6
|
* one level above the map's zoom, 1024 pixels wide on screen, which makes at most six on a desktop view.
|
|
5
7
|
* A box whose west is greater than its east crosses the antimeridian, and longitudes past ±180 (a map showing copies
|
|
6
8
|
* of the world) wrap around. */
|
|
7
|
-
export declare function tilesFor(
|
|
9
|
+
export declare function tilesFor(bounds: GeoBox | MapBounds, zoom: number): MapTile[];
|
|
10
|
+
/** The cluster a record is in, from the clusters of its tile, for one marker an area. Give `clusters` and `spread` the same
|
|
11
|
+
* `cells`. Edges are rounded, so comparing a place with `bounds` directly misses records on an edge. */
|
|
12
|
+
export declare function clusterFor<C extends Cluster>(place: {
|
|
13
|
+
lat: number;
|
|
14
|
+
lng: number;
|
|
15
|
+
}, clusters: readonly C[]): C | undefined;
|
package/dist/map.js
CHANGED
|
@@ -6,12 +6,20 @@ const fractionDown = (lat) => {
|
|
|
6
6
|
const clamped = Math.max(-edge, Math.min(edge, lat));
|
|
7
7
|
return (1 - Math.log(Math.tan(Math.PI / 4 + clamped * Math.PI / 360)) / Math.PI) / 2;
|
|
8
8
|
};
|
|
9
|
+
/** A box from a plain one or a map library's own bounds object (MapLibre, Mapbox, Leaflet). */
|
|
10
|
+
export function plainBox(box) {
|
|
11
|
+
const plain = "getNorth" in box ? { north: box.getNorth(), south: box.getSouth(), east: box.getEast(), west: box.getWest() } : box;
|
|
12
|
+
if (![plain.north, plain.south, plain.east, plain.west].every(Number.isFinite))
|
|
13
|
+
throw new TypeError("A map's box needs north, south, east and west in degrees, or getNorth() and the like");
|
|
14
|
+
return plain;
|
|
15
|
+
}
|
|
9
16
|
/** The fixed tiles covering a map's view, from its bounds and zoom, for `tile` in a search. Everyone looking at the
|
|
10
17
|
* same area asks for the same tiles, so they're answered from the cache where a box almost never would be. They're
|
|
11
18
|
* one level above the map's zoom, 1024 pixels wide on screen, which makes at most six on a desktop view.
|
|
12
19
|
* A box whose west is greater than its east crosses the antimeridian, and longitudes past ±180 (a map showing copies
|
|
13
20
|
* of the world) wrap around. */
|
|
14
|
-
export function tilesFor(
|
|
21
|
+
export function tilesFor(bounds, zoom) {
|
|
22
|
+
const box = plainBox(bounds);
|
|
15
23
|
const span = box.east >= box.west ? box.east - box.west : box.east + 360 - box.west;
|
|
16
24
|
const west = (((box.west + 180) % 360) + 360) % 360 / 360;
|
|
17
25
|
for (let z = Math.max(0, Math.min(22, Math.floor(zoom) - 1));; z--) {
|
|
@@ -32,3 +40,12 @@ export function tilesFor(box, zoom) {
|
|
|
32
40
|
return tiles;
|
|
33
41
|
}
|
|
34
42
|
}
|
|
43
|
+
/** How far a place can be from a cluster's edge and still be in it: edges are rounded to six decimals, on a grid of about a
|
|
44
|
+
* centimeter, and records' places aren't. */
|
|
45
|
+
const margin = 0.000001;
|
|
46
|
+
/** The cluster a record is in, from the clusters of its tile, for one marker an area. Give `clusters` and `spread` the same
|
|
47
|
+
* `cells`. Edges are rounded, so comparing a place with `bounds` directly misses records on an edge. */
|
|
48
|
+
export function clusterFor(place, clusters) {
|
|
49
|
+
return clusters.find(({ bounds }) => place.lat <= bounds.north + margin && place.lat >= bounds.south - margin
|
|
50
|
+
&& place.lng <= bounds.east + margin && place.lng >= bounds.west - margin);
|
|
51
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Cluster, GeoBox, MapCells, MapTile } from "../index.js";
|
|
1
|
+
import type { Cluster, ClusterStat, GeoBox, MapBounds, MapCells, MapTile } from "../index.js";
|
|
2
2
|
import { type ClientOptions } from "./client.js";
|
|
3
3
|
import { type Hit } from "./state.js";
|
|
4
4
|
export type UseMapSearchOptions = ClientOptions & {
|
|
@@ -7,10 +7,11 @@ export type UseMapSearchOptions = ClientOptions & {
|
|
|
7
7
|
/** A sort ID: which of a cell's records `hits` shows. */
|
|
8
8
|
sort?: string;
|
|
9
9
|
/** Groups of matches, to draw as bubbles (default: 16 cells a side, which on `tilesFor`'s tiles is 64 pixels each). `stats`
|
|
10
|
-
* takes up to three number fields your search settings let visitors filter by
|
|
10
|
+
* takes up to three number fields your search settings let visitors filter by: a name, or `{ field, gt }` to leave out values at
|
|
11
|
+
* or below `gt`. false asks for none. */
|
|
11
12
|
clusters?: {
|
|
12
13
|
cells?: MapCells;
|
|
13
|
-
stats?:
|
|
14
|
+
stats?: ClusterStat[];
|
|
14
15
|
} | false;
|
|
15
16
|
/** A few records of each area, to draw as pins (default: none): `perCell` of each of `cells` a side (8), up to `limit`
|
|
16
17
|
* (50) a tile, by the search's ranking. These count as records a visitor took; clusters don't. */
|
|
@@ -24,18 +25,22 @@ export type UseMapSearchOptions = ClientOptions & {
|
|
|
24
25
|
export type UseMapSearchResult<T> = {
|
|
25
26
|
clusters: Cluster[];
|
|
26
27
|
hits: Hit<T>[];
|
|
27
|
-
/** How many records match in the tiles shown.
|
|
28
|
+
/** How many records match in the tiles shown. A tile reaches past the view, so this can be more than the view holds: count
|
|
29
|
+
* the clusters in sight for that. */
|
|
28
30
|
total: number;
|
|
29
31
|
/** The tiles the view needs. */
|
|
30
32
|
tiles: MapTile[];
|
|
31
33
|
/** A tile is on its way. After a zoom or a change of filters, the earlier answer stays until every new tile has arrived. */
|
|
32
34
|
isPending: boolean;
|
|
35
|
+
/** Why a tile of this view failed. What the other tiles found still shows, and the next move asks for the failed one again. */
|
|
33
36
|
error: unknown;
|
|
37
|
+
/** Asks again for the tiles that failed, for a "Try again" button. */
|
|
38
|
+
retry: () => void;
|
|
34
39
|
};
|
|
35
40
|
/** Keeps a map's clusters and pins current. Pass the map's view (its bounds and zoom, from a `moveend` listener) or null to not
|
|
36
41
|
* search. It asks for fixed tiles (`tilesFor`), so moving within them asks for nothing, going back answers from memory, and
|
|
37
42
|
* visitors looking at one area share the edge's cache. Outdated requests stop. */
|
|
38
43
|
export declare function useMapSearch<T = Record<string, unknown>>(view: {
|
|
39
|
-
box: GeoBox;
|
|
44
|
+
box: GeoBox | MapBounds;
|
|
40
45
|
zoom: number;
|
|
41
46
|
} | null, { q, filters, sort, clusters, hits, publicId, baseUrl, client: given }?: UseMapSearchOptions): UseMapSearchResult<T>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { useEffect, useMemo, useRef, useState } from "react";
|
|
3
|
-
import { tilesFor } from "../map.js";
|
|
2
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
3
|
+
import { plainBox, tilesFor } from "../map.js";
|
|
4
4
|
import { cachedTile, peekTile } from "./cache.js";
|
|
5
5
|
import { useClient } from "./client.js";
|
|
6
6
|
import { canonical } from "./state.js";
|
|
@@ -18,18 +18,24 @@ export function useMapSearch(view, { q, filters, sort, clusters = {}, hits = fal
|
|
|
18
18
|
...(hits && hits.attributesToRetrieve ? { attributesToRetrieve: hits.attributesToRetrieve } : {}),
|
|
19
19
|
};
|
|
20
20
|
const requestKey = canonical(request);
|
|
21
|
-
const
|
|
21
|
+
const box = view && plainBox(view.box);
|
|
22
|
+
const needed = view && box ? tilesFor(box, view.zoom) : [];
|
|
22
23
|
const tilesKey = needed.map(tileId).join(",");
|
|
23
24
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
24
25
|
const tiles = useMemo(() => needed, [tilesKey]);
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
const
|
|
26
|
+
const wanted = `${requestKey}\n${tilesKey}`;
|
|
27
|
+
const [shown, setShown] = useState({ answers: "", clusters: [], hits: [], total: 0, isPending: !!view, error: null });
|
|
28
|
+
const [attempt, setAttempt] = useState(0);
|
|
29
|
+
const retry = useCallback(() => setAttempt((count) => count + 1), []);
|
|
30
|
+
// What's showing, the requests the last effect is waiting for, and whether a tile of it failed.
|
|
31
|
+
const current = useRef({ key: "", z: null, controller: null, failed: false });
|
|
28
32
|
useEffect(() => {
|
|
29
33
|
const controller = new AbortController();
|
|
30
34
|
const answers = new Map();
|
|
31
35
|
const z = tiles[0]?.z ?? null;
|
|
32
36
|
let waiting = 0;
|
|
37
|
+
let failure = null;
|
|
38
|
+
current.current.failed = false;
|
|
33
39
|
const show = () => {
|
|
34
40
|
// Moving at one zoom fills in tile by tile. Otherwise the earlier answer stays until the new one is whole.
|
|
35
41
|
const continuing = current.current.key === requestKey && current.current.z === z;
|
|
@@ -39,8 +45,8 @@ export function useMapSearch(view, { q, filters, sort, clusters = {}, hits = fal
|
|
|
39
45
|
}
|
|
40
46
|
current.current = { ...current.current, key: requestKey, z };
|
|
41
47
|
const found = tiles.flatMap((tile) => answers.get(tileId(tile)) ?? []);
|
|
42
|
-
setShown({ clusters: found.flatMap((answer) => answer.clusters ?? []), hits: found.flatMap((answer) => answer.hits),
|
|
43
|
-
total: found.reduce((sum, answer) => sum + answer.total, 0), isPending: waiting > 0, error:
|
|
48
|
+
setShown({ answers: wanted, clusters: found.flatMap((answer) => answer.clusters ?? []), hits: found.flatMap((answer) => answer.hits),
|
|
49
|
+
total: found.reduce((sum, answer) => sum + answer.total, 0), isPending: waiting > 0, error: failure });
|
|
44
50
|
};
|
|
45
51
|
for (const tile of tiles) {
|
|
46
52
|
const asked = { ...request, tile };
|
|
@@ -60,7 +66,9 @@ export function useMapSearch(view, { q, filters, sort, clusters = {}, hits = fal
|
|
|
60
66
|
if (controller.signal.aborted)
|
|
61
67
|
return;
|
|
62
68
|
waiting--;
|
|
63
|
-
|
|
69
|
+
failure = error;
|
|
70
|
+
current.current.failed = true;
|
|
71
|
+
show();
|
|
64
72
|
});
|
|
65
73
|
}
|
|
66
74
|
show();
|
|
@@ -68,7 +76,20 @@ export function useMapSearch(view, { q, filters, sort, clusters = {}, hits = fal
|
|
|
68
76
|
current.current.controller?.abort();
|
|
69
77
|
current.current.controller = controller;
|
|
70
78
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
71
|
-
}, [client, requestKey, tiles]);
|
|
79
|
+
}, [client, requestKey, tiles, attempt]);
|
|
72
80
|
useEffect(() => () => current.current.controller?.abort(), []);
|
|
73
|
-
|
|
81
|
+
// A move within the same tiles needs nothing new, except what failed. By the box's edges, not the view object: a view made
|
|
82
|
+
// anew each render isn't a move, and would ask again without end.
|
|
83
|
+
const place = box && view ? `${box.north},${box.south},${box.east},${box.west},${view.zoom}` : "";
|
|
84
|
+
const lastPlace = useRef(place);
|
|
85
|
+
useEffect(() => {
|
|
86
|
+
if (lastPlace.current === place)
|
|
87
|
+
return;
|
|
88
|
+
lastPlace.current = place;
|
|
89
|
+
if (current.current.failed)
|
|
90
|
+
retry();
|
|
91
|
+
}, [place, retry]);
|
|
92
|
+
const { answers, ...result } = shown;
|
|
93
|
+
// Until its effect has run, a new view's render still holds the last answer: it's on its way.
|
|
94
|
+
return { ...result, isPending: shown.isPending || (!!view && answers !== wanted), tiles, retry };
|
|
74
95
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@findlane/browser",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.35.0",
|
|
4
4
|
"description": "Findlane browser SDK: fast, typo-tolerant public search from the browser with an index's public ID, plus React hooks and components.",
|
|
5
5
|
"keywords": ["findlane", "search", "full-text search", "typo tolerance", "instant search", "ecommerce search", "browser", "react", "autocomplete"],
|
|
6
6
|
"license": "MIT",
|
|
@@ -11,15 +11,18 @@
|
|
|
11
11
|
"exports": {
|
|
12
12
|
".": {
|
|
13
13
|
"types": "./dist/index.d.ts",
|
|
14
|
-
"import": "./dist/index.js"
|
|
14
|
+
"import": "./dist/index.js",
|
|
15
|
+
"default": "./dist/index.js"
|
|
15
16
|
},
|
|
16
17
|
"./react": {
|
|
17
18
|
"types": "./dist/react/index.d.ts",
|
|
18
|
-
"import": "./dist/react/index.js"
|
|
19
|
+
"import": "./dist/react/index.js",
|
|
20
|
+
"default": "./dist/react/index.js"
|
|
19
21
|
},
|
|
20
22
|
"./react/ssr": {
|
|
21
23
|
"types": "./dist/react/ssr.d.ts",
|
|
22
|
-
"import": "./dist/react/ssr.js"
|
|
24
|
+
"import": "./dist/react/ssr.js",
|
|
25
|
+
"default": "./dist/react/ssr.js"
|
|
23
26
|
},
|
|
24
27
|
"./react/styles.css": "./dist/react/styles.css",
|
|
25
28
|
"./package.json": "./package.json"
|