@nuskin/react-search-components 1.0.0-comx-3067.6 → 1.0.0-comx-3067.8

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/index.d.mts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as React from 'react';
2
2
  import React__default, { ComponentPropsWithoutRef, ReactNode } from 'react';
3
- import { CatalogSuggestionItem, SearchService } from '@nuskin/react-search-sdk';
3
+ import { CatalogSuggestionItem, SearchService, SearchGtmContext } from '@nuskin/react-search-sdk';
4
4
  export { CatalogSuggestionItem, DidYouMeanItem, SearchService, createSearchService } from '@nuskin/react-search-sdk';
5
5
  import { Environment } from '@ns/ns-contentstack-lib';
6
6
 
@@ -176,6 +176,14 @@ interface SearchModalAIChatHeaderContent {
176
176
  };
177
177
  [key: string]: unknown;
178
178
  }
179
+ /**
180
+ * Consumer-supplied context for the COMX-3277 AI chat GTM events.
181
+ *
182
+ * `chatId` is derived internally from the AI chat conversation id, so it is
183
+ * intentionally omitted here — callers only provide what the host app knows
184
+ * (page type, user status, user id, etc.).
185
+ */
186
+ type SearchModalGtmContext = Omit<SearchGtmContext, 'chatId'>;
179
187
  interface SearchModalProps {
180
188
  isOpen: boolean;
181
189
  onClose: () => void;
@@ -204,12 +212,19 @@ interface SearchModalProps {
204
212
  aiChatHeaderContent?: SearchModalAIChatHeaderContent | null;
205
213
  /** API configuration for AI Commerce Chat. Required when enableAIChat is true. */
206
214
  aiChatConfig?: SearchModalAIChatConfig;
215
+ /**
216
+ * Optional host-supplied context for the AI chat GTM (dataLayer) events
217
+ * defined in COMX-3277 (page type, country code, language, user status, user id).
218
+ * When omitted, blank-string placeholders are pushed to the dataLayer so the
219
+ * event schema stays stable.
220
+ */
221
+ gtmContext?: SearchModalGtmContext;
207
222
  history?: {
208
223
  push: (path: string) => void;
209
224
  } | null;
210
225
  }
211
226
 
212
- declare function SearchModal({ isOpen, onClose, service, history, copy, logo, cancelLabel, fallbackImageUrl, enableAIChat, aiChatConfig, showPricing, aiChatHeaderContent, }: Readonly<SearchModalProps>): React__default.ReactElement | null;
227
+ declare function SearchModal({ isOpen, onClose, service, history, copy, logo, cancelLabel, fallbackImageUrl, enableAIChat, aiChatConfig, gtmContext, showPricing, aiChatHeaderContent, }: Readonly<SearchModalProps>): React__default.ReactElement | null;
213
228
 
214
229
  interface AIChatHeaderContentConfig {
215
230
  /** Contentstack environment consumed by getStack, e.g. prod/test/stage */
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as React from 'react';
2
2
  import React__default, { ComponentPropsWithoutRef, ReactNode } from 'react';
3
- import { CatalogSuggestionItem, SearchService } from '@nuskin/react-search-sdk';
3
+ import { CatalogSuggestionItem, SearchService, SearchGtmContext } from '@nuskin/react-search-sdk';
4
4
  export { CatalogSuggestionItem, DidYouMeanItem, SearchService, createSearchService } from '@nuskin/react-search-sdk';
5
5
  import { Environment } from '@ns/ns-contentstack-lib';
6
6
 
@@ -176,6 +176,14 @@ interface SearchModalAIChatHeaderContent {
176
176
  };
177
177
  [key: string]: unknown;
178
178
  }
179
+ /**
180
+ * Consumer-supplied context for the COMX-3277 AI chat GTM events.
181
+ *
182
+ * `chatId` is derived internally from the AI chat conversation id, so it is
183
+ * intentionally omitted here — callers only provide what the host app knows
184
+ * (page type, user status, user id, etc.).
185
+ */
186
+ type SearchModalGtmContext = Omit<SearchGtmContext, 'chatId'>;
179
187
  interface SearchModalProps {
180
188
  isOpen: boolean;
181
189
  onClose: () => void;
@@ -204,12 +212,19 @@ interface SearchModalProps {
204
212
  aiChatHeaderContent?: SearchModalAIChatHeaderContent | null;
205
213
  /** API configuration for AI Commerce Chat. Required when enableAIChat is true. */
206
214
  aiChatConfig?: SearchModalAIChatConfig;
215
+ /**
216
+ * Optional host-supplied context for the AI chat GTM (dataLayer) events
217
+ * defined in COMX-3277 (page type, country code, language, user status, user id).
218
+ * When omitted, blank-string placeholders are pushed to the dataLayer so the
219
+ * event schema stays stable.
220
+ */
221
+ gtmContext?: SearchModalGtmContext;
207
222
  history?: {
208
223
  push: (path: string) => void;
209
224
  } | null;
210
225
  }
211
226
 
212
- declare function SearchModal({ isOpen, onClose, service, history, copy, logo, cancelLabel, fallbackImageUrl, enableAIChat, aiChatConfig, showPricing, aiChatHeaderContent, }: Readonly<SearchModalProps>): React__default.ReactElement | null;
227
+ declare function SearchModal({ isOpen, onClose, service, history, copy, logo, cancelLabel, fallbackImageUrl, enableAIChat, aiChatConfig, gtmContext, showPricing, aiChatHeaderContent, }: Readonly<SearchModalProps>): React__default.ReactElement | null;
213
228
 
214
229
  interface AIChatHeaderContentConfig {
215
230
  /** Contentstack environment consumed by getStack, e.g. prod/test/stage */
package/dist/index.js CHANGED
@@ -12006,6 +12006,7 @@ function SearchMessage({ sender, text: text4, ...props }) {
12006
12006
 
12007
12007
  // src/components/Icon/iconConstants.ts
12008
12008
  var ICON_TYPES = {
12009
+ ARROW_DOWN: "arrowDown",
12009
12010
  ARROW_LEFT: "arrowLeft",
12010
12011
  ARROW_RIGHT: "arrowRight",
12011
12012
  ARROW_UP: "arrowUp",
@@ -12051,6 +12052,7 @@ var ICON_COLOR_BY_TYPE = {
12051
12052
  [ICON_TYPES.STAR_GREY]: "#c9c9c9"
12052
12053
  };
12053
12054
  var TEMPORARY_GLYPH_BY_ICON_TYPE = {
12055
+ [ICON_TYPES.ARROW_DOWN]: "\u2193",
12054
12056
  [ICON_TYPES.ARROW_LEFT]: "\u2190",
12055
12057
  [ICON_TYPES.ARROW_RIGHT]: "\u2192",
12056
12058
  [ICON_TYPES.ARROW_UP]: "\u2191",
@@ -12675,7 +12677,7 @@ var SearchPanelRoot = styled6__default.default.section`
12675
12677
  ${({ $chatMode }) => $chatMode && `
12676
12678
  display: flex;
12677
12679
  flex-direction: column;
12678
- max-height: 805px;
12680
+ max-block-size: 92dvh;
12679
12681
  overflow: hidden;
12680
12682
  `}
12681
12683
  `;
@@ -13317,6 +13319,34 @@ var AIStatusCursor = styled6__default.default.span`
13317
13319
  }
13318
13320
  }
13319
13321
  `;
13322
+ var ChatScrollIndicator = styled6__default.default.button`
13323
+ align-self: center;
13324
+ appearance: none;
13325
+ background: ${colors6.hover};
13326
+ border: 0;
13327
+ border-radius: 50%;
13328
+ color: ${colors6.text};
13329
+ cursor: pointer;
13330
+ display: grid;
13331
+ place-items: center;
13332
+ block-size: 2rem;
13333
+ inline-size: 2rem;
13334
+ margin-block: 0.25rem 0.75rem;
13335
+ margin-inline: auto;
13336
+ padding: 0;
13337
+ position: sticky;
13338
+ inset-block-end: 0.75rem;
13339
+ z-index: 1;
13340
+
13341
+ &:hover {
13342
+ background: ${colors6.borderLight};
13343
+ }
13344
+
13345
+ &:focus-visible {
13346
+ outline: 2px solid ${colors6.text};
13347
+ outline-offset: 2px;
13348
+ }
13349
+ `;
13320
13350
  var SuggestionSectionHeading = styled6__default.default.div`
13321
13351
  padding: 12px 24px 8px;
13322
13352
  margin-bottom: 4px;
@@ -13463,7 +13493,7 @@ styled6__default.default.span`
13463
13493
  font-size: 0.8125rem;
13464
13494
  font-weight: 600;
13465
13495
  `;
13466
- var ScrollIndicator = styled6__default.default.button`
13496
+ styled6__default.default.button`
13467
13497
  align-items: center;
13468
13498
  align-self: center;
13469
13499
  appearance: none;
@@ -13593,7 +13623,7 @@ function stripPricingText(text4) {
13593
13623
  );
13594
13624
  }
13595
13625
  function toCarouselItem(product, index2, showPricing, history) {
13596
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
13626
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
13597
13627
  const slotKey = `ai-${(_b = (_a = product.id) != null ? _a : product.sku) != null ? _b : index2}-${index2}`;
13598
13628
  const productName = (_f = (_d = (_c = product.name) == null ? void 0 : _c.trim()) != null ? _d : product.sku) != null ? _f : `Product ${(_e = product.id) != null ? _e : index2}`;
13599
13629
  if (!productName) {
@@ -13608,12 +13638,13 @@ function toCarouselItem(product, index2, showPricing, history) {
13608
13638
  };
13609
13639
  }
13610
13640
  const href = (_i = product.url) != null ? _i : "#";
13611
- const imageUrl = (_j = product.imageUrl) != null ? _j : product.image_url;
13612
- const priceText = (_k = product.price) != null ? _k : product.pv != null ? `${product.pv} PV` : void 0;
13641
+ const routeHref = (_j = product.routeUrl) != null ? _j : product.sku ? `/product/${product.sku}` : href;
13642
+ const imageUrl = (_k = product.imageUrl) != null ? _k : product.image_url;
13643
+ const priceText = (_l = product.price) != null ? _l : product.pv != null ? `${product.pv} PV` : void 0;
13613
13644
  return {
13614
13645
  status: "ready",
13615
13646
  slotKey,
13616
- productId: String((_m = (_l = product.id) != null ? _l : product.sku) != null ? _m : index2),
13647
+ productId: String((_n = (_m = product.id) != null ? _m : product.sku) != null ? _n : index2),
13617
13648
  cardProps: {
13618
13649
  mode: "cta",
13619
13650
  title: productName,
@@ -13623,11 +13654,10 @@ function toCarouselItem(product, index2, showPricing, history) {
13623
13654
  price: showPricing ? priceText : void 0,
13624
13655
  addToCartLabel: "View Product",
13625
13656
  addToCartAriaLabel: `View ${productName}`,
13626
- onProductClick: ({ event } = {}) => {
13657
+ onProductClick: () => {
13627
13658
  if (href === "#") return;
13628
- event == null ? void 0 : event.preventDefault();
13629
13659
  if (history && typeof history.push === "function") {
13630
- history.push(href);
13660
+ history.push(routeHref);
13631
13661
  }
13632
13662
  }
13633
13663
  }
@@ -13640,14 +13670,6 @@ function SearchChatResult({
13640
13670
  productsAriaLabel = "AI recommended products"
13641
13671
  }) {
13642
13672
  var _a;
13643
- const carouselWrapperRef = React.useRef(null);
13644
- function scrollDown() {
13645
- var _a2;
13646
- (_a2 = carouselWrapperRef.current) == null ? void 0 : _a2.scrollIntoView({
13647
- behavior: "smooth",
13648
- block: "nearest"
13649
- });
13650
- }
13651
13673
  const products = (_a = result.products) != null ? _a : [];
13652
13674
  const carouselItems = products.map((p, i) => toCarouselItem(p, i, showPricing, history));
13653
13675
  const summaryText = showPricing ? result.aiResponseText : stripPricingText(result.aiResponseText);
@@ -13668,42 +13690,11 @@ function SearchChatResult({
13668
13690
  "data-testid": "chat-ai-response"
13669
13691
  }
13670
13692
  ),
13671
- carouselItems.length > 0 && /* @__PURE__ */ jsxRuntime.jsxs(ProductCarouselWrapper, { ref: carouselWrapperRef, "data-testid": "chat-product-carousel", children: [
13672
- /* @__PURE__ */ jsxRuntime.jsx(reactProductComponents.ProductCarousel, { items: carouselItems, ariaLabel: productsAriaLabel }),
13673
- /* @__PURE__ */ jsxRuntime.jsx(
13674
- ScrollIndicator,
13675
- {
13676
- type: "button",
13677
- "aria-label": "Scroll to products",
13678
- onClick: scrollDown,
13679
- "data-testid": "chat-scroll-indicator",
13680
- children: /* @__PURE__ */ jsxRuntime.jsxs(
13681
- "svg",
13682
- {
13683
- xmlns: "http://www.w3.org/2000/svg",
13684
- viewBox: "0 0 24 24",
13685
- fill: "none",
13686
- stroke: "currentColor",
13687
- strokeWidth: 2,
13688
- strokeLinecap: "round",
13689
- strokeLinejoin: "round",
13690
- width: 16,
13691
- height: 16,
13692
- "aria-hidden": true,
13693
- children: [
13694
- /* @__PURE__ */ jsxRuntime.jsx("line", { x1: 12, y1: 5, x2: 12, y2: 19 }),
13695
- /* @__PURE__ */ jsxRuntime.jsx("polyline", { points: "19 12 12 19 5 12" })
13696
- ]
13697
- }
13698
- )
13699
- }
13700
- )
13701
- ] })
13693
+ carouselItems.length > 0 && /* @__PURE__ */ jsxRuntime.jsx(ProductCarouselWrapper, { "data-testid": "chat-product-carousel", children: /* @__PURE__ */ jsxRuntime.jsx(reactProductComponents.ProductCarousel, { items: carouselItems, ariaLabel: productsAriaLabel }) })
13702
13694
  ] });
13703
13695
  }
13704
13696
 
13705
13697
  // src/components/SearchModal/SearchModalConstants.ts
13706
- var DEFAULT_AI_AUTO_TRIGGER_DELAY_MS = 4e3;
13707
13698
  var DEFAULT_AI_STATUS_LABELS = {
13708
13699
  analyzing: "Analyzing your question...",
13709
13700
  pulling: "Pulling up your analytics...",
@@ -13750,9 +13741,6 @@ function getUniqueRecentSearches(entries) {
13750
13741
  ) === idx
13751
13742
  );
13752
13743
  }
13753
- function getAITriggerDelayMs(delayMs) {
13754
- return typeof delayMs === "number" && delayMs >= 0 ? delayMs : DEFAULT_AI_AUTO_TRIGGER_DELAY_MS;
13755
- }
13756
13744
  function getProductId(rawId) {
13757
13745
  return typeof rawId === "string" || typeof rawId === "number" ? String(rawId) : JSON.stringify(rawId);
13758
13746
  }
@@ -13777,6 +13765,11 @@ function SearchModalChatContent({
13777
13765
  analyzingLabel,
13778
13766
  chatEndRef
13779
13767
  }) {
13768
+ const hasChatResults = aiChatHistory.length > 0;
13769
+ function scrollToBottom() {
13770
+ var _a;
13771
+ (_a = chatEndRef.current) == null ? void 0 : _a.scrollIntoView({ behavior: "smooth", block: "end" });
13772
+ }
13780
13773
  return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
13781
13774
  aiChatHistory.map((result, idx) => /* @__PURE__ */ jsxRuntime.jsx(
13782
13775
  "div",
@@ -13802,6 +13795,23 @@ function SearchModalChatContent({
13802
13795
  typedStatus || analyzingLabel,
13803
13796
  /* @__PURE__ */ jsxRuntime.jsx(AIStatusCursor, { "aria-hidden": "true" })
13804
13797
  ] }),
13798
+ hasChatResults && /* @__PURE__ */ jsxRuntime.jsx(
13799
+ ChatScrollIndicator,
13800
+ {
13801
+ type: "button",
13802
+ "aria-label": "Scroll to bottom",
13803
+ onClick: scrollToBottom,
13804
+ "data-testid": "chat-scroll-indicator",
13805
+ children: /* @__PURE__ */ jsxRuntime.jsx(
13806
+ Icon,
13807
+ {
13808
+ cursor: ICON_CURSORS.POINTER,
13809
+ size: ICON_SIZES.SMALL,
13810
+ type: ICON_TYPES.ARROW_DOWN
13811
+ }
13812
+ )
13813
+ }
13814
+ ),
13805
13815
  /* @__PURE__ */ jsxRuntime.jsx("div", { ref: chatEndRef })
13806
13816
  ] });
13807
13817
  }
@@ -13950,10 +13960,11 @@ function SearchModal({
13950
13960
  fallbackImageUrl = "",
13951
13961
  enableAIChat = false,
13952
13962
  aiChatConfig,
13963
+ gtmContext,
13953
13964
  showPricing = true,
13954
13965
  aiChatHeaderContent
13955
13966
  }) {
13956
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
13967
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
13957
13968
  const listboxId = React.useId();
13958
13969
  const footerRef = React.useRef(null);
13959
13970
  const suggestionRowRefs = React.useRef({});
@@ -13982,7 +13993,6 @@ function SearchModal({
13982
13993
  const [aiChatHistory, setAiChatHistory] = React.useState([]);
13983
13994
  const [pendingQuery, setPendingQuery] = React.useState("");
13984
13995
  const [aiLoading, setAiLoading] = React.useState(false);
13985
- const [isWaitingForAIFallback, setIsWaitingForAIFallback] = React.useState(false);
13986
13996
  const isInChatMode = aiChatHistory.length > 0 || pendingQuery !== "" || aiLoading;
13987
13997
  const [aiStatus, setAiStatus] = React.useState("");
13988
13998
  const [typedStatus, setTypedStatus] = React.useState("");
@@ -13994,11 +14004,34 @@ function SearchModal({
13994
14004
  aiChatConfigRef.current = aiChatConfig;
13995
14005
  const aiChatHeaderContentRef = React.useRef(aiChatHeaderContent);
13996
14006
  aiChatHeaderContentRef.current = aiChatHeaderContent;
14007
+ const gtmContextRef = React.useRef(gtmContext);
14008
+ gtmContextRef.current = gtmContext;
13997
14009
  const historyEntriesRef = React.useRef(historyEntries);
13998
14010
  historyEntriesRef.current = historyEntries;
13999
14011
  const chatEndRef = React.useRef(null);
14000
- const aiFallbackTimeoutRef = React.useRef(null);
14001
14012
  const aiRequestCounterRef = React.useRef(0);
14013
+ const [suggestionsRequestFailed, setSuggestionsRequestFailed] = React.useState(false);
14014
+ const suggestionAwareService = React.useMemo(
14015
+ () => ({
14016
+ ...service,
14017
+ async fetchSuggestions(searchTerm) {
14018
+ setSuggestionsRequestFailed(false);
14019
+ try {
14020
+ const response = await service.fetchSuggestions(searchTerm);
14021
+ setSuggestionsRequestFailed(false);
14022
+ return response;
14023
+ } catch (error) {
14024
+ setSuggestionsRequestFailed(true);
14025
+ throw error;
14026
+ }
14027
+ }
14028
+ }),
14029
+ [service]
14030
+ );
14031
+ const searchSuggestions = reactSearchSdk.useSearchSuggestions({
14032
+ service: suggestionAwareService,
14033
+ enabled: isOpen && !isInChatMode
14034
+ });
14002
14035
  const {
14003
14036
  didYouMean,
14004
14037
  emptyFetched,
@@ -14008,12 +14041,16 @@ function SearchModal({
14008
14041
  query,
14009
14042
  suggestions,
14010
14043
  updateQuery
14011
- } = reactSearchSdk.useSearchSuggestions({ service, enabled: isOpen && !isInChatMode });
14044
+ } = searchSuggestions;
14045
+ const suggestionFetchFailed = ((_a = searchSuggestions.suggestionFetchFailed) != null ? _a : false) || suggestionsRequestFailed;
14012
14046
  const [searchInputValue, setSearchInputValue] = React.useState(query);
14013
14047
  const searchInputValueRef = React.useRef(query);
14014
14048
  const voiceInputBaseValueRef = React.useRef(query);
14015
14049
  const setSearchInput = React.useCallback(
14016
- (value, updateVoiceBase = true) => {
14050
+ (value, updateVoiceBase = true, resetSuggestionFailure = true) => {
14051
+ if (resetSuggestionFailure) {
14052
+ setSuggestionsRequestFailed(false);
14053
+ }
14017
14054
  searchInputValueRef.current = value;
14018
14055
  if (updateVoiceBase) {
14019
14056
  voiceInputBaseValueRef.current = value;
@@ -14055,20 +14092,14 @@ function SearchModal({
14055
14092
  const hasDidYouMean = didYouMean.length > 0;
14056
14093
  const showDidYouMean = hasDidYouMean && !enableAIChat;
14057
14094
  const visibleSuggestions = enableAIChat ? suggestions : reactSearchSdk.getVisibleSuggestions({ didYouMean, suggestions });
14058
- const visibleSuggestionsRef = React.useRef(visibleSuggestions);
14059
- visibleSuggestionsRef.current = visibleSuggestions;
14060
- const hasDidYouMeanRef = React.useRef(hasDidYouMean);
14061
- hasDidYouMeanRef.current = hasDidYouMean;
14062
- const loadingRef = React.useRef(loading);
14063
- loadingRef.current = loading;
14064
14095
  const safeUseVoiceSearch = reactSearchSdk.useVoiceSearch;
14065
14096
  const voiceSearchRaw = safeUseVoiceSearch({
14066
14097
  onInterimResult: previewVoiceResult,
14067
14098
  onResult: appendVoiceResult
14068
14099
  });
14069
14100
  const voiceSearch = isVoiceSearchState(voiceSearchRaw) ? voiceSearchRaw : null;
14070
- const voiceListening = (_a = voiceSearch == null ? void 0 : voiceSearch.listening) != null ? _a : false;
14071
- const voiceSupported = (_b = voiceSearch == null ? void 0 : voiceSearch.supported) != null ? _b : false;
14101
+ const voiceListening = (_b = voiceSearch == null ? void 0 : voiceSearch.listening) != null ? _b : false;
14102
+ const voiceSupported = (_c = voiceSearch == null ? void 0 : voiceSearch.supported) != null ? _c : false;
14072
14103
  const startVoice = voiceSearch == null ? void 0 : voiceSearch.start;
14073
14104
  const stopVoice = voiceSearch == null ? void 0 : voiceSearch.stop;
14074
14105
  const toggleVoice = React.useCallback(() => {
@@ -14079,10 +14110,9 @@ function SearchModal({
14079
14110
  voiceInputBaseValueRef.current = searchInputValueRef.current.trimEnd();
14080
14111
  startVoice == null ? void 0 : startVoice();
14081
14112
  }, [startVoice, stopVoice, voiceListening]);
14082
- const showPricingFromHeader = ((_d = (_c = aiChatHeaderContentRef.current) == null ? void 0 : _c.configurations) == null ? void 0 : _d.enablepricingpvsv) !== "false";
14113
+ const showPricingFromHeader = ((_e = (_d = aiChatHeaderContentRef.current) == null ? void 0 : _d.configurations) == null ? void 0 : _e.enablepricingpvsv) !== "false";
14083
14114
  const resolvedShowPricing = aiChatHeaderContent ? showPricingFromHeader : showPricing;
14084
14115
  const aiStatusLabels = getAIStatusLabels(copy);
14085
- const aiAutoTriggerDelayMs = getAITriggerDelayMs(aiChatConfig == null ? void 0 : aiChatConfig.aiAutoTriggerDelayMs);
14086
14116
  function voiceSearchProps() {
14087
14117
  var _a2;
14088
14118
  if (!voiceSupported || !startVoice) return {};
@@ -14094,12 +14124,22 @@ function SearchModal({
14094
14124
  onVoiceInput: toggleVoice
14095
14125
  };
14096
14126
  }
14127
+ const buildGtmContext = React.useCallback(() => {
14128
+ var _a2;
14129
+ return (_a2 = gtmContextRef.current) != null ? _a2 : {};
14130
+ }, []);
14131
+ const chatEntryFiredRef = React.useRef(false);
14097
14132
  React.useEffect(() => {
14098
- if (isOpen) return;
14099
- if (aiFallbackTimeoutRef.current) {
14100
- clearTimeout(aiFallbackTimeoutRef.current);
14101
- aiFallbackTimeoutRef.current = null;
14133
+ if (!isOpen) {
14134
+ chatEntryFiredRef.current = false;
14135
+ return;
14102
14136
  }
14137
+ if (!enableAIChat || chatEntryFiredRef.current) return;
14138
+ chatEntryFiredRef.current = true;
14139
+ reactSearchSdk.trackChatEntryPoint(buildGtmContext());
14140
+ }, [isOpen, enableAIChat, buildGtmContext]);
14141
+ React.useEffect(() => {
14142
+ if (isOpen) return;
14103
14143
  updateQuery("");
14104
14144
  setAiChatHistory([]);
14105
14145
  setPendingQuery("");
@@ -14156,43 +14196,19 @@ function SearchModal({
14156
14196
  document.body.style.overflow = "";
14157
14197
  };
14158
14198
  }, [isOpen, onClose]);
14159
- React.useEffect(() => {
14160
- if (aiFallbackTimeoutRef.current) {
14161
- clearTimeout(aiFallbackTimeoutRef.current);
14162
- aiFallbackTimeoutRef.current = null;
14163
- }
14164
- setIsWaitingForAIFallback(false);
14165
- if (!enableAIChat || !isOpen || isInChatMode || !hasQuery) return void 0;
14166
- const trimmedQuery = query.trim();
14167
- if (!trimmedQuery) return void 0;
14168
- const alreadyAnswered = historyEntriesRef.current.some(
14169
- (e) => e.isAI && e.query.toLowerCase() === trimmedQuery.toLowerCase()
14170
- );
14171
- if (alreadyAnswered) return void 0;
14172
- setIsWaitingForAIFallback(true);
14173
- aiFallbackTimeoutRef.current = setTimeout(() => {
14174
- aiFallbackTimeoutRef.current = null;
14175
- setIsWaitingForAIFallback(false);
14176
- if (!loadingRef.current && visibleSuggestionsRef.current.length > 0 && !hasDidYouMeanRef.current) {
14177
- return;
14178
- }
14179
- sendAIMessage(trimmedQuery);
14180
- }, aiAutoTriggerDelayMs);
14181
- return () => {
14182
- if (aiFallbackTimeoutRef.current) {
14183
- clearTimeout(aiFallbackTimeoutRef.current);
14184
- aiFallbackTimeoutRef.current = null;
14185
- }
14186
- setIsWaitingForAIFallback(false);
14187
- };
14188
- }, [enableAIChat, isOpen, isInChatMode, hasQuery, query]);
14189
14199
  const sendAIMessage = React.useCallback(
14190
14200
  (queryText) => {
14191
14201
  const config = aiChatConfigRef.current;
14192
14202
  if (!config || !queryText.trim()) return;
14193
- setSearchInput("");
14203
+ setSearchInput("", true, false);
14194
14204
  setPendingQuery(queryText.trim());
14195
14205
  setAiStatus(aiStatusLabels.analyzing);
14206
+ const trimmedQuery = queryText.trim();
14207
+ reactSearchSdk.trackSearchType({
14208
+ ...buildGtmContext(),
14209
+ searchType: reactSearchSdk.SEARCH_GTM_SEARCH_TYPES.AI_AGENT,
14210
+ searchResponse: trimmedQuery
14211
+ });
14196
14212
  const requestId = aiRequestCounterRef.current;
14197
14213
  const productBasePath = `/${config.market.toLowerCase()}/${config.language.toLowerCase()}/product`;
14198
14214
  const chatService = reactSearchSdk.createChatService({
@@ -14248,14 +14264,24 @@ function SearchModal({
14248
14264
  const priorHistory = historyEntriesRef.current.filter((e) => e.isAI && e.aiResult != null).slice().reverse().map((e) => e.aiResult);
14249
14265
  setAiChatHistory([...priorHistory, newResult]);
14250
14266
  historyAddEntry({ query: queryText.trim(), isAI: true, aiResult: newResult });
14267
+ reactSearchSdk.trackProductSearch({
14268
+ ...buildGtmContext(),
14269
+ searchType: reactSearchSdk.SEARCH_GTM_SEARCH_TYPES.AI_AGENT,
14270
+ productResults: products.length,
14271
+ productResultsData: products
14272
+ });
14251
14273
  }).catch(() => {
14252
14274
  setPendingQuery("");
14253
14275
  setAiStatus("");
14276
+ reactSearchSdk.trackErrorPopup({
14277
+ ...buildGtmContext(),
14278
+ searchType: reactSearchSdk.SEARCH_GTM_SEARCH_TYPES.AI_AGENT
14279
+ });
14254
14280
  }).finally(() => {
14255
14281
  if (requestId === aiRequestCounterRef.current) setAiLoading(false);
14256
14282
  });
14257
14283
  },
14258
- [aiStatusLabels, historyAddEntry, setSearchInput]
14284
+ [aiStatusLabels, historyAddEntry, setSearchInput, buildGtmContext]
14259
14285
  );
14260
14286
  const selectOption = React.useCallback(
14261
14287
  (option) => {
@@ -14263,9 +14289,16 @@ function SearchModal({
14263
14289
  if (reactSearchSdk.isSuggestionDisabled(option)) return;
14264
14290
  const term = String((_b2 = (_a2 = option["value"]) != null ? _a2 : option["label"]) != null ? _b2 : "");
14265
14291
  historyAddEntry({ query: term, isAI: false });
14292
+ if (term.trim()) {
14293
+ reactSearchSdk.trackProductSearch({
14294
+ ...buildGtmContext(),
14295
+ searchType: reactSearchSdk.SEARCH_GTM_SEARCH_TYPES.SEMANTIC,
14296
+ productResultsData: [option]
14297
+ });
14298
+ }
14266
14299
  const optionHref = service.getSuggestionHref(option);
14267
- if (optionHref && history && typeof history.push === "function") {
14268
- history.push(optionHref);
14300
+ if (optionHref) {
14301
+ pushSearchPath(history, optionHref);
14269
14302
  onClose();
14270
14303
  return;
14271
14304
  }
@@ -14276,7 +14309,7 @@ function SearchModal({
14276
14309
  if (!enableAIChat) setLegacyRecentSearches((_d2 = (_c2 = service.getRecentSearches) == null ? void 0 : _c2.call(service)) != null ? _d2 : []);
14277
14310
  onClose();
14278
14311
  },
14279
- [enableAIChat, service, onClose, historyAddEntry, history]
14312
+ [buildGtmContext, enableAIChat, service, onClose, historyAddEntry, history]
14280
14313
  );
14281
14314
  const runSuggestionAction = React.useCallback(
14282
14315
  (option) => {
@@ -14296,6 +14329,13 @@ function SearchModal({
14296
14329
  setHighlightedQuery(entry.query);
14297
14330
  } else {
14298
14331
  historyAddEntry({ query: entry.query, isAI: false });
14332
+ if (entry.query.trim()) {
14333
+ reactSearchSdk.trackProductSearch({
14334
+ ...buildGtmContext(),
14335
+ searchType: reactSearchSdk.SEARCH_GTM_SEARCH_TYPES.SEMANTIC,
14336
+ productResultsData: { query: entry.query.trim() }
14337
+ });
14338
+ }
14299
14339
  const url = service.submitProductSearch(entry.query);
14300
14340
  if (url && history && typeof history.push === "function") {
14301
14341
  history.push(url);
@@ -14304,7 +14344,7 @@ function SearchModal({
14304
14344
  onClose();
14305
14345
  }
14306
14346
  },
14307
- [enableAIChat, service, onClose, historyAddEntry, history]
14347
+ [buildGtmContext, enableAIChat, service, onClose, historyAddEntry, history]
14308
14348
  );
14309
14349
  const handleClearRecentSearches = React.useCallback(() => {
14310
14350
  clearRecentSearches();
@@ -14317,22 +14357,44 @@ function SearchModal({
14317
14357
  const handleSearchSubmit = React.useCallback(
14318
14358
  (value) => {
14319
14359
  var _a2, _b2;
14360
+ if (enableAIChat && suggestionFetchFailed) {
14361
+ sendAIMessage(value);
14362
+ return;
14363
+ }
14320
14364
  historyAddEntry({ query: value, isAI: false });
14365
+ const trimmed = value.trim();
14366
+ if (trimmed) {
14367
+ reactSearchSdk.trackProductSearch({
14368
+ ...buildGtmContext(),
14369
+ searchType: reactSearchSdk.SEARCH_GTM_SEARCH_TYPES.SEMANTIC,
14370
+ productResultsData: visibleSuggestions.length > 0 ? visibleSuggestions : { query: trimmed }
14371
+ });
14372
+ }
14321
14373
  const url = service.submitProductSearch(value);
14322
14374
  pushSearchPath(history, url);
14323
14375
  if (!enableAIChat) setLegacyRecentSearches((_b2 = (_a2 = service.getRecentSearches) == null ? void 0 : _a2.call(service)) != null ? _b2 : []);
14324
14376
  onClose();
14325
14377
  },
14326
- [enableAIChat, history, historyAddEntry, onClose, service]
14378
+ [
14379
+ buildGtmContext,
14380
+ enableAIChat,
14381
+ history,
14382
+ historyAddEntry,
14383
+ onClose,
14384
+ sendAIMessage,
14385
+ service,
14386
+ suggestionFetchFailed,
14387
+ visibleSuggestions
14388
+ ]
14327
14389
  );
14328
14390
  if (!isOpen) return null;
14329
- const placeholder = (_e = copy == null ? void 0 : copy.extended_search_textbox_desktop) != null ? _e : "Search";
14330
- const ariaLabel = (_f = copy == null ? void 0 : copy.extended_search_box_aria_label) != null ? _f : placeholder;
14331
- const didYouMeanText = (_g = copy == null ? void 0 : copy.did_you_mean_text) != null ? _g : "Did You Mean?";
14332
- const noSuggestionsText = (_h = copy == null ? void 0 : copy.no_suggestions_text) != null ? _h : "No suggestions";
14333
- const seeMoreText = (_i = copy == null ? void 0 : copy.see_more_results_text) != null ? _i : "See More Results";
14391
+ const placeholder = (_f = copy == null ? void 0 : copy.extended_search_textbox_desktop) != null ? _f : "Search";
14392
+ const ariaLabel = (_g = copy == null ? void 0 : copy.extended_search_box_aria_label) != null ? _g : placeholder;
14393
+ const didYouMeanText = (_h = copy == null ? void 0 : copy.did_you_mean_text) != null ? _h : "Did You Mean?";
14394
+ const noSuggestionsText = (_i = copy == null ? void 0 : copy.no_suggestions_text) != null ? _i : "No suggestions";
14395
+ const seeMoreText = (_j = copy == null ? void 0 : copy.see_more_results_text) != null ? _j : "See More Results";
14334
14396
  const showRecentSearches = !hasQuery && !aiLoading && !isInChatMode && recentSearches.length > 0;
14335
- const showNoResults = !isInChatMode && !loading && emptyFetched && visibleSuggestions.length === 0 && !aiLoading && !(enableAIChat && isWaitingForAIFallback);
14397
+ const showNoResults = !isInChatMode && !loading && emptyFetched && visibleSuggestions.length === 0 && !aiLoading;
14336
14398
  const searchInput = /* @__PURE__ */ jsxRuntime.jsx(
14337
14399
  SearchMessageInput,
14338
14400
  {
@@ -14418,7 +14480,7 @@ function SearchModal({
14418
14480
  query,
14419
14481
  showRecentSearches,
14420
14482
  handleClearRecentSearches,
14421
- clearAllLabel: (_j = copy == null ? void 0 : copy.clear_all_label) != null ? _j : "Clear all",
14483
+ clearAllLabel: (_k = copy == null ? void 0 : copy.clear_all_label) != null ? _k : "Clear all",
14422
14484
  showAllRecent,
14423
14485
  recentSearches,
14424
14486
  handleRecentSearchSelect,