@docsearch/react 3.5.0 → 3.5.2

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.
@@ -51,12 +51,12 @@ export function DocSearch(props) {
51
51
  });
52
52
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(DocSearchButton, {
53
53
  ref: searchButtonRef,
54
- translations: props === null || props === void 0 ? void 0 : (_props$translations = props.translations) === null || _props$translations === void 0 ? void 0 : _props$translations.button,
54
+ translations: props === null || props === void 0 || (_props$translations = props.translations) === null || _props$translations === void 0 ? void 0 : _props$translations.button,
55
55
  onClick: onOpen
56
56
  }), isOpen && createPortal( /*#__PURE__*/React.createElement(DocSearchModal, _extends({}, props, {
57
57
  initialScrollY: window.scrollY,
58
58
  initialQuery: initialQuery,
59
- translations: props === null || props === void 0 ? void 0 : (_props$translations2 = props.translations) === null || _props$translations2 === void 0 ? void 0 : _props$translations2.modal,
59
+ translations: props === null || props === void 0 || (_props$translations2 = props.translations) === null || _props$translations2 === void 0 ? void 0 : _props$translations2.modal,
60
60
  onClose: onClose
61
61
  })), document.body));
62
62
  }
@@ -119,6 +119,18 @@ export function DocSearchModal(_ref) {
119
119
  recentSearches.add(search);
120
120
  }
121
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]);
122
134
  var autocomplete = React.useMemo(function () {
123
135
  return createAutocomplete({
124
136
  id: 'docsearch',
@@ -211,9 +223,9 @@ export function DocSearchModal(_ref) {
211
223
  throw error;
212
224
  }).then(function (_ref7) {
213
225
  var results = _ref7.results;
214
- var _results$ = results[0],
215
- hits = _results$.hits,
216
- nbHits = _results$.nbHits;
226
+ var firstResult = results[0];
227
+ var hits = firstResult.hits,
228
+ nbHits = firstResult.nbHits;
217
229
  var sources = groupBy(hits, function (hit) {
218
230
  return removeHighlightTags(hit);
219
231
  }, maxResultsPerGroup); // We store the `lvl0`s to display them as search suggestions
@@ -233,7 +245,7 @@ export function DocSearchModal(_ref) {
233
245
  if (insightsActive) {
234
246
  insightsParams = {
235
247
  __autocomplete_indexName: indexName,
236
- __autocomplete_queryID: results[0].queryID,
248
+ __autocomplete_queryID: firstResult.queryID,
237
249
  __autocomplete_algoliaCredentials: {
238
250
  appId: appId,
239
251
  apiKey: apiKey
@@ -390,6 +402,8 @@ export function DocSearchModal(_ref) {
390
402
  translations: screenStateTranslations,
391
403
  getMissingResultsUrl: getMissingResultsUrl,
392
404
  onItemClick: function onItemClick(item, event) {
405
+ // If insights is active, send insights click event
406
+ sendItemClickEvent(item);
393
407
  saveRecentSearch(item);
394
408
 
395
409
  if (!isModifierEvent(event)) {
@@ -70,5 +70,6 @@ export declare type DocSearchHit = {
70
70
  appId: string;
71
71
  apiKey: string;
72
72
  };
73
+ __autocomplete_id?: number;
73
74
  };
74
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,7 +1,7 @@
1
1
  var regexHighlightTags = /(<mark>|<\/mark>)/g;
2
2
  var regexHasHighlightTags = RegExp(regexHighlightTags.source);
3
3
  export function removeHighlightTags(hit) {
4
- var _internalDocSearchHit, _internalDocSearchHit2, _internalDocSearchHit3, _hit$_highlightResult, _hit$_highlightResult2;
4
+ var _internalDocSearchHit, _hit$_highlightResult;
5
5
 
6
6
  var internalDocSearchHit = hit;
7
7
 
@@ -9,7 +9,7 @@ export function removeHighlightTags(hit) {
9
9
  return hit.hierarchy.lvl0;
10
10
  }
11
11
 
12
- var _ref = (internalDocSearchHit.__docsearch_parent ? (_internalDocSearchHit = internalDocSearchHit.__docsearch_parent) === null || _internalDocSearchHit === void 0 ? void 0 : (_internalDocSearchHit2 = _internalDocSearchHit._highlightResult) === null || _internalDocSearchHit2 === void 0 ? void 0 : (_internalDocSearchHit3 = _internalDocSearchHit2.hierarchy) === null || _internalDocSearchHit3 === void 0 ? void 0 : _internalDocSearchHit3.lvl0 : (_hit$_highlightResult = hit._highlightResult) === null || _hit$_highlightResult === void 0 ? void 0 : (_hit$_highlightResult2 = _hit$_highlightResult.hierarchy) === null || _hit$_highlightResult2 === void 0 ? void 0 : _hit$_highlightResult2.lvl0) || {},
12
+ var _ref = (internalDocSearchHit.__docsearch_parent ? (_internalDocSearchHit = internalDocSearchHit.__docsearch_parent) === null || _internalDocSearchHit === void 0 || (_internalDocSearchHit = _internalDocSearchHit._highlightResult) === null || _internalDocSearchHit === void 0 || (_internalDocSearchHit = _internalDocSearchHit.hierarchy) === null || _internalDocSearchHit === void 0 ? void 0 : _internalDocSearchHit.lvl0 : (_hit$_highlightResult = hit._highlightResult) === null || _hit$_highlightResult === void 0 || (_hit$_highlightResult = _hit$_highlightResult.hierarchy) === null || _hit$_highlightResult === void 0 ? void 0 : _hit$_highlightResult.lvl0) || {},
13
13
  value = _ref.value;
14
14
 
15
15
  return value && regexHasHighlightTags.test(value) ? value.replace(regexHighlightTags, '') : value;
@@ -1 +1 @@
1
- export declare const version = "3.5.0";
1
+ export declare const version = "3.5.2";
@@ -1 +1 @@
1
- export var version = '3.5.0';
1
+ export var version = '3.5.2';