@diffsome/react 1.2.11 → 1.2.12

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.js CHANGED
@@ -2805,6 +2805,7 @@ var ChatWidget = ({
2805
2805
 
2806
2806
  // src/components/chat/ChatBubble.tsx
2807
2807
  var import_jsx_runtime6 = require("react/jsx-runtime");
2808
+ var STORAGE_KEY = "diffsome_conversation_id";
2808
2809
  var ChatBubble = ({
2809
2810
  client,
2810
2811
  title = "Chat Support",
@@ -2823,29 +2824,22 @@ var ChatBubble = ({
2823
2824
  }) => {
2824
2825
  const [isOpen, setIsOpen] = (0, import_react24.useState)(false);
2825
2826
  const [messages, setMessages] = (0, import_react24.useState)([]);
2826
- const [conversationId, setConversationId] = (0, import_react24.useState)(() => {
2827
- if (typeof localStorage !== "undefined") {
2828
- const saved = localStorage.getItem("diffsome_conversation_id");
2829
- return saved ? parseInt(saved, 10) : null;
2830
- }
2831
- return null;
2832
- });
2827
+ const [conversationId, setConversationId] = (0, import_react24.useState)(null);
2833
2828
  const [connection, setConnection] = (0, import_react24.useState)(null);
2834
2829
  const [loading, setLoading] = (0, import_react24.useState)(false);
2835
2830
  const [typing, setTyping] = (0, import_react24.useState)(false);
2836
2831
  const [unreadCount, setUnreadCount] = (0, import_react24.useState)(0);
2832
+ const [initialized, setInitialized] = (0, import_react24.useState)(false);
2837
2833
  const seenMessageIds = (0, import_react24.useRef)(/* @__PURE__ */ new Set());
2838
- const isRestoringRef = (0, import_react24.useRef)(false);
2839
- const connectToConversation = (0, import_react24.useCallback)((convId) => {
2840
- const conn = client.chat.connect(convId);
2841
- setConnection(conn);
2834
+ const connectionRef = (0, import_react24.useRef)(null);
2835
+ const setupListeners = (0, import_react24.useCallback)((conn) => {
2842
2836
  conn.onMessage((msg) => {
2843
2837
  if (seenMessageIds.current.has(msg.id)) {
2844
2838
  return;
2845
2839
  }
2846
2840
  seenMessageIds.current.add(msg.id);
2847
2841
  setMessages((prev) => [...prev, msg]);
2848
- if (!isOpen && msg.sender_type !== "visitor") {
2842
+ if (msg.sender_type !== "visitor") {
2849
2843
  setUnreadCount((c) => c + 1);
2850
2844
  }
2851
2845
  });
@@ -2855,26 +2849,33 @@ var ChatBubble = ({
2855
2849
  setTimeout(() => setTyping(false), 3e3);
2856
2850
  }
2857
2851
  });
2858
- return conn;
2859
- }, [client.chat, isOpen]);
2852
+ }, []);
2860
2853
  const initChat = (0, import_react24.useCallback)(async () => {
2861
- if (connection) return;
2854
+ if (initialized || connectionRef.current) return;
2862
2855
  setLoading(true);
2863
2856
  try {
2864
- let convId = conversationId;
2865
- if (convId && !isRestoringRef.current) {
2866
- isRestoringRef.current = true;
2857
+ let savedConvId = null;
2858
+ if (typeof localStorage !== "undefined") {
2859
+ const saved = localStorage.getItem(STORAGE_KEY);
2860
+ if (saved) {
2861
+ savedConvId = parseInt(saved, 10);
2862
+ }
2863
+ }
2864
+ if (savedConvId) {
2867
2865
  try {
2868
- const conn2 = connectToConversation(convId);
2866
+ const conn2 = client.chat.connect(savedConvId);
2867
+ connectionRef.current = conn2;
2868
+ setConnection(conn2);
2869
+ setupListeners(conn2);
2869
2870
  const existingMessages2 = await conn2.getMessages();
2870
2871
  existingMessages2.forEach((m) => seenMessageIds.current.add(m.id));
2871
2872
  setMessages(existingMessages2);
2873
+ setConversationId(savedConvId);
2874
+ setInitialized(true);
2872
2875
  return;
2873
2876
  } catch (error) {
2874
2877
  console.log("Could not restore conversation, starting new one");
2875
- convId = null;
2876
- setConversationId(null);
2877
- localStorage.removeItem("diffsome_conversation_id");
2878
+ localStorage.removeItem(STORAGE_KEY);
2878
2879
  }
2879
2880
  }
2880
2881
  const result = await client.chat.start({
@@ -2882,35 +2883,38 @@ var ChatBubble = ({
2882
2883
  visitor_email: visitorEmail,
2883
2884
  initial_message: greeting
2884
2885
  });
2885
- convId = result.conversation_id;
2886
- setConversationId(convId);
2886
+ const newConvId = result.conversation_id;
2887
+ setConversationId(newConvId);
2887
2888
  if (typeof localStorage !== "undefined") {
2888
- localStorage.setItem("diffsome_conversation_id", convId.toString());
2889
+ localStorage.setItem(STORAGE_KEY, newConvId.toString());
2889
2890
  }
2890
- const conn = connectToConversation(convId);
2891
+ const conn = client.chat.connect(newConvId);
2892
+ connectionRef.current = conn;
2893
+ setConnection(conn);
2894
+ setupListeners(conn);
2891
2895
  const existingMessages = await conn.getMessages();
2892
2896
  existingMessages.forEach((m) => seenMessageIds.current.add(m.id));
2893
2897
  setMessages(existingMessages);
2898
+ setInitialized(true);
2894
2899
  } catch (error) {
2895
2900
  console.error("Failed to initialize chat:", error);
2896
2901
  } finally {
2897
2902
  setLoading(false);
2898
2903
  }
2899
- }, [client.chat, connection, conversationId, connectToConversation, visitorName, visitorEmail, greeting]);
2904
+ }, [client.chat, initialized, setupListeners, visitorName, visitorEmail, greeting]);
2900
2905
  const handleOpen = () => {
2901
2906
  setIsOpen(true);
2902
2907
  setUnreadCount(0);
2903
2908
  onOpen?.();
2904
- if (!conversationId) {
2905
- initChat();
2906
- }
2909
+ initChat();
2907
2910
  };
2908
2911
  const handleClose = () => {
2909
2912
  setIsOpen(false);
2910
2913
  onClose?.();
2911
2914
  };
2912
2915
  const handleSend = async (content) => {
2913
- if (!connection) return;
2916
+ const conn = connectionRef.current;
2917
+ if (!conn) return;
2914
2918
  const tempId = -Date.now();
2915
2919
  const pendingMessage = {
2916
2920
  id: tempId,
@@ -2921,7 +2925,7 @@ var ChatBubble = ({
2921
2925
  };
2922
2926
  setMessages((prev) => [...prev, pendingMessage]);
2923
2927
  try {
2924
- const sentMessage = await connection.send(content);
2928
+ const sentMessage = await conn.send(content);
2925
2929
  if (sentMessage) {
2926
2930
  seenMessageIds.current.add(sentMessage.id);
2927
2931
  setMessages(
@@ -2936,13 +2940,13 @@ var ChatBubble = ({
2936
2940
  }
2937
2941
  };
2938
2942
  const handleTyping = () => {
2939
- connection?.sendTyping();
2943
+ connectionRef.current?.sendTyping();
2940
2944
  };
2941
2945
  (0, import_react24.useEffect)(() => {
2942
2946
  return () => {
2943
- connection?.disconnect();
2947
+ connectionRef.current?.disconnect();
2944
2948
  };
2945
- }, [connection]);
2949
+ }, []);
2946
2950
  const positionClass = position === "bottom-left" ? "diffsome-chat-bubble-left" : "diffsome-chat-bubble-right";
2947
2951
  return /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(
2948
2952
  "div",
package/dist/index.mjs CHANGED
@@ -2713,6 +2713,7 @@ var ChatWidget = ({
2713
2713
 
2714
2714
  // src/components/chat/ChatBubble.tsx
2715
2715
  import { jsx as jsx6, jsxs as jsxs5 } from "react/jsx-runtime";
2716
+ var STORAGE_KEY = "diffsome_conversation_id";
2716
2717
  var ChatBubble = ({
2717
2718
  client,
2718
2719
  title = "Chat Support",
@@ -2731,29 +2732,22 @@ var ChatBubble = ({
2731
2732
  }) => {
2732
2733
  const [isOpen, setIsOpen] = useState22(false);
2733
2734
  const [messages, setMessages] = useState22([]);
2734
- const [conversationId, setConversationId] = useState22(() => {
2735
- if (typeof localStorage !== "undefined") {
2736
- const saved = localStorage.getItem("diffsome_conversation_id");
2737
- return saved ? parseInt(saved, 10) : null;
2738
- }
2739
- return null;
2740
- });
2735
+ const [conversationId, setConversationId] = useState22(null);
2741
2736
  const [connection, setConnection] = useState22(null);
2742
2737
  const [loading, setLoading] = useState22(false);
2743
2738
  const [typing, setTyping] = useState22(false);
2744
2739
  const [unreadCount, setUnreadCount] = useState22(0);
2740
+ const [initialized, setInitialized] = useState22(false);
2745
2741
  const seenMessageIds = useRef3(/* @__PURE__ */ new Set());
2746
- const isRestoringRef = useRef3(false);
2747
- const connectToConversation = useCallback22((convId) => {
2748
- const conn = client.chat.connect(convId);
2749
- setConnection(conn);
2742
+ const connectionRef = useRef3(null);
2743
+ const setupListeners = useCallback22((conn) => {
2750
2744
  conn.onMessage((msg) => {
2751
2745
  if (seenMessageIds.current.has(msg.id)) {
2752
2746
  return;
2753
2747
  }
2754
2748
  seenMessageIds.current.add(msg.id);
2755
2749
  setMessages((prev) => [...prev, msg]);
2756
- if (!isOpen && msg.sender_type !== "visitor") {
2750
+ if (msg.sender_type !== "visitor") {
2757
2751
  setUnreadCount((c) => c + 1);
2758
2752
  }
2759
2753
  });
@@ -2763,26 +2757,33 @@ var ChatBubble = ({
2763
2757
  setTimeout(() => setTyping(false), 3e3);
2764
2758
  }
2765
2759
  });
2766
- return conn;
2767
- }, [client.chat, isOpen]);
2760
+ }, []);
2768
2761
  const initChat = useCallback22(async () => {
2769
- if (connection) return;
2762
+ if (initialized || connectionRef.current) return;
2770
2763
  setLoading(true);
2771
2764
  try {
2772
- let convId = conversationId;
2773
- if (convId && !isRestoringRef.current) {
2774
- isRestoringRef.current = true;
2765
+ let savedConvId = null;
2766
+ if (typeof localStorage !== "undefined") {
2767
+ const saved = localStorage.getItem(STORAGE_KEY);
2768
+ if (saved) {
2769
+ savedConvId = parseInt(saved, 10);
2770
+ }
2771
+ }
2772
+ if (savedConvId) {
2775
2773
  try {
2776
- const conn2 = connectToConversation(convId);
2774
+ const conn2 = client.chat.connect(savedConvId);
2775
+ connectionRef.current = conn2;
2776
+ setConnection(conn2);
2777
+ setupListeners(conn2);
2777
2778
  const existingMessages2 = await conn2.getMessages();
2778
2779
  existingMessages2.forEach((m) => seenMessageIds.current.add(m.id));
2779
2780
  setMessages(existingMessages2);
2781
+ setConversationId(savedConvId);
2782
+ setInitialized(true);
2780
2783
  return;
2781
2784
  } catch (error) {
2782
2785
  console.log("Could not restore conversation, starting new one");
2783
- convId = null;
2784
- setConversationId(null);
2785
- localStorage.removeItem("diffsome_conversation_id");
2786
+ localStorage.removeItem(STORAGE_KEY);
2786
2787
  }
2787
2788
  }
2788
2789
  const result = await client.chat.start({
@@ -2790,35 +2791,38 @@ var ChatBubble = ({
2790
2791
  visitor_email: visitorEmail,
2791
2792
  initial_message: greeting
2792
2793
  });
2793
- convId = result.conversation_id;
2794
- setConversationId(convId);
2794
+ const newConvId = result.conversation_id;
2795
+ setConversationId(newConvId);
2795
2796
  if (typeof localStorage !== "undefined") {
2796
- localStorage.setItem("diffsome_conversation_id", convId.toString());
2797
+ localStorage.setItem(STORAGE_KEY, newConvId.toString());
2797
2798
  }
2798
- const conn = connectToConversation(convId);
2799
+ const conn = client.chat.connect(newConvId);
2800
+ connectionRef.current = conn;
2801
+ setConnection(conn);
2802
+ setupListeners(conn);
2799
2803
  const existingMessages = await conn.getMessages();
2800
2804
  existingMessages.forEach((m) => seenMessageIds.current.add(m.id));
2801
2805
  setMessages(existingMessages);
2806
+ setInitialized(true);
2802
2807
  } catch (error) {
2803
2808
  console.error("Failed to initialize chat:", error);
2804
2809
  } finally {
2805
2810
  setLoading(false);
2806
2811
  }
2807
- }, [client.chat, connection, conversationId, connectToConversation, visitorName, visitorEmail, greeting]);
2812
+ }, [client.chat, initialized, setupListeners, visitorName, visitorEmail, greeting]);
2808
2813
  const handleOpen = () => {
2809
2814
  setIsOpen(true);
2810
2815
  setUnreadCount(0);
2811
2816
  onOpen?.();
2812
- if (!conversationId) {
2813
- initChat();
2814
- }
2817
+ initChat();
2815
2818
  };
2816
2819
  const handleClose = () => {
2817
2820
  setIsOpen(false);
2818
2821
  onClose?.();
2819
2822
  };
2820
2823
  const handleSend = async (content) => {
2821
- if (!connection) return;
2824
+ const conn = connectionRef.current;
2825
+ if (!conn) return;
2822
2826
  const tempId = -Date.now();
2823
2827
  const pendingMessage = {
2824
2828
  id: tempId,
@@ -2829,7 +2833,7 @@ var ChatBubble = ({
2829
2833
  };
2830
2834
  setMessages((prev) => [...prev, pendingMessage]);
2831
2835
  try {
2832
- const sentMessage = await connection.send(content);
2836
+ const sentMessage = await conn.send(content);
2833
2837
  if (sentMessage) {
2834
2838
  seenMessageIds.current.add(sentMessage.id);
2835
2839
  setMessages(
@@ -2844,13 +2848,13 @@ var ChatBubble = ({
2844
2848
  }
2845
2849
  };
2846
2850
  const handleTyping = () => {
2847
- connection?.sendTyping();
2851
+ connectionRef.current?.sendTyping();
2848
2852
  };
2849
2853
  useEffect21(() => {
2850
2854
  return () => {
2851
- connection?.disconnect();
2855
+ connectionRef.current?.disconnect();
2852
2856
  };
2853
- }, [connection]);
2857
+ }, []);
2854
2858
  const positionClass = position === "bottom-left" ? "diffsome-chat-bubble-left" : "diffsome-chat-bubble-right";
2855
2859
  return /* @__PURE__ */ jsxs5(
2856
2860
  "div",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@diffsome/react",
3
- "version": "1.2.11",
3
+ "version": "1.2.12",
4
4
  "description": "React hooks and providers for Diffsome SDK - Headless e-commerce & CMS",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",