@athoscommerce/snap-store-mobx 1.5.0 → 1.5.1-beta.110
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/dist/cjs/Autocomplete/Stores/AutocompleteFacetStore.js +2 -1
- package/dist/cjs/Chat/ChatStore.d.ts +86 -0
- package/dist/cjs/Chat/ChatStore.d.ts.map +1 -0
- package/dist/cjs/Chat/ChatStore.js +539 -0
- package/dist/cjs/Chat/Stores/ChatAttachmentStore.d.ts +98 -0
- package/dist/cjs/Chat/Stores/ChatAttachmentStore.d.ts.map +1 -0
- package/dist/cjs/Chat/Stores/ChatAttachmentStore.js +314 -0
- package/dist/cjs/Chat/Stores/ChatCompareStore.d.ts +16 -0
- package/dist/cjs/Chat/Stores/ChatCompareStore.d.ts.map +1 -0
- package/dist/cjs/Chat/Stores/ChatCompareStore.js +65 -0
- package/dist/cjs/Chat/Stores/ChatSessionStore.d.ts +107 -0
- package/dist/cjs/Chat/Stores/ChatSessionStore.d.ts.map +1 -0
- package/dist/cjs/Chat/Stores/ChatSessionStore.js +635 -0
- package/dist/cjs/Search/Stores/SearchFacetStore.d.ts.map +1 -1
- package/dist/cjs/Search/Stores/SearchFacetStore.js +13 -10
- package/dist/cjs/Search/Stores/index.d.ts +1 -1
- package/dist/cjs/Search/Stores/index.d.ts.map +1 -1
- package/dist/cjs/Search/Stores/index.js +2 -1
- package/dist/cjs/index.d.ts +4 -0
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +5 -1
- package/dist/cjs/types.d.ts +21 -2
- package/dist/cjs/types.d.ts.map +1 -1
- package/dist/esm/Autocomplete/Stores/AutocompleteFacetStore.js +1 -1
- package/dist/esm/Chat/ChatStore.d.ts +86 -0
- package/dist/esm/Chat/ChatStore.d.ts.map +1 -0
- package/dist/esm/Chat/ChatStore.js +478 -0
- package/dist/esm/Chat/Stores/ChatAttachmentStore.d.ts +98 -0
- package/dist/esm/Chat/Stores/ChatAttachmentStore.d.ts.map +1 -0
- package/dist/esm/Chat/Stores/ChatAttachmentStore.js +222 -0
- package/dist/esm/Chat/Stores/ChatCompareStore.d.ts +16 -0
- package/dist/esm/Chat/Stores/ChatCompareStore.d.ts.map +1 -0
- package/dist/esm/Chat/Stores/ChatCompareStore.js +51 -0
- package/dist/esm/Chat/Stores/ChatSessionStore.d.ts +107 -0
- package/dist/esm/Chat/Stores/ChatSessionStore.d.ts.map +1 -0
- package/dist/esm/Chat/Stores/ChatSessionStore.js +601 -0
- package/dist/esm/Search/Stores/SearchFacetStore.d.ts.map +1 -1
- package/dist/esm/Search/Stores/SearchFacetStore.js +13 -10
- package/dist/esm/Search/Stores/index.d.ts +1 -1
- package/dist/esm/Search/Stores/index.d.ts.map +1 -1
- package/dist/esm/Search/Stores/index.js +1 -1
- package/dist/esm/index.d.ts +4 -0
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +2 -0
- package/dist/esm/types.d.ts +21 -2
- package/dist/esm/types.d.ts.map +1 -1
- package/package.json +5 -5
|
@@ -0,0 +1,478 @@
|
|
|
1
|
+
import { makeObservable, observable, computed, reaction } from 'mobx';
|
|
2
|
+
import { MetaStore } from '../Meta/MetaStore';
|
|
3
|
+
import { AbstractStore } from '../Abstract/AbstractStore';
|
|
4
|
+
import { StorageStore } from '@athoscommerce/snap-toolbox';
|
|
5
|
+
import { ChatSessionStore } from './Stores/ChatSessionStore';
|
|
6
|
+
import { Product, Variants } from '../Search/Stores/SearchResultStore';
|
|
7
|
+
import { SearchFacetStore } from '../Search/Stores/SearchFacetStore';
|
|
8
|
+
const CHAT_STATUS_EXPIRATION_TIME = 1000 * 60 * 60 * 12; // 12 hours
|
|
9
|
+
export class ChatStore extends AbstractStore {
|
|
10
|
+
constructor(config, services) {
|
|
11
|
+
super(config);
|
|
12
|
+
this.meta = undefined;
|
|
13
|
+
this.inputValue = '';
|
|
14
|
+
this.open = false;
|
|
15
|
+
this.chats = [];
|
|
16
|
+
this.chatEnabled = null;
|
|
17
|
+
this.initChatLoading = false;
|
|
18
|
+
this.suggestedQuestions = [];
|
|
19
|
+
this.welcomeMessage = '';
|
|
20
|
+
this.features = { imageSearch: { enabled: false }, similarProducts: { enabled: false } };
|
|
21
|
+
this.productQuickview = null;
|
|
22
|
+
this.productQuickviewError = null;
|
|
23
|
+
/** Raw meta kept for lazy hydration of inactive chat sessions. */
|
|
24
|
+
this.storedMetaData = null;
|
|
25
|
+
/** Tracks which message currently owns the displayed facets — guards against redundant rebuilds. */
|
|
26
|
+
this.activeFacetsMessageId = null;
|
|
27
|
+
/** Bumps on every detached-urlManager change so observers re-evaluate isFacetSelected. */
|
|
28
|
+
this.urlVersion = 0;
|
|
29
|
+
/** Snapshot of the applied filter state captured the last time the active message was seeded.
|
|
30
|
+
* Compared against the live urlManager state to decide whether there are pending changes. */
|
|
31
|
+
this.appliedFilterSnapshot = '{}';
|
|
32
|
+
this.services = services;
|
|
33
|
+
this.urlManager = services.urlManager.detach(true);
|
|
34
|
+
this.urlManager.subscribe(() => {
|
|
35
|
+
this.urlVersion++;
|
|
36
|
+
});
|
|
37
|
+
const legacyKey = `ss-${this.config.id}`;
|
|
38
|
+
const storageKey = this.config.siteId ? `ss-${this.config.siteId}-${this.config.id}` : legacyKey;
|
|
39
|
+
this.storage = new StorageStore({
|
|
40
|
+
type: 'local',
|
|
41
|
+
key: storageKey,
|
|
42
|
+
});
|
|
43
|
+
this.facets = this.buildFacetStore();
|
|
44
|
+
const storedChatStatusResponse = this.storage.get('chatStatusResponse');
|
|
45
|
+
if (storedChatStatusResponse) {
|
|
46
|
+
try {
|
|
47
|
+
const storedChatStatus = JSON.parse(storedChatStatusResponse);
|
|
48
|
+
if (storedChatStatus.checkTime && Date.now() - storedChatStatus.checkTime > CHAT_STATUS_EXPIRATION_TIME) {
|
|
49
|
+
// chat status is expired, remove from storage to trigger a new check
|
|
50
|
+
this.storage.set('chatStatusResponse', null);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
// Apply the stored response without persisting, so the 12-hour
|
|
54
|
+
// checkTime keeps counting from the last real API call rather
|
|
55
|
+
// than resetting on every page load.
|
|
56
|
+
this.applyChatStatusResponse(storedChatStatus.response);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
this.storage.set('chatStatusResponse', null);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
// check for entries in storage
|
|
64
|
+
let latestChatId = '';
|
|
65
|
+
const storedChats = this.storage.get('chats');
|
|
66
|
+
storedChats &&
|
|
67
|
+
Object.keys(storedChats || {}).forEach((chatId) => {
|
|
68
|
+
const chatData = storedChats[chatId];
|
|
69
|
+
if (chatData) {
|
|
70
|
+
const restoredChat = new ChatSessionStore({
|
|
71
|
+
data: {
|
|
72
|
+
...chatData,
|
|
73
|
+
id: chatId,
|
|
74
|
+
},
|
|
75
|
+
stores: {
|
|
76
|
+
storage: this.storage,
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
// Mark as unhydrated — results are still raw JSON from storage.
|
|
80
|
+
// Only the active session will be hydrated below.
|
|
81
|
+
restoredChat.hydrated = false;
|
|
82
|
+
this.chats.push(restoredChat);
|
|
83
|
+
latestChatId = chatId;
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
// Prefer the persisted active chat ID so switching chats survives page reloads.
|
|
87
|
+
// Fall back to the most recently created chat if the stored ID is missing or stale.
|
|
88
|
+
const storedCurrentChatId = this.storage.get('currentChatId');
|
|
89
|
+
const activeChatId = storedCurrentChatId && this.chats.some((chat) => chat.id === storedCurrentChatId) ? storedCurrentChatId : latestChatId;
|
|
90
|
+
const storedMeta = this.storage.get('meta');
|
|
91
|
+
if (storedMeta) {
|
|
92
|
+
try {
|
|
93
|
+
const metaData = JSON.parse(storedMeta);
|
|
94
|
+
this.meta = new MetaStore({
|
|
95
|
+
data: {
|
|
96
|
+
meta: metaData,
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
// Keep raw meta for lazy hydration of inactive sessions
|
|
100
|
+
this.storedMetaData = metaData;
|
|
101
|
+
// Only hydrate the active chat session — inactive sessions will be
|
|
102
|
+
// hydrated lazily when switched to via switchChat()
|
|
103
|
+
const activeChat = this.chats.find((chat) => chat.id === activeChatId);
|
|
104
|
+
if (activeChat) {
|
|
105
|
+
activeChat.hydrateResults(metaData);
|
|
106
|
+
activeChat.hydrated = true;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
this.storage.set('meta', null);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
this.currentChatId = activeChatId;
|
|
114
|
+
makeObservable(this, {
|
|
115
|
+
meta: observable,
|
|
116
|
+
inputValue: observable,
|
|
117
|
+
open: observable,
|
|
118
|
+
chats: observable,
|
|
119
|
+
currentChatId: observable,
|
|
120
|
+
chatEnabled: observable,
|
|
121
|
+
initChatLoading: observable,
|
|
122
|
+
blocked: computed,
|
|
123
|
+
currentChat: computed,
|
|
124
|
+
chatsIds: computed,
|
|
125
|
+
features: observable,
|
|
126
|
+
suggestedQuestions: observable,
|
|
127
|
+
welcomeMessage: observable,
|
|
128
|
+
productQuickview: observable,
|
|
129
|
+
productQuickviewError: observable,
|
|
130
|
+
facets: observable,
|
|
131
|
+
urlVersion: observable,
|
|
132
|
+
hasPendingFacetChanges: computed,
|
|
133
|
+
});
|
|
134
|
+
// Sync the root facets display with the active message. Only productSearchResult
|
|
135
|
+
// messages carry facets — for any other active message, clear the display so a
|
|
136
|
+
// stale facet bar from a previous response doesn't linger.
|
|
137
|
+
reaction(() => {
|
|
138
|
+
const active = this.currentChat?.activeMessage;
|
|
139
|
+
return active && active.messageType === 'productSearchResult' ? active : null;
|
|
140
|
+
}, (active) => {
|
|
141
|
+
if (active && active.id !== this.activeFacetsMessageId) {
|
|
142
|
+
const facets = active.facets;
|
|
143
|
+
this.setActiveFacets(facets || [], active.id);
|
|
144
|
+
}
|
|
145
|
+
else if (!active && this.activeFacetsMessageId !== null) {
|
|
146
|
+
this.setActiveFacets([], null);
|
|
147
|
+
}
|
|
148
|
+
}, { fireImmediately: true });
|
|
149
|
+
}
|
|
150
|
+
/** Build a SearchFacetStore from raw facet data using the current detached urlManager.
|
|
151
|
+
* Synthesizes meta entries for each facet so SearchFacetStore's display/meta filter
|
|
152
|
+
* doesn't drop chat facets (the chat backend already decided what to send). */
|
|
153
|
+
buildFacetStore(facets = []) {
|
|
154
|
+
const baseMeta = this.meta?.data || {};
|
|
155
|
+
const baseFacetMeta = baseMeta.facets || {};
|
|
156
|
+
const facetMeta = { ...baseFacetMeta };
|
|
157
|
+
facets.forEach((facet) => {
|
|
158
|
+
if (!facet.field)
|
|
159
|
+
return;
|
|
160
|
+
const expectedDisplay = facet.type === 'range' ? 'slider' : 'list';
|
|
161
|
+
const existing = facetMeta[facet.field];
|
|
162
|
+
if (!existing || existing.display !== expectedDisplay) {
|
|
163
|
+
facetMeta[facet.field] = {
|
|
164
|
+
...(existing || {}),
|
|
165
|
+
field: facet.field,
|
|
166
|
+
label: facet.label,
|
|
167
|
+
display: expectedDisplay,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
return new SearchFacetStore({
|
|
172
|
+
config: {},
|
|
173
|
+
services: { ...this.services, urlManager: this.urlManager },
|
|
174
|
+
stores: { storage: this.storage },
|
|
175
|
+
data: {
|
|
176
|
+
search: { facets },
|
|
177
|
+
meta: { ...baseMeta, facets: facetMeta },
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
/** Reset the detached urlManager and seed it with this message's filtered facet values,
|
|
182
|
+
* then rebuild the root SearchFacetStore so the UI shows the matching display. */
|
|
183
|
+
setActiveFacets(facets, messageId) {
|
|
184
|
+
// reset url state, then seed with the API-reported filtered values
|
|
185
|
+
this.urlManager.reset().go();
|
|
186
|
+
facets.forEach((facet) => {
|
|
187
|
+
// Slider/range facets carry the active selection on the facet itself rather
|
|
188
|
+
// than on individual bucket values.
|
|
189
|
+
if (facet.type === 'range') {
|
|
190
|
+
const active = facet.active;
|
|
191
|
+
const range = facet.range;
|
|
192
|
+
if (active && range && (active.low !== range.low || active.high !== range.high)) {
|
|
193
|
+
this.urlManager.set(`filter.${facet.field}`, { low: active.low, high: active.high }).go();
|
|
194
|
+
}
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const filteredValues = (facet.values || []).filter((v) => v?.filtered);
|
|
198
|
+
filteredValues.forEach((v) => {
|
|
199
|
+
if (typeof v.low !== 'undefined' || typeof v.high !== 'undefined') {
|
|
200
|
+
this.urlManager.merge(`filter.${facet.field}`, [{ low: v.low, high: v.high }]).go();
|
|
201
|
+
}
|
|
202
|
+
else if (typeof v.value !== 'undefined') {
|
|
203
|
+
this.urlManager.merge(`filter.${facet.field}`, v.value).go();
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
this.facets = this.buildFacetStore(facets);
|
|
208
|
+
this.activeFacetsMessageId = messageId;
|
|
209
|
+
this.appliedFilterSnapshot = stableStringify(this.urlManager.state?.filter);
|
|
210
|
+
}
|
|
211
|
+
/** True when the in-progress facet selection differs from the filters that were applied
|
|
212
|
+
* by the active productSearchResult — i.e. the user has selected/removed something that
|
|
213
|
+
* hasn't been sent yet. Used to decide whether to show the Apply button. */
|
|
214
|
+
get hasPendingFacetChanges() {
|
|
215
|
+
// touch the version so mobx re-runs this when urlManager state changes
|
|
216
|
+
void this.urlVersion;
|
|
217
|
+
const current = stableStringify(this.urlManager.state?.filter);
|
|
218
|
+
return current !== this.appliedFilterSnapshot;
|
|
219
|
+
}
|
|
220
|
+
get currentChat() {
|
|
221
|
+
return this.chats.find((chat) => chat.id === this.currentChatId);
|
|
222
|
+
}
|
|
223
|
+
get chatsIds() {
|
|
224
|
+
return this.chats.map((chat) => chat.id);
|
|
225
|
+
}
|
|
226
|
+
get blocked() {
|
|
227
|
+
let isBlocked = false;
|
|
228
|
+
const blockedAttachments = this.currentChat?.attachments.items.some((item) => item.type === 'image' && item.state === 'loading');
|
|
229
|
+
if (this.loading || blockedAttachments) {
|
|
230
|
+
isBlocked = true;
|
|
231
|
+
}
|
|
232
|
+
return isBlocked;
|
|
233
|
+
}
|
|
234
|
+
applyChatStatusResponse(response) {
|
|
235
|
+
const { chatbot, features } = response;
|
|
236
|
+
const { status, suggestedQuestions, welcomeMessage } = chatbot;
|
|
237
|
+
this.chatEnabled = status.enabled === true;
|
|
238
|
+
this.features = features;
|
|
239
|
+
this.suggestedQuestions = suggestedQuestions || [];
|
|
240
|
+
this.welcomeMessage = welcomeMessage || '';
|
|
241
|
+
return this.chatEnabled;
|
|
242
|
+
}
|
|
243
|
+
handleChatStatusResponse(response) {
|
|
244
|
+
const enabled = this.applyChatStatusResponse(response);
|
|
245
|
+
// Always persist on a real API response — restarts the 12-hour expiration
|
|
246
|
+
// so each new chat session refreshes the cache.
|
|
247
|
+
this.storage.set('chatStatusResponse', JSON.stringify({ response, checkTime: Date.now() }));
|
|
248
|
+
return enabled;
|
|
249
|
+
}
|
|
250
|
+
reset() {
|
|
251
|
+
this.chats = [];
|
|
252
|
+
this.clearProductQuickview();
|
|
253
|
+
this.storage.clear();
|
|
254
|
+
this.createChat();
|
|
255
|
+
}
|
|
256
|
+
setProductQuickview(product) {
|
|
257
|
+
// Clone the product so variant selections (and the parent-mappings merge done
|
|
258
|
+
// in updateProductQuickview) only affect the quickview — the carousel and any
|
|
259
|
+
// other surfaces still hold the original instance.
|
|
260
|
+
this.productQuickview = cloneProductForQuickview(product, this.meta?.data);
|
|
261
|
+
this.productQuickviewError = null;
|
|
262
|
+
}
|
|
263
|
+
updateProductQuickview(response) {
|
|
264
|
+
if (!this.productQuickview) {
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
// merge parent-level mappings from the API response into the Product
|
|
268
|
+
this.productQuickview.mappings = {
|
|
269
|
+
...this.productQuickview.mappings,
|
|
270
|
+
core: { ...this.productQuickview.mappings.core, ...response.mappings.core },
|
|
271
|
+
};
|
|
272
|
+
const meta = this.meta?.data || {};
|
|
273
|
+
if (this.productQuickview.variants) {
|
|
274
|
+
// update existing Variants with new data from the Products API
|
|
275
|
+
this.productQuickview.variants.optionConfig = response.variants.optionConfig;
|
|
276
|
+
this.productQuickview.variants.update(response.variants.data);
|
|
277
|
+
}
|
|
278
|
+
else {
|
|
279
|
+
// create Variants for the first time using the Products API data
|
|
280
|
+
this.productQuickview.variants = new Variants({
|
|
281
|
+
data: {
|
|
282
|
+
mask: this.productQuickview.mask,
|
|
283
|
+
variants: response.variants.data,
|
|
284
|
+
optionConfig: response.variants.optionConfig,
|
|
285
|
+
meta,
|
|
286
|
+
},
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
setProductQuickviewError(message) {
|
|
291
|
+
this.productQuickviewError = message;
|
|
292
|
+
}
|
|
293
|
+
clearProductQuickview() {
|
|
294
|
+
this.productQuickview = null;
|
|
295
|
+
this.productQuickviewError = null;
|
|
296
|
+
}
|
|
297
|
+
clearHistory() {
|
|
298
|
+
// drop previous chats while keeping the conversation the user is currently in;
|
|
299
|
+
// if there is no current chat, fall back to a full reset
|
|
300
|
+
const currentChat = this.currentChat;
|
|
301
|
+
if (!currentChat) {
|
|
302
|
+
this.reset();
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
const storedChats = this.storage.get('chats') || {};
|
|
306
|
+
const filtered = {};
|
|
307
|
+
if (storedChats[currentChat.id]) {
|
|
308
|
+
filtered[currentChat.id] = storedChats[currentChat.id];
|
|
309
|
+
}
|
|
310
|
+
this.storage.set('chats', filtered);
|
|
311
|
+
this.chats = [currentChat];
|
|
312
|
+
}
|
|
313
|
+
createChat(data) {
|
|
314
|
+
// abandon any in-flight request state from the previous chat — its
|
|
315
|
+
// response will be discarded by the controller, so the UI shouldn't
|
|
316
|
+
// keep showing its loading spinner or stale error
|
|
317
|
+
this.loading = false;
|
|
318
|
+
this.error = undefined;
|
|
319
|
+
// Prune old sessions before creating a new one to keep storage bounded
|
|
320
|
+
ChatSessionStore.pruneStoredSessions(this.storage);
|
|
321
|
+
const newChat = new ChatSessionStore({
|
|
322
|
+
data: {
|
|
323
|
+
sessionId: data?.sessionId,
|
|
324
|
+
sessionEndTime: data?.sessionEndTime,
|
|
325
|
+
},
|
|
326
|
+
stores: {
|
|
327
|
+
storage: this.storage,
|
|
328
|
+
},
|
|
329
|
+
});
|
|
330
|
+
this.currentChatId = newChat.id;
|
|
331
|
+
this.storage.set('currentChatId', newChat.id);
|
|
332
|
+
this.chats.push(newChat);
|
|
333
|
+
return newChat;
|
|
334
|
+
}
|
|
335
|
+
switchChat(id) {
|
|
336
|
+
const chatExists = this.chats.find((chat) => chat.id === id);
|
|
337
|
+
if (chatExists) {
|
|
338
|
+
// Lazily hydrate results when switching to a session for the first time
|
|
339
|
+
if (!chatExists.hydrated && this.storedMetaData) {
|
|
340
|
+
chatExists.hydrateResults(this.storedMetaData);
|
|
341
|
+
chatExists.hydrated = true;
|
|
342
|
+
}
|
|
343
|
+
this.currentChatId = id;
|
|
344
|
+
this.storage.set('currentChatId', id);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
sendProductQuery(result, options) {
|
|
348
|
+
const display = result?.display || result;
|
|
349
|
+
this.currentChat?.attachments.add({
|
|
350
|
+
type: 'product',
|
|
351
|
+
requestType: options.requestType,
|
|
352
|
+
productId: result.id,
|
|
353
|
+
name: display.mappings?.core?.name,
|
|
354
|
+
thumbnailUrl: display.mappings?.core?.thumbnailImageUrl || display.mappings?.core?.imageUrl || display.mappings?.core?.parentImageUrl,
|
|
355
|
+
});
|
|
356
|
+
// for the productQuery flow we want the secondary chat to immediately
|
|
357
|
+
// show the product details panel — push a productQuery message so it
|
|
358
|
+
// becomes the active side-chat message until a productAnswer arrives
|
|
359
|
+
if (options.requestType === 'productQuery' && this.currentChat) {
|
|
360
|
+
this.currentChat.pushProductQueryMessage(result);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
compareProduct(result) {
|
|
364
|
+
this.currentChat?.comparisons.add({
|
|
365
|
+
result,
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
addFacet(facet) {
|
|
369
|
+
const value = parseFacetValue(facet.value);
|
|
370
|
+
this.urlManager.merge(`filter.${facet.key}`, Array.isArray(value) ? value : value).go();
|
|
371
|
+
}
|
|
372
|
+
removeFacet(key, value) {
|
|
373
|
+
const parsed = parseFacetValue(value);
|
|
374
|
+
this.urlManager.remove(`filter.${key}`, parsed).go();
|
|
375
|
+
}
|
|
376
|
+
clearPendingFacets() {
|
|
377
|
+
this.urlManager.remove('filter').go();
|
|
378
|
+
}
|
|
379
|
+
isFacetSelected(key, value) {
|
|
380
|
+
// touch the version so mobx re-runs this when urlManager state changes (the
|
|
381
|
+
// underlying UrlManager isn't a mobx observable, so we mirror its updates here)
|
|
382
|
+
void this.urlVersion;
|
|
383
|
+
const filterState = this.urlManager.state?.filter;
|
|
384
|
+
if (!filterState || !filterState[key])
|
|
385
|
+
return false;
|
|
386
|
+
const stored = Array.isArray(filterState[key]) ? filterState[key] : [filterState[key]];
|
|
387
|
+
const parsed = parseFacetValue(value);
|
|
388
|
+
return stored.some((entry) => facetValueEquals(entry, parsed));
|
|
389
|
+
}
|
|
390
|
+
request(request) {
|
|
391
|
+
// snapshot facet labels (field + per-value) from the active facets display so the
|
|
392
|
+
// outgoing user message can render "Filter by Color Red" rather than raw keys
|
|
393
|
+
const filterLabels = {};
|
|
394
|
+
for (const facet of this.facets) {
|
|
395
|
+
const valueFacet = facet;
|
|
396
|
+
const values = {};
|
|
397
|
+
(valueFacet.values || []).forEach((v) => {
|
|
398
|
+
if (typeof v.low !== 'undefined' || typeof v.high !== 'undefined') {
|
|
399
|
+
values[`${v.low ?? '*'}:${v.high ?? '*'}`] = v.label;
|
|
400
|
+
}
|
|
401
|
+
else if (typeof v.value !== 'undefined') {
|
|
402
|
+
values[v.value] = v.label;
|
|
403
|
+
}
|
|
404
|
+
});
|
|
405
|
+
filterLabels[valueFacet.field] = { facetLabel: valueFacet.label || valueFacet.field, values };
|
|
406
|
+
}
|
|
407
|
+
this.currentChat?.request(request, filterLabels);
|
|
408
|
+
// remove any productSimilar attachments after request
|
|
409
|
+
const productSimilarAttachments = this.currentChat?.attachments.attached.filter((item) => item.type === 'product' && item.requestType === 'productSimilar') || [];
|
|
410
|
+
productSimilarAttachments.forEach((item) => {
|
|
411
|
+
this.currentChat?.attachments.remove(item.id);
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
update(data) {
|
|
415
|
+
// TODO: handle error
|
|
416
|
+
// if(err?.responseBody?.errorMessage || err?.responseBody?.errorCode) {
|
|
417
|
+
// const errorMessage = err?.responseBody?.errorMessage || 'An unknown error has occurred.';
|
|
418
|
+
// }
|
|
419
|
+
this.currentChat?.update(data);
|
|
420
|
+
if (!this.meta) {
|
|
421
|
+
this.storage.set('meta', JSON.stringify(data.meta));
|
|
422
|
+
}
|
|
423
|
+
this.meta = new MetaStore({
|
|
424
|
+
data: {
|
|
425
|
+
meta: data.meta,
|
|
426
|
+
},
|
|
427
|
+
});
|
|
428
|
+
// Keep raw meta in sync for lazy hydration of inactive sessions
|
|
429
|
+
this.storedMetaData = data.meta;
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
/** Deterministic JSON.stringify — sorts object keys recursively so equality comparisons
|
|
433
|
+
* aren't sensitive to insertion order. */
|
|
434
|
+
function stableStringify(value) {
|
|
435
|
+
if (value === null || value === undefined)
|
|
436
|
+
return 'null';
|
|
437
|
+
if (typeof value !== 'object')
|
|
438
|
+
return JSON.stringify(value);
|
|
439
|
+
if (Array.isArray(value)) {
|
|
440
|
+
return `[${value.map((v) => stableStringify(v)).join(',')}]`;
|
|
441
|
+
}
|
|
442
|
+
const keys = Object.keys(value).sort();
|
|
443
|
+
return `{${keys.map((k) => `${JSON.stringify(k)}:${stableStringify(value[k])}`).join(',')}}`;
|
|
444
|
+
}
|
|
445
|
+
/** Convert a chat-facet UI value into the URL-state shape — range buckets arrive as "low:high". */
|
|
446
|
+
function parseFacetValue(value) {
|
|
447
|
+
if (typeof value === 'string' && value.includes(':')) {
|
|
448
|
+
const [low, high] = value.split(':');
|
|
449
|
+
return {
|
|
450
|
+
low: low === '*' ? undefined : Number(low),
|
|
451
|
+
high: high === '*' ? undefined : Number(high),
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
return value;
|
|
455
|
+
}
|
|
456
|
+
function facetValueEquals(a, b) {
|
|
457
|
+
if (typeof a === 'object' && a !== null && typeof b === 'object' && b !== null) {
|
|
458
|
+
return a.low === b.low && a.high === b.high;
|
|
459
|
+
}
|
|
460
|
+
return a === b;
|
|
461
|
+
}
|
|
462
|
+
/** Build a fresh observable Product from an existing one. Variants are intentionally
|
|
463
|
+
* omitted — `updateProductQuickview` populates them from the products API response. */
|
|
464
|
+
function cloneProductForQuickview(product, meta) {
|
|
465
|
+
const result = {
|
|
466
|
+
id: product.id,
|
|
467
|
+
responseId: product.responseId,
|
|
468
|
+
mappings: JSON.parse(JSON.stringify(product.mappings || {})),
|
|
469
|
+
attributes: JSON.parse(JSON.stringify(product.attributes || {})),
|
|
470
|
+
badges: product.badges?.all?.map((b) => ({ tag: b.tag })) || [],
|
|
471
|
+
};
|
|
472
|
+
return new Product({
|
|
473
|
+
config: {},
|
|
474
|
+
data: { result, meta: meta || {} },
|
|
475
|
+
position: product.position ?? 0,
|
|
476
|
+
responseId: product.responseId,
|
|
477
|
+
});
|
|
478
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
type AttachmentState = 'loading' | 'error' | 'attached' | 'active' | 'saved';
|
|
2
|
+
type AttachmentError = {
|
|
3
|
+
message: string;
|
|
4
|
+
};
|
|
5
|
+
export type ChatAttachmentAddAttachment = ChatAttachmentImageConfig | ChatAttachmentProductConfig | ChatAttachmentFacetConfig;
|
|
6
|
+
export declare class ChatAttachmentStore {
|
|
7
|
+
items: ChatAttachments[];
|
|
8
|
+
constructor();
|
|
9
|
+
get attached(): ChatAttachments[];
|
|
10
|
+
get compared(): ChatAttachmentProduct[];
|
|
11
|
+
add<T extends ChatAttachments>(attachment: ChatAttachmentAddAttachment): T;
|
|
12
|
+
remove(id: string): void;
|
|
13
|
+
get(id: string): ChatAttachments | undefined;
|
|
14
|
+
reset(): void;
|
|
15
|
+
hydrate(attachments: ChatAttachmentAddAttachment[]): void;
|
|
16
|
+
}
|
|
17
|
+
type ChatAttachments = ChatAttachmentImage | ChatAttachmentProduct | ChatAttachmentFacet;
|
|
18
|
+
declare abstract class ChatAttachment {
|
|
19
|
+
abstract type: string;
|
|
20
|
+
id: string;
|
|
21
|
+
state: AttachmentState;
|
|
22
|
+
error?: AttachmentError;
|
|
23
|
+
constructor(params?: {
|
|
24
|
+
data?: {
|
|
25
|
+
id?: string;
|
|
26
|
+
state?: AttachmentState;
|
|
27
|
+
error?: AttachmentError;
|
|
28
|
+
};
|
|
29
|
+
});
|
|
30
|
+
save(): void;
|
|
31
|
+
activate(): void;
|
|
32
|
+
}
|
|
33
|
+
type ChatAttachmentImageConfig = {
|
|
34
|
+
type: 'image';
|
|
35
|
+
id?: string;
|
|
36
|
+
base64?: string;
|
|
37
|
+
fileName?: string;
|
|
38
|
+
imageId?: string;
|
|
39
|
+
imageUrl?: string;
|
|
40
|
+
thumbnailUrl?: string;
|
|
41
|
+
state?: AttachmentState;
|
|
42
|
+
error?: AttachmentError;
|
|
43
|
+
};
|
|
44
|
+
type ChatAttachmentProductConfig = {
|
|
45
|
+
type: 'product';
|
|
46
|
+
id?: string;
|
|
47
|
+
productId: string;
|
|
48
|
+
thumbnailUrl: string;
|
|
49
|
+
name: string;
|
|
50
|
+
requestType: 'productQuery' | 'productSimilar' | 'productComparison';
|
|
51
|
+
state?: AttachmentState;
|
|
52
|
+
error?: AttachmentError;
|
|
53
|
+
};
|
|
54
|
+
type ChatAttachmentFacetConfig = {
|
|
55
|
+
type: 'facet';
|
|
56
|
+
id?: string;
|
|
57
|
+
key: string;
|
|
58
|
+
facetLabel: string;
|
|
59
|
+
value: string;
|
|
60
|
+
label: string;
|
|
61
|
+
count: number;
|
|
62
|
+
state?: AttachmentState;
|
|
63
|
+
error?: AttachmentError;
|
|
64
|
+
};
|
|
65
|
+
export declare class ChatAttachmentProduct extends ChatAttachment {
|
|
66
|
+
type: 'product' | never;
|
|
67
|
+
productId: string;
|
|
68
|
+
thumbnailUrl: string;
|
|
69
|
+
name: string;
|
|
70
|
+
requestType: 'productQuery' | 'productSimilar' | 'productComparison';
|
|
71
|
+
constructor({ id, productId, thumbnailUrl, name, requestType, state, error }: ChatAttachmentProductConfig);
|
|
72
|
+
}
|
|
73
|
+
export declare class ChatAttachmentFacet extends ChatAttachment {
|
|
74
|
+
type: 'facet' | never;
|
|
75
|
+
key: string;
|
|
76
|
+
facetLabel: string;
|
|
77
|
+
value: string;
|
|
78
|
+
label: string;
|
|
79
|
+
count: number;
|
|
80
|
+
constructor({ id, key, facetLabel, value, label, count, state, error }: ChatAttachmentFacetConfig);
|
|
81
|
+
}
|
|
82
|
+
export declare class ChatAttachmentImage extends ChatAttachment {
|
|
83
|
+
type: 'image' | never;
|
|
84
|
+
fileName?: string;
|
|
85
|
+
imageId?: string;
|
|
86
|
+
imageUrl?: string;
|
|
87
|
+
thumbnailUrl?: string;
|
|
88
|
+
base64?: string;
|
|
89
|
+
constructor({ id, base64, fileName, imageId, imageUrl, thumbnailUrl, state, error }: ChatAttachmentImageConfig);
|
|
90
|
+
update: ({ imageId, imageUrl, thumbnailUrl, error, }: {
|
|
91
|
+
imageId?: string;
|
|
92
|
+
imageUrl?: string;
|
|
93
|
+
thumbnailUrl?: string;
|
|
94
|
+
error?: AttachmentError;
|
|
95
|
+
}) => Promise<void>;
|
|
96
|
+
}
|
|
97
|
+
export {};
|
|
98
|
+
//# sourceMappingURL=ChatAttachmentStore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ChatAttachmentStore.d.ts","sourceRoot":"","sources":["../../../../src/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