wealth-alpha-chat-widget 1.0.8 → 1.0.9

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.cjs CHANGED
@@ -891,24 +891,43 @@ function isRootMenuChips(chips) {
891
891
  return chips.length > 0 && chips.every((c) => ROOT_MENU_CHIP_IDS.has(c.id));
892
892
  }
893
893
  var CARD_DISCLAIMER = "AI-assisted, educational only. Not financial advice. Consult a SEBI-registered advisor.";
894
+ var SECTOR_TOGGLE_PREFIX = "__portfolio_sector_toggle:::";
895
+ var SECTORS_DONE_ID = "__portfolio_sectors_done";
896
+ var escapeHtml = (s) => s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
897
+ function collectSelectedSectors(root) {
898
+ return Array.from(
899
+ root.querySelectorAll(`.srow[data-chip^="${SECTOR_TOGGLE_PREFIX}"]`)
900
+ ).filter((row) => row.classList.contains("selected")).map((row) => (row.getAttribute("data-chip") ?? "").slice(SECTOR_TOGGLE_PREFIX.length)).filter(Boolean);
901
+ }
894
902
  function MessageBubble({ message, onChipClick, onFeedback }) {
895
903
  const isBot = message.role === "bot";
896
904
  const [rating, setRating] = react.useState(null);
905
+ const markdownRef = react.useRef(null);
906
+ const isSectorCard = isBot && message.content.includes(SECTOR_TOGGLE_PREFIX);
897
907
  const rate = (value) => {
898
908
  setRating(value);
899
909
  onFeedback?.(message.id, value);
900
910
  };
901
911
  const isCard = isBot && message.content.includes("<div");
902
912
  const bubbleClass = isBot ? `${chat_default.bubble} ${isCard ? chat_default.bubbleCard : chat_default.bubbleBot}` : `${chat_default.bubble} ${chat_default.bubbleUser}`;
913
+ const refreshSectorSummary = () => {
914
+ const root = markdownRef.current;
915
+ if (!root) return;
916
+ const names = collectSelectedSectors(root);
917
+ const summary = root.querySelector(".wa-sel-summary");
918
+ if (summary) {
919
+ const list = names.length ? names.map(escapeHtml).join(", ") : "\u2014";
920
+ summary.innerHTML = `<span class="chk">\u2705</span>Selected (<b>${names.length}</b>): <b>${list}</b>`;
921
+ }
922
+ };
903
923
  const handleMarkdownClick = (e) => {
904
- const row = e.target.closest(".srow[data-chip]");
905
- if (row) {
924
+ const toggleRow = e.target.closest(
925
+ `.srow[data-chip^="${SECTOR_TOGGLE_PREFIX}"]`
926
+ );
927
+ if (toggleRow) {
906
928
  e.preventDefault();
907
- const chipId = row.getAttribute("data-chip");
908
- if (chipId) {
909
- const label = row.querySelector(".nm")?.textContent?.trim() ?? "";
910
- onChipClick({ id: chipId, label });
911
- }
929
+ toggleRow.classList.toggle("selected");
930
+ refreshSectorSummary();
912
931
  return;
913
932
  }
914
933
  const anchor = e.target.closest("a");
@@ -916,12 +935,22 @@ function MessageBubble({ message, onChipClick, onFeedback }) {
916
935
  e.preventDefault();
917
936
  window.location.assign(anchor.href);
918
937
  };
938
+ const handleChipClick = (chip) => {
939
+ if (isSectorCard && chip.id === SECTORS_DONE_ID) {
940
+ const names = markdownRef.current ? collectSelectedSectors(markdownRef.current) : [];
941
+ const id = names.length ? `${SECTORS_DONE_ID}:::${names.join(",")}` : SECTORS_DONE_ID;
942
+ onChipClick({ ...chip, id });
943
+ return;
944
+ }
945
+ onChipClick(chip);
946
+ };
919
947
  const chips = message.chips ?? [];
920
948
  const showMenuGrid = isBot && message.chipsActive && isRootMenuChips(chips);
921
949
  return /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { display: "flex", flexDirection: "column" }, children: [
922
950
  /* @__PURE__ */ jsxRuntime.jsx("div", { className: bubbleClass, children: isBot ? /* @__PURE__ */ jsxRuntime.jsx(
923
951
  "div",
924
952
  {
953
+ ref: markdownRef,
925
954
  className: chat_default.markdown,
926
955
  dangerouslySetInnerHTML: { __html: renderMarkdown(message.content) },
927
956
  onClick: handleMarkdownClick
@@ -996,7 +1025,7 @@ function MessageBubble({ message, onChipClick, onFeedback }) {
996
1025
  {
997
1026
  chips,
998
1027
  disabled: !message.chipsActive,
999
- onClick: onChipClick
1028
+ onClick: handleChipClick
1000
1029
  }
1001
1030
  ) : null
1002
1031
  ] });