Skip to content

Commit abf6a73

Browse files
feat(hooks): generic useGeoSearch code sample (#402)
Co-authored-by: Dhaya <[email protected]>
1 parent 9116fc1 commit abf6a73

File tree

4 files changed

+75
-34
lines changed

4 files changed

+75
-34
lines changed

react-instantsearch-hooks/geo-search/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
},
99
"dependencies": {
1010
"algoliasearch": "4",
11-
"instantsearch.js": "4.43.0",
11+
"instantsearch.js": "4.44.0",
1212
"leaflet": "1.8.0",
1313
"react": "18.2.0",
1414
"react-dom": "18.2.0",
15-
"react-instantsearch-hooks-web": "6.29.0",
15+
"react-instantsearch-hooks-web": "6.31.1",
1616
"react-leaflet": "4.0.1"
1717
},
1818
"devDependencies": {

react-instantsearch-hooks/geo-search/src/Airports.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,22 @@ import { useGeoSearch } from './useGeoSearch';
66

77
import type { GeoHit } from 'instantsearch.js/es/connectors/geo-search/connectGeoSearch';
88

9+
type Airport = {
10+
airport_id: string;
11+
city: string;
12+
country: string;
13+
name: string;
14+
nb_airline_liaisons: number;
15+
};
16+
917
export function Airports() {
1018
const { query, refine: refineQuery } = useSearchBox();
1119
const {
1220
items,
1321
refine: refineItems,
1422
currentRefinement,
1523
clearMapRefinement,
16-
} = useGeoSearch();
24+
} = useGeoSearch<Airport>();
1725

1826
const [previousQuery, setPreviousQuery] = useState(query);
1927
const [skipViewEffect, setSkipViewEffect] = useState(false);
@@ -74,7 +82,7 @@ export function Airports() {
7482
);
7583
}
7684

77-
function createAirportIcon(item: GeoHit) {
85+
function createAirportIcon(item: GeoHit<Airport>) {
7886
return new DivIcon({
7987
html: `<div class="marker">${item.airport_id}</div>`,
8088
popupAnchor: [0, -15],
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
import connectGeoSearch from 'instantsearch.js/es/connectors/geo-search/connectGeoSearch';
1+
import connectGeoSearch, {
2+
GeoSearchConnector,
3+
} from 'instantsearch.js/es/connectors/geo-search/connectGeoSearch';
24
import { useConnector } from 'react-instantsearch-hooks-web';
35

46
import type {
57
GeoSearchConnectorParams,
68
GeoSearchWidgetDescription,
79
} from 'instantsearch.js/es/connectors/geo-search/connectGeoSearch';
10+
import { BaseHit } from 'instantsearch.js';
811

9-
type UseGeoSearchProps = GeoSearchConnectorParams;
12+
type UseGeoSearchProps<THit extends BaseHit> = GeoSearchConnectorParams<THit>;
1013

11-
export function useGeoSearch(props?: UseGeoSearchProps) {
12-
return useConnector<GeoSearchConnectorParams, GeoSearchWidgetDescription>(
13-
connectGeoSearch,
14-
props
15-
);
14+
export function useGeoSearch<THit extends BaseHit>(
15+
props?: UseGeoSearchProps<THit>
16+
) {
17+
return useConnector<
18+
GeoSearchConnectorParams<THit>,
19+
GeoSearchWidgetDescription<THit>
20+
>(connectGeoSearch as GeoSearchConnector<THit>, props);
1621
}

react-instantsearch-hooks/geo-search/yarn.lock

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,19 @@
111111
"@algolia/logger-common" "4.13.1"
112112
"@algolia/requester-common" "4.13.1"
113113

114+
"@algolia/ui-components-highlight-vdom@^1.1.2":
115+
version "1.1.3"
116+
resolved "https://registry.yarnpkg.com/@algolia/ui-components-highlight-vdom/-/ui-components-highlight-vdom-1.1.3.tgz#ba3ec05d4657286e2e688e1446c591d0e4217166"
117+
integrity sha512-KgSiQ+FQf+e2HDNgw9J66HmpbIdZA9VQpLwDn950DCgo7BEZQrMqgqkMPJhHYZfkPvRfxV+1T3XwS43zib8w4w==
118+
dependencies:
119+
"@algolia/ui-components-shared" "1.1.3"
120+
"@babel/runtime" "^7.0.0"
121+
122+
"@algolia/[email protected]", "@algolia/ui-components-shared@^1.1.2":
123+
version "1.1.3"
124+
resolved "https://registry.yarnpkg.com/@algolia/ui-components-shared/-/ui-components-shared-1.1.3.tgz#d62760b20584f628f57e8a144f5796bc42fd68f1"
125+
integrity sha512-eBxvljiwvajSsg9Pz9nYNH+QH/b5q66Z4xRDr1LhICNuLibDF64mH+Vv4mg29qPxmmgMWlmWiwJQmQqR9Z229w==
126+
114127
"@babel/code-frame@^7.0.0":
115128
version "7.18.6"
116129
resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.18.6.tgz#3b25d38c89600baa2dcc219edfa88a74eb2c427a"
@@ -132,6 +145,13 @@
132145
chalk "^2.0.0"
133146
js-tokens "^4.0.0"
134147

148+
"@babel/runtime@^7.0.0":
149+
version "7.18.9"
150+
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.18.9.tgz#b4fcfce55db3d2e5e080d2490f608a3b9f407f4a"
151+
integrity sha512-lkqXDcvlFT5rvEjiu6+QYO+1GXrEHRo2LOtS7E4GtX5ESIZOgepqsZBVIj6Pv+a6zqsya9VCgiK1KAK4BvJDAw==
152+
dependencies:
153+
regenerator-runtime "^0.13.4"
154+
135155
"@babel/runtime@^7.1.2":
136156
version "7.18.6"
137157
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.18.6.tgz#6a1ef59f838debd670421f8c7f2cbb8da9751580"
@@ -932,10 +952,10 @@ acorn@^8.5.0:
932952
resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.7.1.tgz#0197122c843d1bf6d0a5e83220a788f278f63c30"
933953
integrity sha512-Xx54uLJQZ19lKygFXOWsscKUbsBZW0CPykPhVQdhIeIwrbPmJzqeASDInc8nKBnp/JT6igTs82qPXz069H8I/A==
934954

935-
algoliasearch-helper@^3.10.0, algoliasearch-helper@^3.9.0:
936-
version "3.10.0"
937-
resolved "https://registry.yarnpkg.com/algoliasearch-helper/-/algoliasearch-helper-3.10.0.tgz#59a0f645dd3c7e55cf01faa568d1af50c49d36f6"
938-
integrity sha512-4E4od8qWWDMVvQ3jaRX6Oks/k35ywD011wAA4LbYMMjOtaZV6VWaTjRr4iN2bdaXP2o1BP7SLFMBf3wvnHmd8Q==
955+
algoliasearch-helper@^3.10.0:
956+
version "3.11.0"
957+
resolved "https://registry.yarnpkg.com/algoliasearch-helper/-/algoliasearch-helper-3.11.0.tgz#c4355056d97748a92f6ff0d4fce153b96b561ddb"
958+
integrity sha512-TLl/MSjtQ98mgkd8hngWkzSjE+dAWldZ1NpJtv2mT+ZoFJ2P2zDE85oF9WafJOXWN9FbVRmyxpO5H+qXcNaFng==
939959
dependencies:
940960
"@algolia/events" "^4.0.1"
941961

@@ -1230,6 +1250,11 @@ hogan.js@^3.0.2:
12301250
mkdirp "0.3.0"
12311251
nopt "1.0.10"
12321252

1253+
htm@^3.0.0:
1254+
version "3.1.1"
1255+
resolved "https://registry.yarnpkg.com/htm/-/htm-3.1.1.tgz#49266582be0dc66ed2235d5ea892307cc0c24b78"
1256+
integrity sha512-983Vyg8NwUE7JkZ6NmOqpCZ+sh1bKv2iYTlUkzlWmA5JD2acKoxd4KVxbMmxX/85mtfdnDmTFoNKcg5DGAvxNQ==
1257+
12331258
htmlnano@^2.0.0:
12341259
version "2.0.2"
12351260
resolved "https://registry.yarnpkg.com/htmlnano/-/htmlnano-2.0.2.tgz#3e3170941e2446a86211196d740272ebca78f878"
@@ -1257,18 +1282,21 @@ import-fresh@^3.2.1:
12571282
parent-module "^1.0.0"
12581283
resolve-from "^4.0.0"
12591284

1260-
instantsearch.js@4.43.0, instantsearch.js@^4.42.0:
1261-
version "4.43.0"
1262-
resolved "https://registry.yarnpkg.com/instantsearch.js/-/instantsearch.js-4.43.0.tgz#ac5f26bd317649ec3d7b4eebfd2abc13f9b3701e"
1263-
integrity sha512-KcPm3Swkw5nlQFRVY5v8JebBwJ303ZGmxSMxdAevrGJBmIXpGrcGJF5goBqNa+fUpwpCY6PsICcqQS0YMuXVag==
1285+
instantsearch.js@4.44.0, instantsearch.js@^4.43.1:
1286+
version "4.44.0"
1287+
resolved "https://registry.yarnpkg.com/instantsearch.js/-/instantsearch.js-4.44.0.tgz#9365cfe7e0e3455f3c35fdbc848da52eb67b45c7"
1288+
integrity sha512-lfGtyhBJej1KzC6zlJ+YD/XQ2r+pwtHkawecNnOtIwYz+LR1WgZIXb5ZcX6X5Im2d/+XMArNP2k+9x32evdQXQ==
12641289
dependencies:
12651290
"@algolia/events" "^4.0.1"
1291+
"@algolia/ui-components-highlight-vdom" "^1.1.2"
1292+
"@algolia/ui-components-shared" "^1.1.2"
12661293
"@types/google.maps" "^3.45.3"
12671294
"@types/hogan.js" "^3.0.0"
12681295
"@types/qs" "^6.5.3"
12691296
algoliasearch-helper "^3.10.0"
12701297
classnames "^2.2.5"
12711298
hogan.js "^3.0.2"
1299+
htm "^3.0.0"
12721300
preact "^10.6.0"
12731301
qs "^6.5.1 < 6.10"
12741302
search-insights "^2.1.0"
@@ -1488,9 +1516,9 @@ posthtml@^0.16.4, posthtml@^0.16.5:
14881516
posthtml-render "^3.0.0"
14891517

14901518
preact@^10.6.0:
1491-
version "10.8.2"
1492-
resolved "https://registry.yarnpkg.com/preact/-/preact-10.8.2.tgz#b8a614f5cc8ab0cd9e63337a3d60dc80410f4ed4"
1493-
integrity sha512-AKGt0BsDSiAYzVS78jZ9qRwuorY2CoSZtf1iOC6gLb/3QyZt+fLT09aYJBjRc/BEcRc4j+j3ggERMdNE43i1LQ==
1519+
version "10.10.1"
1520+
resolved "https://registry.yarnpkg.com/preact/-/preact-10.10.1.tgz#df67e4348f50fc6ad2e11e813553f15c6543176b"
1521+
integrity sha512-cXljG59ylGtSLismoLojXPAGvnh2ipQr3BYz9KZQr+1sdASCT+sR/v8dSMDS96xGCdtln2wHfAHCnLJK+XcBNg==
14941522

14951523
"qs@^6.5.1 < 6.10":
14961524
version "6.9.7"
@@ -1505,23 +1533,23 @@ [email protected]:
15051533
loose-envify "^1.1.0"
15061534
scheduler "^0.23.0"
15071535

1508-
react-instantsearch-hooks-web@6.29.0:
1509-
version "6.29.0"
1510-
resolved "https://registry.yarnpkg.com/react-instantsearch-hooks-web/-/react-instantsearch-hooks-web-6.29.0.tgz#b058abf8f568c7040768e6432bff29cc08ba1334"
1511-
integrity sha512-k9ch9hdmF7Pn9ZclQTUMhtGeKqrBZKpbKK7HXvQYAM3JiqZhB9OZY4r8oaz8WJ1uITJEgNzID7j6osuJbJmMVg==
1536+
react-instantsearch-hooks-web@6.31.1:
1537+
version "6.31.1"
1538+
resolved "https://registry.yarnpkg.com/react-instantsearch-hooks-web/-/react-instantsearch-hooks-web-6.31.1.tgz#5fe8317fe49785064c1fd3ca20ff34e96f53e9ab"
1539+
integrity sha512-sQHW4PHLd/SfC5CPlCh/+008ecCO5jXaL++EntUHY3GIZnKXU2xgVClUNO4Y/fuoK02/5HOcFiZpTA/hzSheog==
15121540
dependencies:
15131541
"@babel/runtime" "^7.1.2"
1514-
instantsearch.js "^4.42.0"
1515-
react-instantsearch-hooks "6.29.0"
1542+
instantsearch.js "^4.43.1"
1543+
react-instantsearch-hooks "6.31.1"
15161544

1517-
react-instantsearch-hooks@6.29.0:
1518-
version "6.29.0"
1519-
resolved "https://registry.yarnpkg.com/react-instantsearch-hooks/-/react-instantsearch-hooks-6.29.0.tgz#74eba7a06f69465031b50d9da10a57db545cb914"
1520-
integrity sha512-F4kLBkhELffrT7vaYW5gpja2R6b3v7rreDpod9nQmyyRW0ww9nIkzx01ST/6lRcNatHOgvm+mjR+/Zc7/QIbcQ==
1545+
react-instantsearch-hooks@6.31.1:
1546+
version "6.31.1"
1547+
resolved "https://registry.yarnpkg.com/react-instantsearch-hooks/-/react-instantsearch-hooks-6.31.1.tgz#87240b2ac3228df74206274b85490859ef6452e8"
1548+
integrity sha512-3Pjd5D3Z/gbXe70X5ye5xz/SAjh6ePmM3LegpxMOStxa/Ar+NTH9kSzlV6V1iF0uwfSlo+Av8lppiQqgy2mL/w==
15211549
dependencies:
15221550
"@babel/runtime" "^7.1.2"
1523-
algoliasearch-helper "^3.9.0"
1524-
instantsearch.js "^4.42.0"
1551+
algoliasearch-helper "^3.10.0"
1552+
instantsearch.js "^4.43.1"
15251553
use-sync-external-store "^1.0.0"
15261554

15271555

0 commit comments

Comments
 (0)