@docsearch/react 3.4.0 → 3.5.1

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.
@@ -31,5 +31,6 @@ export interface DocSearchProps {
31
31
  getMissingResultsUrl?: ({ query }: {
32
32
  query: string;
33
33
  }) => string;
34
+ insights?: AutocompleteOptions<InternalDocSearchHit>['insights'];
34
35
  }
35
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, maxResultsPerGroup, 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;
@@ -65,7 +65,9 @@ export function DocSearchModal(_ref) {
65
65
  initialQueryFromProp = _ref$initialQuery === void 0 ? '' : _ref$initialQuery,
66
66
  _ref$translations = _ref.translations,
67
67
  translations = _ref$translations === void 0 ? {} : _ref$translations,
68
- getMissingResultsUrl = _ref.getMissingResultsUrl;
68
+ getMissingResultsUrl = _ref.getMissingResultsUrl,
69
+ _ref$insights = _ref.insights,
70
+ insights = _ref$insights === void 0 ? false : _ref$insights;
69
71
 
70
72
  var footerTranslations = translations.footer,
71
73
  searchBoxTranslations = translations.searchBox,
@@ -117,6 +119,18 @@ export function DocSearchModal(_ref) {
117
119
  recentSearches.add(search);
118
120
  }
119
121
  }, [favoriteSearches, recentSearches, disableUserPersonalization]);
122
+ var sendItemClickEvent = React.useCallback(function (item) {
123
+ if (!state.context.algoliaInsightsPlugin || !item.__autocomplete_id) return;
124
+ var insightsItem = item;
125
+ var insightsClickParams = {
126
+ eventName: 'Item Selected',
127
+ index: insightsItem.__autocomplete_indexName,
128
+ items: [insightsItem],
129
+ positions: [item.__autocomplete_id],
130
+ queryID: insightsItem.__autocomplete_queryID
131
+ };
132
+ state.context.algoliaInsightsPlugin.insights.clickedObjectIDsAfterSearch(insightsClickParams);
133
+ }, [state.context.algoliaInsightsPlugin]);
120
134
  var autocomplete = React.useMemo(function () {
121
135
  return createAutocomplete({
122
136
  id: 'docsearch',
@@ -129,6 +143,7 @@ export function DocSearchModal(_ref) {
129
143
  searchSuggestions: []
130
144
  }
131
145
  },
146
+ insights: insights,
132
147
  navigator: navigator,
133
148
  onStateChange: function onStateChange(props) {
134
149
  setState(props.state);
@@ -183,6 +198,7 @@ export function DocSearchModal(_ref) {
183
198
  }];
184
199
  }
185
200
 
201
+ var insightsActive = Boolean(insights);
186
202
  return searchClient.search([{
187
203
  query: query,
188
204
  indexName: indexName,
@@ -192,7 +208,8 @@ export function DocSearchModal(_ref) {
192
208
  snippetEllipsisText: '…',
193
209
  highlightPreTag: '<mark>',
194
210
  highlightPostTag: '</mark>',
195
- hitsPerPage: 20
211
+ hitsPerPage: 20,
212
+ clickAnalytics: insightsActive
196
213
  }, searchParameters)
197
214
  }]).catch(function (error) {
198
215
  // The Algolia `RetryError` happens when all the servers have
@@ -223,6 +240,19 @@ export function DocSearchModal(_ref) {
223
240
  setContext({
224
241
  nbHits: nbHits
225
242
  });
243
+ var insightsParams = {};
244
+
245
+ if (insightsActive) {
246
+ insightsParams = {
247
+ __autocomplete_indexName: indexName,
248
+ __autocomplete_queryID: results[0].queryID,
249
+ __autocomplete_algoliaCredentials: {
250
+ appId: appId,
251
+ apiKey: apiKey
252
+ }
253
+ };
254
+ }
255
+
226
256
  return Object.values(sources).map(function (items, index) {
227
257
  return {
228
258
  sourceId: "hits".concat(index),
@@ -244,11 +274,18 @@ export function DocSearchModal(_ref) {
244
274
  return item.hierarchy.lvl1;
245
275
  }, maxResultsPerGroup)).map(transformItems).map(function (groupedHits) {
246
276
  return groupedHits.map(function (item) {
247
- return _objectSpread(_objectSpread({}, item), {}, {
248
- __docsearch_parent: item.type !== 'lvl1' && groupedHits.find(function (siblingItem) {
249
- return siblingItem.type === 'lvl1' && siblingItem.hierarchy.lvl1 === item.hierarchy.lvl1;
250
- })
277
+ var parent = null;
278
+ var potentialParent = groupedHits.find(function (siblingItem) {
279
+ return siblingItem.type === 'lvl1' && siblingItem.hierarchy.lvl1 === item.hierarchy.lvl1;
251
280
  });
281
+
282
+ if (item.type !== 'lvl1' && potentialParent) {
283
+ parent = potentialParent;
284
+ }
285
+
286
+ return _objectSpread(_objectSpread({}, item), {}, {
287
+ __docsearch_parent: parent
288
+ }, insightsParams);
252
289
  });
253
290
  }).flat();
254
291
  }
@@ -257,7 +294,7 @@ export function DocSearchModal(_ref) {
257
294
  });
258
295
  }
259
296
  });
260
- }, [indexName, searchParameters, maxResultsPerGroup, searchClient, onClose, recentSearches, favoriteSearches, saveRecentSearch, initialQuery, placeholder, navigator, transformItems, disableUserPersonalization]);
297
+ }, [indexName, searchParameters, maxResultsPerGroup, searchClient, onClose, recentSearches, favoriteSearches, saveRecentSearch, initialQuery, placeholder, navigator, transformItems, disableUserPersonalization, insights, appId, apiKey]);
261
298
  var getEnvironmentProps = autocomplete.getEnvironmentProps,
262
299
  getRootProps = autocomplete.getRootProps,
263
300
  refresh = autocomplete.refresh;
@@ -365,6 +402,8 @@ export function DocSearchModal(_ref) {
365
402
  translations: screenStateTranslations,
366
403
  getMissingResultsUrl: getMissingResultsUrl,
367
404
  onItemClick: function onItemClick(item, event) {
405
+ // If insights is active, send insights click event
406
+ sendItemClickEvent(item);
368
407
  saveRecentSearch(item);
369
408
 
370
409
  if (!isModifierEvent(event)) {
@@ -64,5 +64,12 @@ 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
+ };
73
+ __autocomplete_id?: number;
67
74
  };
68
75
  export {};
@@ -0,0 +1,10 @@
1
+ import type { AutocompleteContext, AutocompleteInsightsApi, AutocompleteState, BaseItem } from '@algolia/autocomplete-core';
2
+ interface DocSearchContext extends AutocompleteContext {
3
+ algoliaInsightsPlugin?: {
4
+ insights: AutocompleteInsightsApi;
5
+ };
6
+ }
7
+ export interface DocSearchState<TItem extends BaseItem> extends AutocompleteState<TItem> {
8
+ context: DocSearchContext;
9
+ }
10
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -1,3 +1,4 @@
1
1
  export * from './DocSearchHit';
2
+ export * from './DocSearchState';
2
3
  export * from './InternalDocSearchHit';
3
4
  export * from './StoredDocSearchHit';
@@ -1,3 +1,4 @@
1
1
  export * from './DocSearchHit';
2
+ export * from './DocSearchState';
2
3
  export * from './InternalDocSearchHit';
3
4
  export * from './StoredDocSearchHit';
@@ -1 +1 @@
1
- export declare const version = "3.4.0";
1
+ export declare const version = "3.5.1";
@@ -1 +1 @@
1
- export var version = '3.4.0';
1
+ export var version = '3.5.1';