instantsearch.js 4.79.2 → 4.81.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.
- package/cjs/components/Template/Template.js +1 -1
- package/cjs/connectors/chat/connectChat.js +200 -0
- package/cjs/connectors/index.js +7 -0
- package/cjs/connectors/index.umd.js +239 -0
- package/cjs/index.js +2 -2
- package/cjs/lib/InstantSearch.js +2 -10
- package/cjs/lib/chat/chat.js +177 -0
- package/cjs/lib/chat/index.js +27 -0
- package/cjs/lib/useStickToBottom.js +20 -0
- package/cjs/lib/version.js +1 -1
- package/cjs/templates/carousel/carousel.js +31 -4
- package/cjs/widgets/chat/chat.js +615 -0
- package/cjs/widgets/index/index.js +78 -24
- package/cjs/widgets/index.js +7 -0
- package/cjs/widgets/index.umd.js +267 -0
- package/dist/instantsearch.development.d.ts +123 -15
- package/dist/instantsearch.development.js +186 -91
- package/dist/instantsearch.development.js.map +1 -1
- package/dist/instantsearch.production.d.ts +123 -15
- package/dist/instantsearch.production.min.d.ts +123 -15
- package/dist/instantsearch.production.min.js +2 -2
- package/dist/instantsearch.production.min.js.map +1 -1
- package/es/components/Template/Template.js +1 -1
- package/es/connectors/chat/connectChat.d.ts +413 -0
- package/es/connectors/chat/connectChat.js +194 -0
- package/es/connectors/frequently-bought-together/connectFrequentlyBoughtTogether.d.ts +2 -0
- package/es/connectors/geo-search/connectGeoSearch.d.ts +1 -0
- package/es/connectors/hits/connectHits.d.ts +1 -0
- package/es/connectors/hits/connectHitsWithInsights.d.ts +1 -0
- package/es/connectors/index.d.ts +1 -0
- package/es/connectors/index.js +2 -1
- package/es/connectors/index.umd.d.ts +39 -0
- package/es/connectors/index.umd.js +47 -0
- package/es/connectors/infinite-hits/connectInfiniteHits.d.ts +1 -0
- package/es/connectors/infinite-hits/connectInfiniteHitsWithInsights.d.ts +1 -0
- package/es/connectors/looking-similar/connectLookingSimilar.d.ts +2 -0
- package/es/connectors/related-products/connectRelatedProducts.d.ts +2 -0
- package/es/connectors/trending-items/connectTrendingItems.d.ts +2 -0
- package/es/lib/InstantSearch.d.ts +2 -2
- package/es/lib/InstantSearch.js +2 -10
- package/es/lib/chat/chat.d.ts +40 -0
- package/es/lib/chat/chat.js +166 -0
- package/es/lib/chat/index.d.ts +7 -0
- package/es/lib/chat/index.js +5 -0
- package/es/lib/useStickToBottom.d.ts +1 -0
- package/es/lib/useStickToBottom.js +14 -0
- package/es/lib/version.d.ts +1 -1
- package/es/lib/version.js +1 -1
- package/es/templates/carousel/carousel.d.ts +10 -3
- package/es/templates/carousel/carousel.js +32 -5
- package/es/types/render-state.d.ts +2 -1
- package/es/types/templates.d.ts +1 -1
- package/es/types/widget.d.ts +2 -2
- package/es/widgets/chat/chat.d.ts +555 -0
- package/es/widgets/chat/chat.js +597 -0
- package/es/widgets/frequently-bought-together/frequently-bought-together.d.ts +3 -1
- package/es/widgets/geo-search/geo-search.d.ts +1 -0
- package/es/widgets/hits/hits.d.ts +1 -0
- package/es/widgets/index/index.d.ts +46 -2
- package/es/widgets/index/index.js +78 -24
- package/es/widgets/index.d.ts +1 -0
- package/es/widgets/index.js +2 -1
- package/es/widgets/index.umd.d.ts +44 -0
- package/es/widgets/index.umd.js +51 -0
- package/es/widgets/infinite-hits/infinite-hits.d.ts +1 -0
- package/es/widgets/looking-similar/looking-similar.d.ts +3 -1
- package/es/widgets/related-products/related-products.d.ts +3 -1
- package/es/widgets/trending-items/trending-items.d.ts +3 -1
- package/package.json +11 -7
|
@@ -0,0 +1,615 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
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); }
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
Object.defineProperty(exports, "RecommendToolType", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: function get() {
|
|
10
|
+
return _chat.RecommendToolType;
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
Object.defineProperty(exports, "SearchIndexToolType", {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
get: function get() {
|
|
16
|
+
return _chat.SearchIndexToolType;
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
exports.default = void 0;
|
|
20
|
+
var _instantsearchUiComponents = require("instantsearch-ui-components");
|
|
21
|
+
var _preact = require("preact");
|
|
22
|
+
var _hooks = require("preact/hooks");
|
|
23
|
+
var _Template = _interopRequireDefault(require("../../components/Template/Template"));
|
|
24
|
+
var _connectChat = _interopRequireDefault(require("../../connectors/chat/connectChat"));
|
|
25
|
+
var _chat = require("../../lib/chat");
|
|
26
|
+
var _templating = require("../../lib/templating");
|
|
27
|
+
var _useStickToBottom2 = require("../../lib/useStickToBottom");
|
|
28
|
+
var _utils = require("../../lib/utils");
|
|
29
|
+
var _templates = require("../../templates");
|
|
30
|
+
var _excluded = ["container", "templates", "cssClasses", "resume", "tools", "getSearchPageURL"];
|
|
31
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
32
|
+
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
|
33
|
+
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
34
|
+
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; }
|
|
35
|
+
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; }
|
|
36
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
37
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
38
|
+
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); }
|
|
39
|
+
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
|
40
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
41
|
+
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."); }
|
|
42
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
43
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
44
|
+
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; } }
|
|
45
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
46
|
+
var withUsage = (0, _utils.createDocumentationMessageGenerator)({
|
|
47
|
+
name: 'chat'
|
|
48
|
+
});
|
|
49
|
+
var Chat = (0, _instantsearchUiComponents.createChatComponent)({
|
|
50
|
+
createElement: _preact.h,
|
|
51
|
+
Fragment: _preact.Fragment
|
|
52
|
+
});
|
|
53
|
+
function getDefinedProperties(obj) {
|
|
54
|
+
return Object.fromEntries(Object.entries(obj).filter(function (_ref) {
|
|
55
|
+
var _ref2 = _slicedToArray(_ref, 2),
|
|
56
|
+
value = _ref2[1];
|
|
57
|
+
return value !== undefined;
|
|
58
|
+
}));
|
|
59
|
+
}
|
|
60
|
+
var _ref6 = (0, _preact.h)(_instantsearchUiComponents.ArrowRightIconComponent, {
|
|
61
|
+
createElement: _preact.h
|
|
62
|
+
});
|
|
63
|
+
var _ref7 = (0, _preact.h)(_instantsearchUiComponents.ChevronLeftIconComponent, {
|
|
64
|
+
createElement: _preact.h
|
|
65
|
+
});
|
|
66
|
+
var _ref8 = (0, _preact.h)(_instantsearchUiComponents.ChevronRightIconComponent, {
|
|
67
|
+
createElement: _preact.h
|
|
68
|
+
});
|
|
69
|
+
function createCarouselTool(showViewAll, templates, getSearchPageURL) {
|
|
70
|
+
var Button = (0, _instantsearchUiComponents.createButtonComponent)({
|
|
71
|
+
createElement: _preact.h
|
|
72
|
+
});
|
|
73
|
+
function SearchLayoutComponent(_ref3) {
|
|
74
|
+
var message = _ref3.message,
|
|
75
|
+
indexUiState = _ref3.indexUiState,
|
|
76
|
+
setIndexUiState = _ref3.setIndexUiState,
|
|
77
|
+
onClose = _ref3.onClose;
|
|
78
|
+
var input = message === null || message === void 0 ? void 0 : message.input;
|
|
79
|
+
var output = message === null || message === void 0 ? void 0 : message.output;
|
|
80
|
+
var items = (output === null || output === void 0 ? void 0 : output.hits) || [];
|
|
81
|
+
var MemoedHeaderComponent = (0, _hooks.useMemo)(function () {
|
|
82
|
+
return function (props) {
|
|
83
|
+
return (0, _preact.h)(HeaderComponent, _extends({
|
|
84
|
+
nbHits: output === null || output === void 0 ? void 0 : output.nbHits,
|
|
85
|
+
query: input === null || input === void 0 ? void 0 : input.query,
|
|
86
|
+
hitsPerPage: items.length,
|
|
87
|
+
setIndexUiState: setIndexUiState,
|
|
88
|
+
indexUiState: indexUiState,
|
|
89
|
+
getSearchPageURL: getSearchPageURL,
|
|
90
|
+
onClose: onClose
|
|
91
|
+
}, props));
|
|
92
|
+
};
|
|
93
|
+
}, [items.length, input === null || input === void 0 ? void 0 : input.query, output === null || output === void 0 ? void 0 : output.nbHits, setIndexUiState, indexUiState, onClose]);
|
|
94
|
+
return (0, _templates.carousel)({
|
|
95
|
+
showNavigation: false,
|
|
96
|
+
templates: {
|
|
97
|
+
header: MemoedHeaderComponent
|
|
98
|
+
}
|
|
99
|
+
})({
|
|
100
|
+
items: items,
|
|
101
|
+
templates: {
|
|
102
|
+
item: function item(_ref4) {
|
|
103
|
+
var _item = _ref4.item;
|
|
104
|
+
return (0, _preact.h)(_Template.default, {
|
|
105
|
+
templates: templates,
|
|
106
|
+
templateKey: "item",
|
|
107
|
+
data: _item,
|
|
108
|
+
rootTagName: "fragment"
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
sendEvent: function sendEvent() {}
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
function HeaderComponent(_ref5) {
|
|
116
|
+
var canScrollLeft = _ref5.canScrollLeft,
|
|
117
|
+
canScrollRight = _ref5.canScrollRight,
|
|
118
|
+
scrollLeft = _ref5.scrollLeft,
|
|
119
|
+
scrollRight = _ref5.scrollRight,
|
|
120
|
+
nbHits = _ref5.nbHits,
|
|
121
|
+
query = _ref5.query,
|
|
122
|
+
hitsPerPage = _ref5.hitsPerPage,
|
|
123
|
+
setIndexUiState = _ref5.setIndexUiState,
|
|
124
|
+
indexUiState = _ref5.indexUiState,
|
|
125
|
+
onClose = _ref5.onClose,
|
|
126
|
+
getSearchPageURL = _ref5.getSearchPageURL;
|
|
127
|
+
if ((hitsPerPage !== null && hitsPerPage !== void 0 ? hitsPerPage : 0) < 1) {
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
return (0, _preact.h)("div", {
|
|
131
|
+
className: "ais-ChatToolSearchIndexCarouselHeader"
|
|
132
|
+
}, (0, _preact.h)("div", {
|
|
133
|
+
className: "ais-ChatToolSearchIndexCarouselHeaderResults"
|
|
134
|
+
}, nbHits && (0, _preact.h)("div", {
|
|
135
|
+
className: "ais-ChatToolSearchIndexCarouselHeaderCount"
|
|
136
|
+
}, hitsPerPage !== null && hitsPerPage !== void 0 ? hitsPerPage : 0, " of ", nbHits.toLocaleString(), " result", nbHits > 1 ? 's' : ''), showViewAll && (0, _preact.h)(Button, {
|
|
137
|
+
variant: "ghost",
|
|
138
|
+
size: "sm",
|
|
139
|
+
onClick: function onClick() {
|
|
140
|
+
if (!query) return;
|
|
141
|
+
var nextUiState = _objectSpread(_objectSpread({}, indexUiState), {}, {
|
|
142
|
+
query: query
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
// If no main search page URL or we are on the search page, just update the state
|
|
146
|
+
if (!getSearchPageURL || getSearchPageURL && new URL(getSearchPageURL(nextUiState)).pathname === window.location.pathname) {
|
|
147
|
+
setIndexUiState(nextUiState);
|
|
148
|
+
onClose();
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Navigate to different page
|
|
153
|
+
window.location.href = getSearchPageURL(nextUiState);
|
|
154
|
+
},
|
|
155
|
+
className: "ais-ChatToolSearchIndexCarouselHeaderViewAll"
|
|
156
|
+
}, "View all", _ref6)), (hitsPerPage !== null && hitsPerPage !== void 0 ? hitsPerPage : 0) > 2 && (0, _preact.h)("div", {
|
|
157
|
+
className: "ais-ChatToolSearchIndexCarouselHeaderScrollButtons"
|
|
158
|
+
}, (0, _preact.h)(Button, {
|
|
159
|
+
variant: "outline",
|
|
160
|
+
size: "sm",
|
|
161
|
+
iconOnly: true,
|
|
162
|
+
onClick: scrollLeft,
|
|
163
|
+
disabled: !canScrollLeft,
|
|
164
|
+
className: "ais-ChatToolSearchIndexCarouselHeaderScrollButton"
|
|
165
|
+
}, _ref7), (0, _preact.h)(Button, {
|
|
166
|
+
variant: "outline",
|
|
167
|
+
size: "sm",
|
|
168
|
+
iconOnly: true,
|
|
169
|
+
onClick: scrollRight,
|
|
170
|
+
disabled: !canScrollRight,
|
|
171
|
+
className: "ais-ChatToolSearchIndexCarouselHeaderScrollButton"
|
|
172
|
+
}, _ref8)));
|
|
173
|
+
}
|
|
174
|
+
return {
|
|
175
|
+
templates: {
|
|
176
|
+
layout: SearchLayoutComponent
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
function createDefaultTools(templates, getSearchPageURL) {
|
|
181
|
+
var _ref9;
|
|
182
|
+
return _ref9 = {}, _defineProperty(_ref9, _chat.SearchIndexToolType, createCarouselTool(true, templates, getSearchPageURL)), _defineProperty(_ref9, _chat.RecommendToolType, createCarouselTool(false, templates, getSearchPageURL)), _ref9;
|
|
183
|
+
}
|
|
184
|
+
function ChatWrapper(_ref10) {
|
|
185
|
+
var cssClasses = _ref10.cssClasses,
|
|
186
|
+
chatOpen = _ref10.chatOpen,
|
|
187
|
+
setChatOpen = _ref10.setChatOpen,
|
|
188
|
+
chatMessages = _ref10.chatMessages,
|
|
189
|
+
setChatMessages = _ref10.setChatMessages,
|
|
190
|
+
indexUiState = _ref10.indexUiState,
|
|
191
|
+
setIndexUiState = _ref10.setIndexUiState,
|
|
192
|
+
chatStatus = _ref10.chatStatus,
|
|
193
|
+
chatInput = _ref10.chatInput,
|
|
194
|
+
setChatInput = _ref10.setChatInput,
|
|
195
|
+
sendMessage = _ref10.sendMessage,
|
|
196
|
+
regenerate = _ref10.regenerate,
|
|
197
|
+
stop = _ref10.stop,
|
|
198
|
+
toolsForUi = _ref10.toolsForUi,
|
|
199
|
+
toggleButtonProps = _ref10.toggleButtonProps,
|
|
200
|
+
headerProps = _ref10.headerProps,
|
|
201
|
+
messagesProps = _ref10.messagesProps,
|
|
202
|
+
promptProps = _ref10.promptProps,
|
|
203
|
+
state = _ref10.state;
|
|
204
|
+
var _useStickToBottom = (0, _useStickToBottom2.useStickToBottom)({
|
|
205
|
+
initial: 'smooth',
|
|
206
|
+
resize: 'smooth'
|
|
207
|
+
}),
|
|
208
|
+
scrollRef = _useStickToBottom.scrollRef,
|
|
209
|
+
contentRef = _useStickToBottom.contentRef,
|
|
210
|
+
scrollToBottom = _useStickToBottom.scrollToBottom,
|
|
211
|
+
isAtBottom = _useStickToBottom.isAtBottom;
|
|
212
|
+
state.init();
|
|
213
|
+
var _state$use = state.use(false),
|
|
214
|
+
_state$use2 = _slicedToArray(_state$use, 2),
|
|
215
|
+
isClearing = _state$use2[0],
|
|
216
|
+
setIsClearing = _state$use2[1];
|
|
217
|
+
var _state$use3 = state.use(false),
|
|
218
|
+
_state$use4 = _slicedToArray(_state$use3, 2),
|
|
219
|
+
maximized = _state$use4[0],
|
|
220
|
+
setMaximized = _state$use4[1];
|
|
221
|
+
var onClear = function onClear() {
|
|
222
|
+
return setIsClearing(true);
|
|
223
|
+
};
|
|
224
|
+
var onClearTransitionEnd = function onClearTransitionEnd() {
|
|
225
|
+
setChatMessages([]);
|
|
226
|
+
setIsClearing(false);
|
|
227
|
+
};
|
|
228
|
+
return (0, _preact.h)(Chat, {
|
|
229
|
+
classNames: cssClasses,
|
|
230
|
+
open: chatOpen,
|
|
231
|
+
maximized: maximized,
|
|
232
|
+
toggleButtonComponent: toggleButtonProps.layoutComponent,
|
|
233
|
+
toggleButtonProps: {
|
|
234
|
+
open: chatOpen,
|
|
235
|
+
onClick: function onClick() {
|
|
236
|
+
return setChatOpen(!chatOpen);
|
|
237
|
+
},
|
|
238
|
+
toggleIconComponent: toggleButtonProps.iconComponent
|
|
239
|
+
},
|
|
240
|
+
headerComponent: headerProps.layoutComponent,
|
|
241
|
+
promptComponent: promptProps.layoutComponent,
|
|
242
|
+
headerProps: {
|
|
243
|
+
onClose: function onClose() {
|
|
244
|
+
return setChatOpen(false);
|
|
245
|
+
},
|
|
246
|
+
maximized: maximized,
|
|
247
|
+
onToggleMaximize: function onToggleMaximize() {
|
|
248
|
+
return setMaximized(!maximized);
|
|
249
|
+
},
|
|
250
|
+
onClear: onClear,
|
|
251
|
+
canClear: Boolean(chatMessages === null || chatMessages === void 0 ? void 0 : chatMessages.length) && !isClearing,
|
|
252
|
+
closeIconComponent: headerProps.closeIconComponent,
|
|
253
|
+
minimizeIconComponent: headerProps.minimizeIconComponent,
|
|
254
|
+
maximizeIconComponent: headerProps.maximizeIconComponent,
|
|
255
|
+
titleIconComponent: headerProps.titleIconComponent,
|
|
256
|
+
translations: headerProps.translations
|
|
257
|
+
},
|
|
258
|
+
messagesProps: {
|
|
259
|
+
status: chatStatus,
|
|
260
|
+
onReload: function onReload(messageId) {
|
|
261
|
+
return regenerate({
|
|
262
|
+
messageId: messageId
|
|
263
|
+
});
|
|
264
|
+
},
|
|
265
|
+
onClose: function onClose() {
|
|
266
|
+
return setChatOpen(false);
|
|
267
|
+
},
|
|
268
|
+
messages: chatMessages,
|
|
269
|
+
indexUiState: indexUiState,
|
|
270
|
+
isClearing: isClearing,
|
|
271
|
+
onClearTransitionEnd: onClearTransitionEnd,
|
|
272
|
+
isScrollAtBottom: isAtBottom,
|
|
273
|
+
scrollRef: scrollRef,
|
|
274
|
+
contentRef: contentRef,
|
|
275
|
+
onScrollToBottom: scrollToBottom,
|
|
276
|
+
setIndexUiState: setIndexUiState,
|
|
277
|
+
tools: toolsForUi,
|
|
278
|
+
loaderComponent: messagesProps.loaderComponent,
|
|
279
|
+
errorComponent: messagesProps.errorComponent,
|
|
280
|
+
actionsComponent: messagesProps.actionsComponent,
|
|
281
|
+
translations: messagesProps.translations
|
|
282
|
+
},
|
|
283
|
+
promptProps: {
|
|
284
|
+
promptRef: promptProps.promptRef,
|
|
285
|
+
status: chatStatus,
|
|
286
|
+
value: chatInput,
|
|
287
|
+
onInput: function onInput(event) {
|
|
288
|
+
setChatInput(event.currentTarget.value);
|
|
289
|
+
},
|
|
290
|
+
onSubmit: function onSubmit() {
|
|
291
|
+
sendMessage({
|
|
292
|
+
text: chatInput
|
|
293
|
+
});
|
|
294
|
+
setChatInput('');
|
|
295
|
+
},
|
|
296
|
+
onStop: function onStop() {
|
|
297
|
+
stop();
|
|
298
|
+
},
|
|
299
|
+
headerComponent: promptProps.headerComponent,
|
|
300
|
+
footerComponent: promptProps.footerComponent,
|
|
301
|
+
translations: promptProps.translations
|
|
302
|
+
}
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
var createRenderer = function createRenderer(_ref11) {
|
|
306
|
+
var renderState = _ref11.renderState,
|
|
307
|
+
cssClasses = _ref11.cssClasses,
|
|
308
|
+
containerNode = _ref11.containerNode,
|
|
309
|
+
templates = _ref11.templates,
|
|
310
|
+
tools = _ref11.tools;
|
|
311
|
+
var state = createLocalState();
|
|
312
|
+
var promptRef = {
|
|
313
|
+
current: null
|
|
314
|
+
};
|
|
315
|
+
return function (props, isFirstRendering) {
|
|
316
|
+
var _templates$header, _templates$header2, _templates$header3, _templates$header4, _templates$header5, _templates$header6, _templates$header7, _templates$header8, _templates$header9, _templates$header10, _templates$messages, _templates$messages2, _templates$messages3, _templates$messages4, _templates$messages5, _templates$messages6, _templates$prompt, _templates$prompt2, _templates$prompt3, _templates$prompt4, _templates$prompt5, _templates$prompt6, _templates$prompt7, _templates$prompt8, _templates$prompt9, _templates$toggleButt, _templates$toggleButt2;
|
|
317
|
+
var indexUiState = props.indexUiState,
|
|
318
|
+
input = props.input,
|
|
319
|
+
instantSearchInstance = props.instantSearchInstance,
|
|
320
|
+
messages = props.messages,
|
|
321
|
+
open = props.open,
|
|
322
|
+
sendMessage = props.sendMessage,
|
|
323
|
+
setIndexUiState = props.setIndexUiState,
|
|
324
|
+
setInput = props.setInput,
|
|
325
|
+
setMessages = props.setMessages,
|
|
326
|
+
setOpen = props.setOpen,
|
|
327
|
+
status = props.status,
|
|
328
|
+
error = props.error,
|
|
329
|
+
addToolResult = props.addToolResult,
|
|
330
|
+
regenerate = props.regenerate,
|
|
331
|
+
stop = props.stop;
|
|
332
|
+
if (process.env.NODE_ENV === 'development' && error) {
|
|
333
|
+
throw error;
|
|
334
|
+
}
|
|
335
|
+
if (isFirstRendering) {
|
|
336
|
+
renderState.templateProps = (0, _templating.prepareTemplateProps)({
|
|
337
|
+
defaultTemplates: {},
|
|
338
|
+
templatesConfig: instantSearchInstance.templatesConfig,
|
|
339
|
+
templates: templates
|
|
340
|
+
});
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
var toolsForUi = {};
|
|
344
|
+
Object.entries(tools).forEach(function (_ref12) {
|
|
345
|
+
var _ref13 = _slicedToArray(_ref12, 2),
|
|
346
|
+
key = _ref13[0],
|
|
347
|
+
tool = _ref13[1];
|
|
348
|
+
toolsForUi[key] = _objectSpread(_objectSpread({}, tool), {}, {
|
|
349
|
+
addToolResult: addToolResult,
|
|
350
|
+
layoutComponent: function layoutComponent(layoutComponentProps) {
|
|
351
|
+
return (0, _preact.h)(_Template.default, {
|
|
352
|
+
templates: tool.templates,
|
|
353
|
+
rootTagName: "fragment",
|
|
354
|
+
templateKey: "layout",
|
|
355
|
+
data: layoutComponentProps
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
});
|
|
360
|
+
var headerTemplateProps = (0, _templating.prepareTemplateProps)({
|
|
361
|
+
defaultTemplates: {},
|
|
362
|
+
templatesConfig: instantSearchInstance.templatesConfig,
|
|
363
|
+
templates: templates.header
|
|
364
|
+
});
|
|
365
|
+
var headerLayoutComponent = (_templates$header = templates.header) !== null && _templates$header !== void 0 && _templates$header.layout ? function (headerProps) {
|
|
366
|
+
return (0, _preact.h)(_Template.default, _extends({}, headerTemplateProps, {
|
|
367
|
+
templateKey: "layout",
|
|
368
|
+
rootTagName: "div",
|
|
369
|
+
data: headerProps
|
|
370
|
+
}));
|
|
371
|
+
} : undefined;
|
|
372
|
+
var headerCloseIconComponent = (_templates$header2 = templates.header) !== null && _templates$header2 !== void 0 && _templates$header2.closeIcon ? function () {
|
|
373
|
+
return (0, _preact.h)(_Template.default, _extends({}, headerTemplateProps, {
|
|
374
|
+
templateKey: "closeIcon",
|
|
375
|
+
rootTagName: "span"
|
|
376
|
+
}));
|
|
377
|
+
} : undefined;
|
|
378
|
+
var headerMinimizeIconComponent = (_templates$header3 = templates.header) !== null && _templates$header3 !== void 0 && _templates$header3.minimizeIcon ? function () {
|
|
379
|
+
return (0, _preact.h)(_Template.default, _extends({}, headerTemplateProps, {
|
|
380
|
+
templateKey: "minimizeIcon",
|
|
381
|
+
rootTagName: "span"
|
|
382
|
+
}));
|
|
383
|
+
} : undefined;
|
|
384
|
+
var headerMaximizeIconComponent = (_templates$header4 = templates.header) !== null && _templates$header4 !== void 0 && _templates$header4.maximizeIcon ? function (_ref14) {
|
|
385
|
+
var maximized = _ref14.maximized;
|
|
386
|
+
return (0, _preact.h)(_Template.default, _extends({}, headerTemplateProps, {
|
|
387
|
+
templateKey: "maximizeIcon",
|
|
388
|
+
rootTagName: "span",
|
|
389
|
+
data: {
|
|
390
|
+
maximized: maximized
|
|
391
|
+
}
|
|
392
|
+
}));
|
|
393
|
+
} : undefined;
|
|
394
|
+
var headerTitleIconComponent = (_templates$header5 = templates.header) !== null && _templates$header5 !== void 0 && _templates$header5.titleIcon ? function () {
|
|
395
|
+
return (0, _preact.h)(_Template.default, _extends({}, headerTemplateProps, {
|
|
396
|
+
templateKey: "titleIcon",
|
|
397
|
+
rootTagName: "span"
|
|
398
|
+
}));
|
|
399
|
+
} : undefined;
|
|
400
|
+
var headerTranslations = getDefinedProperties({
|
|
401
|
+
title: (_templates$header6 = templates.header) === null || _templates$header6 === void 0 ? void 0 : _templates$header6.titleText,
|
|
402
|
+
minimizeLabel: (_templates$header7 = templates.header) === null || _templates$header7 === void 0 ? void 0 : _templates$header7.minimizeLabelText,
|
|
403
|
+
maximizeLabel: (_templates$header8 = templates.header) === null || _templates$header8 === void 0 ? void 0 : _templates$header8.maximizeLabelText,
|
|
404
|
+
closeLabel: (_templates$header9 = templates.header) === null || _templates$header9 === void 0 ? void 0 : _templates$header9.closeLabelText,
|
|
405
|
+
clearLabel: (_templates$header10 = templates.header) === null || _templates$header10 === void 0 ? void 0 : _templates$header10.clearLabelText
|
|
406
|
+
});
|
|
407
|
+
var messagesTemplateProps = (0, _templating.prepareTemplateProps)({
|
|
408
|
+
defaultTemplates: {},
|
|
409
|
+
templatesConfig: instantSearchInstance.templatesConfig,
|
|
410
|
+
templates: templates.messages
|
|
411
|
+
});
|
|
412
|
+
var messagesLoaderComponent = (_templates$messages = templates.messages) !== null && _templates$messages !== void 0 && _templates$messages.loader ? function (loaderProps) {
|
|
413
|
+
return (0, _preact.h)(_Template.default, _extends({}, messagesTemplateProps, {
|
|
414
|
+
templateKey: "loader",
|
|
415
|
+
rootTagName: "div",
|
|
416
|
+
data: loaderProps
|
|
417
|
+
}));
|
|
418
|
+
} : undefined;
|
|
419
|
+
var messagesErrorComponent = (_templates$messages2 = templates.messages) !== null && _templates$messages2 !== void 0 && _templates$messages2.error ? function (errorProps) {
|
|
420
|
+
return (0, _preact.h)(_Template.default, _extends({}, messagesTemplateProps, {
|
|
421
|
+
templateKey: "error",
|
|
422
|
+
rootTagName: "div",
|
|
423
|
+
data: errorProps
|
|
424
|
+
}));
|
|
425
|
+
} : undefined;
|
|
426
|
+
var messagesTranslations = getDefinedProperties({
|
|
427
|
+
scrollToBottomLabel: (_templates$messages3 = templates.messages) === null || _templates$messages3 === void 0 ? void 0 : _templates$messages3.scrollToBottomLabelText,
|
|
428
|
+
loaderText: (_templates$messages4 = templates.messages) === null || _templates$messages4 === void 0 ? void 0 : _templates$messages4.loaderText,
|
|
429
|
+
copyToClipboardLabel: (_templates$messages5 = templates.messages) === null || _templates$messages5 === void 0 ? void 0 : _templates$messages5.copyToClipboardLabelText,
|
|
430
|
+
regenerateLabel: (_templates$messages6 = templates.messages) === null || _templates$messages6 === void 0 ? void 0 : _templates$messages6.regenerateLabelText
|
|
431
|
+
});
|
|
432
|
+
var promptTemplateProps = (0, _templating.prepareTemplateProps)({
|
|
433
|
+
defaultTemplates: {},
|
|
434
|
+
templatesConfig: instantSearchInstance.templatesConfig,
|
|
435
|
+
templates: templates.prompt
|
|
436
|
+
});
|
|
437
|
+
var promptLayoutComponent = (_templates$prompt = templates.prompt) !== null && _templates$prompt !== void 0 && _templates$prompt.layout ? function (promptProps) {
|
|
438
|
+
return (0, _preact.h)(_Template.default, _extends({}, promptTemplateProps, {
|
|
439
|
+
templateKey: "layout",
|
|
440
|
+
rootTagName: "div",
|
|
441
|
+
data: promptProps
|
|
442
|
+
}));
|
|
443
|
+
} : undefined;
|
|
444
|
+
var promptHeaderComponent = (_templates$prompt2 = templates.prompt) !== null && _templates$prompt2 !== void 0 && _templates$prompt2.header ? function () {
|
|
445
|
+
return (0, _preact.h)(_Template.default, _extends({}, promptTemplateProps, {
|
|
446
|
+
templateKey: "header",
|
|
447
|
+
rootTagName: "fragment"
|
|
448
|
+
}));
|
|
449
|
+
} : undefined;
|
|
450
|
+
var promptFooterComponent = (_templates$prompt3 = templates.prompt) !== null && _templates$prompt3 !== void 0 && _templates$prompt3.footer ? function () {
|
|
451
|
+
return (0, _preact.h)(_Template.default, _extends({}, promptTemplateProps, {
|
|
452
|
+
templateKey: "footer",
|
|
453
|
+
rootTagName: "fragment"
|
|
454
|
+
}));
|
|
455
|
+
} : undefined;
|
|
456
|
+
var promptTranslations = getDefinedProperties({
|
|
457
|
+
textareaLabel: (_templates$prompt4 = templates.prompt) === null || _templates$prompt4 === void 0 ? void 0 : _templates$prompt4.textareaLabelText,
|
|
458
|
+
textareaPlaceholder: (_templates$prompt5 = templates.prompt) === null || _templates$prompt5 === void 0 ? void 0 : _templates$prompt5.textareaPlaceholderText,
|
|
459
|
+
emptyMessageTooltip: (_templates$prompt6 = templates.prompt) === null || _templates$prompt6 === void 0 ? void 0 : _templates$prompt6.emptyMessageTooltipText,
|
|
460
|
+
stopResponseTooltip: (_templates$prompt7 = templates.prompt) === null || _templates$prompt7 === void 0 ? void 0 : _templates$prompt7.stopResponseTooltipText,
|
|
461
|
+
sendMessageTooltip: (_templates$prompt8 = templates.prompt) === null || _templates$prompt8 === void 0 ? void 0 : _templates$prompt8.sendMessageTooltipText,
|
|
462
|
+
disclaimer: (_templates$prompt9 = templates.prompt) === null || _templates$prompt9 === void 0 ? void 0 : _templates$prompt9.disclaimerText
|
|
463
|
+
});
|
|
464
|
+
var actionsComponent = templates.actions ? function (actionsProps) {
|
|
465
|
+
return (0, _preact.h)(_Template.default, _extends({}, renderState.templateProps, {
|
|
466
|
+
templateKey: "actions",
|
|
467
|
+
rootTagName: "div",
|
|
468
|
+
data: actionsProps
|
|
469
|
+
}));
|
|
470
|
+
} : undefined;
|
|
471
|
+
var toggleButtonTemplateProps = (0, _templating.prepareTemplateProps)({
|
|
472
|
+
defaultTemplates: {},
|
|
473
|
+
templatesConfig: instantSearchInstance.templatesConfig,
|
|
474
|
+
templates: templates.toggleButton
|
|
475
|
+
});
|
|
476
|
+
var toggleButtonLayoutComponent = (_templates$toggleButt = templates.toggleButton) !== null && _templates$toggleButt !== void 0 && _templates$toggleButt.layout ? function (toggleButtonProps) {
|
|
477
|
+
return (0, _preact.h)(_Template.default, _extends({}, toggleButtonTemplateProps, {
|
|
478
|
+
templateKey: "layout",
|
|
479
|
+
rootTagName: "button",
|
|
480
|
+
data: toggleButtonProps
|
|
481
|
+
}));
|
|
482
|
+
} : undefined;
|
|
483
|
+
var toggleButtonIconComponent = (_templates$toggleButt2 = templates.toggleButton) !== null && _templates$toggleButt2 !== void 0 && _templates$toggleButt2.icon ? function (_ref15) {
|
|
484
|
+
var isOpen = _ref15.isOpen;
|
|
485
|
+
return (0, _preact.h)(_Template.default, _extends({}, toggleButtonTemplateProps, {
|
|
486
|
+
templateKey: "icon",
|
|
487
|
+
rootTagName: "span",
|
|
488
|
+
data: {
|
|
489
|
+
isOpen: isOpen
|
|
490
|
+
}
|
|
491
|
+
}));
|
|
492
|
+
} : undefined;
|
|
493
|
+
state.subscribe(rerender);
|
|
494
|
+
function rerender() {
|
|
495
|
+
(0, _preact.render)((0, _preact.h)(ChatWrapper, {
|
|
496
|
+
cssClasses: cssClasses,
|
|
497
|
+
chatOpen: open,
|
|
498
|
+
setChatOpen: setOpen,
|
|
499
|
+
chatMessages: messages,
|
|
500
|
+
setChatMessages: setMessages,
|
|
501
|
+
indexUiState: indexUiState,
|
|
502
|
+
setIndexUiState: setIndexUiState,
|
|
503
|
+
chatStatus: status,
|
|
504
|
+
chatInput: input,
|
|
505
|
+
setChatInput: setInput,
|
|
506
|
+
sendMessage: sendMessage,
|
|
507
|
+
regenerate: regenerate,
|
|
508
|
+
stop: stop,
|
|
509
|
+
toolsForUi: toolsForUi,
|
|
510
|
+
toggleButtonProps: {
|
|
511
|
+
layoutComponent: toggleButtonLayoutComponent,
|
|
512
|
+
iconComponent: toggleButtonIconComponent
|
|
513
|
+
},
|
|
514
|
+
headerProps: {
|
|
515
|
+
layoutComponent: headerLayoutComponent,
|
|
516
|
+
closeIconComponent: headerCloseIconComponent,
|
|
517
|
+
minimizeIconComponent: headerMinimizeIconComponent,
|
|
518
|
+
maximizeIconComponent: headerMaximizeIconComponent,
|
|
519
|
+
titleIconComponent: headerTitleIconComponent,
|
|
520
|
+
translations: headerTranslations
|
|
521
|
+
},
|
|
522
|
+
messagesProps: {
|
|
523
|
+
loaderComponent: messagesLoaderComponent,
|
|
524
|
+
errorComponent: messagesErrorComponent,
|
|
525
|
+
actionsComponent: actionsComponent,
|
|
526
|
+
translations: messagesTranslations
|
|
527
|
+
},
|
|
528
|
+
promptProps: {
|
|
529
|
+
layoutComponent: promptLayoutComponent,
|
|
530
|
+
headerComponent: promptHeaderComponent,
|
|
531
|
+
footerComponent: promptFooterComponent,
|
|
532
|
+
translations: promptTranslations,
|
|
533
|
+
promptRef: promptRef
|
|
534
|
+
},
|
|
535
|
+
state: state
|
|
536
|
+
}), containerNode);
|
|
537
|
+
}
|
|
538
|
+
rerender();
|
|
539
|
+
};
|
|
540
|
+
};
|
|
541
|
+
var defaultTemplates = {
|
|
542
|
+
item: function item(_item2) {
|
|
543
|
+
return JSON.stringify(_item2, null, 2);
|
|
544
|
+
}
|
|
545
|
+
};
|
|
546
|
+
var chat = exports.default = function chat(widgetParams) {
|
|
547
|
+
var _ref16 = widgetParams || {},
|
|
548
|
+
container = _ref16.container,
|
|
549
|
+
_ref16$templates = _ref16.templates,
|
|
550
|
+
userTemplates = _ref16$templates === void 0 ? {} : _ref16$templates,
|
|
551
|
+
_ref16$cssClasses = _ref16.cssClasses,
|
|
552
|
+
cssClasses = _ref16$cssClasses === void 0 ? {} : _ref16$cssClasses,
|
|
553
|
+
_ref16$resume = _ref16.resume,
|
|
554
|
+
resume = _ref16$resume === void 0 ? false : _ref16$resume,
|
|
555
|
+
userTools = _ref16.tools,
|
|
556
|
+
getSearchPageURL = _ref16.getSearchPageURL,
|
|
557
|
+
options = _objectWithoutProperties(_ref16, _excluded);
|
|
558
|
+
if (!container) {
|
|
559
|
+
throw new Error(withUsage('The `container` option is required.'));
|
|
560
|
+
}
|
|
561
|
+
var containerNode = (0, _utils.getContainerNode)(container);
|
|
562
|
+
var templates = _objectSpread(_objectSpread({}, defaultTemplates), userTemplates);
|
|
563
|
+
var defaultTools = createDefaultTools(templates, getSearchPageURL);
|
|
564
|
+
var tools = _objectSpread(_objectSpread({}, defaultTools), userTools);
|
|
565
|
+
var specializedRenderer = createRenderer({
|
|
566
|
+
containerNode: containerNode,
|
|
567
|
+
cssClasses: cssClasses,
|
|
568
|
+
renderState: {},
|
|
569
|
+
templates: templates,
|
|
570
|
+
tools: tools
|
|
571
|
+
});
|
|
572
|
+
var makeWidget = (0, _connectChat.default)(specializedRenderer, function () {
|
|
573
|
+
return (0, _preact.render)(null, containerNode);
|
|
574
|
+
});
|
|
575
|
+
return _objectSpread(_objectSpread({}, makeWidget(_objectSpread({
|
|
576
|
+
resume: resume,
|
|
577
|
+
tools: tools
|
|
578
|
+
}, options))), {}, {
|
|
579
|
+
$$widgetType: 'ais.chat'
|
|
580
|
+
});
|
|
581
|
+
};
|
|
582
|
+
function createLocalState() {
|
|
583
|
+
var state = [];
|
|
584
|
+
var subscriptions = new Set();
|
|
585
|
+
var cursor = 0;
|
|
586
|
+
function use(initialValue) {
|
|
587
|
+
var index = cursor++;
|
|
588
|
+
if (state[index] === undefined) {
|
|
589
|
+
state[index] = initialValue;
|
|
590
|
+
}
|
|
591
|
+
return [state[index], function (value) {
|
|
592
|
+
var prev = state[index];
|
|
593
|
+
if (prev === value) {
|
|
594
|
+
return prev;
|
|
595
|
+
}
|
|
596
|
+
state[index] = value;
|
|
597
|
+
subscriptions.forEach(function (fn) {
|
|
598
|
+
return fn();
|
|
599
|
+
});
|
|
600
|
+
return value;
|
|
601
|
+
}];
|
|
602
|
+
}
|
|
603
|
+
return {
|
|
604
|
+
init: function init() {
|
|
605
|
+
cursor = 0;
|
|
606
|
+
},
|
|
607
|
+
subscribe: function subscribe(fn) {
|
|
608
|
+
subscriptions.add(fn);
|
|
609
|
+
return function () {
|
|
610
|
+
return subscriptions.delete(fn);
|
|
611
|
+
};
|
|
612
|
+
},
|
|
613
|
+
use: use
|
|
614
|
+
};
|
|
615
|
+
}
|