react-instantsearch-core 7.24.0 → 7.26.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.
Files changed (140) hide show
  1. package/dist/cjs/components/Configure.js +11 -17
  2. package/dist/cjs/components/DynamicWidgets.js +46 -53
  3. package/dist/cjs/components/Index.js +19 -22
  4. package/dist/cjs/components/InstantSearch.js +34 -36
  5. package/dist/cjs/components/InstantSearchSSRProvider.js +31 -41
  6. package/dist/cjs/components/InstantSearchServerContext.js +6 -10
  7. package/dist/cjs/connectors/useAutocomplete.js +8 -10
  8. package/dist/cjs/connectors/useBreadcrumb.js +8 -10
  9. package/dist/cjs/connectors/useChat.js +8 -10
  10. package/dist/cjs/connectors/useClearRefinements.js +8 -10
  11. package/dist/cjs/connectors/useConfigure.js +10 -12
  12. package/dist/cjs/connectors/useCurrentRefinements.js +8 -10
  13. package/dist/cjs/connectors/useDynamicWidgets.js +14 -20
  14. package/dist/cjs/connectors/useFilterSuggestions.js +8 -10
  15. package/dist/cjs/connectors/useFrequentlyBoughtTogether.js +8 -10
  16. package/dist/cjs/connectors/useGeoSearch.js +8 -10
  17. package/dist/cjs/connectors/useHierarchicalMenu.js +8 -10
  18. package/dist/cjs/connectors/useHits.js +8 -10
  19. package/dist/cjs/connectors/useHitsPerPage.js +8 -10
  20. package/dist/cjs/connectors/useInfiniteHits.js +8 -10
  21. package/dist/cjs/connectors/useLookingSimilar.js +8 -10
  22. package/dist/cjs/connectors/useMenu.js +8 -10
  23. package/dist/cjs/connectors/useNumericMenu.js +8 -10
  24. package/dist/cjs/connectors/usePagination.js +8 -10
  25. package/dist/cjs/connectors/usePoweredBy.js +18 -19
  26. package/dist/cjs/connectors/useQueryRules.js +8 -10
  27. package/dist/cjs/connectors/useRange.js +8 -10
  28. package/dist/cjs/connectors/useRefinementList.js +8 -10
  29. package/dist/cjs/connectors/useRelatedProducts.js +8 -10
  30. package/dist/cjs/connectors/useSearchBox.js +8 -10
  31. package/dist/cjs/connectors/useSortBy.js +8 -10
  32. package/dist/cjs/connectors/useStats.js +8 -10
  33. package/dist/cjs/connectors/useToggleRefinement.js +8 -10
  34. package/dist/cjs/connectors/useTrendingItems.js +8 -10
  35. package/dist/cjs/hooks/useConnector.js +123 -139
  36. package/dist/cjs/hooks/useInstantSearch.js +59 -59
  37. package/dist/cjs/index.js +90 -519
  38. package/dist/cjs/lib/IndexContext.js +6 -10
  39. package/dist/cjs/lib/InstantSearchContext.js +6 -10
  40. package/dist/cjs/lib/InstantSearchRSCContext.js +11 -12
  41. package/dist/cjs/lib/InstantSearchSSRContext.js +6 -10
  42. package/dist/cjs/lib/createSearchResults.js +24 -25
  43. package/dist/cjs/lib/dequal.js +33 -40
  44. package/dist/cjs/lib/getIndexSearchResults.js +23 -30
  45. package/dist/cjs/lib/invariant.js +11 -17
  46. package/dist/cjs/lib/noop.js +8 -6
  47. package/dist/cjs/lib/use.js +26 -11
  48. package/dist/cjs/lib/useAppIdAndApiKey.js +10 -12
  49. package/dist/cjs/lib/useForceUpdate.js +12 -21
  50. package/dist/cjs/lib/useIndex.js +40 -37
  51. package/dist/cjs/lib/useIndexContext.js +11 -12
  52. package/dist/cjs/lib/useInstantSearchApi.js +174 -197
  53. package/dist/cjs/lib/useInstantSearchContext.js +11 -12
  54. package/dist/cjs/lib/useInstantSearchSSRContext.js +8 -9
  55. package/dist/cjs/lib/useInstantSearchServerContext.js +8 -9
  56. package/dist/cjs/lib/useIsomorphicLayoutEffect.js +6 -8
  57. package/dist/cjs/lib/useRSCContext.js +8 -9
  58. package/dist/cjs/lib/useSearchResults.js +54 -62
  59. package/dist/cjs/lib/useSearchState.js +53 -60
  60. package/dist/cjs/lib/useStableValue.js +15 -24
  61. package/dist/cjs/lib/useWidget.js +99 -95
  62. package/dist/cjs/lib/warn.js +11 -35
  63. package/dist/cjs/lib/wrapPromiseWithState.js +23 -26
  64. package/dist/cjs/package.json +4 -1
  65. package/dist/cjs/server/getServerState.js +81 -91
  66. package/dist/cjs/server/index.js +6 -15
  67. package/dist/cjs/version.js +6 -6
  68. package/dist/es/components/Configure.js +11 -13
  69. package/dist/es/components/DynamicWidgets.js +45 -47
  70. package/dist/es/components/Index.js +19 -17
  71. package/dist/es/components/InstantSearch.js +33 -29
  72. package/dist/es/components/InstantSearchSSRProvider.js +30 -35
  73. package/dist/es/components/InstantSearchServerContext.js +4 -4
  74. package/dist/es/connectors/useAutocomplete.js +8 -5
  75. package/dist/es/connectors/useBreadcrumb.js +8 -5
  76. package/dist/es/connectors/useChat.js +8 -5
  77. package/dist/es/connectors/useClearRefinements.js +8 -5
  78. package/dist/es/connectors/useConfigure.js +10 -7
  79. package/dist/es/connectors/useCurrentRefinements.js +8 -5
  80. package/dist/es/connectors/useDynamicWidgets.js +14 -15
  81. package/dist/es/connectors/useFilterSuggestions.js +8 -5
  82. package/dist/es/connectors/useFrequentlyBoughtTogether.js +8 -5
  83. package/dist/es/connectors/useGeoSearch.js +8 -5
  84. package/dist/es/connectors/useHierarchicalMenu.js +8 -5
  85. package/dist/es/connectors/useHits.js +8 -5
  86. package/dist/es/connectors/useHitsPerPage.js +8 -5
  87. package/dist/es/connectors/useInfiniteHits.js +8 -5
  88. package/dist/es/connectors/useLookingSimilar.js +8 -5
  89. package/dist/es/connectors/useMenu.js +8 -5
  90. package/dist/es/connectors/useNumericMenu.js +8 -5
  91. package/dist/es/connectors/usePagination.js +8 -5
  92. package/dist/es/connectors/usePoweredBy.js +18 -15
  93. package/dist/es/connectors/useQueryRules.js +8 -5
  94. package/dist/es/connectors/useRange.js +8 -5
  95. package/dist/es/connectors/useRefinementList.js +8 -5
  96. package/dist/es/connectors/useRelatedProducts.js +8 -5
  97. package/dist/es/connectors/useSearchBox.js +8 -5
  98. package/dist/es/connectors/useSortBy.js +8 -5
  99. package/dist/es/connectors/useStats.js +8 -5
  100. package/dist/es/connectors/useToggleRefinement.js +8 -5
  101. package/dist/es/connectors/useTrendingItems.js +8 -5
  102. package/dist/es/hooks/useConnector.js +122 -134
  103. package/dist/es/hooks/useInstantSearch.js +58 -54
  104. package/dist/es/index.js +43 -43
  105. package/dist/es/lib/IndexContext.js +4 -4
  106. package/dist/es/lib/InstantSearchContext.js +4 -4
  107. package/dist/es/lib/InstantSearchRSCContext.js +10 -7
  108. package/dist/es/lib/InstantSearchSSRContext.js +4 -4
  109. package/dist/es/lib/createSearchResults.js +23 -19
  110. package/dist/es/lib/dequal.js +33 -36
  111. package/dist/es/lib/getIndexSearchResults.js +23 -26
  112. package/dist/es/lib/invariant.js +10 -12
  113. package/dist/es/lib/noop.js +7 -1
  114. package/dist/es/lib/use.js +4 -2
  115. package/dist/es/lib/useAppIdAndApiKey.js +9 -7
  116. package/dist/es/lib/useForceUpdate.js +9 -15
  117. package/dist/es/lib/useIndex.js +39 -31
  118. package/dist/es/lib/useIndexContext.js +10 -7
  119. package/dist/es/lib/useInstantSearchApi.js +174 -191
  120. package/dist/es/lib/useInstantSearchContext.js +10 -7
  121. package/dist/es/lib/useInstantSearchSSRContext.js +7 -4
  122. package/dist/es/lib/useInstantSearchServerContext.js +7 -4
  123. package/dist/es/lib/useIsomorphicLayoutEffect.js +4 -3
  124. package/dist/es/lib/useRSCContext.js +7 -4
  125. package/dist/es/lib/useSearchResults.js +53 -57
  126. package/dist/es/lib/useSearchState.js +52 -55
  127. package/dist/es/lib/useStableValue.js +14 -19
  128. package/dist/es/lib/useWidget.js +98 -90
  129. package/dist/es/lib/warn.js +9 -29
  130. package/dist/es/lib/wrapPromiseWithState.js +23 -22
  131. package/dist/es/package.json +4 -0
  132. package/dist/es/server/getServerState.js +80 -85
  133. package/dist/es/server/index.js +1 -1
  134. package/dist/es/version.d.ts +1 -1
  135. package/dist/es/version.js +3 -1
  136. package/dist/umd/ReactInstantSearchCore.js +17488 -22329
  137. package/dist/umd/ReactInstantSearchCore.min.js +3 -3
  138. package/package.json +7 -11
  139. package/dist/umd/ReactInstantSearchCore.js.map +0 -1
  140. package/dist/umd/ReactInstantSearchCore.min.js.map +0 -1
@@ -1,132 +1,126 @@
1
- "use strict";
1
+ 'use strict';
2
2
 
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.useInstantSearchApi = useInstantSearchApi;
7
- var _InstantSearch = _interopRequireWildcard(require("instantsearch.js/cjs/lib/InstantSearch"));
8
- var _react = require("react");
9
- var _shim = require("use-sync-external-store/shim");
10
- var _version = _interopRequireDefault(require("../version"));
11
- var _dequal = require("./dequal");
12
- var _useForceUpdate = require("./useForceUpdate");
13
- var _useInstantSearchServerContext = require("./useInstantSearchServerContext");
14
- var _useInstantSearchSSRContext = require("./useInstantSearchSSRContext");
15
- var _useRSCContext2 = require("./useRSCContext");
16
- var _warn = require("./warn");
17
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
18
- function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
19
- function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
20
- function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
21
- function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
22
- function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
23
- function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
24
- function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
25
- var defaultUserAgents = ["react (".concat(_react.version, ")"), "react-instantsearch (".concat(_version.default, ")"), "react-instantsearch-core (".concat(_version.default, ")")];
26
- var serverUserAgent = "react-instantsearch-server (".concat(_version.default, ")");
3
+ var _object_spread = require('@swc/helpers/_/_object_spread');
4
+ var _to_consumable_array = require('@swc/helpers/_/_to_consumable_array');
5
+ var InstantSearch = require('instantsearch.js/cjs/lib/InstantSearch');
6
+ var React = require('react');
7
+ var shim = require('use-sync-external-store/shim');
8
+ var version = require('../version.js');
9
+ var dequal = require('./dequal.js');
10
+ var useForceUpdate = require('./useForceUpdate.js');
11
+ var useInstantSearchServerContext = require('./useInstantSearchServerContext.js');
12
+ var useInstantSearchSSRContext = require('./useInstantSearchSSRContext.js');
13
+ var useRSCContext = require('./useRSCContext.js');
14
+
15
+ var defaultUserAgents = [
16
+ "react (".concat(React.version, ")"),
17
+ "react-instantsearch (".concat(version.default, ")"),
18
+ "react-instantsearch-core (".concat(version.default, ")")
19
+ ];
20
+ var serverUserAgent = "react-instantsearch-server (".concat(version.default, ")");
27
21
  var nextUserAgent = function nextUserAgent(nextVersion) {
28
- return nextVersion ? "next.js (".concat(nextVersion, ")") : null;
22
+ return nextVersion ? "next.js (".concat(nextVersion, ")") : null;
29
23
  };
30
24
  function useInstantSearchApi(props) {
31
- var forceUpdate = (0, _useForceUpdate.useForceUpdate)();
32
- var serverContext = (0, _useInstantSearchServerContext.useInstantSearchServerContext)();
33
- var serverState = (0, _useInstantSearchSSRContext.useInstantSearchSSRContext)();
34
- var _useRSCContext = (0, _useRSCContext2.useRSCContext)(),
35
- waitForResultsRef = _useRSCContext.waitForResultsRef;
36
- var initialResults = serverState === null || serverState === void 0 ? void 0 : serverState.initialResults;
37
- var prevPropsRef = (0, _react.useRef)(props);
38
- var shouldRenderAtOnce = serverContext || initialResults || waitForResultsRef;
39
- var searchRef = (0, _react.useRef)(null);
40
- // As we need to render on mount with SSR, using the local ref above in `StrictMode` will
41
- // create and start two instances of InstantSearch. To avoid this, we instead discard it and use
42
- // an upward ref from `InstantSearchSSRContext` as it has already been mounted a second time at this point.
43
- if (serverState !== null && serverState !== void 0 && serverState.ssrSearchRef) {
44
- searchRef = serverState.ssrSearchRef;
45
- }
46
- if (searchRef.current === null) {
47
- // We don't use the `instantsearch()` function because it comes with other
48
- // top-level APIs that we don't need.
49
- // See https://github.com/algolia/instantsearch/blob/5b529f43d8acc680f85837eaaa41f7fd03a3f833/src/index.es.ts#L63-L86
50
- var search = new _InstantSearch.default(props);
51
- search._schedule = function _schedule(cb) {
52
- search._schedule.queue.push(cb);
53
- clearTimeout(search._schedule.timer);
54
- search._schedule.timer = setTimeout(function () {
55
- search._schedule.queue.forEach(function (callback) {
56
- callback();
57
- });
58
- search._schedule.queue = [];
59
- }, 0);
60
- };
61
- search._schedule.queue = [];
62
- if (shouldRenderAtOnce) {
63
- // InstantSearch.js has a private Initial Results API that lets us inject
64
- // results on the search instance.
65
- // On the server, we default the initial results to an empty object so that
66
- // InstantSearch.js doesn't schedule a search that isn't used, leading to
67
- // an additional network request. (This is equivalent to monkey-patching
68
- // `scheduleSearch` to a noop.)
69
- search._initialResults = initialResults || {};
70
- // We don't rely on the `defer` to reset the schedule search, but will call
71
- // `search._resetScheduleSearch()` manually in the effect after children
72
- // mount in `InstantSearch`.
73
- search._manuallyResetScheduleSearch = true;
74
- }
75
- addAlgoliaAgents(props.searchClient, [].concat(defaultUserAgents, [serverContext && serverUserAgent, nextUserAgent(getNextVersion())]));
76
-
77
- // On the server, we start the search early to compute the search parameters.
78
- // On SSR, we start the search early to directly catch up with the lifecycle
79
- // and render.
80
- if (shouldRenderAtOnce) {
81
- search.start();
82
- }
83
- if (serverContext) {
84
- // We notify `getServerState()` of the InstantSearch internals to retrieve
85
- // the server state and pass it to the render on SSR.
86
- serverContext.notifyServer({
87
- search: search
88
- });
25
+ var forceUpdate = useForceUpdate.useForceUpdate();
26
+ var serverContext = useInstantSearchServerContext.useInstantSearchServerContext();
27
+ var serverState = useInstantSearchSSRContext.useInstantSearchSSRContext();
28
+ var waitForResultsRef = useRSCContext.useRSCContext().waitForResultsRef;
29
+ var initialResults = serverState === null || serverState === void 0 ? void 0 : serverState.initialResults;
30
+ var prevPropsRef = React.useRef(props);
31
+ var shouldRenderAtOnce = serverContext || initialResults || waitForResultsRef;
32
+ var searchRef = React.useRef(null);
33
+ // As we need to render on mount with SSR, using the local ref above in `StrictMode` will
34
+ // create and start two instances of InstantSearch. To avoid this, we instead discard it and use
35
+ // an upward ref from `InstantSearchSSRContext` as it has already been mounted a second time at this point.
36
+ if (serverState === null || serverState === void 0 ? void 0 : serverState.ssrSearchRef) {
37
+ searchRef = serverState.ssrSearchRef;
89
38
  }
90
- warnNextRouter(props.routing);
91
- warnNextAppDir(Boolean(waitForResultsRef));
92
- searchRef.current = search;
93
- }
94
- {
95
- var _search = searchRef.current;
96
- var prevProps = prevPropsRef.current;
97
- if (prevProps.indexName !== props.indexName) {
98
- _search.helper.setIndex(props.indexName || '').search();
99
- prevPropsRef.current = props;
100
- }
101
- if (prevProps.searchClient !== props.searchClient) {
102
- process.env.NODE_ENV === 'development' ? (0, _warn.warn)(false, 'The `searchClient` prop of `<InstantSearch>` changed between renders, which may cause more search requests than necessary. If this is an unwanted behavior, please provide a stable reference: https://www.algolia.com/doc/api-reference/widgets/instantsearch/react/#widget-param-searchclient') : void 0;
103
- addAlgoliaAgents(props.searchClient, [].concat(defaultUserAgents, [serverContext && serverUserAgent]));
104
- _search.mainHelper.setClient(props.searchClient).search();
105
- prevPropsRef.current = props;
106
- }
107
- if (prevProps.onStateChange !== props.onStateChange) {
108
- _search.onStateChange = props.onStateChange;
109
- prevPropsRef.current = props;
110
- }
111
- if (prevProps.searchFunction !== props.searchFunction) {
112
- // Updating the `searchFunction` to `undefined` is not supported by
113
- // InstantSearch.js, so it will throw an error.
114
- // This is a fair behavior until we add an update API in InstantSearch.js.
115
- _search._searchFunction = props.searchFunction;
116
- prevPropsRef.current = props;
117
- }
118
- if (prevProps.stalledSearchDelay !== props.stalledSearchDelay) {
119
- var _props$stalledSearchD;
120
- // The default `stalledSearchDelay` in InstantSearch.js is 200ms.
121
- // We need to reset it when it's undefined to get back to the original value.
122
- _search._stalledSearchDelay = (_props$stalledSearchD = props.stalledSearchDelay) !== null && _props$stalledSearchD !== void 0 ? _props$stalledSearchD : 200;
123
- prevPropsRef.current = props;
124
- }
125
- if (!(0, _dequal.dequal)(prevProps.future, props.future)) {
126
- _search.future = _objectSpread(_objectSpread({}, _InstantSearch.INSTANTSEARCH_FUTURE_DEFAULTS), props.future);
127
- prevPropsRef.current = props;
39
+ if (searchRef.current === null) {
40
+ // We don't use the `instantsearch()` function because it comes with other
41
+ // top-level APIs that we don't need.
42
+ // See https://github.com/algolia/instantsearch/blob/5b529f43d8acc680f85837eaaa41f7fd03a3f833/src/index.es.ts#L63-L86
43
+ var search = new InstantSearch(props);
44
+ search._schedule = function _schedule(cb) {
45
+ search._schedule.queue.push(cb);
46
+ clearTimeout(search._schedule.timer);
47
+ search._schedule.timer = setTimeout(function() {
48
+ search._schedule.queue.forEach(function(callback) {
49
+ callback();
50
+ });
51
+ search._schedule.queue = [];
52
+ }, 0);
53
+ };
54
+ search._schedule.queue = [];
55
+ if (shouldRenderAtOnce) {
56
+ // InstantSearch.js has a private Initial Results API that lets us inject
57
+ // results on the search instance.
58
+ // On the server, we default the initial results to an empty object so that
59
+ // InstantSearch.js doesn't schedule a search that isn't used, leading to
60
+ // an additional network request. (This is equivalent to monkey-patching
61
+ // `scheduleSearch` to a noop.)
62
+ search._initialResults = initialResults || {};
63
+ // We don't rely on the `defer` to reset the schedule search, but will call
64
+ // `search._resetScheduleSearch()` manually in the effect after children
65
+ // mount in `InstantSearch`.
66
+ search._manuallyResetScheduleSearch = true;
67
+ }
68
+ addAlgoliaAgents(props.searchClient, _to_consumable_array._(defaultUserAgents).concat([
69
+ serverContext && serverUserAgent,
70
+ nextUserAgent(getNextVersion())
71
+ ]));
72
+ // On the server, we start the search early to compute the search parameters.
73
+ // On SSR, we start the search early to directly catch up with the lifecycle
74
+ // and render.
75
+ if (shouldRenderAtOnce) {
76
+ search.start();
77
+ }
78
+ if (serverContext) {
79
+ // We notify `getServerState()` of the InstantSearch internals to retrieve
80
+ // the server state and pass it to the render on SSR.
81
+ serverContext.notifyServer({
82
+ search: search
83
+ });
84
+ }
85
+ warnNextRouter(props.routing);
86
+ searchRef.current = search;
128
87
  }
129
-
88
+ {
89
+ var search1 = searchRef.current;
90
+ var prevProps = prevPropsRef.current;
91
+ if (prevProps.indexName !== props.indexName) {
92
+ search1.helper.setIndex(props.indexName || '').search();
93
+ prevPropsRef.current = props;
94
+ }
95
+ if (prevProps.searchClient !== props.searchClient) {
96
+ addAlgoliaAgents(props.searchClient, _to_consumable_array._(defaultUserAgents).concat([
97
+ serverContext && serverUserAgent
98
+ ]));
99
+ search1.mainHelper.setClient(props.searchClient).search();
100
+ prevPropsRef.current = props;
101
+ }
102
+ if (prevProps.onStateChange !== props.onStateChange) {
103
+ search1.onStateChange = props.onStateChange;
104
+ prevPropsRef.current = props;
105
+ }
106
+ if (prevProps.searchFunction !== props.searchFunction) {
107
+ // Updating the `searchFunction` to `undefined` is not supported by
108
+ // InstantSearch.js, so it will throw an error.
109
+ // This is a fair behavior until we add an update API in InstantSearch.js.
110
+ search1._searchFunction = props.searchFunction;
111
+ prevPropsRef.current = props;
112
+ }
113
+ if (prevProps.stalledSearchDelay !== props.stalledSearchDelay) {
114
+ var _props_stalledSearchDelay;
115
+ // The default `stalledSearchDelay` in InstantSearch.js is 200ms.
116
+ // We need to reset it when it's undefined to get back to the original value.
117
+ search1._stalledSearchDelay = (_props_stalledSearchDelay = props.stalledSearchDelay) !== null && _props_stalledSearchDelay !== void 0 ? _props_stalledSearchDelay : 200;
118
+ prevPropsRef.current = props;
119
+ }
120
+ if (!dequal.dequal(prevProps.future, props.future)) {
121
+ search1.future = _object_spread._({}, InstantSearch.INSTANTSEARCH_FUTURE_DEFAULTS, props.future);
122
+ prevPropsRef.current = props;
123
+ }
130
124
  // Updating the `routing` prop is not supported because InstantSearch.js
131
125
  // doesn't let us change it. This might not be a problem though, because `routing`
132
126
  // shouldn't need to be dynamic.
@@ -134,87 +128,70 @@ function useInstantSearchApi(props) {
134
128
  // it privately on the InstantSearch instance. Another way would be to
135
129
  // manually inject the routing middleware in this library, and not rely
136
130
  // on the provided `routing` prop.
137
- }
138
- var cleanupTimerRef = (0, _react.useRef)(null);
139
- var store = (0, _shim.useSyncExternalStore)((0, _react.useCallback)(function () {
140
- var search = searchRef.current;
141
-
142
- // Scenario 1: the component mounts.
143
- if (cleanupTimerRef.current === null) {
144
- // On SSR, the instance is already started so we don't start it again.
145
- if (!search.started) {
146
- search.start();
147
- forceUpdate();
148
- }
149
- }
150
- // Scenario 2: the component updates.
151
- else {
152
- // We cancel the previous cleanup function because we don't want to
153
- // dispose the search during an update.
154
- clearTimeout(cleanupTimerRef.current);
155
- search._preventWidgetCleanup = false;
156
131
  }
157
- return function () {
158
- if (serverState !== null && serverState !== void 0 && serverState.ssrSearchRef) {
159
- return;
160
- }
161
- function cleanup() {
162
- search.dispose();
163
- }
164
- clearTimeout(search._schedule.timer);
165
- // We clean up only when the component that uses this subscription unmounts,
166
- // but not when it updates, because it would dispose the instance, which
167
- // would remove all the widgets and break routing.
168
- // Executing the cleanup function in a `setTimeout()` lets us cancel it
169
- // in the next effect.
170
- // (There might be better ways to do this.)
171
- cleanupTimerRef.current = setTimeout(cleanup);
172
- // We need to prevent the `useWidget` cleanup function so that widgets
173
- // are not removed before the instance is disposed, triggering
174
- // an unwanted search request.
175
- search._preventWidgetCleanup = true;
176
- };
177
- }, [forceUpdate, serverState]), function () {
178
- return searchRef.current;
179
- }, function () {
180
- return searchRef.current;
181
- });
182
- return store;
132
+ var cleanupTimerRef = React.useRef(null);
133
+ var store = shim.useSyncExternalStore(React.useCallback(function() {
134
+ var search = searchRef.current;
135
+ // Scenario 1: the component mounts.
136
+ if (cleanupTimerRef.current === null) {
137
+ // On SSR, the instance is already started so we don't start it again.
138
+ if (!search.started) {
139
+ search.start();
140
+ forceUpdate();
141
+ }
142
+ } else {
143
+ // We cancel the previous cleanup function because we don't want to
144
+ // dispose the search during an update.
145
+ clearTimeout(cleanupTimerRef.current);
146
+ search._preventWidgetCleanup = false;
147
+ }
148
+ return function() {
149
+ if (serverState === null || serverState === void 0 ? void 0 : serverState.ssrSearchRef) {
150
+ return;
151
+ }
152
+ function cleanup() {
153
+ search.dispose();
154
+ }
155
+ clearTimeout(search._schedule.timer);
156
+ // We clean up only when the component that uses this subscription unmounts,
157
+ // but not when it updates, because it would dispose the instance, which
158
+ // would remove all the widgets and break routing.
159
+ // Executing the cleanup function in a `setTimeout()` lets us cancel it
160
+ // in the next effect.
161
+ // (There might be better ways to do this.)
162
+ cleanupTimerRef.current = setTimeout(cleanup);
163
+ // We need to prevent the `useWidget` cleanup function so that widgets
164
+ // are not removed before the instance is disposed, triggering
165
+ // an unwanted search request.
166
+ search._preventWidgetCleanup = true;
167
+ };
168
+ }, [
169
+ forceUpdate,
170
+ serverState
171
+ ]), function() {
172
+ return searchRef.current;
173
+ }, function() {
174
+ return searchRef.current;
175
+ });
176
+ return store;
183
177
  }
184
178
  function addAlgoliaAgents(searchClient, userAgents) {
185
- if (typeof searchClient.addAlgoliaAgent !== 'function') {
186
- return;
187
- }
188
- userAgents.filter(Boolean).forEach(function (userAgent) {
189
- searchClient.addAlgoliaAgent(userAgent);
190
- });
191
- }
192
- function warnNextRouter(routing) {
193
- if (process.env.NODE_ENV === 'development') {
194
- var _routing$router;
195
- if (!routing || typeof window === 'undefined' || !('__NEXT_DATA__' in window)) {
196
- return;
179
+ if (typeof searchClient.addAlgoliaAgent !== 'function') {
180
+ return;
197
181
  }
198
- var isUsingNextRouter =
199
- // @ts-expect-error: _isNextRouter is only set on the Next.js router
200
- routing !== true && (routing === null || routing === void 0 ? void 0 : (_routing$router = routing.router) === null || _routing$router === void 0 ? void 0 : _routing$router._isNextRouter);
201
- process.env.NODE_ENV === 'development' ? (0, _warn.warn)(isUsingNextRouter, "\nYou are using Next.js with InstantSearch without the \"react-instantsearch-router-nextjs\" package.\nThis package is recommended to make the routing work correctly with Next.js.\nPlease check its usage instructions: https://github.com/algolia/instantsearch/tree/master/packages/react-instantsearch-router-nextjs\n\nYou can ignore this warning if you are using a custom router that suits your needs, it won't be outputted in production builds.") : void 0;
202
- }
182
+ userAgents.filter(Boolean).forEach(function(userAgent) {
183
+ searchClient.addAlgoliaAgent(userAgent);
184
+ });
203
185
  }
204
- function warnNextAppDir(isRscContextDefined) {
205
- var _next;
206
- if (!(process.env.NODE_ENV === 'development') || typeof window === 'undefined' || isRscContextDefined) {
207
- return;
208
- }
209
- process.env.NODE_ENV === 'development' ? (0, _warn.warn)(Boolean((_next = window.next) === null || _next === void 0 ? void 0 : _next.appDir) === false, "\nWe've detected you are using Next.js with the App Router.\nWe released a package called \"react-instantsearch-nextjs\" that makes SSR work with the App Router.\nPlease check its usage instructions: https://www.algolia.com/doc/guides/building-search-ui/going-further/server-side-rendering/react/#with-nextjs-app-router\n\nThis warning will not be outputted in production builds.") : void 0;
186
+ function warnNextRouter(routing) {
210
187
  }
211
-
212
188
  /**
213
189
  * Gets the version of Next.js if it is available in the `window` object,
214
190
  * otherwise it returns the NEXT_RUNTIME environment variable (in SSR),
215
191
  * which is either `nodejs` or `edge`.
216
- */
217
- function getNextVersion() {
218
- var _next2, _process$env;
219
- return typeof window !== 'undefined' && ((_next2 = window.next) === null || _next2 === void 0 ? void 0 : _next2.version) || (typeof process !== 'undefined' ? (_process$env = process.env) === null || _process$env === void 0 ? void 0 : _process$env.NEXT_RUNTIME : undefined);
220
- }
192
+ */ function getNextVersion() {
193
+ var _window_next, _process_env;
194
+ return typeof window !== 'undefined' && ((_window_next = window.next) === null || _window_next === void 0 ? void 0 : _window_next.version) || (typeof process !== 'undefined' ? (_process_env = process.env) === null || _process_env === void 0 ? void 0 : _process_env.NEXT_RUNTIME : undefined);
195
+ }
196
+
197
+ exports.useInstantSearchApi = useInstantSearchApi;
@@ -1,14 +1,13 @@
1
- "use strict";
1
+ 'use strict';
2
+
3
+ var React = require('react');
4
+ var InstantSearchContext = require('./InstantSearchContext.js');
5
+ var invariant = require('./invariant.js');
2
6
 
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.useInstantSearchContext = useInstantSearchContext;
7
- var _react = require("react");
8
- var _InstantSearchContext = require("./InstantSearchContext");
9
- var _invariant = require("./invariant");
10
7
  function useInstantSearchContext() {
11
- var search = (0, _react.useContext)(_InstantSearchContext.InstantSearchContext);
12
- (0, _invariant.invariant)(search !== null, 'Hooks must be used inside the <InstantSearch> component.\n\n' + 'They are not compatible with the `react-instantsearch-core@6.x` and `react-instantsearch-dom` packages, so make sure to use the <InstantSearch> component from `react-instantsearch-core@7.x`.');
13
- return search;
14
- }
8
+ var search = React.useContext(InstantSearchContext.InstantSearchContext);
9
+ invariant.invariant(search !== null);
10
+ return search;
11
+ }
12
+
13
+ exports.useInstantSearchContext = useInstantSearchContext;
@@ -1,11 +1,10 @@
1
- "use strict";
1
+ 'use strict';
2
+
3
+ var React = require('react');
4
+ var InstantSearchSSRContext = require('./InstantSearchSSRContext.js');
2
5
 
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.useInstantSearchSSRContext = useInstantSearchSSRContext;
7
- var _react = require("react");
8
- var _InstantSearchSSRContext = require("./InstantSearchSSRContext");
9
6
  function useInstantSearchSSRContext() {
10
- return (0, _react.useContext)(_InstantSearchSSRContext.InstantSearchSSRContext);
11
- }
7
+ return React.useContext(InstantSearchSSRContext.InstantSearchSSRContext);
8
+ }
9
+
10
+ exports.useInstantSearchSSRContext = useInstantSearchSSRContext;
@@ -1,11 +1,10 @@
1
- "use strict";
1
+ 'use strict';
2
+
3
+ var React = require('react');
4
+ var InstantSearchServerContext = require('../components/InstantSearchServerContext.js');
2
5
 
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.useInstantSearchServerContext = useInstantSearchServerContext;
7
- var _react = require("react");
8
- var _InstantSearchServerContext = require("../components/InstantSearchServerContext");
9
6
  function useInstantSearchServerContext() {
10
- return (0, _react.useContext)(_InstantSearchServerContext.InstantSearchServerContext);
11
- }
7
+ return React.useContext(InstantSearchServerContext.InstantSearchServerContext);
8
+ }
9
+
10
+ exports.useInstantSearchServerContext = useInstantSearchServerContext;
@@ -1,13 +1,11 @@
1
- "use strict";
1
+ 'use strict';
2
+
3
+ var React = require('react');
2
4
 
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.useIsomorphicLayoutEffect = void 0;
7
- var _react = require("react");
8
5
  /**
9
6
  * `useLayoutEffect` that doesn't show a warning when server-side rendering.
10
7
  *
11
8
  * It uses `useEffect` on the server (no-op), and `useLayoutEffect` on the browser.
12
- */
13
- var useIsomorphicLayoutEffect = exports.useIsomorphicLayoutEffect = typeof window !== 'undefined' ? _react.useLayoutEffect : _react.useEffect;
9
+ */ var useIsomorphicLayoutEffect = typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect;
10
+
11
+ exports.useIsomorphicLayoutEffect = useIsomorphicLayoutEffect;
@@ -1,11 +1,10 @@
1
- "use strict";
1
+ 'use strict';
2
+
3
+ var React = require('react');
4
+ var InstantSearchRSCContext = require('./InstantSearchRSCContext.js');
2
5
 
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.useRSCContext = useRSCContext;
7
- var _react = require("react");
8
- var _InstantSearchRSCContext = require("./InstantSearchRSCContext");
9
6
  function useRSCContext() {
10
- return (0, _react.useContext)(_InstantSearchRSCContext.InstantSearchRSCContext);
11
- }
7
+ return React.useContext(InstantSearchRSCContext.InstantSearchRSCContext);
8
+ }
9
+
10
+ exports.useRSCContext = useRSCContext;
@@ -1,65 +1,57 @@
1
- "use strict";
1
+ 'use strict';
2
2
 
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.useSearchResults = useSearchResults;
7
- var _utils = require("instantsearch.js/cjs/lib/utils");
8
- var _react = require("react");
9
- var _getIndexSearchResults = require("./getIndexSearchResults");
10
- var _useIndexContext = require("./useIndexContext");
11
- var _useInstantSearchContext = require("./useInstantSearchContext");
12
- function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
13
- function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
14
- function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
15
- function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
16
- function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
17
- function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
18
- function useSearchResults() {
19
- var search = (0, _useInstantSearchContext.useInstantSearchContext)();
20
- var searchIndex = (0, _useIndexContext.useIndexContext)();
21
- var _useState = (0, _react.useState)(function () {
22
- var indexSearchResults = (0, _getIndexSearchResults.getIndexSearchResults)(searchIndex);
23
- // We do this not to leak `recommendResults` in the API.
24
- return {
25
- results: indexSearchResults.results,
26
- scopedResults: indexSearchResults.scopedResults
27
- };
28
- }),
29
- _useState2 = _slicedToArray(_useState, 2),
30
- searchResults = _useState2[0],
31
- setSearchResults = _useState2[1];
32
- (0, _react.useEffect)(function () {
33
- function handleRender() {
34
- var results = searchIndex.getResults();
3
+ var _sliced_to_array = require('@swc/helpers/_/_sliced_to_array');
4
+ var utils = require('instantsearch.js/cjs/lib/utils');
5
+ var React = require('react');
6
+ var getIndexSearchResults = require('./getIndexSearchResults.js');
7
+ var useIndexContext = require('./useIndexContext.js');
8
+ var useInstantSearchContext = require('./useInstantSearchContext.js');
35
9
 
36
- // Results can be `null` when the first search is stalled.
37
- // In this case, we skip the update.
38
- // See: https://github.com/algolia/instantsearch/blob/20996c7a159988c58e00ff24d2d2dc98af8b980f/src/widgets/index/index.ts#L652-L657
39
- if (results !== null) {
40
- setSearchResults({
41
- results: results,
42
- scopedResults: searchIndex.getScopedResults()
43
- });
44
- } else if (search.mainIndex.getIndexName().length === 0) {
45
- // If the main index has no name, we get the scoped results from
46
- // the first child index instead.
47
- var childIndex = search.mainIndex.getWidgets().find(_utils.isIndexWidget);
48
- childIndex && setSearchResults({
49
- results: (0, _getIndexSearchResults.getIndexSearchResults)(searchIndex).results,
50
- scopedResults: childIndex.getScopedResults()
51
- });
52
- }
53
- }
54
- search.addListener('render', handleRender);
10
+ function useSearchResults() {
11
+ var search = useInstantSearchContext.useInstantSearchContext();
12
+ var searchIndex = useIndexContext.useIndexContext();
13
+ var _useState = _sliced_to_array._(React.useState(function() {
14
+ var indexSearchResults = getIndexSearchResults.getIndexSearchResults(searchIndex);
15
+ // We do this not to leak `recommendResults` in the API.
16
+ return {
17
+ results: indexSearchResults.results,
18
+ scopedResults: indexSearchResults.scopedResults
19
+ };
20
+ }), 2), searchResults = _useState[0], setSearchResults = _useState[1];
21
+ React.useEffect(function() {
22
+ function handleRender() {
23
+ var results = searchIndex.getResults();
24
+ // Results can be `null` when the first search is stalled.
25
+ // In this case, we skip the update.
26
+ // See: https://github.com/algolia/instantsearch/blob/20996c7a159988c58e00ff24d2d2dc98af8b980f/src/widgets/index/index.ts#L652-L657
27
+ if (results !== null) {
28
+ setSearchResults({
29
+ results: results,
30
+ scopedResults: searchIndex.getScopedResults()
31
+ });
32
+ } else if (search.mainIndex.getIndexName().length === 0) {
33
+ // If the main index has no name, we get the scoped results from
34
+ // the first child index instead.
35
+ var childIndex = search.mainIndex.getWidgets().find(utils.isIndexWidget);
36
+ childIndex && setSearchResults({
37
+ results: getIndexSearchResults.getIndexSearchResults(searchIndex).results,
38
+ scopedResults: childIndex.getScopedResults()
39
+ });
40
+ }
41
+ }
42
+ search.addListener('render', handleRender);
43
+ // Force setting results to mitigate potential race conditions where
44
+ // render listener is added after search results have been returned.
45
+ // This edge case is currently not covered by the tests.
46
+ handleRender();
47
+ return function() {
48
+ search.removeListener('render', handleRender);
49
+ };
50
+ }, [
51
+ search,
52
+ searchIndex
53
+ ]);
54
+ return searchResults;
55
+ }
55
56
 
56
- // Force setting results to mitigate potential race conditions where
57
- // render listener is added after search results have been returned.
58
- // This edge case is currently not covered by the tests.
59
- handleRender();
60
- return function () {
61
- search.removeListener('render', handleRender);
62
- };
63
- }, [search, searchIndex]);
64
- return searchResults;
65
- }
57
+ exports.useSearchResults = useSearchResults;