Skip to content

Commit 0d50f0d

Browse files
feat(infinite-hits): return banner info with connectInfiniteHits (#6230)
* feat(infinite-hits): return banner info with connectInfiniteHits * chore(hits): extract Banner type
1 parent 7adab0c commit 0d50f0d

File tree

5 files changed

+65
-55
lines changed

5 files changed

+65
-55
lines changed

packages/algoliasearch-helper/index.d.ts

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,41 +1335,7 @@ declare namespace algoliasearchHelper {
13351335
/**
13361336
* Configuration for banners
13371337
*/
1338-
banners?: Array<{
1339-
/**
1340-
* Configuration for the banner image
1341-
*/
1342-
image: {
1343-
/**
1344-
* Set of possible URLs of the banner image
1345-
*/
1346-
urls: Array<{
1347-
/**
1348-
* URL of the banner image
1349-
*/
1350-
url: string;
1351-
}>;
1352-
/**
1353-
* Alt text of the banner image
1354-
*/
1355-
title?: string;
1356-
};
1357-
/**
1358-
* Configuration for the banner click navigation
1359-
*/
1360-
link?: {
1361-
/**
1362-
* URL to navigate to when the banner is clicked
1363-
*/
1364-
url: string;
1365-
/**
1366-
* Target of the navigation
1367-
* - `_blank` opens the URL in a new tab
1368-
* - `_self` opens the URL in the same tab
1369-
*/
1370-
target?: '_blank' | '_self';
1371-
};
1372-
}>;
1338+
banners?: Banner[];
13731339
};
13741340
};
13751341

@@ -1509,6 +1475,42 @@ declare namespace algoliasearchHelper {
15091475
getRefinements(): SearchResults.Refinement[];
15101476
}
15111477

1478+
export type Banner = {
1479+
/**
1480+
* Configuration for the banner image
1481+
*/
1482+
image: {
1483+
/**
1484+
* Set of possible URLs of the banner image
1485+
*/
1486+
urls: Array<{
1487+
/**
1488+
* URL of the banner image
1489+
*/
1490+
url: string;
1491+
}>;
1492+
/**
1493+
* Alt text of the banner image
1494+
*/
1495+
title?: string;
1496+
};
1497+
/**
1498+
* Configuration for the banner click navigation
1499+
*/
1500+
link?: {
1501+
/**
1502+
* URL to navigate to when the banner is clicked
1503+
*/
1504+
url: string;
1505+
/**
1506+
* Target of the navigation
1507+
* - `_blank` opens the URL in a new tab
1508+
* - `_self` opens the URL in the same tab
1509+
*/
1510+
target?: '_blank' | '_self';
1511+
};
1512+
};
1513+
15121514
namespace SearchResults {
15131515
interface Facet {
15141516
name: string;

packages/instantsearch-ui-components/src/components/Hits.tsx

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,12 @@
22
import { cx } from '../lib';
33

44
import type { ComponentProps, Renderer, SendEventForHits } from '../types';
5+
import type { Banner } from 'algoliasearch-helper';
56

67
// Should be imported from a shared package in the future
78
type Hit = Record<string, unknown> & {
89
objectID: string;
910
};
10-
type Banner = {
11-
image: {
12-
urls: Array<{
13-
url: string;
14-
}>;
15-
title?: string;
16-
};
17-
link?: {
18-
url: string;
19-
target?: '_blank' | '_self';
20-
};
21-
};
2211

2312
type BannerProps = ComponentProps<'aside'> & {
2413
banner: Banner;

packages/instantsearch.js/src/connectors/hits/connectHits.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,13 @@ import type {
2121
Renderer,
2222
IndexRenderState,
2323
} from '../../types';
24-
import type { SearchResults } from 'algoliasearch-helper';
24+
import type { Banner, SearchResults } from 'algoliasearch-helper';
2525

2626
const withUsage = createDocumentationMessageGenerator({
2727
name: 'hits',
2828
connector: true,
2929
});
3030

31-
type Banner = NonNullable<
32-
NonNullable<
33-
Required<SearchResults<Hit>['renderingContent']>
34-
>['widgets']['banners']
35-
>[number];
36-
3731
export type HitsRenderState<THit extends NonNullable<object> = BaseHit> = {
3832
/**
3933
* The matched hits from Algolia API.

packages/instantsearch.js/src/connectors/infinite-hits/__tests__/connectInfiniteHits-test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/infinite-hi
13311331
bindEvent: expect.any(Function),
13321332
isFirstPage: true,
13331333
isLastPage: true,
1334+
banner: undefined,
13341335
results: undefined,
13351336
showMore: expect.any(Function),
13361337
showPrevious: expect.any(Function),
@@ -1394,6 +1395,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/infinite-hi
13941395
bindEvent: renderState1.infiniteHits.bindEvent,
13951396
isFirstPage: true,
13961397
isLastPage: true,
1398+
banner: undefined,
13971399
results,
13981400
showMore: renderState1.infiniteHits.showMore,
13991401
showPrevious: renderState1.infiniteHits.showPrevious,
@@ -1424,6 +1426,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/infinite-hi
14241426
bindEvent: expect.any(Function),
14251427
isFirstPage: true,
14261428
isLastPage: true,
1429+
banner: undefined,
14271430
results: undefined,
14281431
showMore: expect.any(Function),
14291432
showPrevious: expect.any(Function),
@@ -1439,6 +1442,12 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/infinite-hi
14391442
const helper = algoliasearchHelper(createSearchClient(), 'indexName', {
14401443
index: 'indexName',
14411444
});
1445+
const banner = { image: { urls: [{ url: 'https://example.com' }] } };
1446+
const renderingContent = {
1447+
widgets: {
1448+
banners: [banner],
1449+
},
1450+
};
14421451

14431452
const initOptions = createInitOptions({ state: helper.state, helper });
14441453

@@ -1450,7 +1459,13 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/infinite-hi
14501459
];
14511460

14521461
const results = new SearchResults(helper.state, [
1453-
createSingleSearchResponse({ hits, queryID: 'theQueryID' }),
1462+
createSingleSearchResponse({
1463+
hits,
1464+
queryID: 'theQueryID',
1465+
// @TODO: remove once algoliasearch js client has been updated
1466+
// @ts-expect-error
1467+
renderingContent,
1468+
}),
14541469
]);
14551470

14561471
const renderOptions = createRenderOptions({
@@ -1488,6 +1503,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/infinite-hi
14881503
bindEvent: renderState1.bindEvent,
14891504
isFirstPage: true,
14901505
isLastPage: true,
1506+
banner,
14911507
results,
14921508
showMore: renderState1.showMore,
14931509
showPrevious: renderState1.showPrevious,

packages/instantsearch.js/src/connectors/infinite-hits/connectInfiniteHits.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import type {
2525
IndexRenderState,
2626
} from '../../types';
2727
import type {
28+
Banner,
2829
AlgoliaSearchHelper as Helper,
2930
PlainSearchParameters,
3031
SearchParameters,
@@ -138,6 +139,11 @@ export type InfiniteHitsRenderState<
138139
* The response from the Algolia API.
139140
*/
140141
results?: SearchResults<Hit<THit>>;
142+
143+
/**
144+
* The banner to display above the hits.
145+
*/
146+
banner?: Banner;
141147
};
142148

143149
const withUsage = createDocumentationMessageGenerator({
@@ -342,6 +348,8 @@ export default (function connectInfiniteHits<
342348

343349
const cachedHits = cache.read({ state: normalizeState(state) }) || {};
344350

351+
const banner = results?.renderingContent?.widgets?.banners?.[0];
352+
345353
if (!results) {
346354
showPrevious = getShowPrevious(helper);
347355
showMore = getShowMore(helper);
@@ -426,6 +434,7 @@ export default (function connectInfiniteHits<
426434
currentPageHits,
427435
sendEvent,
428436
bindEvent,
437+
banner,
429438
results,
430439
showPrevious,
431440
showMore,

0 commit comments

Comments
 (0)