@athoscommerce/snap-store-mobx 1.2.3-beta.4 → 1.4.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 (81) hide show
  1. package/dist/cjs/Autocomplete/AutocompleteStore.d.ts +1 -1
  2. package/dist/cjs/Autocomplete/AutocompleteStore.d.ts.map +1 -1
  3. package/dist/cjs/Autocomplete/AutocompleteStore.js +4 -4
  4. package/dist/cjs/Autocomplete/Stores/AutocompleteFacetStore.d.ts.map +1 -1
  5. package/dist/cjs/Autocomplete/Stores/AutocompleteFacetStore.js +2 -4
  6. package/dist/cjs/Finder/FinderStore.d.ts +1 -1
  7. package/dist/cjs/Finder/FinderStore.d.ts.map +1 -1
  8. package/dist/cjs/Finder/FinderStore.js +3 -3
  9. package/dist/cjs/Finder/Stores/FinderSelectionStore.d.ts +1 -1
  10. package/dist/cjs/Finder/Stores/FinderSelectionStore.d.ts.map +1 -1
  11. package/dist/cjs/Search/SearchStore.d.ts +1 -1
  12. package/dist/cjs/Search/SearchStore.d.ts.map +1 -1
  13. package/dist/cjs/Search/SearchStore.js +2 -2
  14. package/dist/cjs/Search/Stores/SearchFacetStore.d.ts +12 -12
  15. package/dist/cjs/Search/Stores/SearchFacetStore.d.ts.map +1 -1
  16. package/dist/cjs/Search/Stores/SearchFacetStore.js +22 -32
  17. package/dist/cjs/Search/Stores/SearchHistoryStore.js +2 -2
  18. package/dist/cjs/Search/Stores/index.d.ts +1 -1
  19. package/dist/cjs/Search/Stores/index.d.ts.map +1 -1
  20. package/dist/cjs/Search/Stores/index.js +1 -2
  21. package/dist/cjs/index.d.ts +1 -5
  22. package/dist/cjs/index.d.ts.map +1 -1
  23. package/dist/cjs/index.js +4 -8
  24. package/dist/cjs/types.d.ts +2 -13
  25. package/dist/cjs/types.d.ts.map +1 -1
  26. package/dist/esm/Autocomplete/AutocompleteStore.d.ts +1 -1
  27. package/dist/esm/Autocomplete/AutocompleteStore.d.ts.map +1 -1
  28. package/dist/esm/Autocomplete/AutocompleteStore.js +2 -2
  29. package/dist/esm/Autocomplete/Stores/AutocompleteFacetStore.d.ts.map +1 -1
  30. package/dist/esm/Autocomplete/Stores/AutocompleteFacetStore.js +5 -7
  31. package/dist/esm/Finder/FinderStore.d.ts +1 -1
  32. package/dist/esm/Finder/FinderStore.d.ts.map +1 -1
  33. package/dist/esm/Finder/FinderStore.js +1 -1
  34. package/dist/esm/Finder/Stores/FinderSelectionStore.d.ts +1 -1
  35. package/dist/esm/Finder/Stores/FinderSelectionStore.d.ts.map +1 -1
  36. package/dist/esm/Search/SearchStore.d.ts +1 -1
  37. package/dist/esm/Search/SearchStore.d.ts.map +1 -1
  38. package/dist/esm/Search/SearchStore.js +1 -1
  39. package/dist/esm/Search/Stores/SearchFacetStore.d.ts +12 -12
  40. package/dist/esm/Search/Stores/SearchFacetStore.d.ts.map +1 -1
  41. package/dist/esm/Search/Stores/SearchFacetStore.js +22 -31
  42. package/dist/esm/Search/Stores/SearchHistoryStore.js +1 -1
  43. package/dist/esm/Search/Stores/index.d.ts +1 -1
  44. package/dist/esm/Search/Stores/index.d.ts.map +1 -1
  45. package/dist/esm/Search/Stores/index.js +1 -1
  46. package/dist/esm/index.d.ts +1 -5
  47. package/dist/esm/index.d.ts.map +1 -1
  48. package/dist/esm/index.js +1 -3
  49. package/dist/esm/types.d.ts +2 -13
  50. package/dist/esm/types.d.ts.map +1 -1
  51. package/package.json +11 -6
  52. package/dist/cjs/Chat/ChatStore.d.ts +0 -57
  53. package/dist/cjs/Chat/ChatStore.d.ts.map +0 -1
  54. package/dist/cjs/Chat/ChatStore.js +0 -415
  55. package/dist/cjs/Chat/Stores/ChatAttachmentStore.d.ts +0 -98
  56. package/dist/cjs/Chat/Stores/ChatAttachmentStore.d.ts.map +0 -1
  57. package/dist/cjs/Chat/Stores/ChatAttachmentStore.js +0 -314
  58. package/dist/cjs/Chat/Stores/ChatCompareStore.d.ts +0 -14
  59. package/dist/cjs/Chat/Stores/ChatCompareStore.d.ts.map +0 -1
  60. package/dist/cjs/Chat/Stores/ChatCompareStore.js +0 -63
  61. package/dist/cjs/Chat/Stores/ChatSessionStore.d.ts +0 -117
  62. package/dist/cjs/Chat/Stores/ChatSessionStore.d.ts.map +0 -1
  63. package/dist/cjs/Chat/Stores/ChatSessionStore.js +0 -689
  64. package/dist/cjs/Storage/StorageStore.d.ts +0 -27
  65. package/dist/cjs/Storage/StorageStore.d.ts.map +0 -1
  66. package/dist/cjs/Storage/StorageStore.js +0 -176
  67. package/dist/esm/Chat/ChatStore.d.ts +0 -57
  68. package/dist/esm/Chat/ChatStore.d.ts.map +0 -1
  69. package/dist/esm/Chat/ChatStore.js +0 -355
  70. package/dist/esm/Chat/Stores/ChatAttachmentStore.d.ts +0 -98
  71. package/dist/esm/Chat/Stores/ChatAttachmentStore.d.ts.map +0 -1
  72. package/dist/esm/Chat/Stores/ChatAttachmentStore.js +0 -222
  73. package/dist/esm/Chat/Stores/ChatCompareStore.d.ts +0 -14
  74. package/dist/esm/Chat/Stores/ChatCompareStore.d.ts.map +0 -1
  75. package/dist/esm/Chat/Stores/ChatCompareStore.js +0 -49
  76. package/dist/esm/Chat/Stores/ChatSessionStore.d.ts +0 -117
  77. package/dist/esm/Chat/Stores/ChatSessionStore.d.ts.map +0 -1
  78. package/dist/esm/Chat/Stores/ChatSessionStore.js +0 -661
  79. package/dist/esm/Storage/StorageStore.d.ts +0 -27
  80. package/dist/esm/Storage/StorageStore.d.ts.map +0 -1
  81. package/dist/esm/Storage/StorageStore.js +0 -169
@@ -1,27 +0,0 @@
1
- export declare class StorageStore {
2
- private type;
3
- private expiration;
4
- private sameSite;
5
- private key;
6
- private cookieDomain;
7
- state: Record<string, any>;
8
- constructor(config?: StorageConfig);
9
- set(path: string | string[], value: any): void;
10
- get(path: string | string[]): any | undefined;
11
- clear(): void;
12
- }
13
- export type StorageConfig = {
14
- type: StorageType | keyof typeof StorageType;
15
- cookie?: {
16
- expiration?: number;
17
- sameSite?: string;
18
- };
19
- key: string;
20
- };
21
- export declare enum StorageType {
22
- session = "session",
23
- local = "local",
24
- cookie = "cookie",
25
- memory = "memory"
26
- }
27
- //# sourceMappingURL=StorageStore.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"StorageStore.d.ts","sourceRoot":"","sources":["../../../src/Storage/StorageStore.ts"],"names":[],"mappings":"AAMA,qBAAa,YAAY;IACxB,OAAO,CAAC,IAAI,CAA4B;IACxC,OAAO,CAAC,UAAU,CAAe;IACjC,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,GAAG,CAAgB;IAC3B,OAAO,CAAC,YAAY,CAC8G;IAC3H,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAM;gBAE3B,MAAM,CAAC,EAAE,aAAa;IA8C3B,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,EAAE,KAAK,EAAE,GAAG,GAAG,IAAI;IAuD9C,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,GAAG,GAAG,SAAS;IAuC7C,KAAK,IAAI,IAAI;CAcpB;AAED,MAAM,MAAM,aAAa,GAAG;IAC3B,IAAI,EAAE,WAAW,GAAG,MAAM,OAAO,WAAW,CAAC;IAC7C,MAAM,CAAC,EAAE;QACR,UAAU,CAAC,EAAE,MAAM,CAAC;QACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;KAClB,CAAC;IACF,GAAG,EAAE,MAAM,CAAC;CACZ,CAAC;AAEF,oBAAY,WAAW;IACtB,OAAO,YAAY;IACnB,KAAK,UAAU;IACf,MAAM,WAAW;IACjB,MAAM,WAAW;CACjB"}
@@ -1,176 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StorageType = exports.StorageStore = void 0;
4
- var snap_toolbox_1 = require("@athoscommerce/snap-toolbox");
5
- var utils = {
6
- cookies: snap_toolbox_1.cookies,
7
- };
8
- var StorageStore = /** @class */ (function () {
9
- function StorageStore(config) {
10
- var _a, _b;
11
- this.type = null;
12
- this.expiration = 31536000000; // one year (ms)
13
- this.sameSite = 'Lax';
14
- this.key = 'ss-storage';
15
- this.cookieDomain = (typeof window !== 'undefined' && window.location.hostname && '.' + window.location.hostname.replace(/^www\./, '')) || undefined;
16
- this.state = {};
17
- if (config) {
18
- if (config.key.trim() !== '') {
19
- this.key = config.key.trim();
20
- }
21
- if ((_a = config === null || config === void 0 ? void 0 : config.cookie) === null || _a === void 0 ? void 0 : _a.expiration) {
22
- this.expiration = config.cookie.expiration;
23
- }
24
- if ((_b = config === null || config === void 0 ? void 0 : config.cookie) === null || _b === void 0 ? void 0 : _b.sameSite) {
25
- this.sameSite = config.cookie.sameSite;
26
- }
27
- switch (config.type) {
28
- case StorageType.session: {
29
- this.type = snap_toolbox_1.featureFlags.storage ? config.type : null;
30
- if (this.type) {
31
- this.state = JSON.parse(window.sessionStorage.getItem(this.key) || '{}');
32
- window.sessionStorage.setItem(this.key, JSON.stringify(this.state));
33
- }
34
- break;
35
- }
36
- case StorageType.local: {
37
- this.type = snap_toolbox_1.featureFlags.storage ? config.type : null;
38
- if (this.type) {
39
- this.state = JSON.parse(window.localStorage.getItem(this.key) || '{}');
40
- window.localStorage.setItem(this.key, JSON.stringify(this.state));
41
- }
42
- break;
43
- }
44
- case StorageType.cookie: {
45
- if (snap_toolbox_1.featureFlags.cookies) {
46
- this.type = config.type;
47
- var data = utils.cookies.get(this.key);
48
- if (data) {
49
- this.state = JSON.parse(data);
50
- }
51
- }
52
- break;
53
- }
54
- default: {
55
- this.type = StorageType.memory;
56
- }
57
- }
58
- }
59
- }
60
- StorageStore.prototype.set = function (path, value) {
61
- // read from the backing storage first to update local state to ensure we get any changes made outside of the storageStore
62
- switch (this.type) {
63
- case StorageType.session: {
64
- this.state = JSON.parse(window.sessionStorage.getItem(this.key) || '{}');
65
- break;
66
- }
67
- case StorageType.local: {
68
- this.state = JSON.parse(window.localStorage.getItem(this.key) || '{}');
69
- break;
70
- }
71
- case StorageType.cookie: {
72
- var data = utils.cookies.get(this.key);
73
- if (data) {
74
- this.state = JSON.parse(data);
75
- }
76
- break;
77
- }
78
- }
79
- var paths;
80
- if (typeof path == 'string') {
81
- paths = path === null || path === void 0 ? void 0 : path.split('.');
82
- }
83
- else {
84
- paths = path;
85
- }
86
- var location = this.state;
87
- paths === null || paths === void 0 ? void 0 : paths.forEach(function (p, i) {
88
- var leaf = i == paths.length - 1;
89
- if (leaf) {
90
- location[p] = value;
91
- }
92
- else {
93
- location = location[p] = location[p] || {};
94
- }
95
- });
96
- try {
97
- switch (this.type) {
98
- case StorageType.session:
99
- window.sessionStorage.setItem(this.key, JSON.stringify(this.state));
100
- break;
101
- case StorageType.local:
102
- window.localStorage.setItem(this.key, JSON.stringify(this.state));
103
- break;
104
- case StorageType.cookie:
105
- utils.cookies.set(this.key, JSON.stringify(this.state), this.sameSite, this.expiration, this.cookieDomain);
106
- break;
107
- }
108
- }
109
- catch (err) {
110
- console.warn("something went wrong setting ".concat(this.key, " to ").concat(this.type, " storage"));
111
- }
112
- };
113
- // TODO: change any to unknown and refactor
114
- StorageStore.prototype.get = function (path) {
115
- switch (this.type) {
116
- case StorageType.session:
117
- var sessionData = window.sessionStorage.getItem(this.key);
118
- this.state = sessionData ? JSON.parse(sessionData) : {};
119
- break;
120
- case StorageType.local:
121
- var localData = window.localStorage.getItem(this.key);
122
- this.state = localData ? JSON.parse(localData) : {};
123
- break;
124
- case StorageType.cookie:
125
- var data = utils.cookies.get(this.key);
126
- if (data) {
127
- this.state = JSON.parse(data) || {};
128
- }
129
- break;
130
- }
131
- var paths;
132
- if (typeof path == 'string') {
133
- paths = path === null || path === void 0 ? void 0 : path.split('.');
134
- }
135
- else {
136
- paths = path;
137
- }
138
- if (!(paths === null || paths === void 0 ? void 0 : paths.length))
139
- return undefined;
140
- var value = this.state;
141
- for (var _i = 0, paths_1 = paths; _i < paths_1.length; _i++) {
142
- var p = paths_1[_i];
143
- if (value && typeof value[p] != 'undefined') {
144
- value = value[p];
145
- }
146
- else {
147
- value = {};
148
- return;
149
- }
150
- }
151
- return value;
152
- };
153
- StorageStore.prototype.clear = function () {
154
- switch (this.type) {
155
- case StorageType.session:
156
- window.sessionStorage.removeItem(this.key);
157
- break;
158
- case StorageType.local:
159
- window.localStorage.removeItem(this.key);
160
- break;
161
- case StorageType.cookie:
162
- utils.cookies.unset(this.key, this.cookieDomain);
163
- break;
164
- }
165
- this.state = {};
166
- };
167
- return StorageStore;
168
- }());
169
- exports.StorageStore = StorageStore;
170
- var StorageType;
171
- (function (StorageType) {
172
- StorageType["session"] = "session";
173
- StorageType["local"] = "local";
174
- StorageType["cookie"] = "cookie";
175
- StorageType["memory"] = "memory";
176
- })(StorageType || (exports.StorageType = StorageType = {}));
@@ -1,57 +0,0 @@
1
- import { ChatStoreConfig } from '../types';
2
- import { MetaStore } from '../Meta/MetaStore';
3
- import type { MetaResponseModel } from '@athoscommerce/snapi-types';
4
- import { AbstractStore } from '../Abstract/AbstractStore';
5
- import type { ChatResponseModel, ChatRequestModel, ProductsResponseModel } from '@athoscommerce/snap-client';
6
- import { StorageStore } from '../Storage/StorageStore';
7
- import { ChatSessionStore } from './Stores/ChatSessionStore';
8
- import { ChatStatusResponse } from '@athoscommerce/snap-client';
9
- import { Product } from '../Search/Stores/SearchResultStore';
10
- export declare class ChatStore extends AbstractStore<ChatStoreConfig> {
11
- meta?: MetaStore;
12
- inputValue: string;
13
- open: boolean;
14
- storage: StorageStore;
15
- chats: ChatSessionStore[];
16
- currentChatId: string;
17
- chatEnabled: boolean | null;
18
- initChatLoading: boolean;
19
- suggestedQuestions: string[];
20
- welcomeMessage: string;
21
- features: ChatStatusResponse['features'];
22
- productQuickview: Product | null;
23
- productQuickviewError: string | null;
24
- /** Raw meta kept for lazy hydration of inactive chat sessions. */
25
- private storedMetaData;
26
- constructor(config: ChatStoreConfig);
27
- get currentChat(): ChatSessionStore | undefined;
28
- get chatsIds(): string[];
29
- get blocked(): boolean;
30
- private applyChatStatusResponse;
31
- handleChatStatusResponse(response: ChatStatusResponse): boolean;
32
- reset(): void;
33
- setProductQuickview(product: Product): void;
34
- updateProductQuickview(response: ProductsResponseModel): void;
35
- setProductQuickviewError(message: string): void;
36
- clearProductQuickview(): void;
37
- clearHistory(): void;
38
- createChat(data?: {
39
- sessionId: string;
40
- }): ChatSessionStore;
41
- switchChat(id: string): void;
42
- sendProductQuery(result: any, options: {
43
- requestType: 'productQuery' | 'productSimilar' | 'productComparison';
44
- }): void;
45
- compareProduct(result: any): void;
46
- addFacet(facet: any): void;
47
- removeFacet(key: string, value: string): void;
48
- clearPendingFacets(): void;
49
- isFacetSelected(key: string, value: string): boolean;
50
- isFacetRemoved(key: string, value: string): boolean;
51
- request(request: ChatRequestModel): void;
52
- update(data: {
53
- chat: ChatResponseModel;
54
- meta: MetaResponseModel;
55
- }): void;
56
- }
57
- //# sourceMappingURL=ChatStore.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ChatStore.d.ts","sourceRoot":"","sources":["../../../src/Chat/ChatStore.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAC3C,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAC9C,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAC;AACpE,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAC1D,OAAO,KAAK,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AAC7G,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAE7D,OAAO,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAChE,OAAO,EAAE,OAAO,EAAY,MAAM,oCAAoC,CAAC;AAIvE,qBAAa,SAAU,SAAQ,aAAa,CAAC,eAAe,CAAC;IACrD,IAAI,CAAC,EAAE,SAAS,CAAa;IAC7B,UAAU,EAAE,MAAM,CAAM;IACxB,IAAI,EAAE,OAAO,CAAS;IACtB,OAAO,EAAE,YAAY,CAAC;IACtB,KAAK,EAAE,gBAAgB,EAAE,CAAM;IAC/B,aAAa,EAAE,MAAM,CAAC;IACtB,WAAW,EAAE,OAAO,GAAG,IAAI,CAAQ;IACnC,eAAe,EAAE,OAAO,CAAS;IACjC,kBAAkB,EAAE,MAAM,EAAE,CAAM;IAClC,cAAc,EAAE,MAAM,CAAM;IAC5B,QAAQ,EAAE,kBAAkB,CAAC,UAAU,CAAC,CAA4E;IACpH,gBAAgB,EAAE,OAAO,GAAG,IAAI,CAAQ;IACxC,qBAAqB,EAAE,MAAM,GAAG,IAAI,CAAQ;IACnD,kEAAkE;IAClE,OAAO,CAAC,cAAc,CAAkC;gBAE5C,MAAM,EAAE,eAAe;IAsGnC,IAAI,WAAW,IAAI,gBAAgB,GAAG,SAAS,CAE9C;IAED,IAAI,QAAQ,IAAI,MAAM,EAAE,CAEvB;IAED,IAAI,OAAO,IAAI,OAAO,CAQrB;IAED,OAAO,CAAC,uBAAuB;IAUxB,wBAAwB,CAAC,QAAQ,EAAE,kBAAkB,GAAG,OAAO;IAQ/D,KAAK,IAAI,IAAI;IAOb,mBAAmB,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI;IAK3C,sBAAsB,CAAC,QAAQ,EAAE,qBAAqB,GAAG,IAAI;IA8B7D,wBAAwB,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAI/C,qBAAqB,IAAI,IAAI;IAK7B,YAAY,IAAI,IAAI;IAoBpB,UAAU,CAAC,IAAI,CAAC,EAAE;QAAE,SAAS,EAAE,MAAM,CAAA;KAAE,GAAG,gBAAgB;IAyB1D,UAAU,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI;IAa5B,gBAAgB,CAAC,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE;QAAE,WAAW,EAAE,cAAc,GAAG,gBAAgB,GAAG,mBAAmB,CAAA;KAAE,GAAG,IAAI;IAkBtH,cAAc,CAAC,MAAM,EAAE,GAAG,GAAG,IAAI;IAMjC,QAAQ,CAAC,KAAK,EAAE,GAAG,GAAG,IAAI;IAmB1B,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI;IAuB7C,kBAAkB,IAAI,IAAI;IAc1B,eAAe,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO;IAkBpD,cAAc,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO;IAInD,OAAO,CAAC,OAAO,EAAE,gBAAgB,GAAG,IAAI;IAmBxC,MAAM,CAAC,IAAI,EAAE;QAAE,IAAI,EAAE,iBAAiB,CAAC;QAAC,IAAI,EAAE,iBAAiB,CAAA;KAAE,GAAG,IAAI;CAkB/E"}
@@ -1,355 +0,0 @@
1
- import { makeObservable, observable, computed } from 'mobx';
2
- import { MetaStore } from '../Meta/MetaStore';
3
- import { AbstractStore } from '../Abstract/AbstractStore';
4
- import { StorageStore } from '../Storage/StorageStore';
5
- import { ChatSessionStore } from './Stores/ChatSessionStore';
6
- import { Variants } from '../Search/Stores/SearchResultStore';
7
- const CHAT_STATUS_EXPIRATION_TIME = 1000 * 60 * 60 * 12; // 12 hours
8
- export class ChatStore extends AbstractStore {
9
- constructor(config) {
10
- super(config);
11
- this.meta = undefined;
12
- this.inputValue = '';
13
- this.open = false;
14
- this.chats = [];
15
- this.chatEnabled = null;
16
- this.initChatLoading = false;
17
- this.suggestedQuestions = [];
18
- this.welcomeMessage = '';
19
- this.features = { imageSearch: { enabled: false }, similarProducts: { enabled: false } };
20
- this.productQuickview = null;
21
- this.productQuickviewError = null;
22
- /** Raw meta kept for lazy hydration of inactive chat sessions. */
23
- this.storedMetaData = null;
24
- const legacyKey = `ss-${this.config.id}`;
25
- const storageKey = this.config.siteId ? `ss-${this.config.siteId}-${this.config.id}` : legacyKey;
26
- this.storage = new StorageStore({
27
- type: 'local',
28
- key: storageKey,
29
- });
30
- const storedChatStatusResponse = this.storage.get('chatStatusResponse');
31
- if (storedChatStatusResponse) {
32
- try {
33
- const storedChatStatus = JSON.parse(storedChatStatusResponse);
34
- if (storedChatStatus.checkTime && Date.now() - storedChatStatus.checkTime > CHAT_STATUS_EXPIRATION_TIME) {
35
- // chat status is expired, remove from storage to trigger a new check
36
- this.storage.set('chatStatusResponse', null);
37
- }
38
- else {
39
- // Apply the stored response without persisting, so the 12-hour
40
- // checkTime keeps counting from the last real API call rather
41
- // than resetting on every page load.
42
- this.applyChatStatusResponse(storedChatStatus.response);
43
- }
44
- }
45
- catch {
46
- this.storage.set('chatStatusResponse', null);
47
- }
48
- }
49
- // check for entries in storage
50
- let latestChatId = '';
51
- const storedChats = this.storage.get('chats');
52
- storedChats &&
53
- Object.keys(storedChats || {}).forEach((chatId) => {
54
- const chatData = storedChats[chatId];
55
- if (chatData) {
56
- const restoredChat = new ChatSessionStore({
57
- data: {
58
- ...chatData,
59
- id: chatId,
60
- },
61
- stores: {
62
- storage: this.storage,
63
- },
64
- });
65
- // Mark as unhydrated — results are still raw JSON from storage.
66
- // Only the active session will be hydrated below.
67
- restoredChat.hydrated = false;
68
- this.chats.push(restoredChat);
69
- latestChatId = chatId;
70
- }
71
- });
72
- // Prefer the persisted active chat ID so switching chats survives page reloads.
73
- // Fall back to the most recently created chat if the stored ID is missing or stale.
74
- const storedCurrentChatId = this.storage.get('currentChatId');
75
- const activeChatId = storedCurrentChatId && this.chats.some((chat) => chat.id === storedCurrentChatId) ? storedCurrentChatId : latestChatId;
76
- const storedMeta = this.storage.get('meta');
77
- if (storedMeta) {
78
- try {
79
- const metaData = JSON.parse(storedMeta);
80
- this.meta = new MetaStore({
81
- data: {
82
- meta: metaData,
83
- },
84
- });
85
- // Keep raw meta for lazy hydration of inactive sessions
86
- this.storedMetaData = metaData;
87
- // Only hydrate the active chat session — inactive sessions will be
88
- // hydrated lazily when switched to via switchChat()
89
- const activeChat = this.chats.find((chat) => chat.id === activeChatId);
90
- if (activeChat) {
91
- activeChat.hydrateResults(metaData);
92
- activeChat.hydrated = true;
93
- }
94
- }
95
- catch {
96
- this.storage.set('meta', null);
97
- }
98
- }
99
- this.currentChatId = activeChatId;
100
- makeObservable(this, {
101
- meta: observable,
102
- inputValue: observable,
103
- open: observable,
104
- chats: observable,
105
- currentChatId: observable,
106
- chatEnabled: observable,
107
- initChatLoading: observable,
108
- blocked: computed,
109
- currentChat: computed,
110
- chatsIds: computed,
111
- features: observable,
112
- suggestedQuestions: observable,
113
- welcomeMessage: observable,
114
- productQuickview: observable,
115
- productQuickviewError: observable,
116
- });
117
- }
118
- get currentChat() {
119
- return this.chats.find((chat) => chat.id === this.currentChatId);
120
- }
121
- get chatsIds() {
122
- return this.chats.map((chat) => chat.id);
123
- }
124
- get blocked() {
125
- let isBlocked = false;
126
- const blockedAttachments = this.currentChat?.attachments.items.some((item) => item.type === 'image' && item.state === 'loading');
127
- if (this.loading || blockedAttachments) {
128
- isBlocked = true;
129
- }
130
- return isBlocked;
131
- }
132
- applyChatStatusResponse(response) {
133
- const { chatbot, features } = response;
134
- const { status, suggestedQuestions, welcomeMessage } = chatbot;
135
- this.chatEnabled = status.enabled === true;
136
- this.features = features;
137
- this.suggestedQuestions = suggestedQuestions || [];
138
- this.welcomeMessage = welcomeMessage || '';
139
- return this.chatEnabled;
140
- }
141
- handleChatStatusResponse(response) {
142
- const enabled = this.applyChatStatusResponse(response);
143
- // Always persist on a real API response — restarts the 12-hour expiration
144
- // so each new chat session refreshes the cache.
145
- this.storage.set('chatStatusResponse', JSON.stringify({ response, checkTime: Date.now() }));
146
- return enabled;
147
- }
148
- reset() {
149
- this.chats = [];
150
- this.clearProductQuickview();
151
- this.storage.clear();
152
- this.createChat();
153
- }
154
- setProductQuickview(product) {
155
- this.productQuickview = product;
156
- this.productQuickviewError = null;
157
- }
158
- updateProductQuickview(response) {
159
- if (!this.productQuickview) {
160
- return;
161
- }
162
- // merge parent-level mappings from the API response into the Product
163
- this.productQuickview.mappings = {
164
- ...this.productQuickview.mappings,
165
- core: { ...this.productQuickview.mappings.core, ...response.mappings.core },
166
- };
167
- const meta = this.meta?.data || {};
168
- if (this.productQuickview.variants) {
169
- // update existing Variants with new data from the Products API
170
- this.productQuickview.variants.optionConfig = response.variants.optionConfig;
171
- this.productQuickview.variants.update(response.variants.data);
172
- }
173
- else {
174
- // create Variants for the first time using the Products API data
175
- this.productQuickview.variants = new Variants({
176
- data: {
177
- mask: this.productQuickview.mask,
178
- variants: response.variants.data,
179
- optionConfig: response.variants.optionConfig,
180
- meta,
181
- },
182
- });
183
- }
184
- }
185
- setProductQuickviewError(message) {
186
- this.productQuickviewError = message;
187
- }
188
- clearProductQuickview() {
189
- this.productQuickview = null;
190
- this.productQuickviewError = null;
191
- }
192
- clearHistory() {
193
- // drop previous chats while keeping the conversation the user is currently in;
194
- // if there is no current chat, fall back to a full reset
195
- const currentChat = this.currentChat;
196
- if (!currentChat) {
197
- this.reset();
198
- return;
199
- }
200
- const storedChats = this.storage.get('chats') || {};
201
- const filtered = {};
202
- if (storedChats[currentChat.id]) {
203
- filtered[currentChat.id] = storedChats[currentChat.id];
204
- }
205
- this.storage.set('chats', filtered);
206
- this.chats = [currentChat];
207
- }
208
- createChat(data) {
209
- // abandon any in-flight request state from the previous chat — its
210
- // response will be discarded by the controller, so the UI shouldn't
211
- // keep showing its loading spinner or stale error
212
- this.loading = false;
213
- this.error = undefined;
214
- // Prune old sessions before creating a new one to keep storage bounded
215
- ChatSessionStore.pruneStoredSessions(this.storage);
216
- const newChat = new ChatSessionStore({
217
- data: {
218
- sessionId: data?.sessionId,
219
- },
220
- stores: {
221
- storage: this.storage,
222
- },
223
- });
224
- this.currentChatId = newChat.id;
225
- this.storage.set('currentChatId', newChat.id);
226
- this.chats.push(newChat);
227
- return newChat;
228
- }
229
- switchChat(id) {
230
- const chatExists = this.chats.find((chat) => chat.id === id);
231
- if (chatExists) {
232
- // Lazily hydrate results when switching to a session for the first time
233
- if (!chatExists.hydrated && this.storedMetaData) {
234
- chatExists.hydrateResults(this.storedMetaData);
235
- chatExists.hydrated = true;
236
- }
237
- this.currentChatId = id;
238
- this.storage.set('currentChatId', id);
239
- }
240
- }
241
- sendProductQuery(result, options) {
242
- const display = result?.display || result;
243
- this.currentChat?.attachments.add({
244
- type: 'product',
245
- requestType: options.requestType,
246
- productId: result.id,
247
- name: display.mappings?.core?.name,
248
- thumbnailUrl: display.mappings?.core?.thumbnailImageUrl || display.mappings?.core?.imageUrl,
249
- });
250
- // for the productQuery flow we want the secondary chat to immediately
251
- // show the product details panel — push a productQuery message so it
252
- // becomes the active side-chat message until a productAnswer arrives
253
- if (options.requestType === 'productQuery' && this.currentChat) {
254
- this.currentChat.pushProductQueryMessage(result);
255
- }
256
- }
257
- compareProduct(result) {
258
- this.currentChat?.comparisons.add({
259
- result,
260
- });
261
- }
262
- addFacet(facet) {
263
- this.currentChat?.attachments.add({
264
- type: 'facet',
265
- key: facet.key,
266
- facetLabel: facet.facetLabel,
267
- value: facet.value,
268
- label: facet.label,
269
- count: facet.count,
270
- });
271
- // If re-selecting a previously removed server-side filter, remove it from removedFacets
272
- if (this.currentChat) {
273
- const removedIndex = this.currentChat.removedFacets.findIndex((rf) => rf.key === facet.key && rf.value === facet.value);
274
- if (removedIndex !== -1) {
275
- this.currentChat.removedFacets.splice(removedIndex, 1);
276
- }
277
- }
278
- }
279
- removeFacet(key, value) {
280
- const facetAttachment = this.currentChat?.attachments.items.find((item) => item.type === 'facet' &&
281
- item.key === key &&
282
- item.value === value &&
283
- (item.state === 'active' || item.state === 'attached'));
284
- if (facetAttachment) {
285
- const index = this.currentChat?.attachments.items.indexOf(facetAttachment);
286
- if (index !== undefined && index !== -1) {
287
- this.currentChat?.attachments.items.splice(index, 1);
288
- }
289
- }
290
- else if (this.currentChat) {
291
- // No attachment exists — this is a server-side filtered facet.
292
- // Track it as explicitly removed so the next request excludes it.
293
- const alreadyRemoved = this.currentChat.removedFacets.some((rf) => rf.key === key && rf.value === value);
294
- if (!alreadyRemoved) {
295
- this.currentChat.removedFacets.push({ key, value });
296
- }
297
- }
298
- }
299
- clearPendingFacets() {
300
- const facetAttachments = this.currentChat?.attachments.items.filter((item) => item.type === 'facet' && (item.state === 'active' || item.state === 'attached')) || [];
301
- facetAttachments.forEach((item) => {
302
- const index = this.currentChat?.attachments.items.indexOf(item);
303
- if (index !== undefined && index !== -1) {
304
- this.currentChat?.attachments.items.splice(index, 1);
305
- }
306
- });
307
- if (this.currentChat) {
308
- this.currentChat.removedFacets = [];
309
- }
310
- }
311
- isFacetSelected(key, value) {
312
- // A facet that has been explicitly removed by the user is not selected,
313
- // even if the server still reports it as filtered.
314
- if (this.isFacetRemoved(key, value)) {
315
- return false;
316
- }
317
- return (this.currentChat?.attachments.items.some((item) => item.type === 'facet' &&
318
- item.key === key &&
319
- item.value === value &&
320
- (item.state === 'active' || item.state === 'attached')) || false);
321
- }
322
- isFacetRemoved(key, value) {
323
- return this.currentChat?.removedFacets.some((rf) => rf.key === key && rf.value === value) || false;
324
- }
325
- request(request) {
326
- this.currentChat?.request(request);
327
- // remove any facet attachments after request
328
- const facetAttachments = this.currentChat?.attachments.attached.filter((item) => item.type === 'facet') || [];
329
- facetAttachments.forEach((item) => {
330
- this.currentChat?.attachments.remove(item.id);
331
- });
332
- // remove any productSimilar attachments after request
333
- const productSimilarAttachments = this.currentChat?.attachments.attached.filter((item) => item.type === 'product' && item.requestType === 'productSimilar') || [];
334
- productSimilarAttachments.forEach((item) => {
335
- this.currentChat?.attachments.remove(item.id);
336
- });
337
- }
338
- update(data) {
339
- // TODO: handle error
340
- // if(err?.responseBody?.errorMessage || err?.responseBody?.errorCode) {
341
- // const errorMessage = err?.responseBody?.errorMessage || 'An unknown error has occurred.';
342
- // }
343
- this.currentChat?.update(data);
344
- if (!this.meta) {
345
- this.storage.set('meta', JSON.stringify(data.meta));
346
- }
347
- this.meta = new MetaStore({
348
- data: {
349
- meta: data.meta,
350
- },
351
- });
352
- // Keep raw meta in sync for lazy hydration of inactive sessions
353
- this.storedMetaData = data.meta;
354
- }
355
- }