@docsearch/react 3.3.5 → 3.5.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.
@@ -14,6 +14,7 @@ export interface DocSearchProps {
14
14
  indexName: string;
15
15
  placeholder?: string;
16
16
  searchParameters?: SearchOptions;
17
+ maxResultsPerGroup?: number;
17
18
  transformItems?: (items: DocSearchHit[]) => DocSearchHit[];
18
19
  hitComponent?: (props: {
19
20
  hit: InternalDocSearchHit | StoredDocSearchHit;
@@ -30,5 +31,6 @@ export interface DocSearchProps {
30
31
  getMissingResultsUrl?: ({ query }: {
31
32
  query: string;
32
33
  }) => string;
34
+ insights?: AutocompleteOptions<InternalDocSearchHit>['insights'];
33
35
  }
34
36
  export declare function DocSearch(props: DocSearchProps): JSX.Element;
@@ -11,4 +11,4 @@ export declare type DocSearchModalProps = DocSearchProps & {
11
11
  onClose?: () => void;
12
12
  translations?: ModalTranslations;
13
13
  };
14
- export declare function DocSearchModal({ appId, apiKey, indexName, placeholder, searchParameters, onClose, transformItems, hitComponent, resultsFooterComponent, navigator, initialScrollY, transformSearchClient, disableUserPersonalization, initialQuery: initialQueryFromProp, translations, getMissingResultsUrl, }: DocSearchModalProps): JSX.Element;
14
+ export declare function DocSearchModal({ appId, apiKey, indexName, placeholder, searchParameters, maxResultsPerGroup, onClose, transformItems, hitComponent, resultsFooterComponent, navigator, initialScrollY, transformSearchClient, disableUserPersonalization, initialQuery: initialQueryFromProp, translations, getMissingResultsUrl, insights, }: DocSearchModalProps): JSX.Element;
@@ -43,6 +43,7 @@ export function DocSearchModal(_ref) {
43
43
  _ref$placeholder = _ref.placeholder,
44
44
  placeholder = _ref$placeholder === void 0 ? 'Search docs' : _ref$placeholder,
45
45
  searchParameters = _ref.searchParameters,
46
+ maxResultsPerGroup = _ref.maxResultsPerGroup,
46
47
  _ref$onClose = _ref.onClose,
47
48
  onClose = _ref$onClose === void 0 ? noop : _ref$onClose,
48
49
  _ref$transformItems = _ref.transformItems,
@@ -64,7 +65,9 @@ export function DocSearchModal(_ref) {
64
65
  initialQueryFromProp = _ref$initialQuery === void 0 ? '' : _ref$initialQuery,
65
66
  _ref$translations = _ref.translations,
66
67
  translations = _ref$translations === void 0 ? {} : _ref$translations,
67
- getMissingResultsUrl = _ref.getMissingResultsUrl;
68
+ getMissingResultsUrl = _ref.getMissingResultsUrl,
69
+ _ref$insights = _ref.insights,
70
+ insights = _ref$insights === void 0 ? false : _ref$insights;
68
71
 
69
72
  var footerTranslations = translations.footer,
70
73
  searchBoxTranslations = translations.searchBox,
@@ -128,6 +131,7 @@ export function DocSearchModal(_ref) {
128
131
  searchSuggestions: []
129
132
  }
130
133
  },
134
+ insights: insights,
131
135
  navigator: navigator,
132
136
  onStateChange: function onStateChange(props) {
133
137
  setState(props.state);
@@ -182,6 +186,7 @@ export function DocSearchModal(_ref) {
182
186
  }];
183
187
  }
184
188
 
189
+ var insightsActive = Boolean(insights);
185
190
  return searchClient.search([{
186
191
  query: query,
187
192
  indexName: indexName,
@@ -191,7 +196,8 @@ export function DocSearchModal(_ref) {
191
196
  snippetEllipsisText: '…',
192
197
  highlightPreTag: '<mark>',
193
198
  highlightPostTag: '</mark>',
194
- hitsPerPage: 20
199
+ hitsPerPage: 20,
200
+ clickAnalytics: insightsActive
195
201
  }, searchParameters)
196
202
  }]).catch(function (error) {
197
203
  // The Algolia `RetryError` happens when all the servers have
@@ -210,7 +216,7 @@ export function DocSearchModal(_ref) {
210
216
  nbHits = _results$.nbHits;
211
217
  var sources = groupBy(hits, function (hit) {
212
218
  return removeHighlightTags(hit);
213
- }); // We store the `lvl0`s to display them as search suggestions
219
+ }, maxResultsPerGroup); // We store the `lvl0`s to display them as search suggestions
214
220
  // in the "no results" screen.
215
221
 
216
222
  if (sourcesState.context.searchSuggestions.length < Object.keys(sources).length) {
@@ -222,6 +228,19 @@ export function DocSearchModal(_ref) {
222
228
  setContext({
223
229
  nbHits: nbHits
224
230
  });
231
+ var insightsParams = {};
232
+
233
+ if (insightsActive) {
234
+ insightsParams = {
235
+ __autocomplete_indexName: indexName,
236
+ __autocomplete_queryID: results[0].queryID,
237
+ __autocomplete_algoliaCredentials: {
238
+ appId: appId,
239
+ apiKey: apiKey
240
+ }
241
+ };
242
+ }
243
+
225
244
  return Object.values(sources).map(function (items, index) {
226
245
  return {
227
246
  sourceId: "hits".concat(index),
@@ -241,13 +260,20 @@ export function DocSearchModal(_ref) {
241
260
  getItems: function getItems() {
242
261
  return Object.values(groupBy(items, function (item) {
243
262
  return item.hierarchy.lvl1;
244
- })).map(transformItems).map(function (groupedHits) {
263
+ }, maxResultsPerGroup)).map(transformItems).map(function (groupedHits) {
245
264
  return groupedHits.map(function (item) {
246
- return _objectSpread(_objectSpread({}, item), {}, {
247
- __docsearch_parent: item.type !== 'lvl1' && groupedHits.find(function (siblingItem) {
248
- return siblingItem.type === 'lvl1' && siblingItem.hierarchy.lvl1 === item.hierarchy.lvl1;
249
- })
265
+ var parent = null;
266
+ var potentialParent = groupedHits.find(function (siblingItem) {
267
+ return siblingItem.type === 'lvl1' && siblingItem.hierarchy.lvl1 === item.hierarchy.lvl1;
250
268
  });
269
+
270
+ if (item.type !== 'lvl1' && potentialParent) {
271
+ parent = potentialParent;
272
+ }
273
+
274
+ return _objectSpread(_objectSpread({}, item), {}, {
275
+ __docsearch_parent: parent
276
+ }, insightsParams);
251
277
  });
252
278
  }).flat();
253
279
  }
@@ -256,7 +282,7 @@ export function DocSearchModal(_ref) {
256
282
  });
257
283
  }
258
284
  });
259
- }, [indexName, searchParameters, searchClient, onClose, recentSearches, favoriteSearches, saveRecentSearch, initialQuery, placeholder, navigator, transformItems, disableUserPersonalization]);
285
+ }, [indexName, searchParameters, maxResultsPerGroup, searchClient, onClose, recentSearches, favoriteSearches, saveRecentSearch, initialQuery, placeholder, navigator, transformItems, disableUserPersonalization, insights, appId, apiKey]);
260
286
  var getEnvironmentProps = autocomplete.getEnvironmentProps,
261
287
  getRootProps = autocomplete.getRootProps,
262
288
  refresh = autocomplete.refresh;
@@ -64,5 +64,11 @@ export declare type DocSearchHit = {
64
64
  };
65
65
  };
66
66
  _distinctSeqID?: number;
67
+ __autocomplete_indexName?: string;
68
+ __autocomplete_queryID?: string;
69
+ __autocomplete_algoliaCredentials?: {
70
+ appId: string;
71
+ apiKey: string;
72
+ };
67
73
  };
68
74
  export {};
@@ -1 +1 @@
1
- export declare function groupBy<TValue extends Record<string, unknown>>(values: TValue[], predicate: (value: TValue) => string): Record<string, TValue[]>;
1
+ export declare function groupBy<TValue extends Record<string, unknown>>(values: TValue[], predicate: (value: TValue) => string, maxResultsPerGroup?: number): Record<string, TValue[]>;
@@ -1,4 +1,4 @@
1
- export function groupBy(values, predicate) {
1
+ export function groupBy(values, predicate, maxResultsPerGroup) {
2
2
  return values.reduce(function (acc, item) {
3
3
  var key = predicate(item);
4
4
 
@@ -8,7 +8,7 @@ export function groupBy(values, predicate) {
8
8
  // This acts as a frontend alternative to `distinct`.
9
9
 
10
10
 
11
- if (acc[key].length < 5) {
11
+ if (acc[key].length < (maxResultsPerGroup || 5)) {
12
12
  acc[key].push(item);
13
13
  }
14
14
 
@@ -1 +1 @@
1
- export declare const version = "3.3.5";
1
+ export declare const version = "3.5.0";
@@ -1 +1 @@
1
- export var version = '3.3.5';
1
+ export var version = '3.5.0';