react-instantsearch-core 7.41.0 → 7.43.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.
@@ -24,6 +24,10 @@ function InstantSearchSSRProvider(_0) {
24
24
  var ssrSearchRef = _react.default.useRef(null);
25
25
  // This is used to re-map the result index to the requesting widget
26
26
  var recommendIdx = _react.default.useRef(0);
27
+ var hydrationCompleteRef = _react.default.useRef(false);
28
+ _react.default.useEffect(function() {
29
+ hydrationCompleteRef.current = true;
30
+ }, []);
27
31
  // When <DynamicWidgets> is mounted, a second provider is used above the user-land
28
32
  // <InstantSearchSSRProvider> in `getServerState()`.
29
33
  // To avoid the user's provider overriding the context value with an empty object,
@@ -31,10 +35,12 @@ function InstantSearchSSRProvider(_0) {
31
35
  if (Object.keys(props).length === 0) {
32
36
  return /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, children);
33
37
  }
38
+ var contextValue = _object_spread_props._(_object_spread._({}, props), {
39
+ ssrSearchRef: ssrSearchRef,
40
+ recommendIdx: recommendIdx,
41
+ hydrationCompleteRef: hydrationCompleteRef
42
+ });
34
43
  return /*#__PURE__*/ _react.default.createElement(_InstantSearchSSRContext.InstantSearchSSRContext.Provider, {
35
- value: _object_spread_props._(_object_spread._({}, props), {
36
- ssrSearchRef: ssrSearchRef,
37
- recommendIdx: recommendIdx
38
- })
44
+ value: contextValue
39
45
  }, children);
40
46
  }
@@ -10,8 +10,35 @@ Object.defineProperty(exports, "useChat", {
10
10
  }
11
11
  });
12
12
  var _interop_require_default = require("@swc/helpers/_/_interop_require_default");
13
+ var _object_spread = require("@swc/helpers/_/_object_spread");
14
+ var _object_spread_props = require("@swc/helpers/_/_object_spread_props");
13
15
  var _connectChat = /*#__PURE__*/ _interop_require_default._(require("instantsearch.js/cjs/connectors/chat/connectChat"));
14
16
  var _useConnector = require("../hooks/useConnector");
17
+ var _useIsHydrated = require("../lib/useIsHydrated");
15
18
  function useChat(props, additionalWidgetProperties) {
16
- return (0, _useConnector.useConnector)(_connectChat.default, props, additionalWidgetProperties);
19
+ var isHydrated = (0, _useIsHydrated.useIsHydrated)();
20
+ var chatState = (0, _useConnector.useConnector)(_connectChat.default, props, additionalWidgetProperties);
21
+ if (isHydrated) {
22
+ return chatState;
23
+ }
24
+ // Server rendering only promises the closed Chat shell, so a render that has
25
+ // to reproduce that markup shows no conversation either. `status` is pinned
26
+ // with `messages` because it diverges two ways: a server render suppresses
27
+ // `resumeStream()`, which the browser runs synchronously while initialising,
28
+ // and a caller-owned chat can already be streaming. `error` is pinned because
29
+ // a caller-owned chat can already have failed, which a connector-built one
30
+ // cannot, since its failures arrive in a microtask. `suggestions` is pinned
31
+ // because the connector derives them from those messages. Only an `id` given
32
+ // as a connector option passes through, because that value is the same on
33
+ // both sides. Anything else is withheld: the default is random per Chat, and
34
+ // an `id` carried by a caller-owned instance is no safer, since the server and
35
+ // the browser each construct their own.
36
+ return _object_spread_props._(_object_spread._({}, chatState), {
37
+ error: undefined,
38
+ id: 'id' in props && props.id || '',
39
+ messages: [],
40
+ open: false,
41
+ status: 'ready',
42
+ suggestions: undefined
43
+ });
17
44
  }
@@ -0,0 +1,17 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "usePromptSuggestions", {
7
+ enumerable: true,
8
+ get: function() {
9
+ return usePromptSuggestions;
10
+ }
11
+ });
12
+ var _interop_require_default = require("@swc/helpers/_/_interop_require_default");
13
+ var _connectPromptSuggestions = /*#__PURE__*/ _interop_require_default._(require("instantsearch.js/cjs/connectors/prompt-suggestions/connectPromptSuggestions"));
14
+ var _useConnector = require("../hooks/useConnector");
15
+ function usePromptSuggestions(props, additionalWidgetProperties) {
16
+ return (0, _useConnector.useConnector)(_connectPromptSuggestions.default, props, additionalWidgetProperties);
17
+ }
package/dist/cjs/index.js CHANGED
@@ -22,6 +22,7 @@ _export_star._(require("./components/InstantSearchSSRProvider"), exports);
22
22
  _export_star._(require("./connectors/useAutocomplete"), exports);
23
23
  _export_star._(require("./connectors/useBreadcrumb"), exports);
24
24
  _export_star._(require("./connectors/useChat"), exports);
25
+ _export_star._(require("./connectors/usePromptSuggestions"), exports);
25
26
  _export_star._(require("./connectors/useChatTrigger"), exports);
26
27
  _export_star._(require("./connectors/useClearRefinements"), exports);
27
28
  _export_star._(require("./connectors/useConfigure"), exports);
@@ -0,0 +1,49 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "useIsHydrated", {
7
+ enumerable: true,
8
+ get: function() {
9
+ return useIsHydrated;
10
+ }
11
+ });
12
+ var _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
13
+ var _sliced_to_array = require("@swc/helpers/_/_sliced_to_array");
14
+ var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
15
+ var _useInstantSearchServerContext = require("./useInstantSearchServerContext");
16
+ var _useInstantSearchSSRContext = require("./useInstantSearchSSRContext");
17
+ var subscribe = function subscribe() {
18
+ return function() {};
19
+ };
20
+ var getClientSnapshot = function getClientSnapshot() {
21
+ return true;
22
+ };
23
+ var getServerSnapshot = function getServerSnapshot() {
24
+ return false;
25
+ };
26
+ function useNativeIsHydrated() {
27
+ return _react.useSyncExternalStore(subscribe, getClientSnapshot, getServerSnapshot);
28
+ }
29
+ // React 16 and 17 have no `useSyncExternalStore`, so the flip waits for an
30
+ // effect and the render itself cannot tell hydration from a plain mount. These
31
+ // contexts provide that signal for the supported `getServerState` and
32
+ // `InstantSearchSSRProvider` flow: the server context covers state collection,
33
+ // and the SSR context covers HTML rendering and hydration.
34
+ //
35
+ // The shim's React 16 and 17 fallback ignores the server snapshot, so using it
36
+ // here would not distinguish initial hydration from later provider children.
37
+ function useLegacyIsHydrated() {
38
+ var _ssrContext_hydrationCompleteRef;
39
+ var serverContext = (0, _useInstantSearchServerContext.useInstantSearchServerContext)();
40
+ var ssrContext = (0, _useInstantSearchSSRContext.useInstantSearchSSRContext)();
41
+ var isServerRendered = serverContext !== null || ssrContext !== null;
42
+ var isProviderHydrated = (ssrContext === null || ssrContext === void 0 ? void 0 : (_ssrContext_hydrationCompleteRef = ssrContext.hydrationCompleteRef) === null || _ssrContext_hydrationCompleteRef === void 0 ? void 0 : _ssrContext_hydrationCompleteRef.current) === true;
43
+ var _React_useState = _sliced_to_array._(_react.useState(!isServerRendered || isProviderHydrated), 2), isHydrated = _React_useState[0], setIsHydrated = _React_useState[1];
44
+ _react.useEffect(function() {
45
+ setIsHydrated(true);
46
+ }, []);
47
+ return isHydrated;
48
+ }
49
+ var useIsHydrated = typeof _react.useSyncExternalStore === 'function' ? useNativeIsHydrated : useLegacyIsHydrated;
@@ -9,4 +9,4 @@ Object.defineProperty(exports, "default", {
9
9
  return _default;
10
10
  }
11
11
  });
12
- var _default = '7.41.0';
12
+ var _default = '7.43.0';
@@ -16,6 +16,10 @@ import { InstantSearchSSRContext } from '../lib/InstantSearchSSRContext.js';
16
16
  var ssrSearchRef = React__default.useRef(null);
17
17
  // This is used to re-map the result index to the requesting widget
18
18
  var recommendIdx = React__default.useRef(0);
19
+ var hydrationCompleteRef = React__default.useRef(false);
20
+ React__default.useEffect(function() {
21
+ hydrationCompleteRef.current = true;
22
+ }, []);
19
23
  // When <DynamicWidgets> is mounted, a second provider is used above the user-land
20
24
  // <InstantSearchSSRProvider> in `getServerState()`.
21
25
  // To avoid the user's provider overriding the context value with an empty object,
@@ -23,11 +27,13 @@ import { InstantSearchSSRContext } from '../lib/InstantSearchSSRContext.js';
23
27
  if (Object.keys(props).length === 0) {
24
28
  return /*#__PURE__*/ React__default.createElement(React__default.Fragment, null, children);
25
29
  }
30
+ var contextValue = _$1(_$2({}, props), {
31
+ ssrSearchRef: ssrSearchRef,
32
+ recommendIdx: recommendIdx,
33
+ hydrationCompleteRef: hydrationCompleteRef
34
+ });
26
35
  return /*#__PURE__*/ React__default.createElement(InstantSearchSSRContext.Provider, {
27
- value: _$1(_$2({}, props), {
28
- ssrSearchRef: ssrSearchRef,
29
- recommendIdx: recommendIdx
30
- })
36
+ value: contextValue
31
37
  }, children);
32
38
  }
33
39
 
@@ -1,5 +1,5 @@
1
1
  import type { AdditionalWidgetProperties } from '../hooks/useConnector';
2
- import type { ChatConnectorParams } from 'instantsearch.js/es/connectors/chat/connectChat';
2
+ import type { ChatConnectorParams, ChatWidgetDescription } from 'instantsearch.js/es/connectors/chat/connectChat';
3
3
  import type { UIMessage } from 'instantsearch.js/es/lib/chat';
4
4
  export type UseChatProps<TUiMessage extends UIMessage = UIMessage> = ChatConnectorParams<TUiMessage>;
5
- export declare function useChat<TUiMessage extends UIMessage = UIMessage>(props: UseChatProps<TUiMessage>, additionalWidgetProperties?: AdditionalWidgetProperties): import("instantsearch.js/es/connectors/chat/connectChat").ChatRenderState<TUiMessage>;
5
+ export declare function useChat<TUiMessage extends UIMessage = UIMessage>(props: UseChatProps<TUiMessage>, additionalWidgetProperties?: AdditionalWidgetProperties): ChatWidgetDescription<TUiMessage>['renderState'];
@@ -1,8 +1,35 @@
1
+ import { _ as _$1 } from '@swc/helpers/esm/_object_spread.js';
2
+ import { _ } from '@swc/helpers/esm/_object_spread_props.js';
1
3
  import connectChat from 'instantsearch.js/es/connectors/chat/connectChat.js';
2
4
  import { useConnector } from '../hooks/useConnector.js';
5
+ import { useIsHydrated } from '../lib/useIsHydrated.js';
3
6
 
4
7
  function useChat(props, additionalWidgetProperties) {
5
- return useConnector(connectChat, props, additionalWidgetProperties);
8
+ var isHydrated = useIsHydrated();
9
+ var chatState = useConnector(connectChat, props, additionalWidgetProperties);
10
+ if (isHydrated) {
11
+ return chatState;
12
+ }
13
+ // Server rendering only promises the closed Chat shell, so a render that has
14
+ // to reproduce that markup shows no conversation either. `status` is pinned
15
+ // with `messages` because it diverges two ways: a server render suppresses
16
+ // `resumeStream()`, which the browser runs synchronously while initialising,
17
+ // and a caller-owned chat can already be streaming. `error` is pinned because
18
+ // a caller-owned chat can already have failed, which a connector-built one
19
+ // cannot, since its failures arrive in a microtask. `suggestions` is pinned
20
+ // because the connector derives them from those messages. Only an `id` given
21
+ // as a connector option passes through, because that value is the same on
22
+ // both sides. Anything else is withheld: the default is random per Chat, and
23
+ // an `id` carried by a caller-owned instance is no safer, since the server and
24
+ // the browser each construct their own.
25
+ return _(_$1({}, chatState), {
26
+ error: undefined,
27
+ id: 'id' in props && props.id || '',
28
+ messages: [],
29
+ open: false,
30
+ status: 'ready',
31
+ suggestions: undefined
32
+ });
6
33
  }
7
34
 
8
35
  export { useChat };
@@ -0,0 +1,4 @@
1
+ import type { AdditionalWidgetProperties } from '../hooks/useConnector';
2
+ import type { PromptSuggestionsConnectorParams } from 'instantsearch.js/es/connectors/prompt-suggestions/connectPromptSuggestions';
3
+ export type UsePromptSuggestionsProps = PromptSuggestionsConnectorParams;
4
+ export declare function usePromptSuggestions(props: UsePromptSuggestionsProps, additionalWidgetProperties?: AdditionalWidgetProperties): import("instantsearch.js/es/connectors/prompt-suggestions/connectPromptSuggestions").PromptSuggestionsRenderState;
@@ -0,0 +1,8 @@
1
+ import connectPromptSuggestions from 'instantsearch.js/es/connectors/prompt-suggestions/connectPromptSuggestions.js';
2
+ import { useConnector } from '../hooks/useConnector.js';
3
+
4
+ function usePromptSuggestions(props, additionalWidgetProperties) {
5
+ return useConnector(connectPromptSuggestions, props, additionalWidgetProperties);
6
+ }
7
+
8
+ export { usePromptSuggestions };
@@ -9,6 +9,7 @@ export * from './components/InstantSearchSSRProvider';
9
9
  export * from './connectors/useAutocomplete';
10
10
  export * from './connectors/useBreadcrumb';
11
11
  export * from './connectors/useChat';
12
+ export * from './connectors/usePromptSuggestions';
12
13
  export * from './connectors/useChatTrigger';
13
14
  export * from './connectors/useClearRefinements';
14
15
  export * from './connectors/useConfigure';
package/dist/es/index.js CHANGED
@@ -9,6 +9,7 @@ export { InstantSearchSSRProvider } from './components/InstantSearchSSRProvider.
9
9
  export { useAutocomplete } from './connectors/useAutocomplete.js';
10
10
  export { useBreadcrumb } from './connectors/useBreadcrumb.js';
11
11
  export { useChat } from './connectors/useChat.js';
12
+ export { usePromptSuggestions } from './connectors/usePromptSuggestions.js';
12
13
  export { useChatTrigger } from './connectors/useChatTrigger.js';
13
14
  export { useClearRefinements } from './connectors/useClearRefinements.js';
14
15
  export { useConfigure } from './connectors/useConfigure.js';
@@ -1,5 +1,5 @@
1
- import type { InstantSearchServerState } from '../components/InstantSearchSSRProvider';
2
1
  import type { InternalInstantSearch } from './useInstantSearchApi';
2
+ import type { InstantSearchServerState } from '../components/InstantSearchSSRProvider';
3
3
  import type { UiState } from 'instantsearch.js';
4
4
  import type { MutableRefObject } from 'react';
5
5
  export type InstantSearchSSRContextApi<TUiState extends UiState, TRouteState = TUiState> = InstantSearchServerState & {
@@ -0,0 +1,9 @@
1
+ declare function useNativeIsHydrated(): boolean;
2
+ /**
3
+ * Whether this render can use browser state, or has to reproduce the markup a
4
+ * server produced without it.
5
+ *
6
+ * @internal
7
+ */
8
+ export declare const useIsHydrated: typeof useNativeIsHydrated;
9
+ export {};
@@ -0,0 +1,45 @@
1
+ import { _ } from '@swc/helpers/esm/_sliced_to_array.js';
2
+ import * as React from 'react';
3
+ import { useInstantSearchServerContext } from './useInstantSearchServerContext.js';
4
+ import { useInstantSearchSSRContext } from './useInstantSearchSSRContext.js';
5
+
6
+ var subscribe = function subscribe() {
7
+ return function() {};
8
+ };
9
+ var getClientSnapshot = function getClientSnapshot() {
10
+ return true;
11
+ };
12
+ var getServerSnapshot = function getServerSnapshot() {
13
+ return false;
14
+ };
15
+ function useNativeIsHydrated() {
16
+ return React.useSyncExternalStore(subscribe, getClientSnapshot, getServerSnapshot);
17
+ }
18
+ // React 16 and 17 have no `useSyncExternalStore`, so the flip waits for an
19
+ // effect and the render itself cannot tell hydration from a plain mount. These
20
+ // contexts provide that signal for the supported `getServerState` and
21
+ // `InstantSearchSSRProvider` flow: the server context covers state collection,
22
+ // and the SSR context covers HTML rendering and hydration.
23
+ //
24
+ // The shim's React 16 and 17 fallback ignores the server snapshot, so using it
25
+ // here would not distinguish initial hydration from later provider children.
26
+ function useLegacyIsHydrated() {
27
+ var _ssrContext_hydrationCompleteRef;
28
+ var serverContext = useInstantSearchServerContext();
29
+ var ssrContext = useInstantSearchSSRContext();
30
+ var isServerRendered = serverContext !== null || ssrContext !== null;
31
+ var isProviderHydrated = (ssrContext === null || ssrContext === void 0 ? void 0 : (_ssrContext_hydrationCompleteRef = ssrContext.hydrationCompleteRef) === null || _ssrContext_hydrationCompleteRef === void 0 ? void 0 : _ssrContext_hydrationCompleteRef.current) === true;
32
+ var _React_useState = _(React.useState(!isServerRendered || isProviderHydrated), 2), isHydrated = _React_useState[0], setIsHydrated = _React_useState[1];
33
+ React.useEffect(function() {
34
+ setIsHydrated(true);
35
+ }, []);
36
+ return isHydrated;
37
+ }
38
+ /**
39
+ * Whether this render can use browser state, or has to reproduce the markup a
40
+ * server produced without it.
41
+ *
42
+ * @internal
43
+ */ var useIsHydrated = typeof React.useSyncExternalStore === 'function' ? useNativeIsHydrated : useLegacyIsHydrated;
44
+
45
+ export { useIsHydrated };
@@ -1,7 +1,7 @@
1
- import { useLayoutEffect } from 'react';
1
+ import { useEffect } from 'react';
2
2
  /**
3
3
  * `useLayoutEffect` that doesn't show a warning when server-side rendering.
4
4
  *
5
5
  * It uses `useEffect` on the server (no-op), and `useLayoutEffect` on the browser.
6
6
  */
7
- export declare const useIsomorphicLayoutEffect: typeof useLayoutEffect;
7
+ export declare const useIsomorphicLayoutEffect: typeof useEffect;
@@ -1,2 +1,2 @@
1
- declare const _default: "7.41.0";
1
+ declare const _default: "7.43.0";
2
2
  export default _default;
@@ -1,3 +1,3 @@
1
- var version = '7.41.0';
1
+ var version = '7.43.0';
2
2
 
3
3
  export { version as default };