@algolia/autocomplete-core 1.11.1 → 1.12.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.
@@ -28,6 +28,7 @@ export function createAutocomplete(options) {
28
28
  navigator: props.navigator
29
29
  }, setters));
30
30
  function onStoreStateChange(_ref) {
31
+ var _state$context, _state$context$algoli;
31
32
  var prevState = _ref.prevState,
32
33
  state = _ref.state;
33
34
  props.onStateChange(_objectSpread({
@@ -36,6 +37,13 @@ export function createAutocomplete(options) {
36
37
  refresh: refresh,
37
38
  navigator: props.navigator
38
39
  }, setters));
40
+ if (!isAlgoliaInsightsPluginEnabled() && (_state$context = state.context) !== null && _state$context !== void 0 && (_state$context$algoli = _state$context.algoliaInsightsPlugin) !== null && _state$context$algoli !== void 0 && _state$context$algoli.__automaticInsights && props.insights !== false) {
41
+ var plugin = createAlgoliaInsightsPlugin({
42
+ __autocomplete_clickAnalytics: false
43
+ });
44
+ props.plugins.push(plugin);
45
+ subscribePlugins([plugin]);
46
+ }
39
47
  }
40
48
  function refresh() {
41
49
  return onInput(_objectSpread({
@@ -50,34 +58,40 @@ export function createAutocomplete(options) {
50
58
  store: store
51
59
  }, setters));
52
60
  }
53
- if (options.insights && !props.plugins.some(function (plugin) {
54
- return plugin.name === 'aa.algoliaInsightsPlugin';
55
- })) {
56
- var insightsParams = typeof options.insights === 'boolean' ? {} : options.insights;
61
+ function subscribePlugins(plugins) {
62
+ plugins.forEach(function (plugin) {
63
+ var _plugin$subscribe;
64
+ return (_plugin$subscribe = plugin.subscribe) === null || _plugin$subscribe === void 0 ? void 0 : _plugin$subscribe.call(plugin, _objectSpread(_objectSpread({}, setters), {}, {
65
+ navigator: props.navigator,
66
+ refresh: refresh,
67
+ onSelect: function onSelect(fn) {
68
+ subscribers.push({
69
+ onSelect: fn
70
+ });
71
+ },
72
+ onActive: function onActive(fn) {
73
+ subscribers.push({
74
+ onActive: fn
75
+ });
76
+ },
77
+ onResolve: function onResolve(fn) {
78
+ subscribers.push({
79
+ onResolve: fn
80
+ });
81
+ }
82
+ }));
83
+ });
84
+ }
85
+ function isAlgoliaInsightsPluginEnabled() {
86
+ return props.plugins.some(function (plugin) {
87
+ return plugin.name === 'aa.algoliaInsightsPlugin';
88
+ });
89
+ }
90
+ if (props.insights && !isAlgoliaInsightsPluginEnabled()) {
91
+ var insightsParams = typeof props.insights === 'boolean' ? {} : props.insights;
57
92
  props.plugins.push(createAlgoliaInsightsPlugin(insightsParams));
58
93
  }
59
- props.plugins.forEach(function (plugin) {
60
- var _plugin$subscribe;
61
- return (_plugin$subscribe = plugin.subscribe) === null || _plugin$subscribe === void 0 ? void 0 : _plugin$subscribe.call(plugin, _objectSpread(_objectSpread({}, setters), {}, {
62
- navigator: props.navigator,
63
- refresh: refresh,
64
- onSelect: function onSelect(fn) {
65
- subscribers.push({
66
- onSelect: fn
67
- });
68
- },
69
- onActive: function onActive(fn) {
70
- subscribers.push({
71
- onActive: fn
72
- });
73
- },
74
- onResolve: function onResolve(fn) {
75
- subscribers.push({
76
- onResolve: fn
77
- });
78
- }
79
- }));
80
- });
94
+ subscribePlugins(props.plugins);
81
95
  injectMetadata({
82
96
  metadata: getMetadata({
83
97
  plugins: props.plugins,
@@ -26,7 +26,7 @@ export function getDefaultProps(props, pluginSubscribers) {
26
26
  autoFocus: false,
27
27
  defaultActiveItemId: null,
28
28
  stallThreshold: 300,
29
- insights: false,
29
+ insights: undefined,
30
30
  environment: environment,
31
31
  shouldPanelOpen: function shouldPanelOpen(_ref) {
32
32
  var state = _ref.state;
@@ -28,7 +28,8 @@ export function onInput(_ref) {
28
28
  setIsOpen = setters.setIsOpen,
29
29
  setQuery = setters.setQuery,
30
30
  setActiveItemId = setters.setActiveItemId,
31
- setStatus = setters.setStatus;
31
+ setStatus = setters.setStatus,
32
+ setContext = setters.setContext;
32
33
  setQuery(query);
33
34
  setActiveItemId(props.defaultActiveItemId);
34
35
  if (!query && props.openOnFocus === false) {
@@ -78,6 +79,20 @@ export function onInput(_ref) {
78
79
  return preResolve(itemsOrDescription, source.sourceId, store.getState());
79
80
  });
80
81
  })).then(resolve).then(function (responses) {
82
+ var __automaticInsights = responses.some(function (_ref2) {
83
+ var items = _ref2.items;
84
+ return isSearchResponseWithAutomaticInsightsFlag(items);
85
+ });
86
+
87
+ // No need to pollute the context if `__automaticInsights=false`
88
+ if (__automaticInsights) {
89
+ var _store$getState$conte;
90
+ setContext({
91
+ algoliaInsightsPlugin: _objectSpread(_objectSpread({}, ((_store$getState$conte = store.getState().context) === null || _store$getState$conte === void 0 ? void 0 : _store$getState$conte.algoliaInsightsPlugin) || {}), {}, {
92
+ __automaticInsights: __automaticInsights
93
+ })
94
+ });
95
+ }
81
96
  return postResolve(responses, sources, store);
82
97
  }).then(function (collections) {
83
98
  return reshape({
@@ -122,4 +137,7 @@ export function onInput(_ref) {
122
137
  }
123
138
  });
124
139
  return store.pendingRequests.add(request);
140
+ }
141
+ function isSearchResponseWithAutomaticInsightsFlag(items) {
142
+ return !Array.isArray(items) && Boolean(items === null || items === void 0 ? void 0 : items._automaticInsights);
125
143
  }
@@ -88,7 +88,7 @@ export function onKeyDown(_ref) {
88
88
  } else if (event.key === 'Tab') {
89
89
  store.dispatch('blur', null);
90
90
 
91
- // Hitting the `Escape` key signals the end of a user interaction with the
91
+ // Hitting the `Tab` key signals the end of a user interaction with the
92
92
  // autocomplete. At this point, we should ignore any requests that are still
93
93
  // pending and could reopen the panel once they resolve, because that would
94
94
  // result in an unsolicited UI behavior.
@@ -1,5 +1,6 @@
1
1
  import type { ExecuteResponse, RequesterDescription, TransformResponse } from '@algolia/autocomplete-preset-algolia';
2
- import { MultipleQueriesQuery, SearchForFacetValuesResponse, SearchResponse } from '@algolia/client-search';
2
+ import type { SearchResponse } from '@algolia/autocomplete-shared';
3
+ import { MultipleQueriesQuery, SearchForFacetValuesResponse } from '@algolia/client-search';
3
4
  import { AutocompleteState, AutocompleteStore, BaseItem, InternalAutocompleteSource } from './types';
4
5
  declare type RequestDescriptionPreResolved<TItem extends BaseItem> = Pick<RequesterDescription<TItem>, 'execute' | 'requesterId' | 'searchClient' | 'transformResponse'> & {
5
6
  requests: Array<{
@@ -103,6 +103,7 @@ export var stateReducer = function stateReducer(state, action) {
103
103
  // we lose track of the highlighted index. (Query-suggestions use-case)
104
104
  action.props.openOnFocus === true ? action.props.defaultActiveItemId : null,
105
105
  status: 'idle',
106
+ completion: null,
106
107
  query: ''
107
108
  });
108
109
  }
@@ -2,17 +2,21 @@ export * from '@algolia/autocomplete-shared/dist/esm/core';
2
2
  export * from './AutocompleteStore';
3
3
  export * from './AutocompleteSubscribers';
4
4
  import { CreateAlgoliaInsightsPluginParams, AutocompleteInsightsApi as _AutocompleteInsightsApi, AlgoliaInsightsHit as _AlgoliaInsightsHit } from '@algolia/autocomplete-plugin-algolia-insights';
5
- import { AutocompleteOptions as _AutocompleteOptions, BaseItem } from '@algolia/autocomplete-shared/dist/esm/core';
5
+ import { AutocompleteOptions as _AutocompleteOptions, InternalAutocompleteOptions as _InternalAutocompleteOptions, BaseItem } from '@algolia/autocomplete-shared/dist/esm/core';
6
6
  export declare type AutocompleteInsightsApi = _AutocompleteInsightsApi;
7
7
  export declare type AlgoliaInsightsHit = _AlgoliaInsightsHit;
8
- export interface AutocompleteOptions<TItem extends BaseItem> extends _AutocompleteOptions<TItem> {
8
+ declare type InsightsOption = {
9
9
  /**
10
10
  * Whether to enable the Insights plugin and load the Insights library if it has not been loaded yet.
11
11
  *
12
12
  * See [**autocomplete-plugin-algolia-insights**](https://www.algolia.com/doc/ui-libraries/autocomplete/api-reference/autocomplete-plugin-algolia-insights/) for more information.
13
13
  *
14
- * @default false
14
+ * @default undefined
15
15
  * @link https://www.algolia.com/doc/ui-libraries/autocomplete/api-reference/autocomplete-js/autocomplete/#param-insights
16
16
  */
17
- insights?: CreateAlgoliaInsightsPluginParams | boolean;
17
+ insights?: CreateAlgoliaInsightsPluginParams | boolean | undefined;
18
+ };
19
+ export interface AutocompleteOptions<TItem extends BaseItem> extends _AutocompleteOptions<TItem>, InsightsOption {
20
+ }
21
+ export interface InternalAutocompleteOptions<TItem extends BaseItem> extends _InternalAutocompleteOptions<TItem>, InsightsOption {
18
22
  }
@@ -1,4 +1,5 @@
1
- import type { SearchForFacetValuesResponse, SearchResponse } from '@algolia/client-search';
1
+ import type { SearchResponse } from '@algolia/autocomplete-shared';
2
+ import type { SearchForFacetValuesResponse } from '@algolia/client-search';
2
3
  export declare function mapToAlgoliaResponse<THit>(rawResults: Array<SearchResponse<THit> | SearchForFacetValuesResponse>): {
3
4
  results: (SearchResponse<THit> | SearchForFacetValuesResponse)[];
4
5
  hits: import("@algolia/client-search").Hit<THit>[][];
@@ -1,4 +1,4 @@
1
- /*! @algolia/autocomplete-core 1.11.1 | MIT License | © Algolia, Inc. and contributors | https://github.com/algolia/autocomplete */
1
+ /*! @algolia/autocomplete-core 1.12.1 | MIT License | © Algolia, Inc. and contributors | https://github.com/algolia/autocomplete */
2
2
  (function (global, factory) {
3
3
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
4
4
  typeof define === 'function' && define.amd ? define(['exports'], factory) :
@@ -291,7 +291,7 @@
291
291
  return undefined;
292
292
  }
293
293
 
294
- var version = '1.11.1';
294
+ var version = '1.12.1';
295
295
 
296
296
  var userAgents = [{
297
297
  segment: 'autocomplete-core',
@@ -824,7 +824,8 @@
824
824
  providedInsightsClient = _getOptions.insightsClient,
825
825
  onItemsChange = _getOptions.onItemsChange,
826
826
  onSelectEvent = _getOptions.onSelect,
827
- onActiveEvent = _getOptions.onActive;
827
+ onActiveEvent = _getOptions.onActive,
828
+ __autocomplete_clickAnalytics = _getOptions.__autocomplete_clickAnalytics;
828
829
  var insightsClient = providedInsightsClient;
829
830
  if (!providedInsightsClient) {
830
831
  safelyRunOnBrowser(function (_ref2) {
@@ -887,9 +888,9 @@
887
888
  function setInsightsContext(userToken) {
888
889
  setContext({
889
890
  algoliaInsightsPlugin: {
890
- __algoliaSearchParameters: _objectSpread({
891
+ __algoliaSearchParameters: _objectSpread(_objectSpread({}, __autocomplete_clickAnalytics ? {
891
892
  clickAnalytics: true
892
- }, userToken ? {
893
+ } : {}), userToken ? {
893
894
  userToken: userToken
894
895
  } : {}),
895
896
  insights: insights
@@ -954,27 +955,36 @@
954
955
  __autocomplete_pluginOptions: options
955
956
  };
956
957
  }
958
+ function getAlgoliaSources() {
959
+ var _context$algoliaInsig;
960
+ var algoliaSourceBase = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
961
+ var context = arguments.length > 1 ? arguments[1] : undefined;
962
+ return [].concat(_toConsumableArray(algoliaSourceBase), ['autocomplete-internal'], _toConsumableArray((_context$algoliaInsig = context.algoliaInsightsPlugin) !== null && _context$algoliaInsig !== void 0 && _context$algoliaInsig.__automaticInsights ? ['autocomplete-automatic'] : []));
963
+ }
957
964
  function getOptions(options) {
958
965
  return _objectSpread({
959
966
  onItemsChange: function onItemsChange(_ref8) {
960
967
  var insights = _ref8.insights,
961
- insightsEvents = _ref8.insightsEvents;
968
+ insightsEvents = _ref8.insightsEvents,
969
+ state = _ref8.state;
962
970
  insights.viewedObjectIDs.apply(insights, _toConsumableArray(insightsEvents.map(function (event) {
963
971
  return _objectSpread(_objectSpread({}, event), {}, {
964
- algoliaSource: [].concat(_toConsumableArray(event.algoliaSource || []), ['autocomplete-internal'])
972
+ algoliaSource: getAlgoliaSources(event.algoliaSource, state.context)
965
973
  });
966
974
  })));
967
975
  },
968
976
  onSelect: function onSelect(_ref9) {
969
977
  var insights = _ref9.insights,
970
- insightsEvents = _ref9.insightsEvents;
978
+ insightsEvents = _ref9.insightsEvents,
979
+ state = _ref9.state;
971
980
  insights.clickedObjectIDsAfterSearch.apply(insights, _toConsumableArray(insightsEvents.map(function (event) {
972
981
  return _objectSpread(_objectSpread({}, event), {}, {
973
- algoliaSource: [].concat(_toConsumableArray(event.algoliaSource || []), ['autocomplete-internal'])
982
+ algoliaSource: getAlgoliaSources(event.algoliaSource, state.context)
974
983
  });
975
984
  })));
976
985
  },
977
- onActive: noop
986
+ onActive: noop,
987
+ __autocomplete_clickAnalytics: true
978
988
  }, options);
979
989
  }
980
990
  function loadInsights(environment) {
@@ -1378,7 +1388,7 @@
1378
1388
  autoFocus: false,
1379
1389
  defaultActiveItemId: null,
1380
1390
  stallThreshold: 300,
1381
- insights: false,
1391
+ insights: undefined,
1382
1392
  environment: environment,
1383
1393
  shouldPanelOpen: function shouldPanelOpen(_ref) {
1384
1394
  var state = _ref.state;
@@ -1639,7 +1649,8 @@
1639
1649
  setIsOpen = setters.setIsOpen,
1640
1650
  setQuery = setters.setQuery,
1641
1651
  setActiveItemId = setters.setActiveItemId,
1642
- setStatus = setters.setStatus;
1652
+ setStatus = setters.setStatus,
1653
+ setContext = setters.setContext;
1643
1654
  setQuery(query);
1644
1655
  setActiveItemId(props.defaultActiveItemId);
1645
1656
  if (!query && props.openOnFocus === false) {
@@ -1689,6 +1700,20 @@
1689
1700
  return preResolve(itemsOrDescription, source.sourceId, store.getState());
1690
1701
  });
1691
1702
  })).then(resolve).then(function (responses) {
1703
+ var __automaticInsights = responses.some(function (_ref2) {
1704
+ var items = _ref2.items;
1705
+ return isSearchResponseWithAutomaticInsightsFlag(items);
1706
+ });
1707
+
1708
+ // No need to pollute the context if `__automaticInsights=false`
1709
+ if (__automaticInsights) {
1710
+ var _store$getState$conte;
1711
+ setContext({
1712
+ algoliaInsightsPlugin: _objectSpread2(_objectSpread2({}, ((_store$getState$conte = store.getState().context) === null || _store$getState$conte === void 0 ? void 0 : _store$getState$conte.algoliaInsightsPlugin) || {}), {}, {
1713
+ __automaticInsights: __automaticInsights
1714
+ })
1715
+ });
1716
+ }
1692
1717
  return postResolve(responses, sources, store);
1693
1718
  }).then(function (collections) {
1694
1719
  return reshape({
@@ -1734,6 +1759,9 @@
1734
1759
  });
1735
1760
  return store.pendingRequests.add(request);
1736
1761
  }
1762
+ function isSearchResponseWithAutomaticInsightsFlag(items) {
1763
+ return !Array.isArray(items) && Boolean(items === null || items === void 0 ? void 0 : items._automaticInsights);
1764
+ }
1737
1765
 
1738
1766
  var _excluded$1 = ["event", "props", "refresh", "store"];
1739
1767
  function onKeyDown(_ref) {
@@ -1815,7 +1843,7 @@
1815
1843
  } else if (event.key === 'Tab') {
1816
1844
  store.dispatch('blur', null);
1817
1845
 
1818
- // Hitting the `Escape` key signals the end of a user interaction with the
1846
+ // Hitting the `Tab` key signals the end of a user interaction with the
1819
1847
  // autocomplete. At this point, we should ignore any requests that are still
1820
1848
  // pending and could reopen the panel once they resolve, because that would
1821
1849
  // result in an unsolicited UI behavior.
@@ -2367,6 +2395,7 @@
2367
2395
  // we lose track of the highlighted index. (Query-suggestions use-case)
2368
2396
  action.props.openOnFocus === true ? action.props.defaultActiveItemId : null,
2369
2397
  status: 'idle',
2398
+ completion: null,
2370
2399
  query: ''
2371
2400
  });
2372
2401
  }
@@ -2422,6 +2451,7 @@
2422
2451
  navigator: props.navigator
2423
2452
  }, setters));
2424
2453
  function onStoreStateChange(_ref) {
2454
+ var _state$context, _state$context$algoli;
2425
2455
  var prevState = _ref.prevState,
2426
2456
  state = _ref.state;
2427
2457
  props.onStateChange(_objectSpread2({
@@ -2430,6 +2460,13 @@
2430
2460
  refresh: refresh,
2431
2461
  navigator: props.navigator
2432
2462
  }, setters));
2463
+ if (!isAlgoliaInsightsPluginEnabled() && (_state$context = state.context) !== null && _state$context !== void 0 && (_state$context$algoli = _state$context.algoliaInsightsPlugin) !== null && _state$context$algoli !== void 0 && _state$context$algoli.__automaticInsights && props.insights !== false) {
2464
+ var plugin = createAlgoliaInsightsPlugin({
2465
+ __autocomplete_clickAnalytics: false
2466
+ });
2467
+ props.plugins.push(plugin);
2468
+ subscribePlugins([plugin]);
2469
+ }
2433
2470
  }
2434
2471
  function refresh() {
2435
2472
  return onInput(_objectSpread2({
@@ -2444,34 +2481,40 @@
2444
2481
  store: store
2445
2482
  }, setters));
2446
2483
  }
2447
- if (options.insights && !props.plugins.some(function (plugin) {
2448
- return plugin.name === 'aa.algoliaInsightsPlugin';
2449
- })) {
2450
- var insightsParams = typeof options.insights === 'boolean' ? {} : options.insights;
2484
+ function subscribePlugins(plugins) {
2485
+ plugins.forEach(function (plugin) {
2486
+ var _plugin$subscribe;
2487
+ return (_plugin$subscribe = plugin.subscribe) === null || _plugin$subscribe === void 0 ? void 0 : _plugin$subscribe.call(plugin, _objectSpread2(_objectSpread2({}, setters), {}, {
2488
+ navigator: props.navigator,
2489
+ refresh: refresh,
2490
+ onSelect: function onSelect(fn) {
2491
+ subscribers.push({
2492
+ onSelect: fn
2493
+ });
2494
+ },
2495
+ onActive: function onActive(fn) {
2496
+ subscribers.push({
2497
+ onActive: fn
2498
+ });
2499
+ },
2500
+ onResolve: function onResolve(fn) {
2501
+ subscribers.push({
2502
+ onResolve: fn
2503
+ });
2504
+ }
2505
+ }));
2506
+ });
2507
+ }
2508
+ function isAlgoliaInsightsPluginEnabled() {
2509
+ return props.plugins.some(function (plugin) {
2510
+ return plugin.name === 'aa.algoliaInsightsPlugin';
2511
+ });
2512
+ }
2513
+ if (props.insights && !isAlgoliaInsightsPluginEnabled()) {
2514
+ var insightsParams = typeof props.insights === 'boolean' ? {} : props.insights;
2451
2515
  props.plugins.push(createAlgoliaInsightsPlugin(insightsParams));
2452
2516
  }
2453
- props.plugins.forEach(function (plugin) {
2454
- var _plugin$subscribe;
2455
- return (_plugin$subscribe = plugin.subscribe) === null || _plugin$subscribe === void 0 ? void 0 : _plugin$subscribe.call(plugin, _objectSpread2(_objectSpread2({}, setters), {}, {
2456
- navigator: props.navigator,
2457
- refresh: refresh,
2458
- onSelect: function onSelect(fn) {
2459
- subscribers.push({
2460
- onSelect: fn
2461
- });
2462
- },
2463
- onActive: function onActive(fn) {
2464
- subscribers.push({
2465
- onActive: fn
2466
- });
2467
- },
2468
- onResolve: function onResolve(fn) {
2469
- subscribers.push({
2470
- onResolve: fn
2471
- });
2472
- }
2473
- }));
2474
- });
2517
+ subscribePlugins(props.plugins);
2475
2518
  injectMetadata({
2476
2519
  metadata: getMetadata({
2477
2520
  plugins: props.plugins,