@redotech/redo-hydrogen 1.4.4 → 1.4.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@redotech/redo-hydrogen",
3
- "version": "1.4.4",
3
+ "version": "1.4.6",
4
4
  "description": "Utilities to enable and disable Redo coverage on Hydrogen stores",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
package/src/utils/cart.ts CHANGED
@@ -7,6 +7,8 @@ import { CartWithActionsDocs } from "@shopify/hydrogen-react/dist/types/cart-typ
7
7
  import { CartLine, ComponentizableCartLine } from "@shopify/hydrogen-react/storefront-api-types";
8
8
 
9
9
  const DEFAULT_REDO_ENABLED_CART_ATTRIBUTE = 'redo_opted_in_from_cart';
10
+ const CONCIERGE_ATTRIBUTION_CART_ATTRIBUTE_KEY = "redo.conciergeAssisted";
11
+ const CONCIERGE_CONVERSATION_IDS_STORAGE_KEY = "redoConciergeConversationIds";
10
12
 
11
13
  const isCartWithActionsDocs = (cart: CartReturn | CartWithActionsDocs| OptimisticCart): cart is CartWithActionsDocs => {
12
14
  return (Array.isArray(cart.lines) && 'linesAdd' in cart && typeof cart.linesAdd === 'function');
@@ -197,6 +199,32 @@ const addProductToCart = async ({
197
199
  }
198
200
  };
199
201
 
202
+ interface ConversationIdWithExpiry {
203
+ conversationId: string;
204
+ expiresAt: number;
205
+ }
206
+
207
+ function getConciergeConversationIdsFromStorage(): string[] | null {
208
+ try {
209
+ const stored = localStorage.getItem(CONCIERGE_CONVERSATION_IDS_STORAGE_KEY);
210
+ if (!stored) {
211
+ return null;
212
+ }
213
+
214
+ const conversationIdsWithExpiry: ConversationIdWithExpiry[] =
215
+ JSON.parse(stored);
216
+ const now = Date.now();
217
+
218
+ const validConversationIds = conversationIdsWithExpiry
219
+ .filter((item) => item.expiresAt > now)
220
+ .map((item) => item.conversationId);
221
+
222
+ return validConversationIds.length > 0 ? validConversationIds : null;
223
+ } catch (error) {
224
+ return null;
225
+ }
226
+ }
227
+
200
228
  const setCartRedoEnabledAttribute = async ({
201
229
  cart,
202
230
  fetcher,
@@ -222,7 +250,16 @@ const setCartRedoEnabledAttribute = async ({
222
250
  );
223
251
 
224
252
  existingAttributesMap.set(redoCartAttribute.key, redoCartAttribute.value);
225
-
253
+ const conciergeConversationIds = getConciergeConversationIdsFromStorage();
254
+ if (conciergeConversationIds && conciergeConversationIds.length > 0) {
255
+ existingAttributesMap.set(
256
+ CONCIERGE_ATTRIBUTION_CART_ATTRIBUTE_KEY,
257
+ JSON.stringify({
258
+ conciergeConversationIds: conciergeConversationIds,
259
+ })
260
+ );
261
+ }
262
+
226
263
  const updatedAttributes = Array.from(existingAttributesMap.entries()).map(([key, value]) => ({
227
264
  key,
228
265
  value: value ?? ""